Tag:json turn monitoring print result ace dump .sql master
#!/bin/env python# -*- encoding: utf-8 -*-import timeimport osimport Sysimport jsonimport reimport threadmysql_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 &NBsp 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], &nbsP; ' Messages ': "%s %s"% (' slave_io_running ', ' normal ' if str_string[' slave_io_running '] = = ' Yes ' else ' Stop '), } 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 ', "normal" if str_string[' slave_sql_running '] == ' Yes ' else ' Stop '), } Return mysQl_stat_list.append (Result) else: return {} Def get_data (Host,tid): return mysqlmonitorinfo (). Stat_info () if len (SYS.ARGV) > &NBSP;4:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FOR&NBSP;HOST&NBSP;IN&NBSP;SYS.ARGV[1]. Split (","): thread.start_ New_thread (get_data, (host, 1)) else: get_data (", " ") &Nbsp; print json.dumps (Mysql_stat_list)
Daily management 03-monitor MySQL master-slave delay 3-second script;