Skip to content

Instantly share code, notes, and snippets.

@ruant
Last active June 19, 2025 08:58
Show Gist options
  • Save ruant/65d0f898c15e2b9bf6504c25f6a76735 to your computer and use it in GitHub Desktop.
Save ruant/65d0f898c15e2b9bf6504c25f6a76735 to your computer and use it in GitHub Desktop.
Parses IIS logs and groups them per day to give you a total count of requests for the path per day
import os
from datetime import datetime, timedelta
from collections import defaultdict
import csv
log_path = r"C:\Inetpub\logs\LogFiles\W3SVC1"
target_path_suffix = "SupplierStockInfo/GetSupplierStockInfo".lower()
target_method = "POST"
cutoff_date = datetime.now() - timedelta(days=60)
# Date -> count mapping
date_counts = defaultdict(int)
def parse_log_file(file_path):
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
fields = []
for line in f:
line = line.strip()
if line.startswith("#Fields:"):
fields = line.replace("#Fields:", "").strip().split()
continue
if line.startswith("#") or not fields:
continue
values = line.split()
if len(values) != len(fields):
continue
entry = dict(zip(fields, values))
if entry.get('cs-method') == target_method and entry.get('cs-uri-stem', '').lower().endswith(target_path_suffix):
try:
entry_date = datetime.strptime(entry['date'], "%Y-%m-%d")
if entry_date >= cutoff_date:
date_str = entry_date.strftime("%Y-%m-%d")
date_counts[date_str] += 1
except Exception:
continue
# Process all log files
for root, _, files in os.walk(log_path):
for filename in files:
if filename.lower().endswith(".log"):
file_path = os.path.join(root, filename)
print(f"Processing {file_path}")
parse_log_file(file_path)
# Output results sorted by date
sorted_counts = sorted(date_counts.items())
# Print to console
print("{:<12} {:>6}".format("Date", "Count"))
for date, count in sorted_counts:
print(f"{date:<12} {count:>6}")
# Export to CSV file
with open("PostRequestsPerDay.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Date", "Count"])
writer.writerows(sorted_counts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment