zabbix微信报警脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/python

__author__ = 'xiemx'

import sys
import json,requests
import os
import logging

class Weixin(object):

    def get_token(self):
        CorpID = '-------4fa4'
        Secret = 'Aew6oxx-----------FaTClkjXlmw_zH'
        token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}'.format(CorpID, Secret)
        response = requests.get(token_url, verify=False).content
        p = json.loads(response)
        token = p['access_token']
        return token

    def send_msg(self, user_id, msg):
        token = self.get_token()
        url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}'.format(token)
        send_content={
            "touser": user_id,
            "msgtype": "text",
            "agentid": "2",
            "text": {
               "content": msg
            },
            "safe":"0"
            }
       
        p = requests.post(url, verify=False, data=json.dumps(send_content))
        print p.content
        logging.debug("weixin send success")

if __name__ == "__main__":
    user_id = sys.argv[1]
    msg = sys.argv[2] + '\n' + sys.argv[3]
    weixin = Weixin()
    weixin.send_msg(user_id, msg)