使用微信告警


一、 使用企業微信告警

地址如下:

https://work.weixin.qq.com/wework_admin/frame
沒有企業微信需要注冊:https://work.weixin.qq.com/

二、主要是用獲取的token去發送微信消息

利用企業id和企業secret去獲取tocken,利用獲取到的tocken來發送微信消息

三、具體的python腳本實現如下

#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import sys
import urllib,urllib2

#需要三個變量corpid、corpsecret、agentid
agentid = 'xxx'
corpid = 'xxxxx'
corpsecret = 'xxxx'

#獲取tocken,存在my_token里面
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
token_file = urllib2.urlopen(gettoken_url)
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
my_token = token_json['access_token']

#利用獲取到的tocken發送微信信息
touser=sys.argv[1] #發送給誰,多個用分號分享,例如'zhangsan|wangwu'
content=sys.argv[2] #發送的內容
post_content = {
        "touser":touser,
        "agentid":agentid,
        "msgtype": "text",
        "text":{
                "content":content,
        }
}
json_content = json.dumps(post_content)

url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + my_token
response = urllib2.urlopen(url,json_content)

#查看返回結果
print response.read().decode('utf-8')

四、調用

python /usr/local/src/script 'zhangsan' '所剩余內存不足100M'

五、zabbix設置(到alertscripts目錄下)

vim zabbix_wx.py
把上述腳本拷貝進去

六、報警媒介添加微信告警

{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}

七、設置企業微信用戶名

就是企業微信的賬號

八、添加觸發微信告警的動作

九、進行測試

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM