zabbix 钉钉告警
钉钉建机器人
会得到一个url,将此url复制到/etc/zabbix/下新建自定义文件,以.conf结。以下为内容


正常在/usr/lib/zabbix/alertscripts/,新建一个文件名字自定义
#!/usr/bin/env python
#coding=utf8
import requests
import json
import sys
import os
import time
import configparser
headers = {'Content-Type': 'application/json'}
time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
config=configparser.ConfigParser()
config.read('/etc/zabbix/pre-dingding.conf')
log_file = config.get('config','log')
api_url = config.get('config','webhook')
def log(info):
if os.path.isfile(log_file) == False:
f = open(log_file, 'a+')
f = open(log_file,'a+')
f.write(info)
f.close()
def msg(text,user):
json_text= {
"msgtype": "markdown",
"markdown": {
"title": "GS告警信息",
"text": text
},
"at": {
"atMobiles": [
user
],
"isAtAll": True
}
}
print json_text
r=requests.post(api_url,data=json.dumps(json_text),headers=headers).json()
code = r["errcode"]
if code == 0:
log(time + ":消息发送成功 返回码:" + str(code) + "\n")
else:
log(time + ":消息发送失败 返回码:" + str(code) + "\n")
exit(3)
if __name__ == '__main__':
text = sys.argv[2]
user = sys.argv[1]
msg(text,user)
注意有需要改动的地方。执行py脚本。这里的py版本要在2.6以上,且要支持一个指定模块。
zabbix上
创建用户媒介,用户中指定该报警媒介


创建动作

条件不是必须的,如果需要指定到某机器人,则通过条件指定即可。

产生告警!
告警主机:{HOST.NAME}
主机地址:{HOST.IP}
告警等级:{TRIGGER.SEVERITY}
当前状态:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}: {ITEM.VALUE}
告警时间:{EVENT.TIME}
告警日期:{EVENT.DATE}
到此,测试机器人是否可用。