您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關python日志分析統計腳本是什么,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
腳本類似shell的tail功能,用來統計每分鐘的4xx、5xx的狀態碼數量,統計php執行時間小于1秒、1-5秒、5秒以上的數量,和每秒的并發請求。將結果放到/tmp目錄下, 也可以用cacti將結果畫圖。
#!/usr/bin/env python import time, os #----------------------------- log_file = '/var/log/httpd/cmi_access_log' #----------------------------- log_name = log_file.split('/')[-1] file = open(log_file, 'r') lt1,to1_5,gt5,status4,status5,concurrent = 0,0,0,0,0,0 Time2 = '0' st_results = os.stat(log_file) st_size = st_results[6] file.seek(st_size) size = os.path.getsize(log_file) while 1: Time = time.strftime("%Y_%m_%d",time.localtime(time.time() - 60)) where = file.tell() line = file.readline() L = line.split('"') Time1 = time.strftime("%Y_%m_%d_%H:%M",time.localtime(time.time() - 60)) if not line: size = os.path.getsize(log_file) if size < where: file = open(log_file, 'r') else: time.sleep(1) file.seek(where) else: try: Phptime = float(L[-2]) url = L[1] status = L[2][1:4] if '4' in status[0]: status4 += 1 elif '5' in status[0]: status5 += 1 if Time1 in Time2: if 'f5.php' not in url: concurrent += 1 if Phptime < 1: lt1 += 1 elif 1 <= Phptime <= 5: to1_5 += 1 else: gt5 += 1 else: concurrent = concurrent / 60 out = Time1 + "," + str(lt1) + "," + str(to1_5) + "," + str(gt5) + "," + str(status4) + "," + str(status5) + "," + str(concurrent) F = open('/tmp/' + log_name + '_' + Time + '.data', 'a') print >> F, out F.close() Time2 = Time1 lt1,to1_5,gt5,status4,status5,concurrent = 0,0,0,0,0,0 except: pass
上述就是小編為大家分享的python日志分析統計腳本是什么了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。