此监控是由真实的项目平台,从MySQL统计成功率来进行监控
1、创建存放监控的Python程序的目录
[root@MySQL-Slave ~]# mkdir /data/zabbix-4.4.3/script
2、编写监控的程序
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/1/2 17:03 # @Author : suk # @File : monitor_username.py # @Software: PyCharm import pymysql import sys import datetime if __name__ == '__main__': import datetime today = datetime.date.today() formatted_today = today.strftime('%Y%m%d') # 生产库 db = pymysql.connect(host='数据库的IP地址', port=3306, database='数据库的名字', user='数据库用户名', password='数据库的密码') ec_user_channel = db.cursor(cursor=pymysql.cursors.DictCursor) ec_user_channel.execute(""" SELECT SUM( IF ( status_ != 13, 1, 0 ) ) AS 'sendFailCount', SUM( IF ( status_ = 13, 1, 0 ) ) AS 'sendSuccessCount' FROM xxxxx_""" + formatted_today + """ WHERE user_id = ( SELECT uid FROM ec_user WHERE username = %s )""", args=(sys.argv[1],)) rows = ec_user_channel.fetchone() if (rows.get('sendSuccessCount') and rows.get('sendFailCount')) or (rows.get('sendSuccessCount') != 0 and rows.get('sendFailCount') == 0): print( '%02f' % (rows.get('sendSuccessCount') / (rows.get('sendFailCount') + rows.get('sendSuccessCount')) * 100)) sys.exit(0) else: print(0) sys.exit(1)