zabbix自定义监控进程和监控日志
文章目录
自定义监控:
- 进程
- 日志
- mysql主从状态
- mysql主从延迟
监控进程
安装httpd
[root@localhost ~]# dnf -y install httpd
[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# ss -anlt
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 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# ps -ef|grep httpd
root 12994 1 0 13:16 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 12995 12994 0 13:16 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 12996 12994 0 13:16 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 12997 12994 0 13:16 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 12998 12994 0 13:16 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 14988 1522 0 13:17 pts/0 00:00:00 grep --color=auto httpd
创建一个脚本
[root@localhost etc]# mkdir -p /scripts
[root@localhost etc]# cd /scripts/
[root@localhost scripts]# ls
[root@localhost scripts]# vim check_httpd.sh
#!/bin/bash
count=$(ps -ef|grep -Ev "grep|$0" |grep httpd | wc -l)
if [ $count -eq 0 ];then
echo '1'
else
echo '0'
fi
[root@localhost scripts]# chmod +x check_httpd.sh
[root@localhost scripts]# systemctl stop httpd
[root@localhost scripts]# ./check_httpd.sh
1
[root@localhost scripts]# systemctl start httpd.service
[root@localhost scripts]# ./check_httpd.sh
0
监控所有进程
[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 $1 | wc -l)
if [ $count -eq 0 ];then
echo '1'
else
echo '0'
fi
[root@localhost scripts]# ./check_process.sh httpd
0
[root@localhost scripts]# ./check_process.sh mysql
1
[root@localhost scripts]# ./check_process.sh zabbix
0
[root@localhost scripts]# ./check_process.sh httpd
0
[root@localhost scripts]# ./check_process.sh ssh
0
修改配置文件
[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# ls
yyz zabbix_agentd.conf zabbix_agentd.conf.d
[root@localhost etc]# vim zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
重启一下
[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd
[root@localhost etc]# grep check_process zabbix_agentd.conf
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
检查配置可用
[root@yz ~]# zabbix_get -s 192.168.17.130 -k check_process[httpd]
0
[root@localhost etc]# systemctl stop httpd.service
[root@yz ~]# zabbix_get -s 192.168.17.130 -k check_process[httpd]
1
[root@yz ~]# zabbix_get -s 192.168.17.130 -k check_process[mysql]
1
配置监控项
报错的结果出来了
配置触发器
收到报警邮箱
监控日志
log文件上传上去,然后给它一个可执行的权限
[root@localhost scripts]# ls
check_process.sh log.py
[root@localhost scripts]# chmod +x log.py
[root@localhost scripts]# ls
check_process.sh log.py
[root@localhost scripts]# ll
total 8
-rwxr-xr-x 1 root root 120 Sep 6 13:30 check_process.sh
-rwxr-xr-x 1 root root 1854 Sep 6 14:08 log.py
[root@localhost httpd]# chmod 755 /var/log/httpd/
[root@localhost httpd]# ll /var/log/httpd/
total 4
-rw-r--r-- 1 root root 0 Sep 6 09:27 access_log
-rw-r--r-- 1 root root 3920 Sep 6 13:53 error_log
安装python
[root@localhost scripts]# dnf provides python3
[root@localhost scripts]# dnf -y install python3
加点内容看发生变化
[root@localhost scripts]# cat /tmp/logseek
3920[root@localhost scripts]#
[root@localhost httpd]# echo "xixi" >> error_log
[root@localhost httpd]# echo "xixi" >> error_log
[root@localhost httpd]# echo "xixi" >> error_log
[root@localhost httpd]# echo "xixi" >> error_log
[root@localhost scripts]# ./log.py /var/log/httpd/error_log
0
[root@localhost scripts]# cat /tmp/logseek
3940[root@localhost scripts]#
发现区分大小写
[root@localhost httpd]# echo 'error' >> error_log
[root@localhost scripts]# ./log.py /var/log/httpd/error_log
0
[root@localhost httpd]# echo 'Error' >> error_log
[root@localhost scripts]# ./log.py /var/log/httpd/error_log
1
修改配置文件,顺便检查服务端
[root@localhost etc]# vim zabbix_agentd.conf
UserParameter=check_logs[*],/scripts/log.py $1 $2 $3
检查发现权限呗拒绝
[root@yz ~]# zabbix_get -s 192.168.17.130 -k check_logs['/var/log/httpd/error_log']
Traceback (most recent call last):
File "/scripts/log.py", line 84, in <module>
result = getResult(sys.argv[1],seekfile,tagkey)
File "/scripts/log.py", line 73, in getResult
with open(seekfile,'w') as sf:
PermissionError: [Errno 13] Permission denied: '/tmp/logseek'
删除这个文件
[root@localhost etc]# rm -f /tmp/logseek
发现检查无误 ,可以配置网页
[root@yz ~]# zabbix_get -s 192.168.17.130 -k check_logs['/var/log/httpd/error_log']
1
[root@localhost etc]# ll /tmp/
total 20
-rw-rw-r-- 1 zabbix zabbix 4 Sep 6 14:32 logseek
drwx------ 2 root root 6 Sep 6 13:10 vmware-root_926-2731217702
drwx------. 2 root root 6 Sep 5 20:01 vmware-root_927-3980167416
drwx------ 2 root root 6 Sep 6 09:21 vmware-root_930-2722763397
drwx------. 2 root root 6 Sep 5 19:57 vmware-root_932-2722632322
drwx------. 2 root root 6 Sep 5 20:06 vmware-root_942-2697663758
-rw-rw-r--. 1 zabbix zabbix 11041 Sep 6 14:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix 6 Sep 6 14:30 zabbix_agentd.pid
[root@yz ~]# zabbix_get -s 192.168.17.130 -k check_logs['/var/log/httpd/error_log']
0
添加监控项
添加触发器
写一个错误的看看能不能收到报警邮箱
[root@localhost httpd]# echo 'Error' >> error_log