zabbix配置自定义监控
目录
zabbix配置自定义监控项---进程监控
环境说明
| 服务端 | 192.168.32.136 |
|---|---|
| 客户端 | 192.169.32.125 |
服务端和客户端一配置完成,参考文档:
1. 编写获取进程状态的脚本
[root@client ~]# mkdir /scripts
[root@client ~]# cd /scripts/
[root@client scripts]# vim check_process.sh
#!/bin/bash
process_status=$(ps -ef | grep $1 | grep -Ev "$0|grep" |wc -l)
if [ $process_status -eq 0 ];then
echo '1'
else
echo '0'
fi
//进程存在就输出0,进程不存在就输出1
[root@client scripts]# ls
check_process.sh
[root@client scripts]# chmod +x check_process.sh
2. 修改配置文件,添加自定义key
[root@client scripts]# vim /usr/local/etc/zabbix_agentd.conf
......
# UnsafeUserParameters=0
UnsafeUserParameters=1 //0改为1
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=<key>,<shell command>
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
//改完之后重启zabbix_agentd
3. 配置监控项


4. 添加触发器


5. 媒介和动作
媒介和动作使用之前脚本的方式



6. 触发并验证
//客户端关闭服务
[root@client etc]# systemctl stop postfix

查看邮箱

zabbix配置自定义监控项---日志监控
1. 客户端编写获取进程状态的脚本
[root@client ~]# vim /scripts/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)
2. 在客户端编辑配置文件
[root@client ~]# vim /usr/local/etc/zabbix_agentd.conf
......
UnsafeUserParameters=1
......
UserParameter=check_log[*],/usr/bin/python /scripts/log.py $1 $2 $3
#第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
#第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)
#第三个参数为搜索关键字,默认为 Error
#配置完成之后重启服务
#让zabbix对/var/log/secure有读权限
[root@client ~]# setfacl -m u:zabbix:r /var/log/secure
3. 配置监控项和触发器


已经配置了邮箱告警,这里直接触发
[root@localhost ~]#
[root@localhost ~]# ssh root@192.168.32.125
root@192.168.32.125's password:
Permission denied, please try again.



浙公网安备 33010602011771号