python 监控mysql主从

#!/usr/bin/env python#-*- coding: utf-8 -*-import osos.environ[PYTHON_EGG_CACHE] = /tmp/.python-eggsimport sys,datetime,MySQLdb,socket,paramikodef final_check_mysql(): status = "true" try: conn = MySQLdb.Connect(host=xxx,port=3306,user=root,passwd=xxx,db=zabbix) cur = conn.cursor() cur.execute("show slave status;") result = cur.fetchall() io_thread = result[0][10] sql_thread = result[0][11] seconds_behind_master = result[0][32] #print io_thread,sql_thread cur.close() conn.close() except Exception,e: print Exception,":",e try: if io_thread=="Yes" and sql_thread=="Yes" and seconds_behind_master<=1000: return 1 else: status = "false" return 0 except Exception,e: print Exception,":",eif __name__==__main__: result=final_check_mysql() print result

 

  

相关文章