zabbix监控
服务 | ip | 主机名 |
---|
服务端 | 192.168.87.129 | server | 客户端 | 192.168.87.133 | agent |
一、自定义监控进程
1.以httpd服务为例,在agent中安装httpd
[root@agent ~]# yum install -y httpd
[root@agent ~]# systemctl restart httpd
[root@agent ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@agent ~]# ps -ef | grep httpd | grep -v grep
root 1976 1 0 18:43 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1977 1976 0 18:43 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1978 1976 0 18:43 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1979 1976 0 18:43 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1980 1976 0 18:43 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
[root@agent ~]# ps -ef | grep httpd | grep -v grep| wc -l
5
2.新建脚本存放目录
[root@agent ~]# mkdir /etc/zabbix/script
[root@agent ~]# cd /etc/zabbix/script/
[root@agent script]# vim check_httpd.sh
[root@agent script]# cat 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@agent script]# chmod +x check_httpd.sh
[root@agent script]# chown -R zabbix.zabbix /etc/zabbix/script/
[root@agent script]# ll
total 4
-rwxr-xr-x 1 zabbix zabbix 117 Sep 6 18:47 check_httpd.sh
3.测试脚本–0是httpd服务开启,1为关闭
[root@agent script]# ./check_httpd.sh
0
[root@agent script]# systemctl stop httpd
[root@agent script]# ./check_httpd.sh
1
4.修改zabbix_agentd.conf文件
[root@agent script]# vim /etc/zabbix/zabbix_agentd.conf
UserParameter=logusers,who | wc -l
UserParameter=check_httpd,/bin/bash /etc/zabbix/script/check_httpd.sh
[root@agent script]# systemctl restart zabbix-agent.service
服务端测试脚本
[root@server ~]# zabbix_get -s 192.168.87.133 -k check_httpd
0
5.zabbix web平台配置 添加监控项
添加触发器
6.测试–关闭httpd服务,测试告警信息
[root@agent script]# systemctl stop httpd
[root@agent script]# ./check_httpd.sh
1
登录163邮箱查看信息
当httpd重新启动是
[root@agent script]# systemctl restart httpd
[root@agent script]# ./check_httpd.sh
0
二、自定义监控日志
1.将log.py上传到/etc/zabbix/script/目录下,然后给执行权限,修改所有者和所属组为zabbix
[root@agent script]# rz -E
rz waiting to receive.
[root@agent script]# ll
total 8
-rwxr-xr-x 1 zabbix zabbix 117 Sep 6 18:47 check_httpd.sh
-rw-r--r-- 1 root root 1854 Sep 6 16:09 log.py
[root@agent script]# chmod +x log.py
[root@agent script]# chown -R zabbix.zabbix /etc/zabbix/script/
[root@agent script]# ll
total 8
-rwxr-xr-x 1 zabbix zabbix 117 Sep 6 18:47 check_httpd.sh
-rwxr-xr-x 1 zabbix zabbix 1854 Sep 6 16:09 log.py
2.让zabbix用户有权限去访问/var/log/httpd/目录
[root@agent log]# setfacl -m u:zabbix:r-x httpd/
3.下载python3来执行log.py脚本
[root@agent log]# yum install -y python3
4.修改zabbix_agentd.conf文件,并重启服务
[root@agent ~]# vim /etc/zabbix/zabbix_agentd.conf
UserParameter=logusers,who | wc -l
UserParameter=check_httpd,/bin/bash /etc/zabbix/script/check_httpd.sh
UserParameter=check_logs[*],/usr/bin/python3 /etc/zabbix/script/log.py $1 $2 $3
[root@agent ~]# systemctl restart zabbix-agent.service
5.测试脚本 0为没有ErrError日志信息or日志信息,1为有
[root@agent ~]# cd /etc/zabbix/script
[root@agent script]# python3 log.py /var/log/httpd/error_log
0
[root@agent script]# echo "Error" >> /var/log/httpd/error_log
[root@agent script]# python3 log.py /var/log/httpd/error_log
1
测试完成,去到error日志中删去添加的‘Error’
因文件/tmp/logseek属于root账户,在web端写入写不进去,所以删除。
[root@agent script]# rm -rf /tmp/logseek
6.配置监控项
7.创建触发器
8.测试,echo Error >> /var/log/httpd/error_log
[root@agent script]# echo "Error" >> /var/log/httpd/error_log
[root@agent script]# python3 log.py /var/log/httpd/error_log
1
当删除Error时
|