1. 程式人生 > >python分析nginx日誌,每分鐘nginx請求超過10ms的比例

python分析nginx日誌,每分鐘nginx請求超過10ms的比例

python分析nginx日誌每分鐘n

代碼如下: #!/usr/bin/python # --*-- coding:utf-8 --*-- import time import datetime import sys import os import os.path import re import json import socket import requests import subprocess class NginxLog(object): def __init__(self, log_file, seek_file): self.log_file = log_file self.seek_file = seek_file def hostname(self): """host_name: 主機名""" host_name = socket.gethostname() return host_name def writeSeek(self, seek): """讀過的遊標寫入臨時文件""" with open(self.seek_file,'w') as f: f.write(time.strftime("%Y-%m-%d %H:%M:%s", time.localtime(time.time())) + '\n') f.write(str(seek) + "\n") def LogRead(self): """讀出新生成的日誌 # 如果第一次運行,或是刪除臨時文件,從頭運行,否則,從上次讀取之後運行 # 0代表從頭開始,1代表當前位置,2代表文件最末尾位置 chunk: 返回一行日誌 """ if os.path.exists(self.seek_file): with open(self.seek_file) as f: seek_tmp = f.readlines() seek_old = int(seek_tmp[1].strip()) else: seek_old = 0 with open(self.log_file) as f: #記錄當前最新文件遊標 f.seek(0,2) #最新遊標位置 seek_now = f.tell() # 讀取上次讀完之後的日誌 if seek_now >= seek_old: f.seek(seek_old,0) #從文件開頭位置偏移 chunk = f.read(seek_now - seek_old) #如果seek_now-seek_old小於0說明日誌輪訓 else: f.seek(0,0) chunk = f.read(seek_now) # 將這次的遊標寫入臨時文件 self.writeSeek(seek_now) return chunk def Log_percent(self): """獲取分鐘超過10ms請求數的百分比 low_request_time: 低於10ms的請求數 high_request_time: 高於10ms的請求數 """ low_request_time = [] high_request_time = [] for line in self.LogRead().split('\n'): tmp_time = line.split(' ')[-1] if tmp_time: tmp_data = float('%.3f' % float(tmp_time)) request_time = int(tmp_data * 1000) if request_time > 10: high_request_time.append(request_time) else: low_request_time.append(request_time) # 一分鐘請求總數 count = float(len(low_request_time) + len(high_request_time)) # 超過10ms的百分比 if count: result = float(len(high_request_time))/count #只取分子 percent = int(result * 100) return percent else: return 0 #當一分鐘請求數為0時,返回0 def push_falcon(self, data, url): """數據推送到openfalcon""" host = self.hostname() current_time = int(time.time()) payload = [ { "endpoint": host, "metric": "nginx_request_percent", "timestamp": current_time, "step": 60, "value": data, "counterType": "GAUGE", "tags": "nginx_request_percent=10ms", } ] json_data=json.dumps(payload) print json_data res = requests.post("http://127.0.0.1:1988/v1/push", data=json_data) def main(): # 日誌文件位置 log_file = "/root/access.log" seek_file = "/root/seek_temp.log" url = "http://127.0.0.1:1988/v1/push" nginx_log = NginxLog(log_file,seek_file) percent = nginx_log.Log_percent() nginx_log.push_falcon(percent,url) if __name__ == '__main__': main()


python分析nginx日誌,每分鐘nginx請求超過10ms的比例