search
HomeOperation and MaintenanceNginxHow 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

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
#!/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

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

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)

测试

python weixin.py test

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
内存飙升!记一次nginx拦截爬虫内存飙升!记一次nginx拦截爬虫Mar 30, 2023 pm 04:35 PM

本篇文章给大家带来了关于nginx的相关知识,其中主要介绍了nginx拦截爬虫相关的,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

nginx限流模块源码分析nginx限流模块源码分析May 11, 2023 pm 06:16 PM

高并发系统有三把利器:缓存、降级和限流;限流的目的是通过对并发访问/请求进行限速来保护系统,一旦达到限制速率则可以拒绝服务(定向到错误页)、排队等待(秒杀)、降级(返回兜底数据或默认数据);高并发系统常见的限流有:限制总并发数(数据库连接池)、限制瞬时并发数(如nginx的limit_conn模块,用来限制瞬时并发连接数)、限制时间窗口内的平均速率(nginx的limit_req模块,用来限制每秒的平均速率);另外还可以根据网络连接数、网络流量、cpu或内存负载等来限流。1.限流算法最简单粗暴的

nginx+rsync+inotify怎么配置实现负载均衡nginx+rsync+inotify怎么配置实现负载均衡May 11, 2023 pm 03:37 PM

实验环境前端nginx:ip192.168.6.242,对后端的wordpress网站做反向代理实现复杂均衡后端nginx:ip192.168.6.36,192.168.6.205都部署wordpress,并使用相同的数据库1、在后端的两个wordpress上配置rsync+inotify,两服务器都开启rsync服务,并且通过inotify分别向对方同步数据下面配置192.168.6.205这台服务器vim/etc/rsyncd.confuid=nginxgid=nginxport=873ho

nginx php403错误怎么解决nginx php403错误怎么解决Nov 23, 2022 am 09:59 AM

nginx php403错误的解决办法:1、修改文件权限或开启selinux;2、修改php-fpm.conf,加入需要的文件扩展名;3、修改php.ini内容为“cgi.fix_pathinfo = 0”;4、重启php-fpm即可。

如何解决跨域?常见解决方案浅析如何解决跨域?常见解决方案浅析Apr 25, 2023 pm 07:57 PM

跨域是开发中经常会遇到的一个场景,也是面试中经常会讨论的一个问题。掌握常见的跨域解决方案及其背后的原理,不仅可以提高我们的开发效率,还能在面试中表现的更加

nginx部署react刷新404怎么办nginx部署react刷新404怎么办Jan 03, 2023 pm 01:41 PM

nginx部署react刷新404的解决办法:1、修改Nginx配置为“server {listen 80;server_name https://www.xxx.com;location / {root xxx;index index.html index.htm;...}”;2、刷新路由,按当前路径去nginx加载页面即可。

Linux系统下如何为Nginx安装多版本PHPLinux系统下如何为Nginx安装多版本PHPMay 11, 2023 pm 07:34 PM

linux版本:64位centos6.4nginx版本:nginx1.8.0php版本:php5.5.28&php5.4.44注意假如php5.5是主版本已经安装在/usr/local/php目录下,那么再安装其他版本的php再指定不同安装目录即可。安装php#wgethttp://cn2.php.net/get/php-5.4.44.tar.gz/from/this/mirror#tarzxvfphp-5.4.44.tar.gz#cdphp-5.4.44#./configure--pr

nginx怎么禁止访问phpnginx怎么禁止访问phpNov 22, 2022 am 09:52 AM

nginx禁止访问php的方法:1、配置nginx,禁止解析指定目录下的指定程序;2、将“location ~^/images/.*\.(php|php5|sh|pl|py)${deny all...}”语句放置在server标签内即可。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

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),