#!/usr/bin/env python3
# lsTop: LiteSpeed script to display top domains by requests/s.
# Tsvetan Gerov <tsvetan@worldhost.group>
# v0.1

import re
import glob

domain_info = {}
directory = '/tmp/lshttpd/'
file_pattern = '.rtreport*'
files = glob.glob(directory + file_pattern)

for filename in files:
    with open(filename, 'r') as file:
        content = file.read()
        matches = re.findall(r'REQ_RATE \[APVH_(.*?)\]: .* REQ_PER_SEC: ([0-9.]+)', content)
        for domain, requests_per_sec in matches:
            modified_domain = domain.split(':')[0]
            modified_domain = modified_domain.replace("APVH_", "")
            if modified_domain in domain_info:
                domain_info[modified_domain] += int(float(requests_per_sec))
            else:
                domain_info[modified_domain] = int(float(requests_per_sec))

sorted_domains = sorted(domain_info.items(), key=lambda x: x[1])

print("| {:<70s} | {:<8s} |".format("Domain", "Req/s"))
print("|" + "-" * 72 + "|" + "-" * 10 + "|")

for domain, requests_per_sec in sorted_domains:
    print(f"| {domain:<70} | {requests_per_sec:8d} |")

print("|" + "-" * 72 + "|" + "-" * 10 + "|")
print("| {:<70s} | {:<8s} |".format("Domain", "Req/s"))

