How to use Zabbix custom script to monitor nginx and WeChat alarms

WBOY
Release: 2023-05-21 17:10:06
forward
1605 people have browsed it

项目描述

本项目的目的是构建一个能实现微信告警的zabbix监控系统,方便的监控整个NGINX集群,同时能批量的去部署和管理整个集群。

项目步骤

  • zabbix服务端(192.168.2.138)安装好zabbix server,nginx端安装好zabbix agent(192.168.2.58),配置好Nginx负载均衡集群,并打开状态统计。

  • nginx端编写监控脚本,取到nginx的状态。

  • 服务端web添加监控项,出图。

  • 注册企业微信,配置好微信接口。

  • zabbix服务端添加接口推送脚本,测试接口正常。

  • 在web端配置报警媒介,群组和用户,添加相关触发器和动作。

  • nginx关停服务,测试是否能通过微信告警。

项目心得

在测试接收消息过程中,返回值一直是参数{ALTER.MESSAGE},排查脚本问题,版本问题,最后发现是参数{ALERT.MESSAGE},所以打字一定要细心,认真。同时对监控也有了一定的认识,运维人员不可能7*24小时盯着zabbix看,所以做到及时告警是非常必要的,对之前的高可用web集群项目叶可以进行完善。

详细步骤

zabbix-agent客户端nginx配置监控

nginx打开状态统计功能

nginx配置增加stub_status模块

location = /wyt_status{
stub_status;}
Copy after login

测试状态统计功能是否打开

http://192.168.2.58/wyt_status\

nginx端(zabbix客户端)编写监控脚本

cd /etc/zabbix/zabbix_agentd.d 
#在zabbix_agentd.d目录下编写监控脚本
vim zabbix-nginx_status.sh
Copy after login
#!/bin/bash
case $1 in
        active)
                curl http://192.168.2.58:80/wyt_status 2>/dev/null|awk '/Active/ {print $NF}'
                ;;
        accepts)
                curl http://192.168.2.58:80/wyt_status 2>/dev/null|awk 'NR==3 {print $1}'
                ;;
        handled)
                curl http://192.168.2.58:80/wyt_status 2>/dev/null |awk 'NR==3 {print $2}'
                ;;
        requests)
                curl http://192.168.2.58:80/wyt_status 2>/dev/null |awk 'NR==3 {print $3}'
                ;;
        reading)
                curl http://192.168.2.58:80/wyt_status 2>/dev/null |awk 'NR==4 {print $2}'
                ;;
        writing)
                curl http://192.168.2.58:80/wyt_status 2>/dev/null |awk 'NR==4 {print $4}'
                ;;
        waiting)
                curl http://192.168.2.58:80/wyt_status 2>/dev/null |awk 'NR==4 {print $NF}'
                ;;
           ping)
                pidof nginx |wc -l
                #通过查询进程PID值,测试nginx存活状态
                ;;
esac
Copy after login
vim userparameter_nginx.conf
#在zabbix_agentd.d目录下自定义参数配置文件
UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/zabbix-nginx_status.sh $1
#指定动作
chmod +x zabbix-nginx_status.sh
#授予可执行权限
service zabbix-agent restart
#刷新服务
zabbix_get -k nginx.status[ping] -s 192.168.2.58
#去服务端测试是否返回参数1
Copy after login

zabbix-server-web配置监控

先创建nginx主机master-nginx

How to use Zabbix custom script to monitor nginx and WeChat alarms

添加应用集nginx

How to use Zabbix custom script to monitor nginx and WeChat alarms

在应用集nginx增加监控项

How to use Zabbix custom script to monitor nginx and WeChat alarms

自定义触发器nginx-up-down,监控项为nginx-ping,正常为1,每5s监控一次,若为0,严重警告。

How to use Zabbix custom script to monitor nginx and WeChat alarms

How to use Zabbix custom script to monitor nginx and WeChat alarms

除nginx-ping,nginx-accepts外,需要添加所有状态监控项,只有nginx-ping创建触发器,这里不一一举例。

How to use Zabbix custom script to monitor nginx and WeChat alarms

添加好所有监控项,下一步制图,图中包含所有监控项

How to use Zabbix custom script to monitor nginx and WeChat alarms

因为我们监控实际上就是在一直请求,所以看到nginx-requests在不断增加。

