Use Python to write a script that monitors MySQL, customizing the template on the Zabbix web

Source: Internet
Author: User

# #先使用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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.