231fds 发表于 2016-1-19 08:26:22

zabbix使用微信报警python脚本

本帖最后由 231fds 于 2016-1-19 08:29 编辑

没有学过python语言,使用二天时间查了网上相关的脚本,有些不支持中文报警,自己重新写了一份。给大家参考下。
只提供脚本,减少大家的时间 ,微信申请参考http://www.iyunv.com/thread-166308-1-1.html

一、效果图:






二、python2脚本:
#!/usr/bin/python
# coding: utf-8
#python2将zabbix报警信息发送到微信。
#by linwangyi #2016-01-18
import urllib,urllib2
import json
import sys

def gettoken(corpid,corpsecret):
    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
    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,content):
    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
    send_values = {
      "touser":user,    #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
      "toparty":"1",    #企业号中的部门id
      "msgtype":"text",#企业号中的应用id,消息类型。
      "agentid":"1",
      "text":{
            "content":content
         },
      "safe":"0"
      }
    send_data = json.dumps(send_values, ensure_ascii=False)
    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)   #zabbix传过来的第一个参数
    content = str(sys.argv)#zabbix传过来的第三个参数
    corpid = 'XXXX'   #CorpID是企业号的标识
    corpsecret = 'XXXXX'#corpsecretSecret是管理组凭证密钥
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,user,content)

三、python3脚本:
#!/usr/bin/python3
# coding: utf-8
#python3将zabbix报警信息发送到微信。
#by linwangyi #2016-01-18
import urllib.request
import json
import sys

def gettoken(corp_id,corp_secret):
    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corp_id + '&corpsecret=' + corp_secret
    try:
      token_file = urllib.request.urlopen(gettoken_url)
    except urllib.error.HTTPError as e:
      print(e.code)
      print(e.read().decode("utf8"))
    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,content):
    try:
      send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
      send_values = {
            "touser":user,#企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
            "toparty":"1",#企业号中的部门id
            "msgtype":"text",
            "agentid":"1",   #企业号中的应用id,消息类型。
            "text":{
                "content":content
                },
            "safe":"0"
            }
      send_data = json.dumps(send_values, ensure_ascii=False).encode(encoding='UTF8')
      send_request = urllib.request.Request(send_url, send_data)
      response = urllib.request.urlopen(send_request)
      msg = response.read()
      print("returned value : " + str(msg))
    except:
      print("returned value : " + str(msg))

default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)
user = str(sys.argv)   #zabbix传过来的第一个参数
content = str(sys.argv)#zabbix传过来的第三个参数
corpid = 'XXXX'   #CorpID是企业号的标识
corpsecret = 'XXXXX'#corpsecretSecret是管理组凭证密钥
accesstoken = gettoken(corpid,corpsecret)
senddata(accesstoken,user,content)

四、脚本使用方法:
脚本名称微信用户ID标题(无用,只是zabbix传的第二个参数)   报警内容



五、centos6中安装python3方法:
YUM包安装
yum install https://centos6.iuscommunity.org/ius-release.rpm
yum -y install python35u
就可以安装python3.5版本。

页: [1]
查看完整版本: zabbix使用微信报警python脚本