zabbix40自定义监控项及触发动作

zabbix自定义监控项:

  • 在上一篇文章中,我们通过添加模板zabbix自动为我们创建了很多监控项,但是往往模板中的监控项并不能满足我们业务需要,我们时常需要自己根据业务需要创建一系列的监控项,也成为自定义监控项
  • 监控项:zabbix进行监控的一个指标,zabbix中称为item,它的值由独立的key进行识别。

1. 监控redis服务:

1.1利用zabbix自带的功能创建监控项:

  1. 点击《配置》,然后选择《主机》,在下面双击《监控项》,注意:这里的创建监控项根据主机定义:
    在这里插入图片描述2. 选择创建监控项:
    在这里插入图片描述3. 下面可以填写你要监控的信息了填完后记得选择添加:
    在这里插入图片描述在这里插入图片描述

1.2. 自定义脚本创建监控项:

1.2.1. 修改zabbix-agent配置文件:

[root@localhost ~]# vim /etc/zabbix_agentd.conf
# 指定监控项及获取值的方式:
 UserParameter=redis.status,bash  /data/sh/redis_status.sh
 # redis.status: 键(监控项)
# bash /data/sh/redis_status.sh 值(获取监控项的方式)

1.2.2. 创建脚本:

[root@localhost ~]# mkdir -p /data/sh/
[root@localhost ~]# vim /data/sh/redis_status.sh
#!/bin/bash
#killall 这个命令没有的话需要安装psmisc-22.20-16.el7.x86_64
killall -0 redis-server  &>/dev/null

if [ $?  -eq  0 ];then
        echo 1
else
        echo 0
fi
#授权,因为普通用户无法使用killall这个命令所有需要授权
[root@localhost ~]# chmod u+s /usr/bin/killall
[root@localhost ~]# chmod +x /data/sh/redis_status.sh
[root@localhost ~]# systemctl restart zabbix-agent.service 

1.2.3. 测试脚本:

  • 一定要先测试,脚本有没有问题,没有问题才能继续。而且,测试需要在客户端及服务端都进行测试。
###客户端测试:
#当redis进程存在时:
[root@localhost ~]# ps -ef | grep redis
redis      2543      1  0 01:26 ?        00:00:02 /usr/bin/redis-server 127.0.0.1:6379
root       2910   1687  0 01:49 pts/0    00:00:00 grep --color=auto redis
[root@localhost ~]# /data/sh/redis_status.sh 
1
#停止redis服务测试:
[root@localhost ~]# systemctl stop redis.service 
[root@localhost ~]# ps -ef | grep redis
root       2954   1687  0 01:51 pts/0    00:00:00 grep --color=auto redis
[root@localhost ~]# /data/sh/redis_status.sh 
0
###服务端测试:
# 当redis进程存在时测试:
[root@localhost ~]# zabbix_get -s 192.168.2.20  -k  "redis.status"
1
#当redis进程不存在时测试:
[root@localhost ~]# zabbix_get -s 192.168.2.20  -k  "redis.status"
0

1.2.4. web界面添加监控项:

在这里插入图片描述

1.2.5. 查看redis.status图形:

在这里插入图片描述在这里插入图片描述乱码解决方法:

# 上传字体(simkai.ttf),然后替换字体,再次刷新网页即可:
[root@localhost ~]# mv -b  simkai.ttf /usr/share/zabbix/assets/fonts/DejaVuSans.ttf 

2. 监控nginx服务:

2.1. 修改zabbix-agent配置文件:

[root@localhost ~]# vim /etc/zabbix_agentd.conf
# 指定监控项及获取值的方式
 UserParameter=nginx.status[*],bash  /data/sh/nginx_status.sh $1

2.2. 创建脚本:

[root@localhost ~]# vim /data/sh/nginx_status.sh 
#!/bin/bash
function active {
        curl 192.168.2.20/status 2>/dev/null |awk 'NR==1{print $NF}'
        }
function accepts {
        curl 192.168.2.20/status 2>/dev/null | awk 'NR==3{print $1}'
        }
function handled {
        curl 192.168.2.20/status 2>/dev/null | awk 'NR==3{print $2}'
        }
function requests {
        curl 192.168.2.20/status 2>/dev/null | awk 'NR==3{print $3}'
}
function reading {
        curl 192.168.2.20/status 2>/dev/null | awk 'NR==4{print $2}'
}
function writing {
        curl 192.168.2.20/status 2>/dev/null | awk 'NR==4{print $4}'
}
function waiting {
        curl 192.168.2.20/status 2>/dev/null | awk 'NR==4{print $NF}'
}
$1
[root@localhost ~]# chmod +x /data/sh/nginx_status.sh
[root@localhost ~]# systemctl restart zabbix-agent.service 

2.3. 测试脚本:

[root@localhost ~]# zabbix_get -s 192.168.2.20 -k nginx.status[waiting]
0

2.4. web界面添加监控项:

在这里插入图片描述

3.触发器:

  • 触发器是对监控项采集的数据进行评估的逻辑表达式,如果接收的数据超过了可接受的状态,则触发器会被触发。

3.1. 创建触发器:

  • 在前面课程中,我们创建了对redis服务进行监控的监控项。现在对该监控项设置一个触发器,如果监控项的值为0,则触发一系列动作。

  • 点击配置–>主机–>触发器–>点击创建触发器
    在这里插入图片描述

  • 创建触发器名字及级别:
    在这里插入图片描述

  • 设置触发器表达式:
    在这里插入图片描述

  • 确认之后,可以在主机-触发器中看到刚才创建的触发器。
    在这里插入图片描述

3.2. 验证触发器:

  • 关闭nginx服务,可以在问题中看到事件。
[root@localhost ~]# systemctl stop nginx.service
  • 点击检测–仪表板中问题项:
    在这里插入图片描述

4. 动作:

  • 触发器可以在当接受到某个值超过预设的值时,直观的显示出问题,但是也只是仅仅显示在web界面,监控人员还是需要时刻盯着屏幕,才能及时看到问题。这样工作效率还是没有明显提升,我们需要当这个触发器被触发时,有一个动作帮我们报警或者直接帮我们恢复故障

4.1. 自动邮件报警:

4.1.1 为监控负责人绑定媒介:

  • 点击管理–>报警媒介类型–>点击Email:
    在这里插入图片描述在这里插入图片描述在这里插入图片描述
  • 点击配置–>用户–>点击发送使用的用户:
  • 在这里插入图片描述

4.1.2. 添加动作:

  • 点击配置–>动作–>创建动作
    在这里插入图片描述
  • 选择报警媒介填写内容–>点击添加–点击更新:
    在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

4.1.3. 测试:

  • 把nginx服务关闭掉:
[root@localhost ~]# systemctl stop nginx

在这里插入图片描述可以看的出来这里已经收到短信了

4.1.4. 自动重启服务:

  • 在配置动作中,我们可以设置相应的报警媒介给工作人员报警,也可以尝试先让zabbix为我们重启相应的服务,如果多次重启都失败了,则继续报警,让负责人来处理相关问题。
  • 点击动作–>操作–>选择远程命令:
    在这里插入图片描述在这里插入图片描述
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值