修改zabbix_agentd.conf配置文件
第一个:默认为0,此处将它改为1
[root@noed1 etc]# sed -n "322p" zabbix_agentd.conf UnsafeUserParameters=1
脚本名字:process_check.sh
脚本目录(这个目录可以自定义):/usr/local/etc/zabbix_scripts
[root@noed1 zabbix_scripts]# cat process_check.sh #!/bin/bash result=$(ps -ef |grep -Ev "grep|$0" | grep -c $1) if [ $result -eq 0 ];then echo "1" else echo "0" fi
通过ps检查“传参”进程是否存在,如果存在则脚本反馈1,如果不存在则返回0
自定义监控项,格式为:UserParameter=<键值>,<命令>。
root@noed1 etc]# sed -n "332p" zabbix_agentd.conf UserParameter=process_check[*],/usr/local/etc/zabbix_scripts/process_check.sh $1
python脚本
第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)
[root@noed1 zabbix_scripts]# cat 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@noed1 etc]# sed -n "334p" zabbix_agentd.conf UserParameter=error_log[*],/usr/local/etc/zabbix_scripts/log.py $1 $2 $3
[root@zabbix_server ~]# zabbix_get -s 192.168.248.200 -k "error_log["/tmp/logs","/tmp/testseek", "Error"]" 1
root@noed1 etc]# for i in `seq 10`;do echo "$i" >> /tmp/logs;done #到监控端取值 [root@zabbix_server ~]# zabbix_get -s 192.168.248.200 -k "error_log["/tmp/logs","/tmp/testseek", "Error"]" 0 #输出Error至/tmp/logs文件中再次取值 [root@noed1 etc]# echo "Error" >> /tmp/logs #取值 [root@zabbix_server ~]# zabbix_get -s 192.168.248.200 -k "error_log["/tmp/logs","/tmp/testseek", "Error"]" 1
[root@noed1 ~]# cat /usr/local/etc/zabbix_scripts/mysql_repliction_status.sh #!/bin/bash USER="root" PASSWD="123123123" result=`mysql -u${USER} -p${PASSWD} -e "show slave status\G" | grep "Running:"|awk ‘{print$2}‘|grep -c Yes` if [ $result -eq 2 ];then echo "0" else echo "1" fi
修改被监控端配置文件
[root@noed1 zabbix_scripts]# sed -n ‘335p‘ ../zabbix_agentd.conf UserParameter=mysql_replication,/usr/local/etc/zabbix_scripts/mysql_repliction_status.sh
在监控端取值
[root@zabbix_server ~]# zabbix_get -s 192.168.248.200 -k "mysql_replication" 0
MariaDB [(none)]> stop slave; Query OK, 0 rows affected (0.002 sec)
原文:https://www.cnblogs.com/diqiyao/p/14715830.html