-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathmysql_replication.py
executable file
·93 lines (80 loc) · 2.95 KB
/
mysql_replication.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
#!/usr/bin/env python
## Rackspace Cloud Monitoring Plug-In
## MySQL Replication State Validation
#
# (C)2013 Chris Mecca <[email protected]>
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Usage:
# Place plug-in in /usr/lib/rackspace-monitoring-agent/plugins
#
# The following is an example 'criteria' for a Rackspace Monitoring Alarm:
#
# if (metric['SLAVE_STATUS'] != 'ONLINE') {
# return new AlarmStatus(CRITICAL, 'MySQL Replication is OFFLINE.');
# }
#
# if (metric['SLAVE_STATUS'] == 'ONLINE' && metric['SECONDS_BEHIND_MASTER'] \
# >= 120 && metric['SECONDS_BEHIND_MASTER'] < 300) {
# return new AlarmStatus(WARNING, 'MySQL Replication ONLINE \
# but Slave is more than 2 minutes behind Master.');
# }
#
# if (metric['SLAVE_STATUS'] == 'ONLINE' && metric['SECONDS_BEHIND_MASTER'] \
# >= 300) {
# return new AlarmStatus(CRITICAL, 'MySQL Replication ONLINE \
# but Slave is more than 5 minutes behind Master.');
# }
#
# return new AlarmStatus(OK, 'MySQL Replication is ONLINE');
import sys
import subprocess
import shlex
def mysql_repchk(arg):
proc = subprocess.Popen(shlex.split(arg),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False)
out, err = proc.communicate()
ret = proc.returncode
return ret, out, err
RETCODE, OUTPUT, ERR = mysql_repchk('/usr/bin/mysql \
--defaults-file=/root/.my.cnf \
-e "SHOW SLAVE STATUS\\G"')
if RETCODE:
print >> sys.stderr, "There was an error (%d): \n" % RETCODE
print >> sys.stderr, ERR
if OUTPUT != "":
SHOW_STATUS_LIST = OUTPUT.split('\n')
del SHOW_STATUS_LIST[0]
del SHOW_STATUS_LIST[-1]
SLAVE_STATUS = {}
for i in SHOW_STATUS_LIST:
if ":" in i:
SLAVE_STATUS[i.split(':')[0].strip()] = i.split(':')[1].strip()
if SLAVE_STATUS["Slave_IO_Running"] == "Yes" and \
SLAVE_STATUS["Slave_SQL_Running"] == "Yes" and \
SLAVE_STATUS["Last_Errno"] == "0":
print "status OK\n" \
"metric SLAVE_STATUS string ONLINE\n" \
"metric SECONDS_BEHIND_MASTER int " \
+ SLAVE_STATUS["Seconds_Behind_Master"]
else:
print "status OK\n" \
"metric SLAVE_STATUS string OFFLINE\n" \
"metric SECONDS_BEHIND_MASTER int " \
+ SLAVE_STATUS["Seconds_Behind_Master"]
else:
print "status ERROR\nmetric SLAVE_STATUS string NOT_CONFIGURED"