zabbix 微信报警脚本

时间:2017-05-19 17:47:43   收藏:0   阅读:365

 

#! /bin/bash
userid=$1
content=$2
python /data/zabbix/alertscripts/weixin.py  $userid $content

 

#!/usr/bin/env python
# _*_ coding:utf8 _*_
import urllib2
import simplejson as json
import sys
class weChat:
    def __init__(self,url,Corpid,Secret):
        url = ‘%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s‘ % (url,Corpid,Secret)
        res = self.url_req(url)
        self.token = res[‘access_token‘]

    def url_req(self,url,method=‘get‘,data={}):
        if method == ‘get‘:
            req = urllib2.Request(url)
            res = json.loads(urllib2.urlopen(req).read())
        elif method == ‘post‘:
                        req = urllib2.Request(url,data)
                        res = json.loads(urllib2.urlopen(req).read())
        else:
            print ‘error request method...exit‘
            sys.exit()
        return res
    def send_message(self,userlist,content,agentid=1000002):
        self.userlist = userlist
        self.content = content
        url = ‘https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s‘ % self.token
        data = {
                      "touser": "",
                      "toparty": "",
                      "totag": "",
                      "msgtype": "text",
                      "agentid": "0",
                      "text": {
                          "content": ""
                      },
                      "safe":"0"
                   }
        data[‘touser‘] = userlist
        data[‘agentid‘] = agentid
        data[‘text‘][‘content‘] = content
        data = json.dumps(data,ensure_ascii=False)
    #   print data
        res = self.url_req(url,method=‘post‘,data=data)
        if res[‘errmsg‘] == ‘ok‘:
            print ‘send sucessed!!!‘
        else:
            print ‘send failed!!‘
            print res




if __name__ == ‘__main__‘:
      userlist = sys.argv[1]
#      userlist = ‘ZhangSen‘
      content = sys.argv[2:]
      content = ‘\n‘.join(content)
#      content = ‘test1‘
      Corpid = ‘wwdc52e74bcaa0dfc2‘  #此处对应修改
      Secret = ‘Y1Xh5eQmlc7QSJ1RblNXEjppUVYfqwvWhiKJmBncOBk‘  #此处对应修改
      url = ‘https://qyapi.weixin.qq.com‘

wechat = weChat(url,Corpid,Secret)
wechat.send_message(userlist,content)

 

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!