zabbix 微信api告警调用

2019-05-31 20:41:12 浏览数 (1)

代码语言:javascript复制
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# --------------------------------------
#Author:  LJ
#Email:   admin@attacker.club
#Last Modified: 2018-04-27 23:37:38

import requests,sys,json

from requests.packages.urllib3.exceptions import InsecureRequestWarning
# 禁用安全请求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


def GetToken(Corpid,Secret):
    Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
    Data = {
        "corpid":Corpid,
        "corpsecret":Secret
    }
    r = requests.get(url=Url,params=Data,verify=False)
    Token = r.json()['access_token']
    return Token


def SendMessage(Token,User,Agentid,Subject,Content):
    Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
    Data = {
        "touser": User,                                 # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        "msgtype": "text",                              # 消息类型。
        "agentid": Agentid,                             # 企业号中的应用id。
        "text": {
            "content": Subject   'n'   Content
        },
        "safe": "0"
    }
    r = requests.post(url=Url,data=json.dumps(Data),verify=False)
    return r.text


if __name__ == '__main__':
    User = sys.argv[1]                                                              # zabbix传过来的第一个参数
    Subject = sys.argv[2]                                                           # zabbix传过来的第二个参数
    Content = sys.argv[3]                                                           # zabbix传过来的第三个参数

    Corpid = "wwe53bb448xxx"                                                   # CorpID是企业号的标识
    Secret = "ljB556jd8p_F8kZMM58uzqJEtK8W3_xxx"                          # Secret是管理组凭证密钥
    #Tagid = "1"                                                                    # 通讯录标签ID
    Agentid = "1000002"

    Token = GetToken(Corpid, Secret)
    Status = SendMessage(Token, User, Agentid, Subject, Content)
    print(Status)

0 人点赞