自定义监控

该博客详细介绍了如何使用Zabbix进行自定义监控,包括配置监控脚本以检查指定进程(如httpd和mysql)的状态,监控日志错误,以及监测MySQL主从同步状态和延迟。通过编写和调用shell脚本,实现了对系统状态的实时监控,并设置了触发器以实现报警功能。
摘要由CSDN通过智能技术生成

自定义监控进程

配置监控脚本

//修改被监控端配置文件
[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以上报警
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值