-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_mysql_slave.py
executable file
·95 lines (56 loc) · 1.71 KB
/
check_mysql_slave.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
#!/usr/bin/env python2
# coding: utf-8
import sys
import MySQLdb
from pykit import mysqlconnpool
def get_slave_status(port):
pool = mysqlconnpool.make({
'unix_socket': '/var/run/mysqld/mysqld-{p}.sock'.format(p=port)
})
try:
slave_status = pool.query('show slave status')
except (MySQLdb.OperationalError, MySQLdb.InternalError):
return None
return slave_status
def check_instance_role(slave_status):
if len(slave_status) == 0:
return 'Master'
else:
return "Slaveof-{n}-master".format(n=len(slave_status))
def check_slave_is_healthy(slave_status):
health_value = 0
if len(slave_status) == 0:
return 0
for st in slave_status:
if st['Slave_SQL_Running'] == 'Yes':
if st['Slave_IO_Running'] == 'Yes':
health_value += 0
else:
health_value += 1
else:
if st['Slave_IO_Running'] == 'Yes':
health_value += 10
else:
health_value += 100
return health_value
def check_behind_master(slave_status):
behind_sec = 0
if len(slave_status) == 0:
return 0
for st in slave_status:
behind_sec += int(st['Seconds_Behind_Master'])
return behind_sec
if __name__ == "__main__":
port, metric = sys.argv[1:3]
slave_status = get_slave_status(port)
if slave_status is None:
print -1
sys.exit(-1)
if metric == 'role':
print check_instance_role(slave_status)
elif metric == 'slave_health':
print check_slave_is_healthy(slave_status)
elif metric == 'slave_behind':
print check_behind_master(slave_status)
else:
print -2