zabbix自定义监控进程和日志
自定义监控
进程
[root@client ~]# cd /usr/local/etc/
[root@client etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@client 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 [::]:22 [::]:*
[root@client etc]# dnf -y install httpd
...
[root@client etc]# systemctl start httpd
[root@client 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@client etc]# ps -ef | grep httpd
root 831117 1 0 14:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 831789 831117 0 14:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 831791 831117 0 14:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 831792 831117 0 14:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 831793 831117 0 14:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 834012 706474 0 14:13 pts/0 00:00:00 grep --color=auto httpd
[root@client etc]# ps -ef | grep -v grep | grep httpd | wc -l
5
[root@client etc]# mkdir /scripts
[root@client etc]# cd /scripts/
[root@client scripts]# ls
[root@client 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@client scripts]# chmod +x check_httpd.sh
[root@client scripts]# systemctl stop httpd
[root@client scripts]# ./check_httpd.sh
1
[root@client scripts]# ps -ef | grep httpd
root 854234 706474 0 14:25 pts/0 00:00:00 grep --color=auto httpd
[root@client scripts]# ps -ef | grep -v grep | grep httpd | wc -l
0 (因为脚本本身有http)
[root@client scripts]# systemctl start httpd
[root@client 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@client scripts]#
[root@client scripts]# ps -ef | grep httpd
root 877386 1 0 14:40 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 878090 877386 0 14:40 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 878091 877386 0 14:40 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 878092 877386 0 14:40 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 878093 877386 0 14:40 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 878791 706474 0 14:40 pts/0 00:00:00 grep --color=auto httpd
[root@client scripts]# ./check_httpd.sh
0
监控多个服务
[root@client scripts]# ls
check_httpd.sh
[root@client scripts]# mv check_httpd.sh check_process.sh
[root@client scripts]# ls
check_process.sh
[root@client 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@client scripts]# ls
check_process.sh
[root@client 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@client scripts]# ./check_process.sh httpd (有httpd)
0
[root@client scripts]# ./check_process.sh mysql (没有mysql)
1
[root@client scripts]# ./check_process.sh zabbix (有zabbix)
0
[root@client scripts]# cd
[root@client ~]# ll /scripts/
total 4
-rwxr-xr-x. 1 root root 153 Sep 6 14:53 check_process.sh
[root@client ~]# cd /usr/local/etc/
[root@client etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@client etc]# vim zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
[root@client etc]# pkill zabbix
[root@client etc]# zabbix_agentd
[root@client etc]# tail zabbix_agentd.conf
# The maximum number of pending connections in the queue. This parameter is passed to
# listen() function as argument 'backlog' (see "man listen").
#
# Mandatory: no
# Range: 0 - INT_MAX (depends on system, too large values may be silently truncated to implementation-specified maximum)
# Default: SOMAXCONN (hard-coded constant, depends on system)
# ListenBacklog=
UnsafeUserParameters=1
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
[root@client etc]# grep process zabbix_agentd.conf
# 0 - basic information about starting and stopping of Zabbix processes
# Parameters are processed one by one according their appearance order.
# Parameters are processed one by one according their appearance order.
# Number of pre-forked instances of zabbix_agentd that process passive checks.
# Host metadata is used at host auto-registration process.
# Host metadata is used at host auto-registration process.
# Host interface is used at host auto-registration process.
# Host interface is used at host auto-registration process.
# or Proxy processing 'log' and 'logrt' active checks.
# Spend no more than Timeout seconds on processing
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
服务端
[root@localhost ~]# zabbix_get -s 192.168.29.141 -k check_process[httpd]
0
[root@localhost ~]# zabbix_get -s 192.168.29.141 -k check_process[mysql]
1
[root@client etc]# systemctl stop httpd
[root@localhost ~]# zabbix_get -s 192.168.29.141 -k check_process[httpd]
1
配置触发器
日志
同进程监控使用脚本
https://github.com/chendao2015/pyscripts
[root@client ~]# vim log.py
[root@client ~]# ls
anaconda-ks.cfg httpd_html log.py web
[root@client ~]# mv log.py /scripts/
[root@client ~]# cd /scripts/
[root@client scripts]# ll
total 8
-rwxr-xr-x. 1 root root 153 Sep 6 14:53 check_process.sh
-rw-r--r--. 1 root root 1989 Sep 6 16:05 log.py
[root@client scripts]# chmod +x log.py
[root@client scripts]# ll
total 8
-rwxr-xr-x. 1 root root 153 Sep 6 14:53 check_process.sh
-rwxr-xr-x. 1 root root 1989 Sep 6 16:05 log.py
[root@client ~]# cd /var/log/httpd/
[root@client httpd]# ls
access_log error_log
[root@client httpd]# ll
total 4
-rw-r--r--. 1 root root 0 Sep 6 14:11 access_log
-rw-r--r--. 1 root root 3549 Sep 6 15:16 error_log
[root@client httpd]# chmod 755 /var/log/httpd/
[root@client httpd]# ll
作用:检查日志文件中是否有指定的关键字
第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)
第三个参数为搜索关键字,默认为 Error
[root@client scripts]# yum -y install python3
[root@client scripts]# chmod +x log.py
[root@client scripts]# ll
total 8
-rwxr-xr-x. 1 root root 153 Sep 6 14:53 check_process.sh
-rwxr-xr-x. 1 root root 1854 Sep 6 16:20 log.py
[root@client scripts]# ./log.py /var/log/httpd/error_log
0
[root@client scripts]# ls /tmp/
abc vmware-root_932-2722632322
logseek vmware-root_941-4022177618
vmware-root_866-2722763301 zabbix_agentd.log
vmware-root_916-2689078442 zabbix_agentd.pid
vmware-root_927-3980167416
[root@client scripts]# cat /tmp/logseek
3549[root@client scripts]#
[root@client httpd]# echo "mary" >> error_log
[root@client httpd]# echo "mary" >> error_log
[root@client httpd]# echo "mary" >> error_log
3549[root@client scripts]# ./log.py /var/log/httpd/error_log
0
[root@client scripts]# cat /tmp/logseek
3564[root@client scripts]#
(3564-3549=4个字符+1个回车:字符串X3=15)
[root@client httpd]# echo "error" >> error_log
3564[root@client scripts]# ./log.py /var/log/httpd/error_log
0
[root@client httpd]# echo "Error" >> error_log
3570[root@client scripts]# ./log.py /var/log/httpd/error_log
1
[root@client scripts]# ls
check_process.sh log.py
[root@client scripts]# cd /usr/local/etc/
[root@client etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@client etc]# vim zabbix_agentd.conf
UserParameter=check_logs[*],/scripts/log.py $1 $2 $3
[root@client etc]# pkill zabbix
[root@client etc]# zabbix_agentd
[root@client etc]# ll /tmp/
total 24
-rw-r--r--. 1 root root 65 Sep 6 02:56 abc
-rw-r--r--. 1 root root 4 Sep 6 16:31 logseek
drwx------. 2 root root 6 Sep 5 23:41 vmware-root_866-2722763301
drwx------. 2 root root 6 Sep 5 23:43 vmware-root_916-2689078442
drwx------. 2 root root 6 Sep 5 23:39 vmware-root_927-3980167416
drwx------. 2 root root 6 Sep 5 23:23 vmware-root_932-2722632322
drwx------. 2 root root 6 Sep 4 20:43 vmware-root_941-4022177618
-rw-rw-r--. 1 zabbix zabbix 8310 Sep 6 16:36 zabbix_agentd.log
-rw-rw-r--. 1 zabbix zabbix 7 Sep 6 16:36 zabbix_agentd.pid
[root@client etc]# rm -f /tmp/logseek
[root@localhost ~]# zabbix_get -s 192.168.29.141 -k check_logs['/var/log/httpd/error_log']
1
[root@client etc]# ll /tmp/
total 24
-rw-r--r--. 1 root root 65 Sep 6 02:56 abc
-rw-rw-r--. 1 zabbix zabbix 4 Sep 6 16:41 logseek
drwx------. 2 root root 6 Sep 5 23:41 vmware-root_866-2722763301
drwx------. 2 root root 6 Sep 5 23:43 vmware-root_916-2689078442
drwx------. 2 root root 6 Sep 5 23:39 vmware-root_927-3980167416
drwx------. 2 root root 6 Sep 5 23:23 vmware-root_932-2722632322
drwx------. 2 root root 6 Sep 4 20:43 vmware-root_941-4022177618
-rw-rw-r--. 1 zabbix zabbix 8310 Sep 6 16:36 zabbix_agentd.log
-rw-rw-r--. 1 zabbix zabbix 7 Sep 6 16:36 zabbix_agentd.pid
[root@client etc]# rm -f /tmp/logseek
[root@client etc]# ll /tmp/
total 20
-rw-r--r--. 1 root root 65 Sep 6 02:56 abc
drwx------. 2 root root 6 Sep 5 23:41 vmware-root_866-2722763301
drwx------. 2 root root 6 Sep 5 23:43 vmware-root_916-2689078442
drwx------. 2 root root 6 Sep 5 23:39 vmware-root_927-3980167416
drwx------. 2 root root 6 Sep 5 23:23 vmware-root_932-2722632322
drwx------. 2 root root 6 Sep 4 20:43 vmware-root_941-4022177618
-rw-rw-r--. 1 zabbix zabbix 8310 Sep 6 16:36 zabbix_agentd.log
-rw-rw-r--. 1 zabbix zabbix 7 Sep 6 16:36 zabbix_agentd.pid
[root@localhost ~]# zabbix_get -s 192.168.29.141 -k check_logs['/var/log/httpd/error_log']
0
故意触发警告
[root@client httpd]# echo "Error" >> error_log
mysql主从状态
两台主机需配置主从配置
[root@client ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_status,/bin/bash /scripts/mysql_status.sh
[root@client ~]# pkill zabbix_agentd
[root@client ~]# zabbix_agentd
[root@client ~]# vim /scripts/mysql_status.sh
#!/bin/bash
yes=`mysql -uroot -p123456 -e " show slave status\G" 2> /dev/null |grep "Running:"|grep -c "Yes"`
if [ $yes == 2 ]; then
echo "0"
else
echo "1"
fi
[root@client ~]# chmod +x /scripts/mysql_status.sh
[root@localhost ~]# zabbix_get -s 192.168.29.141 -k check_mysql_status
0
[root@mr ~]# mysql -uroot -p123456 -e "stop client;"
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql主从延迟
[root@client ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysqlyc,/bin/bash /scripts/mysqlyc.sh
[root@client ~]# pkill zabbix_agentd
[root@client ~]# zabbix_agentd
[root@client ~]# vim /scripts/mysqlyc.sh
[root@client ~]# cat /scripts/mysqlyc.sh
#!/bin/bash
yc=`mysql -uroot -p123456 -e "show slave status\G" 2> /dev/null |awk '/Seconds_Behind_Master/ {print $2}' `
echo $yc
[root@client ~]# chmod +x /scripts/mysqlyc.sh
[root@localhost ~]# zabbix_get -s 192.168.29.141 -k check_mysqlyc
0
延迟达到200就报警
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~