zabbix自定义监控

1自定义监控进程

1.1 自定义监控配置流程

  1. 写脚本,脚本放到统一位置
  2. 修改被监控机上的zabbix_agentd.conf配置文件
    UnsafeParameters=1
    UserParameter=,
  3. 重启zabbix_agent服务
  4. 在web界面配置监控项和触发器

1.2 监控网站

// 安装httpd
[root@maste ~]# yum -y install httpd
[root@maste ~]# systemctl start httpd
[root@maste ~]# 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                [::]:*          
    

访问httpd
在这里插入图片描述

1.3 监控httpd进程

// 查看进程
[root@maste ~]# ps -ef | grep httpd
root        2038       1  0 02:54 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      2040    2038  0 02:54 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      2041    2038  0 02:54 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      2042    2038  0 02:54 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      2043    2038  0 02:54 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root        2257    1536  0 02:56 pts/0    00:00:00 httpd
root        2259    1536  0 02:56 pts/0    00:00:00 grep --color=auto httpd

监控进程之前先修改agentd配置/usr/local/etc/zabbix_agentd.conf

[root@maste etc]# vim zabbix_agentd.conf
 UnsafeUserParameters=1    // 取消注释并将0修改为1
UserParameter=<key>,<shell command>    //将此行复制到末尾,key是自定义监控值的key,shell command 是获取值的方式

编写监控进程的脚本(脚本是在客户端执行)

// 创建脚本目录
[root@maste ~]# mkdir /scripts
[root@maste script]# cat check_process.sh 
#! /bin/bash

web=$( ps -ef | grep -Ev "grep|$0" | grep -c "$1" )
if [ $web -eq 0 ];then
    echo '1'
else
    echo '0'
fi

//修改/usr/local/etc/zabbix_agentd.conf
[root@maste ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_process[*],/scripts/check_process.sh $1


//修改完配置文件重启服务
[root@maste ~]# pkill zabbix_agentd 
[root@maste ~]# zabbix_agentd 

1.4 在zabbix网页上添加监控项

在这里插入图片描述

在这里插入图片描述
测试key键值是否有效

[root@server ~]# zabbix_get -s 192.168.25.147 -k check_httpd
0

在这里插入图片描述

1.5 添加动作

在这里插入图片描述
在这里插入图片描述

// 关闭httpd触犯动作报警
[root@maste ~]# systemctl stop httpd
[root@maste ~]# 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                [::]:*              

在这里插入图片描述

2 自定义监控日志

// 修改配置文件
[root@maste ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_log[*],/scripts/log.py $1 $2 $3

创建监控项
在这里插入图片描述
在这里插入图片描述
添加动作
在这里插入图片描述
在这里插入图片描述
手动触发报警

[root@maste scripts]# echo "Error" >> /var/log/httpd/error_log

在这里插入图片描述
监控多个关键字
在这里插入图片描述
在这里插入图片描述

3 自定义监控mysql主从状态

搭建MySQL主从后,很多时候不知道从的状态是否ok,有时候出现异常不能及时知道,这里通过shell脚本结合zabbix实现监控并告警

一般情况下,在MySQL的从上查看从的运行状态是通过Slave_IO_Running线程和Slave_SQL_Running线程是否ok,通过命令“show slave status\G;”即可查看。所以这里根据这两个值进行判断。

  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: Yes

// 监控脚本
[root@slave scripts]# cat ./check_replication.sh
#! /bin/bash

count=$(mysql --defaults-file=/scripts/.passwd -e 'show slave status\G' | grep '_Running:' | grep -c 'Yes')

if [ $count -ne 2 ];then
    echo '1'
else
    echo '0'
fi

修改配置文件

[root@maste ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_replication,/scripts/check_replication.sh

给zabbix账户权限

mysql> grant SUPER, REPLICATION CLIENT on *.* to 'zabbix'@'localhost' identified by '123';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

创建存放zabbix的密码文件

[root@slave ~]# cat /scripts/.passwd
[client]
user=zabbix
password=123

创建存放root的密码文件

[root@slave ~]# cat .my.cnf 
[client]
user=root
password=123456

测试

// 脚本测试
[root@slave scripts]# ./check_replication.sh 
0
[root@slave scripts]# ./check_replication.sh 
1

// 服务端测试
[root@server ~]# zabbix_get -s 192.168.25.147 -k check_replication
0
[root@server ~]# zabbix_get -s 192.168.25.147 -k check_replication
1

配置zabbix网页监控
在这里插入图片描述
在这里插入图片描述

测试
在这里插入图片描述

在这里插入图片描述

4 自定义监控mysql主从延迟

监控脚本

[root@slave scripts]# cat check_replication_delay.sh
#! /bin/bash

delay=$(mysql --defaults-file=/scripts/.passwd -e 'show slave status\G '| grep 'Behind' | awk '{print $2}')
if [ $delay -eq 0 ];then
    echo $delay
else
    echo '0'
fi

编写配置文件

[root@slave ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_replication_delay,/scripts/check_replication_delay.sh

配置zabbi网页监控
在这里插入图片描述
在这里插入图片描述
测试(因为本机没有延迟为了能触发报警,所以手动触发)
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值