自定义监控进程
配置监控脚本
//修改被监控端配置文件
[root@slave1 ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
//重启服务
[root@slave1 ~]# zabbix_agentd
[root@slave1 ~]# ss -antl
[root@slave1 ~]# ss -antl |grep 10050
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
//编写脚本
[root@slave1 ~]# mkdir /scripts
[root@slave1 ~]# vim /scripts/check_process.sh
#!/bin/bash
count=`ps -ef| grep $1 |grep -Ev "grep|$0" |wc -l`
if [ $count == 0 ];then
echo "1"
else
echo "0"
fi
[root@slave1 ~]# chmod +x /scripts/check_process.sh
//在服务端验证效果
[root@zabbix ~]# zabbix_get -s 192.168.58.135 -k check_process[httpd]
0
[root@zabbix ~]# zabbix_get -s 192.168.58.135 -k check_process[mysql]
1
添加监控项
监控mysql服务
查看监控数据
添加触发器
手动触发报警
[root@slave1 ~]# systemctl stop mysqld
自定义监控日志
//编写脚本
[root@slave1 ~]# dnf -y install python36
[root@slave1 ~]# vim /scripts/log.py
[root@slave1 ~]# chmod +x /scripts/log.py
//修改agentd配置文件
[root@slave1 ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_logs[*],/scripts/log.py $1 $2 $3
[root@slave1 ~]# pkill zabbix_agentd
[root@slave1 ~]# zabbix_agentd
/var/log/httpd目录没有执行权限,我们需要给他添加权限
[root@slave1 ~]# ll -d /var/log/httpd/
drwx------. 2 root root 4096 Nov 12 2021 /var/log/httpd/
[root@slave1 ~]# chmod 755 /var/log/httpd/
[root@slave1 ~]# ll /var/log/httpd/ -d
drwxr-xr-x. 2 root root 4096 Nov 12 2021 /var/log/httpd/
//主动添加报错信息
[root@slave1 ~]# echo "Error1" >> /var/log/httpd/error_log
[root@zabbix ~]# zabbix_get -s 192.168.58.135 -k check_logs[/var/log/httpd/error_log]
1
添加监控项
查看监控数据
添加触发器
手动添加报错信息
[root@slave1 ~]# echo "Error1" >> /var/log/httpd/error_log
自定义监控MySQL主从状态
[root@slave1 ~]# mysql -uroot -pyxt123! -e " show slave status\G"
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.202.141
Master_User: alg
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Ye
配置监控脚本
//修改配置文件
[root@slave1 ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_status,/bin/bash /scripts/mysql_status.sh
[root@slave1 ~]# pkill zabbix_agentd
[root@slave1 ~]# zabbix_agentd
//编写监控脚本
[root@slave1 ~]# vim /scripts/mysql_status.sh
#!/bin/bash
yes=`mysql -uroot -pyxt123! -e " show slave status\G" 2> /dev/null |grep "Running:"|grep -c "Yes"`
if [ $yes == 2 ]; then
echo "0"
else
echo "1"
fi
[root@slave1 ~]# chmod +x /scripts/mysql_status.sh
//检查key是否可用
[root@zabbix ~]# zabbix_get -s 192.168.160.132 -k check_mysql_status
0
添加监控项
添加触发器
手动触发报警
关闭主从
[root@slave1 ~]# mysql -uroot -pyxt123! -e "stop slave;"
mysql: [Warning] Using a password on the command line interface can be insecure.
自定义监控MySQL延迟
配置监控脚本
//在被监控端,也就是从库,首先修改配置文件
[root@slave1 ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysqlyc,/bin/bash /scripts/mysqlyc.sh
//重启服务生效配置
[root@slave1 ~]# pkill zabbix_agentd
[root@slave1 ~]# zabbix_agentd
//再编写脚本
[root@slave1 ~]# vim /scripts/mysqlyc.sh
#!/bin/bash
yc=`mysql -uroot -pyxt123! -e "show slave status\G" 2> /dev/null |awk '/Seconds_Behind_Master/ {print $2}' `
echo $yc
[root@slave1 ~]# chmod +x /scripts/mysqlyc.sh
//去服务端检查key是否可用
[root@zabbix ~]# zabbix_get -s 192.168.160.132 -k check_mysqlyc
0
添加监控项
查看监控数据
添加触发器
延迟200以上报警