準備事項:
-> 微信企業號
-> 企業號已經被部門成員關注
-> 企業號有一個能夠發送消息的應用,一個受權管理員,能夠使用應用給成員發送消息 html
須要獲得的信息
-> 記錄用戶的帳號(惟一ID)
-> CorpID
-> Secret、Agentldnode
1、Zabbix微信報警設置python
[root@node1 zabbix]# cat /usr/local/zabbix/etc/zabbix_server.conf | grep alertscripts AlertScriptsPath=/etc/zabbix/alertscripts [root@node1 zabbix]# cd /etc/zabbix/alertscripts/ [root@node1 alertscripts]# ll 總用量 4 -rwxr-xr-x 1 root root 1627 1月 9 2018 weixin.py [root@node1 alertscripts]# cat weixin.py #!/usr/bin/python #_*_coding:utf-8 _*_ import urllib,urllib2 import json import sys import simplejson reload(sys) sys.setdefaultencoding('utf-8') def gettoken(corpid,corpsecret): gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret print gettoken_url try: token_file = urllib2.urlopen(gettoken_url) except urllib2.HTTPError as e: print e.code print e.read().decode("utf8") sys.exit() token_data = token_file.read().decode('utf-8') token_json = json.loads(token_data) token_json.keys() token = token_json['access_token'] return token def senddata(access_token,subject,content): send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token send_values = { "toparty":"2", "msgtype":"text", "agentid":"1000002", "text":{ "content":subject + '\n' + content }, "safe":"0" } # send_data = json.dumps(send_values, ensure_ascii=False) send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8') send_request = urllib2.Request(send_url, send_data) response = json.loads(urllib2.urlopen(send_request).read()) print str(response) if __name__ == '__main__': user = str(sys.argv[1]) subject = str(sys.argv[2]) content = str(sys.argv[3]) corpid = 'ww48787a74d12332db' corpsecret = 'FoYOji4ITNfxzRngJItrgG59qPG-rR4Z62U_3IAXZTE' accesstoken = gettoken(corpid,corpsecret) senddata(accesstoken,subject,content) python腳本提示: 執行腳本的時候須要本身下載python安裝依賴 只須要在執行腳本的時候在後面傳參,傳參內容包括(發件人,告警標題,告警內容) --corpid= 咱們企業裏面的id --corpsecret= 這裏就是咱們Secret裏面的id [root@node1 alertscripts]# ./weixin.py 100002 告警標題 告警內容 {u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}
2、zabbix web 界面配置web
添加"報警媒介"json
腳本參數:api
{ALERT.SENDTO} 服務器
{ALERT.SUBJECT}微信
{ALERT.MESSAGE}url
添加"用戶的報警媒介",這裏選擇admin用戶code
添加報警觸發的"動做"
默認接收人:服務器:{HOST.NAME}發生: {TRIGGER.NAME}故障!
默認信息:
告警主機:{HOST.NAME}
告警地址:{HOST.IP}
監控項目:{ITEM.NAME}
監控取值:{ITEM.LASTVALUE}
告警等級:{TRIGGER.SEVERITY}
當前狀態:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警時間:{EVENT.DATE} {EVENT.TIME}
事件ID:{EVENT.ID}
默認標題:服務器:{HOST.NAME}: {TRIGGER.NAME}已恢復!
消息內容:
告警主機:{HOST.NAME}
告警地址:{HOST.IP}
監控項目:{ITEM.NAME}
監控取值:{ITEM.LASTVALUE}
告警等級:{TRIGGER.SEVERITY}
當前狀態:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警時間:{EVENT.DATE} {EVENT.TIME}
恢復時間:{EVENT.RECOVERY.DATE} {EVENT.RECOVERY.TIME}
持續時間:{EVENT.AGE}
事件ID:{EVENT.ID}