1、微信企業號注冊與使用
企業號注冊:https://qy.weixin.qq.com/
2、企業號使用教程
2.1、通訊錄添加企業員工
登錄新建的企業號,通過提前把企業成員信息添加到組織或者部門,需要填寫手機號、微信號或郵箱,通過這樣方式讓別人掃碼關注企業公眾號,為了后面企業號推送消息給企業成員。
#新增賬戶,填寫信息
3、應用中心創建應用
4、給部門設置管理員
設置--->功能設置---->權限管理---->新建管理組
管理員需要事先關注企業號,並且設置好郵箱地址
#需要確定管理員有權限使用應用發送消息,需要管理員的CorpID和Sercrt。(重要)
#准備事項:
微信企業號
企業號已經被部門成員關注
企業號有一個可以發送消息的應用(Abcdocker),一個授權管理員,可以使用應用給成員發送消息
#需要先添加管理員信息,然后使其關注企業號
#需要得到的信息
成員賬號
組織部門ID
應用ID
CorpID和Secret
5、微信接口調用
調用微信接口需要一個調用接口的憑證:access_token
通過CorpID和Secret可以獲得access_token
微信企業號接口調試地址: http://qydev.weixin.qq.com/debug
6、腳本調用原理
#設置腳本執行路徑,編輯zabbix_server.conf文件,添加一行
AlertScriptsPath=/etc/zabbix/bin
6.1、Shell腳本使用
獲取 AccessToken
curl -s -G url
傳送憑證調用企業號接口
curl --data url
編輯腳本
[root@iot-svndata02 bin]# cat weixin.sh #!/bin/bash ###SCRIPT_NAME:weixin.sh### ###send message from weixin for zabbix monitor### ###zongx### ###V1-2017-06-12### #CropID='wx01975981c5d1502c' #Secret='k6hjyg4UY1Sx4YZIA6teqq5Q_cjjuDKliN3fVCUksN0' #GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" #Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F \" '{print $10}') Gtoken=(`cat /etc/zabbix/bin/token_new.txt`) echo "Gtoken="$Gtoken PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken" function body() { local int AppID=1000002 #企業號中的應用id # local UserID=@all #部門成員id,zabbix中定義的微信接收者 local UserID=$1 local PartyID=@all #部門id,定義了范圍,組內成員都可接收到消息 local Msg=$(echo "$@" | cut -d" " -f3-) #過濾出zabbix中傳遞的第三個參數 printf '{\n' printf '\t"touser": "'"$UserID"\"",\n" # printf '\t"toparty": "'"$PartyID"\"",\n" printf '\t"msgtype": "text",\n' printf '\t"agentid": "'"$AppID"\"",\n" printf '\t"text": {\n' printf '\t\t"content": "'"$Msg"\""\n" printf '\t},\n' printf '\t"safe":"0"\n' printf '}\n' } /usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL time=`date +"%Y-%m-%d"` echo "`date` $1 $2 $3">>/var/log/zabbix/zbx_weixin-$time.log [root@iot-svndata02 bin]#
#http://qydev.weixin.qq.com/wiki/index.php?title=消息類型及數據格式
#測試:
#./weixin.sh test hello.world!
{"errcode":0,"errmsg":"ok","invaliduser":"all user invalid"}
6.2、python腳本
#安裝simplejson
wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz tar zxvf simplejson-3.8.2.tar.gz && cd simplejson-3.8.2 python setup.py build python setup.py install
#下載wechat.py腳本
git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.git cp Zabbix-Alert-WeChat/wechat.py /opt/zabbix/share/zabbix/alertscripts/ chmod +x wechat.py && chown zabbix:zabbix wechat.py
#腳本修改
#!/usr/bin/python #_*_coding:utf-8 _*_ import urllib,urllib2 import json import sys import simplejson 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":"touser", #企業號中的用戶帳號,在zabbix用戶Media中配置,如果配置不正常,將按部門發送。 "toparty":"8", #企業號中的部門id。 "msgtype":"text", #消息類型。 "agentid":"10", #企業號中的應用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 = 'xxxxxx' #CorpID是企業號的標識 corpsecret = 'M3FMhnFh8nTI6SxLAEbbLLZaj-1BpZIyqkJRskeMMUXObGx4mfQsAg7Jw-nUMXe9' #corpsecretSecret是管理組憑證密鑰 accesstoken = gettoken(corpid,corpsecret) senddata(accesstoken,user,subject,content) #28,29,31行分別改為用戶賬號,部門ID,和應用ID #48,49 改為CropID和Secret #文中使用的用戶為,test-msg,部門iD為8,應用ID為10. #腳本測試 [root@zabbix alertscripts]# ./wechat.py test-msg test hello https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx11ac451376ae0e98&corpsecret=M3FMhnFh8nTI6SxLAEbbLLZaj-1BpZIyqkJRskeMMUXObGx4mfQsAg7Jw-nUMXe9 {u'invaliduser': u'all user invalid', u'errcode': 0, u'errmsg': u'ok'} #可以顯示成功
7、腳本路徑設置
#將腳本放到zabbix默認執行的路徑下
mv wechat.php weixin.sh /opt/zabbix/share/zabbix/alertscripts/ chown zabbix:zabbix /opt/zabbix/share/zabbix/alertscripts/wechat.php chmod +x /opt/zabbix/share/zabbix/alertscripts/wechat.php 或 chown zabbix:zabbix /opt/zabbix/share/zabbix/alertscripts/weixin.sh chmod +x /opt/zabbix/share/zabbix/alertscripts/weixin.sh #設置腳本的啟動用戶為zabbix,並給腳本可執行權限 #修改zabbix_server.conf文件,添加腳本執行目錄 AlertScriptsPath=/opt/zabbix/share/zabbix/alertscripts #修改完成重啟zabbix_server /etc/init.d/zabbix_server restart
8、Zabbix-web前端設置
8.1、設置通知媒介
8.2、創建用戶
8.3、創建觸發動作及發送內容
告警操作:
默認標題:{TRIGGER.STATUS},{HOSTNAME1}:{TRIGGER.NAME}
消息內容:
[生產環境]
告警名稱: {TRIGGER.NAME}
告警狀態: {TRIGGER.STATUS}
發生時間: {EVENT.DATE} {EVENT.TIME}
告警級別: {TRIGGER.SEVERITY}
告警內容:
{ITEM.NAME1} ({HOST.NAME1}:{ITEM.KEY1}): {ITEM.VALUE1}
恢復主題:
默認標題:{TRIGGER.STATUS}: {TRIGGER.NAME}
消息內容:
[生產環境]
告警名稱: {TRIGGER.NAME}
告警狀態: {TRIGGER.STATUS}
發生時間: {EVENT.DATE} {EVENT.TIME}
告警級別: {TRIGGER.SEVERITY}
告警內容:
{ITEM.NAME1} ({HOST.NAME1}:{ITEM.KEY1}): {ITEM.VALUE1}
確認操作:
默認標題:Acknowledged: {TRIGGER.NAME}
消息內容:
{USER.FULLNAME} acknowledged problem at {ACK.DATE} {ACK.TIME} with the following message:
{ACK.MESSAGE}
Current problem status is {EVENT.STATUS}
#設置完成記得新增或更新(add&update)
9、測試微信告警發送
#主動觸發相關trigger告警,查看微信發送狀態
#到此Zabbix結合微信告警完成。
10、注意事項及報錯處理
#Zabbix-web頁面新增用戶權限處理,發送對象選擇(應用ID)
#應用當中可見范圍選擇注意(選這要發送的對象(部門,及部門成員))
#需要配合企業微信加公眾號一起使用,微信關注公眾號以后才能收到消息。