1, make a python script to check mysql replication status

  1. #!/usr/bin/python 
  2.   
  3. #this is nagios plugin for check mysql replication 
  4. #Author lxm 
  5.   
  6. import getopt 
  7. import sys 
  8. import MySQLdb 
  9.   
  10. def usage(): 
  11.     print "Usage %s [-?|--help] -h|--slavehost -u|--slaveuser -p|--slavepasswd -H|--masterhost -U|--masteruser -P|--masterpasswd [--masterport] [--slaveport]" 
  12.   
  13.   
  14. def getMysqlConnect(host, user, passwd, port=3306): 
  15.     try: 
  16.         conn = MySQLdb.connect(hosthost=host, useruser=user, passwdpasswd=passwd, portport=port) 
  17.         cursor = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor) 
  18.         return cursor 
  19.     except: 
  20.         print "UNKNOWN - Can't connect %s:%s" % (host, port) 
  21.         sys.exit(3) 
  22.   
  23. def getMasterStatus(cursor): 
  24.     try: 
  25.         cursor.execute('show master status') 
  26.         rs = cursor.fetchone() 
  27.         cursor.close() 
  28.         return rs 
  29.     except: 
  30.         print "UNKNOWN - Can't get master status" 
  31.         sys.exit(3) 
  32.   
  33. def getSlaveStatus(cursor): 
  34.     try: 
  35.         cursor.execute('show slave status') 
  36.         rs = cursor.fetchone() 
  37.         return rs 
  38.         cursor.close() 
  39.     except: 
  40.         print "UNKNOWN - Can't get slave status" 
  41.         sys.exit(3) 
  42.   
  43. def do(masterhost, masteruser, masterpasswd, slavehost, slaveuser, slavepasswd, masterport=3306slaveport=3306): 
  44.     master = getMysqlConnect(masterhost, masteruser, masterpasswd, masterport) 
  45.     slave = getMysqlConnect(slavehost, slaveuser,slavepasswd, slaveport) 
  46.     masterrs = getMasterStatus(master) 
  47.     slavers = getSlaveStatus(slave) 
  48.   
  49.     if(slavers['Slave_IO_Running'] != 'Yes') or (slavers['Slave_SQL_Running'] != 'Yes'): 
  50.         print 'CRITICAL - Slave_IO_Running: %s\t Slave_SQL_Running:%s' % (slavers['Slave_IO_Running'], slavers['Slave_SQL_Running']) 
  51.         sys.exit(2) 
  52.   
  53.     if(masterrs['File'] != slavers['Master_Log_File']): 
  54.         print 'CRITICAL - Master binlog file is %s but slave read master log file is %s' % (masterrs['File'], slavers['Master_Log_File']) 
  55.         sys.exit(2) 
  56.       
  57.     if(slavers['Master_Log_File'] != slavers['Relay_Master_Log_File']): 
  58.         print 'WARING - Master_Log_File is %s but Relay_Master_Log_File is %s' % (slavers['Master_Log_File'], slavers['Relay_Master_Log_File']) 
  59.         sys.exit(1) 
  60.   
  61.     if(masterrs['Position'] != slavers['Read_Master_Log_Pos']): 
  62.         print 'WARING - Master binlog position is %d but slave read master log position is %d The Offset is %d' % (masterrs['Position'], slavers['Read_Master_Log_Pos'], masterrs['Position'] - slavers['Read_Master_Log_Pos']) 
  63.         sys.exit(1) 
  64.   
  65.     if(slavers['Read_Master_Log_Pos'] != slavers['Exec_Master_Log_Pos']): 
  66.         print 'WARING - Read Master Log Position is %d but Exec Master Log Position is %d The Offset is %d' % (slavers['Read_Master_Log_Pos'], slavers['Exec_Master_Log_Pos'], slavers['Read_Master_Log_Pos'] - slavers['Exec_Master_Log_Pos']) 
  67.         sys.exit(1) 
  68.   
  69.     print "OK - mysql replication is ok" 
  70.     sys.exit(0) 
  71.   
  72. def main(): 
  73.     slavehost = slaveuser = slavepasswd = slaveport = masterhost = masteruser = masterpasswd = masterport = None 
  74.     try: 
  75.         opts,args = getopt.getopt(sys.argv[1:],'?h:u:p:H:U:P:',["help","slavehost=","slaveuser=","slavepasswd=",'slaveport=','masterhost=','masteruser=','masterpasswd=','masterport=']) 
  76.     except getopt.GetoptError: 
  77.         usage() 
  78.         sys.exit(3) 
  79.     for o,a in opts: 
  80.         if o in ('-?', '--help'): 
  81.             usage() 
  82.             sys.exit() 
  83.         if o in ('-h', '--slavehost'): 
  84.             slavehost = a 
  85.         if o in ('-u', '--slaveuser'): 
  86.             slaveuser = a 
  87.         if o in ('-p', '--slavepasswd'): 
  88.             slavepasswd = a 
  89.         if o in ('-H', '--masterhost'): 
  90.             masterhost = a 
  91.         if o in ('-U', '--masteruser'): 
  92.             masteruser = a 
  93.         if o in ('-P', '--masterpasswd'): 
  94.             masterpasswd = a 
  95.         if o in ('--slaveport'): 
  96.             slaveport = a 
  97.         if o in ('--masterport'): 
  98.             masterport = a 
  99.   
  100.     if not (slavehost and slaveuser and slavepasswd and masterhost and masteruser and masterpasswd): 
  101.         usage() 
  102.         sys.exit(3) 
  103.   
  104.     if not slaveport: 
  105.         slaveport = 3306 
  106.     if not masterport: 
  107.         masterport = 3306 
  108.   
  109.     do(masterhost, masteruser, masterpasswd, slavehost, slaveuser, slavepasswd, masterport, slaveport) 
  110.   
  111. if __name__ == '__main__': 
  112.     main() 

2, Deploy this file to $NAGIOS_HOME/libexec and add a nagios check command.

Modify $NAGIOS_HOME/etc/objects/command.cfg, add the flowing line

  1. define command{ 
  2.     command_name check_mysql_replication 
  3.     command_line /usr/local/nagios/libexec/check_mysql_replication.py -h $HOSTADDRESS$ -u **** -p ***** -U **** -P ***** -H $ARG1$ 

3, Define the services

Modify $NAGIOS_HOME/etc/servers/servers.cfg, add the flowing line

  1. define service { 
  2.         use                     generic-service 
  3.         host_name               web-sh-unicom-01 
  4.         service_description     shanghai-user-databases 
  5.         check_command           check_mysql_replication!203.116.12.160 
  6.   
  7. define service { 
  8.         use                     generic-service 
  9.         host_name               web-sg-01 
  10.         service_description     Singapore-user-databases 
  11.         check_command           check_mysql_replication!60.29.233.97 

4, Restart nagios

First, check the configure file

  1. $NAGIOS_HOME/bin/nagios -v $NAGIOS_HOME/etc/nagios.cfg 

if no any errors, restart nagios.