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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| ######shell脚本:weixin.sh #!/bin/bash ###SCRIPT_NAME:weixin.sh### ###send message from weixin for zabbix monitor### ###wuhf### ###V1-2019-06-18### CropID='ww37662****' Secret='yD04iYsPMipqWRfGduBjuJ8uzb7j******' GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}') PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken" function body() { local int AppID=1000002 #企业号中的应用id local UserID=$1 #部门成员id,zabbix中定义的微信接收者 local PartyID=1 #部门id,定义了范围,组内成员都可接收到消息 local Msg=$(echo "$@" | cut -d" " -f3-) printf '{\n' printf '\t"touser": "'"$UserID"\"",\n" printf '\t"toparty": "'"$PartyID"\"",\n" printf '\t"msgtype": "text",\n' printf '\t"agentid": "'" $AppID "\"",\n" printf '\t"text": {\n' printf '\t\t"content": "'"$Msg"\""\n" printf '\t},\n' printf '\t"safe":"0"\n' printf '}\n' } /usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL ######python脚本:wechat.py #!/usr/bin/python2.7 #_*_coding:utf-8 _*_ import requests,sys,json import urllib3 urllib3.disable_warnings() reload(sys) sys.setdefaultencoding('utf-8') 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中配置,如果配置不正常,将按部门发送。 #"totag": Tagid, # 企业号中的部门id,群发时使用。 "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 = "ww376622********" # CorpID是企业号的标识 Secret = "yD04iYsPM***************" # Secret是管理组凭证密钥 #Tagid = "1" # 通讯录标签ID User = "PenHu" #指定用户名 Agentid = "1000002" # 应用ID Token = GetToken(Corpid, Secret) Status = SendMessage(Token,User,Agentid,Subject,Content) print Status #####python3.5版本以上 #!/usr/local/bin/python3.5 #coding:utf-8 import requests,sys,json import urllib3 import sys import imp import importlib importlib.reload(sys) urllib3.disable_warnings() 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中配置,如果配置不正常,将按部门发送。 "totag": Tagid, # 企业号中的部门id,群发时使用。 "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 = "wwc*****f25*****c1" # CorpID是企业号的标识 Secret = "vL*****wE6F5h1*****0sLc" # Secret是管理组凭证密钥 Tagid = "47" # 通讯录标签ID User = "0*****822" #指定用户名 Agentid = "10*****13" # 应用ID Token = GetToken(Corpid, Secret) Status = SendMessage(Token,User,Agentid,Subject,Content) print (Status)
|