zabbix-自定义进程监控
2022-06-07 本文已影响0人
无味wy
监控进程步骤:
编写脚本,脚本放到统一的位置
修改/usr/local/etc/zabbix_agentd.conf文件
UnsafeUserParameters=1
UserParameter= < key > , < shell command >
重启zabbix_agent
在web界面配置监控项和触发器
编写脚本
编写脚本前我们要确定一个防止脚本的位置,因为管理的客户端不止一台主机,如果每台主机上面的脚本放置脚本的位置不一样,在管理起来的时候只会更复杂
[root@localhost ~]# mkdir /scripts
我们以监控httpd服务进程为例来编写脚本,过滤进程
//开启服务
[root@localhost ~]# systemctl start httpd
//过滤进程
[root@localhost ~]# ps -ef | grep -v grep | grep httpd | wc -l
5
[root@localhost ~]# ps -ef | grep -v grep | grep -c httpd
5
//以上两种过滤方式均可,选择一种使用
编写脚本
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# touch check_httpd.sh
[root@localhost scripts]# chmod +x check_httpd.sh
[root@localhost scripts]# vim check_httpd.sh
#!/bin/bash
count=$(ps -ef | grep -Ev "grep|$0" | grep -c httpd)
if [ $count -eq 0 ];then
echo '1'
else
echo '0'
fi
//测试一下脚本
[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
修改zabbix_agentd.conf文件
[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# vim zabbix_agentd.conf
//搜索UnsafeUserParameters,把UnsafeUserParameters=0改为取消注释并且UnsafeUserParameters=1
//在文章结尾添加UserParameter=<key>,<command>
UserParameter=check_httpd,/scripts/check_httpd.sh
//check_httpd是key ,key 是由/scripts/check_httpd.sh这个脚本执行所获得
重启服务
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd
[root@localhost ~]# 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 [::]:*
我们在配置文件中写了key,可以在服务端测试一下,使用zabbix_get 命令
[root@localhost ~]# zabbix_get -s 192.168.218.133 -k check_httpd
0
//客户端服务在开启状态,所以执行脚本所获得的key是1,测试key可以正常使用
web界面配置监控项


web界面配置触发器

测试触发器,关闭客户端httpd服务
[root@localhost ~]# systemctl stop httpd
[root@localhost ~]# 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 [::]:22 [::]:*

以上自定义进程监控配置完成,但是此配置存在一些弊端,只能监控单个服务进程,如果同时需要监控多个进程就不可以了,以下演示优化此配置,使之可以监控多个进程
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# mv check_httpd.sh check_process.sh //把脚本名字修改一下,见名知意
[root@localhost scripts]# vim check_process.sh
#!/bin/bash
count=$(ps -ef | grep -Ev "grep|$0" | grep -c "$1") //这里把之前的httpd改为$1,位置变量
if [ $count -eq 0 ];then
echo '1'
else
echo '0'
fi
//测试脚本
[root@localhost scripts]# ./check_process.sh httpd
0 //httpd服务开启状态
修改配置文件并重启
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
//把之前编辑的UserParameter=check_httpd,/scripts/check_httpd.sh改为
UserParameter=check_process[*],/scripts/check_process.sh $1
#重启服务
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd
[root@localhost ~]# 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 [::]:*
测试key check_process[*]
[root@localhost ~]# 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 ~]# zabbix_get -s 192.168.218.133 -k check_process[httpd]
0 //后台有httpd进程
[root@localhost ~]# zabbix_get -s 192.168.218.133 -k check_process[mysqld]
1 //后台没有mysql进程
web界面配置监控项

查看监控项是否配置成功

httpd服务开启的时候,value的值是0,配置成功
如果需要新的服务加空项,可以使用clone,把key的内容改为自己需要的进程即可