Zabbix自定义监控
监控指标
- 系统指标(内存、CPU、硬盘)
- 文件监控
- 网络监控
- 硬件监控(硬盘温度、电源是否异常、CPU温度),通过IPMI实现
- 业务监控
自定义监控流程:
-
开启自定义监控的功能:
在agentd.conf中设置,
UnsafeUserParameters=1
UserParameters=key,command
-
写脚本
-
网页上配置监控项、触发器
自定义监控进程
注意事项
- 脚本放哪里
- 脚本让谁执行
- 规范化
写脚本+开启自定义监控功能
[root@cxx ~]# mkdir /scripts //编写监控脚本 [root@cxx ~]# vim /scripts/check_process.sh #!/bin/bash count=$(ps -ef | grep -Ev "grep|$0" | grep -c "$1") if [ $count -eq 0 ];then echo "1" else echo "0" fi [root@cxx ~]# chmod +x /scripts/check_process.sh //开启httpd测试 [root@cxx ~]# systemctl start httpd [root@cxx ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:10050 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 128 *:80 *:* [root@cxx ~]# /scripts/check_process.sh httpd 0 //关闭httpd测试 [root@cxx ~]# systemctl stop httpd [root@cxx ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:10050 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* [root@cxx ~]# /scripts/check_process.sh httpd 1 //在文件最后面加入两行代码 [root@cxx ~]# vim /usr/local/etc/zabbix_agentd.conf UnsafeUserParameters=1 UserParameter=check_apache,/scripts/check_process.sh /usr/local/apache/bin/httpd [root@cxx ~]# pkill zabbix [root@cxx ~]# zabbix_agentd [root@cxx ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:10050 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* //使用服务端测试是否能获取客户端的指标 [root@zabbix ~]# zabbix_get -s 192.168.100.2 -k check_apache 1







配置监控日志流程
[root@zabbix ~]# cd /scripts/ [root@zabbix scripts]# dnf -y install python36 [root@zabbix scripts]# vim log.py #!/usr/bin/env python3 import sys import re def prePos(seekfile): global curpos try: cf = open(seekfile) except IOError: curpos = 0 return curpos except FileNotFoundError: curpos = 0 return curpos else: try: curpos = int(cf.readline().strip()) except ValueError: curpos = 0 cf.close() return curpos cf.close() return curpos def lastPos(filename): with open(filename) as lfile: if lfile.readline(): lfile.seek(0,2) else: return 0 lastPos = lfile.tell() return lastPos def getSeekFile(): try: seekfile = sys.argv[2] except IndexError: seekfile = '/tmp/logseek' return seekfile def getKey(): try: tagKey = str(sys.argv[3]) except IndexError: tagKey = 'Error' return tagKey def getResult(filename,seekfile,tagkey): destPos = prePos(seekfile) curPos = lastPos(filename) if curPos < destPos: curpos = 0 try: f = open(filename) except IOError: print('Could not open file: %s' % filename) except FileNotFoundError: print('Could not open file: %s' % filename) else: f.seek(destPos) while curPos != 0 and f.tell() < curPos: rresult = f.readline().strip() global result if re.search(tagkey, rresult): result = 1 break else: result = 0 with open(seekfile,'w') as sf: sf.write(str(curPos)) finally: f.close() return result if __name__ == "__main__": result = 0 curpos = 0 tagkey = getKey() seekfile = getSeekFile() result = getResult(sys.argv[1],seekfile,tagkey) print(result) [root@zabbix scripts]# chmod +x log.py [root@zabbix scripts]# touch /usr/local/apache/logs/error.log //在文件最后添加 [root@zabbix scripts]# vim /usr/local/etc/zabbix_agentd.conf UserParameter=check_logs[*],/scripts/log.py $1 $2 $3 [root@zabbix scripts]# pkill zabbix [root@zabbix scripts]# zabbix_agentd




监控客户端的mysql主从状态
[root@zabbix scripts]# vim check_mysql_repl.sh #!/bin/bash count=$(mysql -uroot -e 'show slave status\G'|grep ' Running:'|awk '{print $2}'|grep -c 'Yes') if [ $count -ne 2 ];then echo '1' else echo '0' fi [root@zabbix scripts]# vim /usr/local/etc/zabbix_agentd.conf UserParameter=check_mysql_repl,/scripts/check_mysql_repl.sh [root@zabbix scripts]# pkill zabbix [root@zabbix scripts]# zabbix_agentd




浙公网安备 33010602011771号