Python 簡單腳本對接釘釘機器人


本腳本是簡單調用釘釘api接口實現報警

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: Mr.xiong
# Date: 20190329

import requests
import json
import os
import time

headers = {'Content-Type': 'application/json'}
hostname = os.popen("hostname").read().strip()
apiurl = "https://oapi.dingtalk.com/robot/send?access_token=8afz79e4381784a3938f4e68303e5bd31e640c5ce556ece86801d76a5028c258"


def monitor():
    li = []
    text = """
    【xx科技】故障: socket在線用戶數趨勢監測 > 100

     告警主機: {}

     告警時間: {}

     問題詳情: socketonlineuser

     當前狀態: {}
    """
    while True:
        fp = open('/tmp/socketuser.log', 'a', encoding='utf-8')
        r = os.popen("curl http://10.10.17.32:9099/ping 2>/dev/null").read()
        d = json.loads(r)
        onlineuser = d["onlineuser"]
        currOnline = d["currOnline"]
        if len(li) < 2:
            li.append(onlineuser)
        elif len(li) == 2:
            li.pop()
            li.append(onlineuser)
            time.sleep(60)
            abscount = abs(li[1] - li[0])
            if abscount == 0:
                currenttime = time.strftime("%Y-%m-%d %X")
                value = text.format(hostname, currenttime, currOnline)
                json_text = {"msgtype": "text", "text": {"content": value}}
                rep = requests.post(apiurl, data=json.dumps(json_text), headers=headers).json()
                code = rep["errcode"]
                if code == 0:
                    fp.write(currenttime + ":消息發送成功 返回碼:" + str(code) + "\n")
                else:
                    fp.write(currenttime + ":消息發送失敗 返回碼:" + str(code) + "\n")
                fp.close()


if __name__ == '__main__':
    monitor()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM