Python钉钉报警及Zabbix集成钉钉报警
钉钉报警设置
创建群机器人
11111
接口地址
发送短消息
发送普通消息
import requests
import json
url = 'https://oapi.dingtalk.com/robot/send?access_token=71638980426c030'
headers = {
"Content-Type": "application/json",
"Chartset": "utf-8"
}
# 要发送的文本是json格式
request_data = {
# 此消息的类型为固定的text
"msgtype": "text",
"text": {
# 消息的内容
"content": "大家新年快乐test"
},
"at": {
# 被@人的手机号
"atMobiles": [],
# 控制@所有人
"isAtAll": True
}
}
# 把json转变为字符串格式数据
send_data = json.dumps(request_data)
# 这个是发送post请求,请求钉钉接口
response = requests.post(url=url, headers=headers, data=send_data)
# 讲求成功后返回的数据
content = response.content.decode()
# 打印
# 课程 vip 标准
# 替换 视频
print(content)
接口开发
修改结构
import requests
import json
import sys
url = 'https://oapi.dingtalk.com/robot/send?access_token=71389c030'
def WriteLogByDing(content):
headers = {
"Content-Type": "application/json",
"Chartset": "utf-8"
}
request_data = {
"msgtype": "text",
"text": {
"content": content
},
"at": {
"atMobiles": [],
"isAtAll": True
}
}
sendData = json.dumps(request_data)
response = requests.post(url=url, headers=headers, data=sendData)
content = response.content.decode()
print(content)
if __name__ == "__main__":
content = input('请输入想要的信息: ')
content += "test"
# content = sys.argv[1]
WriteLogByDing(content)
发送带链接的文档
import requests
import json
url = 'https://oapi.dingtalk.com/robot/send?access_token=7164d45fa912dc12ed721522371ecf7428ad912740e87a63c3bf38980426c030'
headers = {
"Content-Type": "application/json",
"Chartset": "utf-8"
}
# 要发送的文本是json格式
request_data = {
# 发送链接类型的数据
"msgtype": "link",
"link": {
# 链接提示
"text": "群机器人是钉钉群的高级扩展功能。群机器人可以将第三方服务的信息聚合到群聊中,实现自动化的信息同步。例如:通过聚合GitHub,GitLab等源码管理服务,实现源码更新同步;通过聚合Trello,JIRA等项目协调服务,实现项目信息同步。不仅如此,群机器人支持Webhook协议的自定义接入,支持更多可能性,例如:你可将运维报警提醒通过自定义机器人聚合到钉钉群。",
# 链接标题
"title": "自定义机器人协议test",
# 图片url地址
"picUrl": "http://p3.so.qhmsg.com/sdr/200_200_/t013d7a21145c708288.jpg",
# 信息的链接跳转
"messageUrl": "http://www.zcj.net.cn"
}
}
# 把json转变为字符串格式数据
send_data = json.dumps(request_data)
# 这个是发送post请求,请求钉钉接口
response = requests.post(url=url, headers=headers, data=send_data)
# 讲求成功后返回的数据
content = response.content.decode()
# 打印
# 课程 vip 标准
# 替换 视频
print(content)
发送makedown
import requests
import json
url = 'https://oapi.dingtalk.com/robot/send?access_token=7164d45fa912dc12ed721522371ecf7428ad912740e87a63c3bf38980426c030'
headers = {
"Content-Type": "application/json",
"Chartset": "utf-8"
}
# 要发送的文本是json格式
request_data = {
"msgtype": "markdown",
"markdown": {"title": "杭州天气",
"text": "#### 杭州天气 \n > 9度, 西北风1级,空气良89,相对温度73%\n\n > ![screenshot](http://i01.lw.aliimg.com/media/lALPBbCc1ZhJGIvNAkzNBLA_1200_588.png)\n > ###### 10点20分发布 [天气](http://www.thinkpage.cn/) test"
},
"at": {
"atMobiles": [],
"isAtAll": False
}
}
# 把json转变为字符串格式数据
send_data = json.dumps(request_data)
# 这个是发送post请求,请求钉钉接口
response = requests.post(url=url, headers=headers, data=send_data)
# 讲求成功后返回的数据
content = response.content.decode()
# 打印
print(content)
zabbix集成钉钉报警
钉钉报警python脚本
https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
https://blog.csdn.net/smile_mr/article/details/87357942
#!/usr/bin/env python
#coding:utf-8
#zabbix钉钉报警
import requests,json,sys,os,datetime
webhook="https://oapi.dingtalk.com/robot/send?access_token=ce0d39251"
user=sys.argv[1]
text=sys.argv[3]
data={
"msgtype": "text",
"text": {
"content": "test"
},
"at": {
"atMobiles": [
# user
],
"isAtAll": False
}
}
headers = {'Content-Type': 'application/json'}
x=requests.post(url=webhook,data=json.dumps(data),headers=headers)
if os.path.exists("/usr/local/zabbix_server/logs/dingding.log"):
f=open("/usr/local/zabbix_server/logs/dingding.log","a+")
else:
f=open("/usr/local/zabbix_server/logs/dingding.log","w+")
f.write("\n"+"--"*30)
if x.json()["errcode"] == 0:
f.write("\n"+str(datetime.datetime.now())+" "+str(user)+" "+"发送成功"+"\n"+str(text))
f.close()
else:
f.write("\n"+str(datetime.datetime.now()) + " " + str(user) + " " + "发送失败" + "\n" + str(text))
f.write(str(x)+str(x.json()))
f.close()