第十二篇、【Zabbix监控项之自定义脚本监控】
1、创建存放监控的Python程序的目录
此监控是由真实的项目平台,从MySQL统计成功率来进行监控
[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)
默认脚本是以zabbix用户运行的,所有需要授权
[root@MySQL-Slave ~]# chown -R zabbix.zabbix -R /data/zabbix-4.4.3/script
[root@MySQL-Slave ~]# chmod 755 /data/zabbix-4.4.3/script/monitor_success_rate.py
[root@MySQL-Slave ~]# ll /data/zabbix-4.4.3/script/
total 4
-rwxr-xr-x 1 zabbix zabbix 1325 Jan 3 10:14 monitor_success_rate.py
3、配置Zabbix Agent参数
[root@MySQL-Slave ~]# vi /data/zabbix-4.4.3/etc/zabbix_agentd.conf.d/monitor_success_rate.conf UnsafeUserParameters=1 UserParameter=custom.success.rate[*],/usr/local/Python-3.6.6/bin/python3 /data/zabbix-4.4.3/script/monitor_success_rate.py $1 #注意 #如果Zabbix Agent需要以root身份运行的脚本的话,需要做如下修改,让Zabbix Agent以root身份运行,默认是zaabix身份运行 [root@MySQL-Slave ~]# vi /data/zabbix-4.4.3/etc/zabbix_agentd.conf 259 ### Option: AllowRoot 260 # Allow the agent to run as 'root'. If disabled and the agent is started by 'root', the agent 261 # will try to switch to the user specified by the User configuration option instead. 262 # Has no effect if started under a regular user. 263 # 0 - do not allow 264 # 1 - allow 265 # 266 # Mandatory: no 267 # Default: 268 AllowRoot=1
4、配置完成,重启Zabbix Agent服务
[root@MySQL-Slave ~]# /etc/init.d/zabbix_agentd restart
5、到Zabbix服务端调用监控项
[root@filestore-v2 ~]# /data/application/zabbix-4.4.3/bin/zabbix_get -s 192.168.10.103 -p 10050 -k "custom.success.rate[wxxxxxx]" 94.918435
6、自定义脚本监控配置成功
7、配置触发器,这里不再介绍
下一篇【Zabbix监控项之Jvm监控】请点击查看