Table of Contents
项目描述
项目步骤
项目心得
详细步骤
zabbix-agent客户端nginx配置监控
zabbix-server-web配置监控
注册企业微信接口
zabbix-server接口脚本配置告警
zabbix-server-web配置告警
zabbix-agent关停nginx服务测试
Home Operation and Maintenance 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

May 21, 2023 pm 05:10 PM
nginx zabbix

项目描述

本项目的目的是构建一个能实现微信告警的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!

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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

See all articles