文章目录
zabbix自定义监控(进程、日志)
1.监控进程
例:监控httpd的进程,首先得yum安装httpd
[root@centos8-1 ~]# yum -y install httpd
上次元数据过期检查:15:31:35 前,执行于 2021年10月07日 星期四 10时46分24秒。
依赖关系解决。
==========================================================================================================
软件包 架构 版本 仓库 大小
==========================================================================================================
[root@centos8-1 ~]# systemctl start httpd
[root@centos8-1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@centos8-1 ~]#
更改配置文件
[root@centos8-1 ~]# cd /usr/local/etc/
[root@centos8-1 etc]# vim zabbix_agentd.conf
# Mandatory: no
# Range: 0-1
# Default:
UnsafeUserParameters=1 //取消注释,并改为1
1.1.写脚本
[root@centos8-1 ~]# mkdir /scripts
[root@centos8-1 ~]# cd /scripts/
[root@centos8-1 scripts]# touch httpd.sh
[root@centos8-1 scripts]# chmod +x httpd.sh
[root@centos8-1 scripts]# cat httpd.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@centos8-1 scripts]#
[root@centos8-1 scripts]# ./httpd.sh httpd
0
[root@centos8-1 scripts]# systemctl stop httpd
[root@centos8-1 scripts]# ./httpd.sh httpd
1
[root@centos8-1 scripts]#
1.2.修改配置文件
[root@centos8-1 etc]# vim zabbix_agentd.conf
# ListenBacklog=
UserParameter=check_httpd[*],/scripts/httpd.sh $1 //在文件最后添加改内容
[root@centos8-1 scripts]# pkill zabbix
[root@centos8-1 scripts]# zabbix_agentd //重启
1.3.添加监控项
[root@node2 ~]# zabbix_get -s 192.168.100.146 -k check_httpd[httpd]
0 //在服务端查看我们得check_httpd是否正确
[root@node2 ~]#
1.4.添加触发器
[root@centos8-1 etc]# systemctl stop httpd //此时我们停掉httpd
[root@centos8-1 etc]#
结果:
2.监控日志
2.1,写脚本
写脚本
[root@centos8-1 ~]# cd /scripts/
[root@centos8-1 scripts]# chmod +x log.py
[root@centos8-1 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@centos8-1 scripts]#
2.2.修改配置文件
[root@centos8-1 etc]# vim zabbix_agentd.conf
# ListenBacklog=
UserParameter=check_httpd[*],/scripts/httpd.sh $1
UserParameter=check_log[*],/scripts/log.py $1 $2 $3 //添加此内容
[root@centos8-1 scripts]# pkill zabbix
[root@centos8-1 scripts]# zabbix_agentd //重启
2.3.添加监控项
2.4.添加触发器
结果:
[root@centos8-1 scripts]# echo "Error" >> /var/log/httpd/error_log
[root@centos8-1 scripts]# ./log.py /var/log/httpd/error_log
1
[root@centos8-1 scripts]#