• zabbix 发送邮件到企业微信




    #!/usr/bin/python2.7
    #_*_coding:utf-8 _*_
    #auther:拿来用用
    import requests,sys,json
    import urllib3
    urllib3.disable_warnings()




    def GetTokenFromServer(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)
    print(r.json())
    if r.json()['errcode'] != 0:
    return False
    else:
    Token = r.json()['access_token']
    file = open(r'zabbix_wechat_config.json', 'w')
    file.write(r.text)
    file.close()
    return Token

    def SendMessage(User,Agentid,Subject,Content):
    try:
    file = open(r'zabbix_wechat_config.json', 'r')
    Token = json.load(file)['access_token']
    file.close()
    except:
    Token = GetTokenFromServer(Corpid, Secret)

    n = 0
    Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
    Data = {
    "touser": User, # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
    #"totag": Tagid, # 企业号中的标签id,群发使用(推荐)
    #"toparty": Partyid, # 企业号中的部门id,群发时使用。
    "msgtype": "text", # 消息类型。
    "agentid": Agentid, # 企业号中的应用id。
    "text": {
    "content": Subject + ' ' + Content
    },
    "safe": "0"
    }
    r = requests.post(url=Url,data=json.dumps(Data),verify=False)
    while r.json()['errcode'] != 0 and n < 4:
    n+=1
    Token = GetTokenFromServer(Corpid, Secret)
    if Token:
    Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
    r = requests.post(url=Url,data=json.dumps(Data),verify=False)
    print("fgdfkjgndfkjbgndfk")

    #return r.json()


    if __name__ == '__main__':
    User = "six wood"
    Subject = "sys.argv[2] # zabbix传过来的第二个参数"
    Content = "sys.argv[3] # zabbix传过来的第三个参数"
    Corpid = "ww22352a6cfdfd3ecd55da" # CorpID是企业号的标识
    Secret = "DKQwfdfd5ZjLbvbT2eVA4K3_JB_d6WJ12t7RE5mehqERWsQ"
    #Tagid = "1" # 通讯录标签ID
    Agentid = "1000002" # 应用ID
    #Partyid = "1" # 部门ID

    Status = SendMessage(User,Agentid,Subject,Content)

    改行去放羊
  • 相关阅读:
    最小二乘法拟合(python numpy) Littlefish
    我的话
    亿万富豪们给2013年毕业生的忠告
    网站色彩搭配<转载>
    灾难专用使你的网站变黑为雅安默哀
    <转载>协议森林13 9527 (DNS协议)
    不常见的HTML标签<转载>
    tomcat支持shml配置详解
    <转载>struts2 拦截器 interceptor
    乱码解决
  • 原文地址:https://www.cnblogs.com/musen/p/12055417.html
Copyright © 2020-2023  润新知