# #先使用MySQLdb的接口关联数据库.
[[Email protected] python]# cat check_mysql_custom.py#!/usr/local/bin/python '] Author = chenmingle "" Description:get mysql status "import osimport systry: import MySQLdb as mysqlexcept Exception, e: print e print "Pip install mysql-python" Sys.exit (1) con = mysql.connect (host= ' localhost ', user= ' root ', passwd= ' passwd ',) def processlist (): Cur = con.cursor () sql1 = ' show processlist ' A = str (Cur.execute (SQL1)) print adef slave_status (): cur = con.cursor () sql2 = cur.execute (' show status like "%slave_running%"; ') status2 = str (Cur.fetchall ()) &nBsp; check2 = status2.split ("'") [3] if check2 == ' on ': print 0 else: print 1def show_status (type): Cur = con.cursor () b = cur.execute (' show status like "%s"; ') % (type)) For i in cur.fetchall (): Cat = str (i) Check = str (Cat.split ("'") ) [3]) Print checkdef main (type): if type == ' processlist ':p rocesslist () elif type == ' slave_status ': Slave_status () else:show_ Status (Type) if __name__ == ' __main__ ': try: type = sys.argv[1] except exception, e: print "Usage: python %s type" % sys.argv[0] sys.exit (1) main (type)
If you perform the following error in the test times:
[email protected] python]# python check_mysql_custom.py slave_status
Traceback (most recent):
File "test.py", line +, in <module>
con = mysql.connect (user= ' Zabbix ', passwd= ', port=3306)
File "/usr/local/lib/python2.7/site-packages/mysqldb/__init__.py", line Bayi, in Connect
Return Connection (*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/mysqldb/connections.py", line 193, in __init__
Super (Connection, self). __init__ (*args, **KWARGS2)
_mysql_exceptions. Operationalerror: (2002, "Can ' t connect to local MySQL server through socket '/var/lib/mysql/mysql.sock ' (2)")
[Email protected] python]# mkdir/var/lib/mysql[[email protected] python]# ln-s/tmp/mysql.sock/var/lib/mysql/ Mysql.sock
# #测试一下脚本:
[[email protected] python]# python check_mysql_custom.py processlist3[[email protected] python]# python check_mysql_ custom.py Connections55876
# #测试显示所有在mysql中show status can output information:
# #写一个模板配置文件 (custom key value):
[email protected] zabbix_agentd.d]# cat Mysql_custom.confuserparameter=mysql. custom[*],/usr/local/bin/python/home/python/check_mysql_custom.py $
# Add a template #在zabbix the Web interface :
# #增加一个Appaliactions:
# #加入item值:
Use Python to write a script that monitors MySQL, customizing the template on the Zabbix web