https://oapi.dingtalk.com/robot/send?access_token=02f6ba03e133beb0e62499a6e7550b64f03a1120e47c4922a2db80f981834697
cd /usr/lib/zabbix/alertscripts
cat dingding.py
---------------------------------------------------------------------------
#!/usr/bin/env python
#coding:utf-8
#zabbix钉钉报警
import
requests,json,sys,os,datetime
webhook=
"https://oapi.dingtalk.com/robot/send?**********************************************"
user=sys.argv[1]
text=sys.argv[3]
data={
"msgtype"
:
"text"
,
"text"
: {
"content"
: text
},
"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/logs/dingding.log"
):
f=
open
(
"/usr/local/zabbix/logs/dingding.log"
,
"a+"
)
else
:
f=
open
(
"/usr/local/zabbix/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.close()
---------------------------------------------------------------------------
mkdir -p /usr/local/zabbix/logs
touch /usr/local/zabbix/logs/dingding.log
chown zabbix.zabbix dingding.py
chmod 755 dingding.py
chown zabbix.zabbix /usr/local/zabbix/logs/dingding.log
pip install requests(如果没有模块就安装)