Zabbix realizes enterprise WeChat monitoring and alarm (7)

zabbix uses enterprise WeChat to monitor alarms

1. Registered enterprise WeChat

To use the corporate WeChat alarm, you need to register the corporate WeChat first

企业微信地址:https://work.weixin.qq.com/

Just fill in the registration information

Insert picture description here

Registration is complete

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-JSFbgHlA-1611739005163)(.\zabbix-note picture storage\image-20201005101144092.png)]

2. Create an alarm robot

2.1. Create an application

Application Management-Application-Create Application
Insert picture description here

2.2. Edit application information

Set up robot log, description information, application department

Insert picture description here

2.3. Created successfully

The most important thing is these two information

AgentId 1000002

Secret iV9ljCjBVm2BvFWd-t0rZGTakxaH2izz7degTA41naI

Insert picture description here

3. WeChat affiliated company WeChat receives alarm content

Click on My Business—WeChat Plug-in—find the QR code and scan the attention to receive the corporate WeChat alarm information

Insert picture description here

Scan the QR code to follow my company to receive alarm information

Insert picture description here

Mobile WeChat View

Insert picture description here

3. The command line test found WeChat messages

3.1. Get the company ID

Click on my company-company information-company ID

ww48f74fc8ed3a07ba

Insert picture description here

3.2. Get app ID and password

Click Application Management-find the corresponding application-copy the value of AgentId, Secret

AgentId		1000002
Secret		iV9ljCjBVm2BvFWd-t0rZGTakxaH2izz7degTA41naI

Insert picture description here

3.3. Prepare Python script to send messages to enterprise WeChat

Fill in the information such as the enterprise ID, application ID, and application password prepared in the first two steps into the Python script

You only need to modify rows 17, 18, and 19 each time you use it

corpid='xx'
#corporate ID appsecret='xx' #application password
agentid=xx #application ID

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#comment: zabbix接入微信报警脚本

import requests
import sys
import os
import json
import logging

logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
	datefmt = '%a, %d %b %Y %H:%M:%S',
	filename = os.path.join('/tmp','weixin.log'),
	filemode = 'a')
	
	
corpid='ww48f74fc8ed3a07ba' #企业ID
appsecret='iV9ljCjBVm2BvFWd-t0rZGTakxaH2izz7degTA41naI'  #secret
agentid=1000002  #AgentID

#获取accesstoken
token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']

#发送消息
msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken

touser=sys.argv[1]
subject=sys.argv[2]
toparty='3|4|5|6'
message=sys.argv[3]

params={
    
    
        "touser": touser,
        "toparty": toparty,
        "msgtype": "text",
        "agentid": agentid,
        "text": {
    
    
                "content": message
        },
        "safe":0
}

req=requests.post(msgsend_url, data=json.dumps(params))

logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)

3.4. Upload the script to zabbix-server and test the alarm content

1.准备Python环境安装所依赖的模块
安装Python2-pip时依赖epel源提前准备好
[root@zabbix-server ~]# yum -y install python2-pip

2.去清华源找到对应的加速器并安装缺少的模块
这个地址有加速器的使用方法https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
[root@zabbix-server ~]# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple request

3.命令行测试并发生内容到微信
[root@zabbix-server ~]# python weixin.py JiangXiaoLong 'ceshi' 'ceshi'
语法格式:python 脚本名 要发生给谁 '标题' '内容'

要发生给谁可以在企业微信部门中找到要发生的人点进去即可看到账号信息

WeChat check whether the message has been received

Insert picture description here

4.zabbix realizes enterprise WeChat alarm

4.1 Alarm content

报警内容

发生的
问题发生
当前状态: 未解决❎️
告警主机: {HOSTNAME1}
告警时间: {EVENT.DATE} {EVENT.TIME}
告警等级: {TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目: {TRIGGER.KEY1}
问题详情: {ITEM.NAME}:{ITEM.VALUE}
事件ID: {EVENT.ID}

恢复的
问题恢复
当前状态: 已处理✅
告警主机: {HOSTNAME1}
告警时间: {EVENT.DATE} {EVENT.TIME}
告警等级: {TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目: {TRIGGER.KEY1}
问题详情: {ITEM.NAME}:{ITEM.VALUE}
事件ID: {EVENT.ID}

4.2. Delete the alarm log generated by the command line test

由于我们测试是以root身份测试的,如果不删除,zabbix报警的时候则无法报警,会提示无权限讲报警信息写入到日志
[root@zabbix-server ~]# rm -rf /tmp/weixin.log

4.3. Move the alarm script to the zabbix alarm script directory

1.获取报警脚本存放路径
[root@zabbix-server ~]# egrep -v '^$|^#' /etc/zabbix/zabbix_server.conf 
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=123456
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=30
AlertScriptsPath=/usr/lib/zabbix/alertscripts			#这个目录存放报警脚本
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000

2.移动报警脚本至脚本目录
[root@zabbix-server ~]# mv weixin.py /usr/lib/zabbix/alertscripts/

4.4. New WeChat alarm media-define sender

Click Manage—Alarm Media Type—Add Alarm Media—Fill in Name—Type Selection Script—Add three script parameters

{ALERT.SENDTO}			//发件人
{ALERT.SUBJECT}			//标题
{ALERT.MESSAGE}			//内容

Insert picture description here

Created successfully

Insert picture description here

4.5. Define recipients

Click User—Alarm Media—Add—Select WeChat Alarm Type—Recipients can get it from the enterprise WeChat—Add—Update

Insert picture description here

4.6. Mailbox alarm before deactivation

Insert picture description here

4.7. Trigger the host down alarm and check the alarm message

随便找一台监控主机执行poweroff
[root@192_168_81_220 ~]# poweroff

The dashboard has alarmed

Insert picture description here

The message in the action log has been delivered

Insert picture description here

View the alarm message when the problem occurs

Insert picture description here

View the problem recovery alarm message

Insert picture description here

4.8. Trigger the number of terminal open and check the alarm message

Here you can see that the maximum number of open terminals for this machine of 192.168.81.230 is 3, and if there are more than 3, it will alarm. The alarm
Insert picture description here
has been alarmed.
Insert picture description here
View the alarm content
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44953658/article/details/113256550