Skip to content

Instantly share code, notes, and snippets.

@meeuw
Created January 30, 2019 08:38
Show Gist options
  • Save meeuw/abf53a763e10efe2d2a9f9a6648a73e5 to your computer and use it in GitHub Desktop.
Save meeuw/abf53a763e10efe2d2a9f9a6648a73e5 to your computer and use it in GitHub Desktop.
combine cpu load with output from mod_status to create a apache like top
#!/usr/bin/env python
import psutil
import time
import requests
import lxml.html
print('\033c')
while 1:
print('\033[H')
r = requests.get('http://localhost/server-status')
root = lxml.html.fromstring(r.text)
headers = []
data = []
for tr in root.xpath('//table')[0]:
if not headers:
for td in tr:
headers.append(td.text)
continue
d = []
for td in tr:
d.append(td.text)
data.append(dict(zip(headers, d)))
procs = {}
for proc in psutil.process_iter():
if proc.username() == "apache":
procs[proc.pid] = {'cpu_percent': proc.cpu_percent()}
for d in data:
if d["PID"].isdigit():
pid = int(d["PID"])
if pid in procs:
procs[pid]['Request'] = d["Request"]
procs[pid]['VHost'] = d["VHost"]
i = 0
for pid, proc in sorted(procs.items(), key=lambda p: p[1]["cpu_percent"], reverse=True):
if 'Request' in proc:
print("{:7} {cpu_percent:7} {VHost:40} {Request}\033[K".format(pid, **proc))
#if (i > 5): break
i+=1
print("\033[J")
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment