Loading

使用微信告警

一、 使用企业微信告警

地址如下:

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}

七、设置企业微信用户名

就是企业微信的账号

八、添加触发微信告警的动作

九、进行测试

 

posted @ 2018-07-10 21:30  KubeSec  阅读(1695)  评论(0编辑  收藏  举报