


How to use Zabbix custom script to monitor nginx and WeChat alarms
项目描述
本项目的目的是构建一个能实现微信告警的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;}
测试状态统计功能是否打开
http://192.168.2.58/wyt_status\
nginx端(zabbix客户端)编写监控脚本
cd /etc/zabbix/zabbix_agentd.d #在zabbix_agentd.d目录下编写监控脚本 vim zabbix-nginx_status.sh
#!/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
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
zabbix-server-web配置监控
先创建nginx主机master-nginx
添加应用集nginx
在应用集nginx增加监控项
自定义触发器nginx-up-down,监控项为nginx-ping,正常为1,每5s监控一次,若为0,严重警告。
除nginx-ping,nginx-accepts外,需要添加所有状态监控项,只有nginx-ping创建触发器,这里不一一举例。
添加好所有监控项,下一步制图,图中包含所有监控项
因为我们监控实际上就是在一直请求,所以看到nginx-requests在不断增加。
注册企业微信接口
注册成功之后创建一个运维部门
记住自己的企业ID
自建应用
应用名称为zabbix监控
创建成功后,查看信息记住自己的AgentId和Secret
微信扫码企业微信插件就可以在微信接收消息
zabbix-server接口脚本配置告警
Shell脚本
cd /usr/lib/zabbix/alertscripts 进入脚本配置文件夹 vim weixin.sh
#!/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
测试脚本能否正常接收消息
可以看到正常接收。
然后我们回到zabbix-server-web配置
zabbix-server-web配置告警
管理->报警媒介类型->创建媒体类型
创建用户群组
创建用户
报警媒介
权限->超级管理员
配置->动作
添加触发器
操作
故障{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}
恢复操作
恢复{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}
zabbix-agent关停nginx服务测试
可以看到推送成功
Python webhook机器人脚本
新建一个测试群聊,在群里添加机器人,记住webhook地址
除了使用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)
测试
python weixin.py test
web配置同上
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!

NGINX can be used to serve files and manage traffic. 1) Configure NGINX service static files: define the listening port and file directory. 2) Implement load balancing and traffic management: Use upstream module and cache policies to optimize performance.

NGINX is suitable for handling high concurrency and static content, while Apache is suitable for dynamic content and complex URL rewrites. 1.NGINX adopts an event-driven model, suitable for high concurrency. 2. Apache uses process or thread model, which is suitable for dynamic content. 3. NGINX configuration is simple, Apache configuration is complex but more flexible.

NGINX and Apache each have their own advantages, and the choice depends on the specific needs. 1.NGINX is suitable for high concurrency, with simple deployment, and configuration examples include virtual hosts and reverse proxy. 2. Apache is suitable for complex configurations and is equally simple to deploy. Configuration examples include virtual hosts and URL rewrites.

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
