zabbix自定义监控进程

本文介绍了如何在Zabbix中进行自定义监控,包括监控进程和日志。首先,通过创建postfix用户和启动httpd进程来设置基础。接着,编写脚本并检验,配置Zabbix命令进行进程监控。在服务器端验证后,创建监控项和触发器,确保当特定条件满足时能发送邮件通知。对于日志监控,下载预配置脚本,调整权限,配置文件并执行验证。最后,在Zabbix界面完成配置,并创建触发器,通过故意触发错误来验证监控系统的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义监控

登入会自动创建了一个postfix用户
在这里插入图片描述

[root@cys ~]# tail /etc/passwd
pesign:x:990:986:Group for the pesign signing daemon:/var/run/pesign:/sbin/nologin
chrony:x:989:985::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
cys:x:1000:1000:cys:/home/cys:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologin
nginx:x:988:984:Nginx web server:/var/lib/nginx:/sbin/nologin
zabbix:x:987:983:Zabbix Monitoring System:/usr/lib/zabbix:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@cys ~]# 

进程

在被监控的虚拟机写
没有网站先创建一个网站

[root@web etc]# dnf -y install httpd
...

启动httpd

[root@web etc]# systemctl start httpd
[root@web etc]# 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                  *:80                 *:*            
LISTEN 0       128               [::]:22              [::]:*            
[root@web etc]# 

查看需要监控的进程

[root@web etc]# ps -ef |grep httpd
root        2183       1  0 22:34 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      2193    2183  0 22:35 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      2194    2183  0 22:35 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      2195    2183  0 22:35 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      2196    2183  0 22:35 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root        2425    1551  0 22:35 pts/0    00:00:00 grep --color=auto httpd
[root@web etc]# ps -ef |grep -v grep |grep httpd |wc -l
5

写一个脚本

[root@web etc]# mkdir /scripts
[root@web 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@web scripts]# chmod +x check_httpd.sh 
[root@web scripts]# ./check_httpd.sh
0
[root@web scripts]# 

检验一下脚本写的有没有问题

[root@web scripts]# systemctl stop httpd.service 
[root@web scripts]# ./check_httpd.sh
1
[root@web scripts]# systemctl start httpd.service 
[root@web scripts]# 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                  *:80                 *:*            
LISTEN 0       128               [::]:22              [::]:*            
[root@web scripts]# ./check_httpd.sh
0
[root@web scripts]# 

监控进程

[root@web scripts]# mv check_httpd.sh check_process.sh 
[root@web scripts]# ls
check_process.sh
[root@web 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@web scripts]# ./check_process.sh httpd
0
[root@web scripts]# ./check_process.sh mysql
1
[root@web scripts]# 

给zabbix配置命令

[root@web ~]# cd /usr/local/etc/
[root@web etc]# vim zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
"zabbix_agentd.conf" 549L, 17126C written   
[root@web etc]# 
[root@web etc]# pkill zabbix
[root@web etc]# zabbix_agentd 
[root@web etc]# 

在服务端检验

[root@cys ~]# zabbix_get -s 192.168.64.130 -k check_process[httpd]
0
[root@cys ~]# zabbix_get -s 192.168.64.130 -k check_process[mysql]
1
[root@cys ~]# 

在这里插入图片描述
创建监控项
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
创建触发器
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
这里就会发邮件
在这里插入图片描述
在这里插入图片描述

日志监控

先下载一个配置好的脚本

[root@web scripts]# wget https://github.com/chendao2015/pyscripts/blob/master/log.py
--2022-09-07 00:07:21--  https://github.com/chendao2015/pyscripts/blob/master/log.py
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘log.py’

log.py                [  <=>         ] 173.42K   712KB/s    in 0.2s    

2022-09-07 00:07:22 (712 KB/s) - ‘log.py’ saved [177581]

[root@web scripts]# ls
check_process.sh  log.py
[root@web scripts]# chmod +x log.py 
[root@web scripts]# ll
total 180
-rwxr-xr-x 1 root root    118 Sep  6 22:52 check_process.sh
-rwxr-xr-x 1 root root 177581 Sep  7 00:07 log.py
[root@web scripts]# 

给httpd所有人都有权限访问

[root@web ~]# cd /var/log/httpd/
[root@web httpd]# ls
access_log  error_log
[root@web httpd]# ll
total 4
-rw-r--r-- 1 root root    0 Sep  6 22:34 access_log
-rw-r--r-- 1 root root 2022 Sep  6 23:47 error_log
[root@web httpd]# chmod 755 /var/log/httpd/

执行验证一下

[root@web scripts]# /scripts/log.py /var/log/httpd/error_log
0
[root@web scripts]# ls /tmp/
abc                         vmware-root_950-2697008400
logseek                     zabbix_agentd.log
vmware-root_944-2697139479  zabbix_agentd.pid
vmware-root_946-2688685205
[root@web scripts]# cat /tmp/logseek 
2022[root@web scripts]# 

[root@web httpd]# echo "wdf" >> error_log
[root@web httpd]# echo "wdf" >> error_log
[root@web httpd]# echo "wdf" >> error_log
[root@web httpd]# 
[root@web scripts]# /scripts/log.py /var/log/httpd/error_log
0
[root@web scripts]# cat /tmp/logseek 
[root@web httpd]# echo "error" >> error_log
[root@web httpd]# echo "Error" >> error_log
2034[root@web scripts]# /scripts/log.py /var/log/httpd/error_log
1

在添加一个配置文件

[root@web scripts]# cd /usr/local/etc/
[root@web etc]# ls
zabbix_agentd.conf  zabbix_agentd.conf.d
[root@web etc]# vim zabbix_agentd.conf

UserParameter=check_logs[*],/scripts/log.py $1 $2 $3
[root@web etc]# pkill zabbix			//重启进程
[root@web etc]# zabbix_agentd 
[root@web etc]# 

执行验证一下,错误的话就先把/tmp/logseek 下删了就行

[root@cys ~]# zabbix_get -s 192.168.64.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@web etc]# rm -f /tmp/logseek 
[root@web etc]# ll /tmp/
total 16
-rw-r--r--. 1 root   root     45 Sep  6 03:12 abc
-rw-rw-r--  1 zabbix zabbix    4 Sep  7 01:14 logseek
drwx------  2 root   root      6 Sep  6 22:17 vmware-root_944-2697139479
drwx------. 2 root   root      6 Sep  4 23:07 vmware-root_946-2688685205
drwx------. 2 root   root      6 Sep  5 23:20 vmware-root_950-2697008400
-rw-rw-r--. 1 zabbix zabbix 3650 Sep  7 01:11 zabbix_agentd.log
-rw-rw-r--  1 zabbix zabbix    5 Sep  7 01:11 zabbix_agentd.pid
[root@cys ~]# zabbix_get -s 192.168.64.130 -k check_logs['/var/log/httpd/error_log']
1
[root@cys ~]# 

然后去网页配置一下
在这里插入图片描述
在这里插入图片描述
配置好了添加
在这里插入图片描述
在创建一个触发器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
再去验证一下故意写一个错误

[root@web httpd]# echo "Error" >> error_log
[root@web httpd]# 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值