1. 程式人生 > >阿里雲日誌報警

阿里雲日誌報警

這裡簡單介紹下基於阿里雲日誌服務的報警設定

1:首先通過loghub輸出到阿里雲日誌服務

2:基於日誌服務這裡簡單介紹兩種報警

    2.1:基於阿里雲本身提供的報警設定

        2.1.1:進入需要查詢的標籤頁面,在輸入框輸入查詢語句。例如:

            *|select inst_id,request_uri,requestId,duration where duration > 8000

            這樣就查詢出來符合條件的日誌啦,當然查詢的條件必須加入到索引裡

        2.1.2:右上角 另存為快速查詢,起個名字就完事啦

        2.1.3:右上角 另存為報警,快遞查詢名就選剛才2.1.2的名字,報警動作這裡 自己選擇,我一般都是通過釘釘,寫入釘釘機器人的地址即可

    2.2:基於阿里雲API

        如果你想嘗試通過API去寫程式碼實現以上步驟也完全ok

def get_log_duration():
    topic = ""
    query = "duration"
    From = int(time.time()) - 60
    To = int(time.time())

    endpoint = 'cn-hangzhou.log.aliyuncs.com'       # 選擇與上面步驟建立Project所屬區域匹配的Endpoint
    accessKeyId = ''    # 使用您的阿里雲訪問金鑰AccessKeyId
    accessKey = ''      # 使用您的阿里雲訪問金鑰AccessKeySecret
project = '' # 上面步驟建立的專案名稱 logstore = '' # 上面步驟建立的日誌庫名稱 # 構建一個client client = LogClient(endpoint, accessKeyId, accessKey) req4 = GetLogsRequest(project, logstore, From, To, topic, query) res4 = client.get_logs(req4) # if res4 is not None and res4.is_completed(): # print "is_completed" if res4 is not None: return map(lambda x: x.contents, res4.get_logs()) def get_max_duration(): info = get_log_duration() date = { "duration": 4000, "requestUri": "", "requestId": "", "inst_id":"", "timestamp":"" } for line in info: dura = line.get("duration") Uri = line.get("requestUri") reqid = line.get("requestId") inst_id = line.get("inst_id") timestamp = line.get("@timestamp") if int(dura) > int(date["duration"]): date["duration"] = dura date["requestUri"] = Uri date["requestId"] = reqid date["inst_id"] = inst_id date["timestamp"] = timestamp if date["duration"] > 4000: return date date = get_max_duration() 下面就是報警程式碼,在上篇 阿里雲統計每日消費 已有釘釘報警,這裡就不做介紹啦,當然必須在資料存在的情況下再做報警,否者就退出。