How to use Zabbix custom script to monitor nginx and WeChat alarms

注册企业微信接口

注册成功之后创建一个运维部门

How to use Zabbix custom script to monitor nginx and WeChat alarms

记住自己的企业ID

How to use Zabbix custom script to monitor nginx and WeChat alarms

自建应用

How to use Zabbix custom script to monitor nginx and WeChat alarms

应用名称为zabbix监控

How to use Zabbix custom script to monitor nginx and WeChat alarms

How to use Zabbix custom script to monitor nginx and WeChat alarms

创建成功后,查看信息记住自己的AgentId和Secret

How to use Zabbix custom script to monitor nginx and WeChat alarms

微信扫码企业微信插件就可以在微信接收消息

How to use Zabbix custom script to monitor nginx and WeChat alarms

zabbix-server接口脚本配置告警

Shell脚本

cd /usr/lib/zabbix/alertscripts
进入脚本配置文件夹
vim weixin.sh
Copy after login
#!/bin/bash

CorpID="wwaa6fb8ff1b81aa77"     # 你的企业id
Secret="Cxyd*****"    #你的SecretID
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CorpID&corpsecret=$Secret"
Token=$(/usr/bin/curl -s -G $GURL |awk -F\": '{print $4}'|awk -F\" '{print $2}')
# echo $Token
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Token"

function body(){
        local int agentid=1000002               # 你的agentdid
        local UserID="@all"                 # 发送的用户ID
        local PartyID=1                  # 部门ID
        local Msg=$(echo "$@" | cut -d" " -f3-) # 发送给所有人
        printf '{\n'
        printf '\t"touser": "'"$UserID"\"",\n"
        printf '\t"toparty": "'"$PartyID"\"",\n"
        printf '\t"msgtype": "text",\n'
        printf '\t"agentid": "'"$agentid"\"",\n"
        printf '\t"text": {\n'
        printf '\t\t"content": "'"$Msg"\""\n"
        printf '\t},\n'
        printf '\t"safe":"0"\n'
        printf '}\n'
}
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
Copy after login

测试脚本能否正常接收消息

How to use Zabbix custom script to monitor nginx and WeChat alarms

可以看到正常接收。

How to use Zabbix custom script to monitor nginx and WeChat alarms

然后我们回到zabbix-server-web配置

zabbix-server-web配置告警

管理->报警媒介类型->创建媒体类型

How to use Zabbix custom script to monitor nginx and WeChat alarms

创建用户群组

How to use Zabbix custom script to monitor nginx and WeChat alarms

创建用户

How to use Zabbix custom script to monitor nginx and WeChat alarms

报警媒介

How to use Zabbix custom script to monitor nginx and WeChat alarms

How to use Zabbix custom script to monitor nginx and WeChat alarms

权限->超级管理员

How to use Zabbix custom script to monitor nginx and WeChat alarms

配置->动作

添加触发器

How to use Zabbix custom script to monitor nginx and WeChat alarms

操作

故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生{TRIGGER.NAME}故障!
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}

How to use Zabbix custom script to monitor nginx and WeChat alarms

恢复操作

恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}

How to use Zabbix custom script to monitor nginx and WeChat alarms

zabbix-agent关停nginx服务测试

How to use Zabbix custom script to monitor nginx and WeChat alarms


可以看到推送成功

How to use Zabbix custom script to monitor nginx and WeChat alarms

Python webhook机器人脚本

新建一个测试群聊,在群里添加机器人,记住webhook地址

How to use Zabbix custom script to monitor nginx and WeChat alarms

除了使用Shell脚本配置接口之外,还可以使用Python脚本

#!/usr/bin/python
#-*- coding: utf-8 -*-
import requests
import json
import sys
import os

headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=****”
#填写自己的webhook地址
def msg(text):
    json_text= {
     "msgtype": "text",
        "text": {
            "content": text
        },
    }
    print requests.post(api_url,json.dumps(json_text),headers=headers).content

if __name__ == '__main__':
    text = sys.argv[1]
    msg(text)
Copy after login

测试

python weixin.py test
Copy after login

web配置同上

How to use Zabbix custom script to monitor nginx and WeChat alarms

The above is the detailed content of How to use Zabbix custom script to monitor nginx and WeChat alarms. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!