关闭
Hit
enter
to search or
ESC
to close
May I Suggest ?
#leanote #leanote blog #code #hello world
柯仓无居所
Home
Archives
Tags
Search
Zabbix 基于 Python的钉钉告警
? python ?
? 钉钉告警 ?
? zabbix ?
658
0
0
swimming
? python ?
? 钉钉告警 ?
? zabbix ?
### **准备** > 钉钉可以自己创建一个组,然后添加智能助手或者其他的具有`webhook`功能的机器人,机器人头像不可以自定义 --- ### **zabbix设置** > zabbix 上在管理页--->报警媒介类型下,新建类型为脚本的媒介,设置如下图:  --- ### **Python告警脚本** > 具体脚本如下: ```python #!/usr/bin/env python # coding:utf-8 import json import sys import time import urllib2 def SendMsg(user, subject, msg): token = "a0127268c22d1779596419ca515299912ae0a0b51eec4ab8b02f3e177cd428d9" url = "https://oapi.dingtalk.com:1443/robot/send?access_token=" + token header = {"Content-Type": "application/json", "charset": "utf-8"} data = { "msgtype": "text", # 指定推送人 "at": {"atMobiles": [user], "isAtAll": False}, "text": {"content": msg}, } res = urllib2.Request(url, data=json.dumps(data), headers=header) try: urlopen = urllib2.urlopen(res) except Exception as msg: with open("/tmp/dingtalk.log", "a") as f: f.writelines( "%s |<--- ERROR %s\n\n" % (str(time.ctime()), msg) ) with open("/tmp/dingtalk.log", "a") as f: f.writelines( "%s |<--- 推送返回结果:%s\n\n" % ( str(time.ctime()), urlopen.read(), ) ) return True if __name__ == "__main__": # str_time = time.strftime("%Y-%M-%d %H:%m:%S", time.strptime(time.ctime())) # 用户,手机号 user = sys.argv[1] # 标题,告警概要 subject = sys.argv[2] # 告警详细内容 msg = sys.argv[3] with open("/tmp/dingtalk.log", "a") as f: f.writelines( "%s --->| 接收到告警,通知账户为:%s,\n告警标题:%s,\n告警详细内容;%s\n" % (str(time.ctime()), user, subject, msg) ) SendMsg(user, subject, msg) ``` > 将以上脚本放在 zabbix server 的 **AlertScriptsPath** 文件下,并设置+x运行权限,文件属性改为zabbix,记得修改token值。 如,需要发送告警内容不需要‘@’功能,则将data内的‘at’删除即可。
觉得不错,点个赞?
提交评论
Sign in
to leave a comment.
No Leanote account ?
Sign up now
.
0
条评论
More...
文章目录
No Leanote account ? Sign up now.