zabbix 微信报警脚本

Posted 三木燕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zabbix 微信报警脚本相关的知识,希望对你有一定的参考价值。

 

#! /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 微信报警脚本的主要内容,如果未能解决你的问题,请参考以下文章

Zabbix微信报警脚本及写触发记录

zabbix 微信报警脚本

Zabbix--03 邮件报警微信报警

Zabbix报警脚本-微信

zabbix之企业微信报警通知

zabbix微信报警