zabbix自定义监控
1.配置监控脚本
//修改配置文件
[root@zwl ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
//重启服务
[root@zwl ~]# pkill zabbix
[root@zwl ~]# zabbix_agentd
[root@zwl ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10051 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
[root@zwl ~]#
//编写脚本
[root@zwl ~]# vim /scripts/check_process.sh
[root@zwl ~]# cat /scripts/check_process.sh
#!/bin/bash
count=`ps -ef| grep $1 |grep -Ev "grep|$0" |wc -l`
if [ $count == 0 ];then
echo "1"
else
echo "0"
fi
[root@zwl ~]# chmod +x /scripts/check_process.sh
[root@zwl ~]# 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@zwl ~]#
//去服务端检查key是否可用
[root@167 ~]# zabbix_get -s 192.168.159.168 -k check_process[httpd]
0
[root@167 ~]# zabbix_get -s 192.168.159.168 -k check_process[mysql]
1
[root@167 ~]#
添加监控项
查看监控的数据
添加触发器
手动关闭httpd服务,触发报警
[root@zwl ~]# 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@zwl ~]# systemctl stop httpd.service
[root@zwl ~]# 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@zwl ~]#
自定义监控日志
编写脚本
//安装python环境
[root@zwl ~]# dnf -y install python36
[root@zwl scripts]# chmod +x log.py
[root@zwl scripts]# ll
total 180
-rwxr-xr-x 1 root root 132 Sep 6 20:14 check_process.sh
-rwxr-xr-x 1 root root 177585 Sep 6 20:44 log.py
[root@zwl scripts]#
//修改agentd配置文件
[root@zwl ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_logs[*],/scripts/log.py $1 $2 $3
//重启服务
[root@zwl ~]# pkill zabbix_agentd
[root@zwl ~]# zabbix_agentd
[root@zwl ~]# 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@zwl ~]#
//给/var/log/httpd目录权限
[root@zwl ~]# ll -d /var/log/httpd/
drwx------ 2 root root 41 Sep 6 20:18 /var/log/httpd/
[root@zwl ~]# chmod 755 /var/log/httpd/
[root@zwl ~]# ll -d /var/log/httpd/
drwxr-xr-x 2 root root 41 Sep 6 20:18 /var/log/httpd/
//给httpd的error日志,添加一个错误信息
[root@zwl ~]# echo "Error" >> /var/log/httpd/error_log
//测试一下该脚本是否可用
[root@zwl ~]# /scripts/log.py /var/log/httpd/error_log
1
[root@zwl ~]# /scripts/log.py /var/log/httpd/error_log
0
[root@zwl ~]# cat /tmp/logseek
1013
[root@zwl ~]# echo "Error" >> /var/log/httpd/error_log
[root@zwl ~]# echo "Error" >> /var/log/httpd/error_log
[root@zwl ~]# echo "Error" >> /var/log/httpd/error_log
[root@zwl ~]# cat /tmp/logseek
1031
//去服务端检测key是否可用
[root@167 etc]# zabbix_get -s 192.168.159.168 -k check_logs[/var/log/httpd/error_log1
1
[root@167 etc]# zabbix_get -s 192.168.159.168 -k check_logs[/var/log/httpd/error_log]
0
查看监控数据
添加触发器
触发报警
[root@zwl ~]# echo "Error" >> /var/log/httpd/error_log