author:headsen chen
date:2019-03-05 18:06:53
1,網友的
import json,requests
def info():
url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxx'
headers = { 'Content-Type': 'application/json;charset=utf-8' }
formdata = { "msgtype": "text", "text": {"content":str('測試釘釘')} }
hhh = requests.post(url=url, data=json.dumps(formdata), headers=headers)
print hhh.text if __name__ == '__main__':
info()
2,實際使用的:
import json,requests def Alert(msg): headers = {"Content-Type": "application/json"} data = {"msgtype": "text", "text": {"content": msg}} r = requests.post( "https://oapi.dingtalk.com/robot/send?access_token= xxx ", data=json.dumps(data), headers=headers) Alert(msg)
3,網友的釘釘報警設置:
3.1,申請釘釘賬號,登錄賬號。
3.2,創建一個告警群組,添加一個機器人。
記錄下來這個webhook地址。
3.3,腳本配置
在/usr/local/zabbix/share/zabbix/alertscripts目錄下面創建腳本,名稱為zabbix_dingding.py
腳本的內容如下:
#!/usr/bin/python # -*- coding: utf-8 -*- import requests import json import sys import os headers = {'Content-Type': 'application/json;charset=utf-8'} api_url = "剛才復制的" def msg(text): json_text= { "msgtype": "text", "text": { "content": text }, "at": { "atMobiles": [ "1xxxxxxxxxxxxx" ], "isAtAll": False } } print requests.post(api_url,json.dumps(json_text),headers=headers).content if __name__ == '__main__': text = sys.argv[1] msg(text)
3.4 給腳本添加執行權限測試腳本
[root alertscripts]# chmod +x zabbix_dingding.py [root alertscripts]# ll total 4 -rwxr-xr-x 1 zabbix zabbix 933 Aug 17 10:05 zabbix_dingding.py [root alertscripts]# ./zabbix_dingding.py test {"errmsg":"ok","errcode":0} [root alertscripts]#
釘釘正確的收到了我們的測試告警,證明監控腳本生效了。