Python釘釘報警及Zabbix整合釘釘報警

men發表於2020-08-16

釘釘報警設定

建立群機器人

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()

相關文章