監控某一文件是否有錯誤日誌

# encoding=utf-8
import re
import json
import time
import requests
import os,sys
import subprocess
logFile1="/opt/pubtrans/logs/ods-flume/logs/flume.log"
city = sys.argv[1]
def monitorLog(logFile):
    # print'監控的日誌文件 是 %s'%logFile
    popen = subprocess.Popen('tail -f '+ logFile,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
    pid = popen.pid
    #print 'Popen.pid:'+str(pid)
    while True:
        line=popen.stdout.readline().strip()
# 判斷內容是否爲空
        if line:
            # print line
            #errorlog = re.findall('The channel is full or unexpected failure',line)
            errorlog = re.findall('The channel is full or unexpected failure',line)
            if errorlog:
                dingdingwarning()
        time.sleep(1)
def dingdingwarning():
    url = 'https://oapi.dingtalk.com/robot/send?access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXX'  # 釘釘機器人的webhook地址
    HEADERS = {
        "Content-Type": "application/json ;charset=utf-8 "
    }
    message = city +"flume隊列已滿,請重啓應用."

    String_textMsg = {
        "msgtype": "text",
        "text": {"content": message},
        "at": {
            "atMobiles": [
                "15888826546"  # 如果需要@某人,這裏寫他的手機號
            ],
            "isAtAll": 0  # 如果需要@所有人,這些寫1
        }
    }
    String_textMsg = json.dumps(String_textMsg)
    res = requests.post(url, data=String_textMsg, headers=HEADERS)

if __name__ == "__main__":
    while True:
        monitorLog(logFile1)
        time.sleep(4*60)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章