编写脚本
[root@localhostetc]# mkdir /scripts
[root@localhost scripts]# touch check_httpd.sh
[root@localhost scripts]# chmod +x check_httpd.sh
[root@localhost scripts]# vim 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@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# vim zabbix_agentd.conf
...
UnsafeUserParameters=1
...
# 配置文件末尾添加
UserParameter=check_process[*],/scripts/check_process.sh $1
//“check_process[*]”是在web界面中要写的key *表示某个服务,$1的参数来自于check_process[*]
也就是说check_process[*]中*是什么服务,$1就会传什么服务名
例如httpd:
web界面中的key是 check_process[httpd] 说明是监控httpd服务是否开启
重启
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd
测试
[root@localhost scripts]# ./check_httpd.sh
1
[root@localhost scripts]# 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@localhost scripts]# ./check_httpd.sh
0
//服务是开启状态,所有测试脚本的时候是0,如果关闭服务,运行脚本结果则为1
web界面监控
关闭客户端httpd服务
[root@localhost scripts]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
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@localhost scripts]# systemctl stop httpd.service