#!/bin/env python
# -*- encoding: utf-8 -*-
import time
import os
import sys
import json
import re
import thread
mysql_stat_list = []
class MySQLMonitorInfo():
def __init__(self):
pass
def is_slave(self):
m = "mysql -e 'SHOW Slave status\G' "
data = os.popen(m).readlines()
if data == []:
return '',False
else:
return data,True
def stat_info(self):
data,isslave = self.is_slave()
if is slave:
Str_string = {}
for d in data[1:]:
ds = d.replace(" ","").replace("\t","").split(":")
Str_string[ds[0].strip()] = ds[1].strip()
#print Str_string
result = {
'name': '%s' % ('slave.iorun'),
'groups': "MysqlDelay3",
'value': 1 if Str_string['Slave_IO_Running'] == 'Yes' else 2,
'status': 1 if Str_string['Slave_IO_Running'] == 'Yes' else 2,
'command': sys.argv[0],
'messages': "%s %s"%('Slave_IO_Running', '正常' if Str_string['Slave_IO_Running'] == 'Yes' else "停止"),
}
mysql_stat_list.append(result)
result = {
'name': '%s' % ('slave.delay3'),
'groups': "MysqlDelay3",
'value': Str_string['Seconds_Behind_Master'],
'status': 1 if int(Str_string['Seconds_Behind_Master']) < int(sys.argv[1]) else 2,
'command': sys.argv[0],
'messages': "%s %s"%('Seconds_Behind_Master', Str_string['Seconds_Behind_Master']),
}
mysql_stat_list.append(result)
result = {
'name': '%s' % ('slave.sqlrun'),
'groups': "MysqlNew",
'value': 1 if Str_string['Slave_SQL_Running'] == 'Yes' else 2,
'status': 1 if Str_string['Slave_SQL_Running'] == 'Yes' else 2,
'command': sys.argv[0],
'messages': "%s %s"%('Slave_SQL_Running', "正常" if Str_string['Slave_SQL_Running'] == 'Yes' else "停止"),
}
return mysql_stat_list.append(result)
else:
return {}
def get_data(host,tid):
return MySQLMonitorInfo().stat_info()
if len(sys.argv) > 4:
for host in sys.argv[1].split(","):
thread.start_new_thread(get_data, (host, 1))
else:
get_data("", "")
print json.dumps(mysql_stat_list)
原文地址:http://blog.51cto.com/breaklinux/2150207