/
restart_daemons.py
executable file
·144 lines (113 loc) · 4.66 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/python
import argparse
import logging
import psutil
from lib import mysql_lib
from lib import host_utils
from lib import environment_specific
import mysql_cnf_builder
log = logging.getLogger(__name__)
def restart_maxwell_if_not_exists(instance):
""" Start Maxwell if it isn't currently running.
Args:
instance: (host_utils.HostAddr): host to check
Returns:
none
"""
zk = host_utils.MysqlZookeeper()
replica_type = zk.get_replica_type_from_instance(instance)
gvars = mysql_lib.get_global_variables(instance)
client_id = gvars['server_uuid']
gtid_mode = True if gvars.get('gtid_mode') == 'ON' else False
(username, _) = mysql_lib.get_mysql_user_for_role('maxwell')
output_target = 'file'
# master writes to kafka, everything else writes to /dev/null,
# at least for now.
if instance.hostname_prefix in environment_specific.MAXWELL_TARGET_MAP \
and replica_type == host_utils.REPLICA_ROLE_MASTER:
output_target = 'kafka'
# we need to rewrite the config each time, because something may
# have changed - i.e., a failover. this is just a stopgap solution
# pending resolution of LP-809
mysql_cnf_builder.create_maxwell_config(client_id, instance,
None, output_target,
gtid_mode)
# Check for the Maxwell PID file and then see if it belongs to Maxwell.
maxwell_running = False
try:
with open(environment_specific.MAXWELL_PID, "r") as f:
pid = f.read()
proc = psutil.Process(int(pid))
cmdline = proc.cmdline()
if 'java' in cmdline and 'com.zendesk.maxwell.Maxwell' in cmdline:
maxwell_running = True
except (IOError, psutil.NoSuchProcess, psutil.ZombieProcess):
# No PID file or no process matching said PID, so maxwell is definitely
# not running. If maxwell is a zombie then it's not running either.
pass
if maxwell_running:
log.debug('Maxwell is already running')
return
if instance.hostname_prefix in environment_specific.MAXWELL_TARGET_MAP:
host_utils.manage_maxwell(instance.port)
log.info('Started Maxwell process')
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.manage_pt_kill(instance.port)
log.info('Started Processes ptkill')
def manage_pt_heartbeat(instance):
"""
Restarts ptheartbeat if it isn't currently running and the
replica role type is master, or stop it if it is running on
a non-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_type_from_instance(instance)
except:
replica_type = None
pthb_user, pthb_pass = mysql_lib.get_mysql_user_for_role('ptheartbeat')
if replica_type == host_utils.REPLICA_ROLE_MASTER and \
pthb_user not in connected_users:
host_utils.manage_pt_heartbeat(instance.port)
log.info('Started process pt-heartbeat')
elif replica_type != host_utils.REPLICA_ROLE_MASTER and \
pthb_user in connected_users:
host_utils.manage_pt_heartbeat(instance.port, action='stop')
log.info('Stopped pt-heartbeat on non-master replica')
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', 'maxwell', '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':
manage_pt_heartbeat(instance)
if args.action == 'all' or args.action == 'maxwell':
restart_maxwell_if_not_exists(instance)
if __name__ == "__main__":
environment_specific.initialize_logger()
main()