#!/usr/bin/python
#!gbk
Import OS
Import Sys
Mysqlbase = '/usr/bin/mysql '
host = ' IP '
user = ' root '
PASSW = ' xxxxxx '
info = Os.popen (mysqlbase+ '-H%s-u%s-p%s-e "show slave Status\g" |grep-e "slave_io_running| Slave_sql_running| seconds_behind_master| master_log_file| read_master_log_pos| relay_master_log_file| Exec_master_log_pos "'% (HOST,USER,PASSW)). Read ()
Info_list = Info.split ()
Info_tup = {}
JS = 0
_IDX = 0
PD = Len (info_list)/2
For Idx,item in Enumerate (info_list):
JS + = 1
If JS > PD:
Continue
INFO_TUP[INFO_LIST[_IDX]] = info_list[_idx+1]
_idx + = 2
If Len (sys.argv) = = 1:
Print (' Your can insert Python + pyname + seconds_behind_master or File_pos or Io_sql ')
Elif sys.argv[1] = = ' Seconds_behind_master ':
If info_tup[' seconds_behind_master: '] = = ' 0 ':
Print (0)
Else:print (info_tup[' seconds_behind_master: '])
Elif sys.argv[1] = = ' File_pos ':
If info_tup[' master_log_file: '] = = info_tup[' relay_master_log_file: '] and info_tup[' read_master_log_pos: '] = = Info_ tup[' Exec_master_log_pos: ']:
Print (0)
Else:print (5)
Elif sys.argv[1] = = ' Io_sql ':
If info_tup[' slave_io_running: '] = = ' yes ' or info_tup[' slave_sql_running: '] = = ' Yes ':
Print (0)
Else:print (5)
Python script monitors mysql slave status