Use Python to write a script to monitor MySQL and add a template to the Zabbix Web

Source: Internet
Author: User

UsepythonWrite a monitoringMySQLthe script, inZabbix WebAdd the Template:


## First Use MySQLdb interface associated with the database.

[[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)


## test the script:

[[email protected] python]# python check_mysql_custom.py processlist3[[email protected] python]# python check_mysql_ custom.py Connections55876


## The test shows all the MySQL in Show Status can output information:



## Write a template configuration file (custom Key value):

[email protected] zabbix_agentd.d]# cat Mysql_custom.confuserparameter=mysql. custom[*],/usr/local/bin/python/home/python/check_mysql_custom.py $


## in the Zabbix Web add a template to the interface:  

## Add a appaliactions:


## Join Item values:  

Use Python to write a script to monitor MySQL and add a template to 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.