一、註冊微信企業號
1、註冊微信企業號
https://work.weixin.qq.com
2、通訊錄添加用戶
3、查看運維部門ID號
4、創建應用
5、創建ZABBIX應用
6、查看應用Agentld和secret,並且發送測試信息
發送zabbix測試信息
運維部門成員獲取測試信息成功
二、zabbix-server 端配置
1、獲取腳本文件存放目錄位置
[root@zabbix ~]# grep alertscripts /etc/zabbix/zabbix_server.conf
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/usr/lib/zabbix/alertscripts
2、安裝simplejson
[root@zabbix ~]# wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz
[root@zabbix ~]# cd simplejson-3.8.2/
[root@zabbix simplejson-3.8.2]# tar zxvf simplejson-3.8.2.tar.gz && cd simplejson-3.8.2
[root@zabbix simplejson-3.8.2]# python setup.py build
[root@zabbix simplejson-3.8.2]# python setup.py install
3、編寫python腳本
[root@zabbix ~]# cd /usr/lib/zabbix/alertscripts/
[root@zabbix alertscripts]# vim wechat.py
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,user,subject,content):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
send_values = {
"touser":"villian",
"toparty":"1",
"msgtype":"text",
"agentid":"1000002", #企業號中的應用(zabbix)id
"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]) #zabbix傳過來的第一個參數
subject = str(sys.argv[2]) #zabbix傳過來的第二個參數
content = str(sys.argv[3]) #zabbix傳過來的第三個參數
corpid = 'ww36dc92439b4' #CorpID是企業號的標識
corpsecret = 'LC770B_r8v6fwck8xqNMXnPzLMn8yMRvhwQrCXxs....' #corpsecretSecret是管理組憑證密鑰
accesstoken = gettoken(corpid,corpsecret)
senddata(accesstoken,subject,content,user)
4、測試腳本
[root@zabbix alertscripts]# python wechat.py test1 test2 test3
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww36dc9243a31699b4&corpsecret=LC770B_r8v6fwck8xqNMXnPzLMn8yMRvhwQrCLhOXxs
{u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}
5、授予腳本可執行權限,並修改所屬關係
[root@zabbix alertscripts]# chmod +x wechat.py
[root@zabbix alertscripts]# chown -R zabbix.zabbix wechat.py
三、zabbix-web 配置微信報警
1、創建報警媒介
腳本參數:
- {ALERT.SENDTO}
- {ALERT.SUBJECT}
- {ALERT.MESSAGE}
2、用戶添加報警媒介
3、定義動作
- 操作:
定義標題,內容,發送給指定的用戶,通過微信發送
默認標題:{TRIGGER.STATUS}: {TRIGGER.NAME}
消息內容
告警狀態: {TRIGGER.STATUS}
告警名稱: {TRIGGER.NAME}
告警主機: {HOST.NAME}
告警 IP: {HOST.IP}
告警時間: {EVENT.DATE}-{EVENT.TIME}
告警等級: {TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
問題詳情: {ITEM.NAME}:{ITEM.VALUE}
事件 ID: {EVENT.ID}
- 恢復操作
默認標題:{TRIGGER.STATUS}: {TRIGGER.NAME}
消息內容
告警狀態: {TRIGGER.STATUS}
告警名稱: {TRIGGER.NAME}
告警主機: {HOST.NAME}
告警 IP: {HOST.IP}
告警時間: {EVENT.DATE}-{EVENT.TIME}
告警等級: {TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
問題詳情: {ITEM.NAME}:{ITEM.VALUE}
事件 ID: {EVENT.ID}
- 更新操作
默認標題:{TRIGGER.STATUS}: {TRIGGER.NAME}
消息內容
告警狀態:{TRIGGER.STATUS}
告警名稱:{TRIGGER.NAME}
告警主機:{HOST.NAME}
主機IP: {HOST.IP}
告警時間:{EVENT.DATE} {EVENT.TIME}
告警等級:{TRIGGER.SEVERITY}
告警信息:{TRIGGER.NAME}
問題詳情:{ITEM.NAME}:{ITEM.VALUE}
當前狀態: {TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID: {EVENT.ID}
4、測試報警(將zabbix-agent關閉)
[root@zabbix alertscripts]# systemctl stop zabbix-agent