Created
June 6, 2024 07:42
-
-
Save nemasu/de5adabb2d45fd51e200e5eb3f246371 to your computer and use it in GitHub Desktop.
Python script to count nginx average requests per hour/minute.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import dateutil.parser | |
import sys | |
def find(pat, text, match_item): | |
match = re.findall(pat, text) | |
if match: | |
return match | |
else: | |
return False | |
def get_requests(f): | |
log_line = f.read() | |
pat = (r'' | |
'(\d+.\d+.\d+.\d+)\s-\s-\s' #IP address | |
'\[(.+)\]\s' #datetime | |
'"GET\s(.+)\s\w+/.+"\s\d+\s' #requested file | |
'\d+\s"(.+)"\s' #referrer | |
'"(.+)"' #user agent | |
) | |
requests = find(pat, log_line, None) | |
return requests | |
def process_log(log): | |
requests = get_requests(log) | |
counter={} | |
for r in requests: | |
d = dateutil.parser.parse(r[1].replace(':',' ', 1)) | |
key = str(d.year)+str(d.month)+str(d.day)+str(d.hour) | |
if key not in counter: | |
counter[key] = 0 | |
counter[key] += 1 | |
return counter | |
with open(sys.argv[1], 'r') as log_file: | |
per_hour = process_log(log_file) | |
avg = 0 | |
for i in per_hour: | |
print(str(i) + ": " + str(per_hour[i])) | |
avg += per_hour[i] | |
p_hour=avg/len(per_hour.keys()) | |
print("avg/h: " + str(p_hour)) | |
print("avg/m: " + str(p_hour/60)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment