File: //opt/sp_scripts/aptop.py
#!/usr/bin/env python3
# Fetching Apache workers per domain utilizing curl and mod_status
# Based on lstop.py by Tsvetan Gerov
# aptop.py by Nikolay Ilchev
#
import requests
import re
from collections import defaultdict
def main():
response = requests.get(
'http://127.0.0.1/whm-server-status',
headers={'User-Agent': 'A2 User Agent OPS TEAM/4147 Gecko/20190813'}
)
content = response.text
response.raise_for_status()
content = response.text
domain_counts = defaultdict(int)
for match in re.finditer(r'<td nowrap>([^<:]+)(?::\d+)?</td><td nowrap>', content):
vhost = match.group(1).strip()
domain_counts[vhost] += 1
sorted_domains = sorted(domain_counts.items(), key=lambda x: x[1])
print("| {:<70s} | {:>8s} |".format("Domain", "Workers"))
print("|" + "-" * 72 + "|" + "-" * 10 + "|")
for domain, count in sorted_domains:
print("| {:<70} | {:8d} |".format(domain, count))
print("|" + "-" * 72 + "|" + "-" * 10 + "|")
print("| {:<70s} | {:>8s} |".format("Domain", "Workers"))
if __name__ == '__main__':
main()