zabbix自定义监控进程和日志
目录自定义监控进程
配置监控脚本
给客户端dnf安装httpd服务,并启动服务
[root@136 ~]# dnf -y install httpd
[root@136 ~]# systemctl start httpd
创建脚本存放目录,并编辑脚本
[root@136 ~]# mkdir /etc/scripts
[root@136 ~]# cd /etc/scripts/
[root@136 scripts]# vim check_process.sh
#!/bin/bash
count=$(ps -ef |grep -Ev "grep|$0"|grep $1|wc -l)
if [ $count -eq 0 ];then
echo 0
else
echo 1
fi
[root@136 scripts]# chmod +x check_process.sh
配置zabbix_agentd文件
[root@136 ~]# cd /usr/local/etc/
[root@136 etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@136 etc]# vim zabbix_agentd.conf
...
UnsafeUserParameters=1
UserParameter=check_process[*],/bin/bash /etc/scripts/check_process.sh $1
#更改配置文件后重启zabbix服务
[root@136 etc]# pkill zabbix
[root@136 etc]# zabbix_agentd
(服务端)检测一下check_process能否使用
[root@zabbix ~]# zabbix_get -s 192.168.118.136 -k check_process[httpd]
0
#客户端httpd服务正常运行中,所以显示为0
[root@zabbix ~]# zabbix_get -s 192.168.118.136 -k check_process[mysql]
1
#客户端没有安装mysql服务,所以显示为1
添加监控项
查看监控数据
添加触发器
手动关闭httpd服务,触发报警
[root@136 ~]# systemctl stop httpd
发生报错
发送了报错邮件
查看邮件,下载文件查看报错信息
自定义监控日志
服务端和客户端操作
编写脚本
这里用到python脚本,所有需要安装python环境
[root@136 ~]# dnf -y install python36
由于我们是初学者
脚本内容直接借鉴忘情公子提供的log脚本日志
[root@136 ~]# cd /etc/scripts/
[root@136 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@136 scripts]# chmod +x log.py
[root@136 scripts]# ls
check_process.sh log.py
修改httpd日志目录权限
[root@136 ~]# chmod 755 /var/log/httpd/
修改配置文件,重启服务
[root@136 httpd]# cd /usr/local/etc/
[root@136 etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@136 etc]# vim zabbix_agentd.conf
...
UserParameter=check_logs[*],/etc/scripts/log.py $1 $2 $3
#直接在最后一行加入此内容
#重启服务
[root@136 etc]# pkill zabbix
[root@136 etc]# zabbix_agentd
服务端测试
#先删除(客户机)上的logseek文件,原logseek文件是属于root用户,zabbix没有权限访问,删除后去服务端运行测试命令会生成一个属于zabbix用户的logseek文件。
[root@136 etc]# rm -f /tmp/logseek
#再去服务端进行测试
[root@zabbix ~]# zabbix_get -s 192.168.118.136 -k check_logs['/var/log/httpd/error_log']
1
[root@zabbix ~]# zabbix_get -s 192.168.118.136 -k check_logs['/var/log/httpd/error_log']
0
添加监控项
添加触发器
手动触发警告,查看警告
[root@136 etc]# cd /var/log/httpd/
[root@136 httpd]# ls
access_log error_log
[root@136 httpd]# echo 'Error' >> error_log
标签:httpd,log,自定义,etc,zabbix,136,日志,root
From: https://www.cnblogs.com/Clannaddada/p/16663505.html