【逗老师带你学IT】PRTG监控系统通过企业微信推送图文混排告警消息
原文在操作上有一些细节bug,对像我一样的python菜鸟是个坑。为了填这些坑,花了2天时间使用new bing 对逗老师的代码进行修改和优化。以下直接说明我测试的成果。
无图无真相,先上图!
效果一:一条图文推送
效果二:一条文字推送,一条图文推送
对“逗老师”的代码进行修改优化,代码部分都是new bing完成的,我只负责提问题。😁
过程部分参考逗老师原文,以下是我踩坑的部分:
1. PRTG调用程序文件“wechat_wbhook.bat”的8个参数
"%probe" "%group" "%device" "%name" "%status" "%down" "%message" "%sensorid"
2. wechat_webhook.bat 脚本记得用笔记本保存为ANSI编码
解决PRTG变量参数带出的数据包含空格和符号导致python识别错误的问题。
@echo off
setlocal enabledelayedexpansion
set i=1
for %%a in (%*) do (
set arg[!i!]=%%~a
set /a i+=1
)
"C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe" "C:\Program Files (x86)\PRTG Network Monitor\Notifications\EXE\wechat_webhook.py" "!arg[1]!" "!arg[2]!" "!arg[3]!" "!arg[4]!" "!arg[5]!" "!arg[6]!" "!arg[7]!" "!arg[8]!"
3. 效果一的代码,wechat_webhook.py记得用笔记本保存为UTF-8编码
webhook链接自备,记得修改url、id、passhash为自己的。
import json
import requests
import sys
import datetime
webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=******"
def wechatwork_robot():
now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
paramsList = ["", "【探针设备】:", "【设备群组】:", "【节点】:", "【传感器名称】:", "【现在状态】:", "【停机时间】:", "【附加消息】:", "【传感器ID】:"]
content = ""
headers = {"Content-Type": "text/plain"}
for i in range(len(sys.argv)):
if i > 0:
content = content + paramsList[i] + sys.argv[i] + "\n"
data = {
"msgtype": "news",
"news": {
"articles": [
{
"title": sys.argv[5] + "@" + sys.argv[3],
"description": "**通知时间:" + now_time + "**\n" + content,
"url": "http://www.prtg.com/chart.svg?type=graph&width=500&height=215&graphid=0&id=" + sys.argv[8] + "&username=****&passhash=****",
"picurl": "http://www.prtg.com/chart.png?type=graph&width=500&height=215&graphid=0&id=" + sys.argv[8] + "&username=****&passhash=****"
}
]
}
}
r = requests.post(url=webhook_url, headers=headers, json=data)
print(r.text)
wechatwork_robot()
4. 效果二的代码,wechat_webhook.py记得用笔记本保存为UTF-8编码(原)
webhook链接自备,记得修改url、id、passhash为自己的。
import json
import requests
import sys
import datetime
webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=*****"
def wechatwork_robot():
now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
paramsList =[ "", "探针设备:", "设备群组:", "节点:", "传感器名称:" , "现在状态:", "停机时间:", "附加消息:", "传感器ID:"]
content = ""
headers = {"Content-Type": "text/plain"}
for i in range(len(sys.argv)):
if i > 0:
content = content + "<font color=\"comment\">" + paramsList[i] + "</font>" + sys.argv[i] + "\n"
data1 = {
"msgtype": "markdown",
"markdown": {
"content": "**<font color=\"info\">【PTRG微信小机器人】</font>**\n**通知时间:" + now_time + "**\n" + content,
}
}
r = requests.post(url=webhook_url, headers=headers, json=data1)
print(r.text)
# 纯文本的告警消息
data2 = {
"msgtype": "news",
"news": {
"articles" : [
{
"title" : "告警节点实时状态,ID:" + sys.argv[8],
"description" : "点击图片进入PRTG查看当前状态详细信息\n传感器名称:" + sys.argv[4],
"url" : "http://www.prtg.com/chart.svg?type=graph&width=500&height=215&graphid=0&id=" + sys.argv[8] + "&username=****&passhash=****",
"picurl" : "http://www.prtg.com/chart.png?type=graph&width=500&height=215&graphid=0&id=" + sys.argv[8] + "&username=****&passhash=****"
}
]
}
}
r = requests.post(url=webhook_url, headers=headers, json=data2)
print(r.text)
# 图文混排的告警消息
wechatwork_robot()
5. 解决消息轰炸的问题
如果通过端口映射获取设备的SNMP,同一个IP或域名下有多个设备,如果有一设备故障,但是IP或域名正常,PRTG会推送所有故障的传感器,会导致消息轰炸。为了解决这个问题,要对设备的一个其他传感器设置一个“从属性类型”来解决。
看图:
这样设置后,设备的从属性就从“ping”改为“SNMP系统运行时间”,解决了消息轰炸的问题。