自定义监控配置:
以mysql为例:
1.创建脚本存放目录,给脚本执行权限
[root@client ~]# mkdir -p /opt/scripts/
[root@client ~]# cd /opt/scripts/
[root@client scripts]# ls
mysql.sh
[root@client scripts]# vim mysql.sh
[root@client scripts]# cat mysql.sh
#!/bin/bash
count=$(ps -ef|grep -Ev "grep|$0"|grep -c mysql)
if [ $count -gt 0 ];then
echo '0'
else
echo '1'
fi
[root@client scripts]# chmod +x mysql.sh
[root@client scripts]# bash mysql.sh
0
[root@client scripts]# ll
-rwxr-xr-x. 1 root root 122 8月 24 22:08 mysql.sh
2.修改脚本存放目录的属主属组为zabbix
[root@client opt]# chown -R zabbix.zabbix scripts
[root@client opt]# ll
总用量 4
drwxr-xr-x. 2 zabbix zabbix 46 8月 24 22:08 scripts
-rw-r--r--. 1 root root 70 8月 24 21:54 www
[root@client scripts]# chown -R zabbix.zabbix mysql.sh
[root@client scripts]# ll
-rwxr-xr-x. 1 zabbix zabbix 122 8月 24 22:08 mysql.sh
3.修改客户端配置文件agentd.conf
[root@client ~]# vim /usr/local/etc/zabbix_agentd.conf
//添加下面两项
UnsafeUserParameters=1
UserParameter=key_mysql,/bin/bash /opt/scripts/mysql.sh
4.重启zabbix_agent
[root@client ~]# zabbix_agentd
[root@client ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
5.在服务端手动测试
[root@server2 ~]# zabbix_get -s 192.168.93.129 -k key_mysql
0
6.配置网页界面,添加监控项以及触发器
7.在网页上验证
8.关闭mysql服务,在服务端手动测试
[root@client ~]# systemctl stop mariadb
[root@client ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@server2 ~]# zabbix_get -s 192.168.93.129 -k key_mysql
1
以httpd为例:
1.创建脚本存放目录,给脚本执行权限
[root@client ~]# mkdir -p /opt/scripts/
[root@client ~]# cd /opt/scripts/
[root@client scripts]# vim check_process.sh
[root@client scripts]# cat check_process.sh
#!/bin/bash
process=$(ps -ef|grep -Ev "grep|$0" |grep -c $1)
if [ $process -gt 0 ];then
echo '0'
else
echo '1'
fi
[root@client scripts]# ls
check_process.sh mysql.sh
[root@client scripts]# chmod +x check_process.sh
[root@client scripts]# bash check_process.sh httpd
0
2.修改脚本存放目录的属主属组为zabbix
[root@client scripts]# chown -R zabbix.zabbix check_process.sh
[root@client scripts]# ll
总用量 8
-rwxr-xr-x. 1 zabbix zabbix 124 8月 24 23:14 check_process.sh
-rwxr-xr-x. 1 zabbix zabbix 122 8月 24 22:08 mysql.sh
3.修改客户端配置文件agentd.conf
[root@client ~]# vim /usr/local/etc/zabbix_agentd.conf
//添加下面两项
UnsafeUserParameters=1
UserParameter=key_process[*],/bin/bash /opt/scripts/check_process.sh $1
4.重启zabbix_agent
[root@client ~]# zabbix_agentd
[root@client ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
5.在服务端手动测试
[root@server2 ~]# zabbix_get -s 192.168.93.129 -k key_process[httpd]
0
6.配置网页界面,添加监控项以及触发器
7.在网页上创建
8.关闭httpd服务,在服务端手动测试
[root@client ~]# pkill httpd
[root@client ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@server2 ~]# zabbix_get -s 192.168.93.129 -k key_process[httpd]
1