vi wechat.py
#!/usr/bin/env python
-- coding: utf-8 --
import requests
import json
import sys
企业号及应用相关信息
corp_id = ‘ww9f15d225845117a‘
corp_secret = ‘aV_ZC0Ahw0Ob-EhjWMh3x1CDdOxWQLtK6FtI-oki7‘
agent_id = ‘1000002‘
存放access_token文件路径
file_path = ‘/tmp/access_token.log‘
def get_access_token_from_file():
try:
f = open(file_path, ‘r+‘)
this_access_token = f.read()
print(‘get success %s‘ % this_access_token)
f.close()
return this_access_token
except Exception as e:
print(e)
获取token函数,文本里记录的token失效时调用
def get_access_token():
get_token_url = ‘https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s‘ % (corp_id, corp_secret)
print(get_token_url)
r = requests.get(get_token_url)
request_json = r.json()
this_access_token = request_json[‘access_token‘]
print(this_access_token)
r.close()
把获取到的access_token写入文本
try:
f = open(file_path, ‘w+‘)
f.write(this_access_token)
f.close()
except Exception as e:
print(e)
# 返回获取到的access_token值
return this_access_token
snedMessage
死循环,直到消息成功发送
flag = True
while (flag):
从文本获取access_token
access_token = get_access_token_from_file()
try:
to_user = ‘@all‘
message = sys.argv[3]
send_message_url = ‘https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s‘ % access_token
# print(send_message_url)
message_params = {
"touser": to_user,
"msgtype": "text",
"agentid": agent_id,
"text": {
"content": message
},
"safe": 0
}
r = requests.post(send_message_url, data=json.dumps(message_params))
print(‘post success %s ‘ % r.text)
# 判断是否发送成功,如不成功则跑出异常,让其执行异常处理里的函数
request_json = r.json()
errmsg = request_json[‘errmsg‘]
if errmsg != ‘ok‘:
raise ValueError(‘发送告警微信失败!‘)
# 消息成功发送,停止死循环
flag = False
except Exception as e:
print(e)
access_token = get_access_token()
微信需要接一个脚本发送相关内容
vi send_wx.sh
#!/bin/bash
#$1 : 发送相关人员
#$2 : 告警标题
#$3 : 告警内容
to=$1
subject=$2
body=$3
/usr/bin/python /usr/lib/zabbix/alertscripts/wechat.py "$to" "$subject" "$body"
以下是钉钉告警
vi Dingding.py
#!/usr/bin/env python
#coding:utf-8
import requests,json,sys,os,datetime
webhook="https://oapi.dingtalk.com/robot/send?access_token=c7385a46a77cc94c755d3e8a77d0773ac54a5ffb84a61a88324077281d1" #说明:这里改为自己创建的机器人的webhook的值
user=sys.argv[1]
text=sys.argv[3]
data={
"msgtype": "text",
"text": {
"content": text
},
}
headers = {‘Content-Type‘: ‘application/json‘}
x=requests.post(url=webhook,data=json.dumps(data),headers=headers)