码迷,mamicode.com
首页 > 微信 > 详细

zabbix 微信报警脚本

时间:2017-05-19 17:47:43      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:init   ini   test   zabbix   ext   url   failed   脚本   list   

 

#! /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)

 

zabbix 微信报警脚本

标签:init   ini   test   zabbix   ext   url   failed   脚本   list   

原文地址:http://www.cnblogs.com/37yan/p/6879475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!