zabbix自定义监控项:
- 在上一篇文章中,我们通过添加模板,zabbix自动为我们创建了很多监控项,但是往往模板中的监控项并不能满足我们业务需要,我们时常需要自己根据业务需要创建一系列的监控项,也成为自定义监控项。
- 监控项:zabbix进行监控的一个指标,zabbix中称为item,它的值由独立的key进行识别。
1. 监控redis服务:
1.1利用zabbix自带的功能创建监控项:
- 点击《配置》,然后选择《主机》,在下面双击《监控项》,注意:这里的创建监控项根据主机定义:
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为我们重启相应的服务,如果多次重启都失败了,则继续报警,让负责人来处理相关问题。
- 点击动作–>操作–>选择远程命令: