当前位置:Gxlcms > 数据库问题 > 使用python写一个监控mysql的脚本,在zabbix web上加上模板

使用python写一个监控mysql的脚本,在zabbix web上加上模板

时间:2021-07-01 10:21:17 帮助过:15人阅读

python写一个监控mysql的脚本,在zabbix web上加上模板:


##先使用MySQLdb的接口关联数据库。

[root@cml python]# cat check_Mysql_custom.py
#!/usr/local/bin/python
'''author = chenmingle'''
'''Description:get mysql status'''
import os
import sys
try:
    import MySQLdb as mysql
except 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 a
def slave_status():
        cur = con.cursor()
        sql2 = cur.execute('show status like "%Slave_running%";')
        status2 = str(cur.fetchall())
        check2 = status2.split("'")[3]
        if check2 == 'ON':
                print 0
        else:
                print 1
def 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 check
def main(type):
   if type == 'processlist':
processlist()
   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)


##测试一下脚本:

[root@cml python]# python check_Mysql_custom.py processlist
3
[root@cml python]# python check_Mysql_custom.py Connections
55876


##测试显示所有在mysqlshow status的都可以输出信息出来:



##写一个模板配置文件(自定义key值):

[root@cml zabbix_agentd.d]# cat Mysql_Custom.conf
UserParameter=mysql.Custom[*],/usr/local/bin/python /home/python/check_Mysql_custom.py $1


##zabbix web界面上加入模板: 

技术分享图片

##增加一个Appaliactions:

技术分享图片


##加入item值: 

技术分享图片

使用python写一个监控mysql的脚本,在zabbix web上加上模板

标签:[1]   bin   $1   slave   .exe   屏幕   3.5   erp   conf   

人气教程排行