forked from pinterest/mysql_utils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
restart_daemons.py
executable file
·74 lines (58 loc) · 2.23 KB
/
restart_daemons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
import argparse
from lib import mysql_lib
from lib import host_utils
from lib import environment_specific
def restart_pt_kill_if_not_exists(instance):
"""
Restarts ptkill if it isn't currently running
Args:
instance (host_utils.HostAddr): host to check for ptkill
Returns:
None
"""
connected_users = mysql_lib.get_connected_users(instance)
ptkill_user, ptkill_pass = mysql_lib.get_mysql_user_for_role('ptkill')
if ptkill_user not in connected_users:
host_utils.restart_pt_kill(instance.port)
log.info('Started Processes ptkill')
def restart_pt_heartbeat_if_not_exists(instance):
"""
Restarts ptheartbeat if it isn't currently running and the
replica role type is master
Args:
instance (host_utils.HostAddr): host to check for ptheartbeat
Returns:
None
"""
connected_users = mysql_lib.get_connected_users(instance)
zk = host_utils.MysqlZookeeper()
try:
(_, replica_type) = zk.get_replica_set_from_instance(instance)
except:
replica_type = None
pthb_user, pthb_pass = mysql_lib.get_mysql_user_for_role('ptheartbeat')
if replica_type in (host_utils.REPLICA_ROLE_MASTER, None) and \
pthb_user not in connected_users:
host_utils.restart_pt_heartbeat(instance.port)
log.info('Started Processes ptheartbeat')
def main():
parser = argparse.ArgumentParser(
description='Restarts ptkill and ptheartbeat '
'if they aren\'t running under '
'the right conditions'
)
parser.add_argument('action',
help='Action to take',
default='all',
nargs='?',
choices=['ptkill', 'ptheartbeat', 'all'])
args = parser.parse_args()
instance = host_utils.HostAddr(host_utils.HOSTNAME)
if args.action == 'all' or args.action == 'ptkill':
restart_pt_kill_if_not_exists(instance)
if args.action == 'all' or args.action == 'ptheartbeat':
restart_pt_heartbeat_if_not_exists(instance)
if __name__ == "__main__":
log = environment_specific.setup_logging_defaults(__name__)
main()