This repository has been archived by the owner on Nov 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum_process_resources.py
47 lines (42 loc) · 1.91 KB
/
sum_process_resources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# sum_process_resources.py
from collections import OrderedDict
import subprocess
import psutil
def run_cmd(cmd_string):
"""Runs commands and saves output to variable"""
cmd_list = cmd_string.split(" ")
popen_obj = subprocess.Popen(cmd_list, stdout=subprocess.PIPE)
output = popen_obj.stdout.read()
output = output.decode("utf8")
return output
def sum_process_resources(cpu_ram):
"""Sums top X cpu and memory usages grouped by processes"""
ps_memory, ps_cpu, ps_rss = {}, {}, {}
top = 6
output = run_cmd('ps aux').split("\n")
for i, line in enumerate(output):
cleaned_list = " ".join(line.split())
line_list = cleaned_list.split(" ")
if i > 0 and len(line_list) > 10:
cpu = float(line_list[2])
memory = float(line_list[3])
rss = float(line_list[5])
command = line_list[10]
ps_cpu[command] = round(ps_cpu.get(command, 0) + cpu, 2)
ps_memory[command] = round(ps_memory.get(command, 0) + memory, 2)
ps_rss[command] = round(ps_rss.get(command, 0) + rss, 2)
sorted_cpu = OrderedDict(sorted(ps_cpu.items(), key=lambda x: x[1], reverse=True))
sorted_memory = OrderedDict(sorted(ps_memory.items(), key=lambda x: x[1], reverse=True))
sorted_rss = OrderedDict(sorted(ps_rss.items(), key=lambda x: x[1], reverse=True))
printfile = open("cache/proc.txt", "w")
if cpu_ram == "ram":
for i, k in enumerate(sorted_memory.items()):
if i < top:
print("{}. {} | {}".format(i+1, k[0].split("/")[-1], str(round(float(k[1])*100, 1)) + " MB"), file=printfile)
else:
for i, k in enumerate(sorted_cpu.items()):
if i < top:
print("{}. {} | {}".format(i+1, k[0].split("/")[-1], str(round(float(k[1]) / psutil.cpu_count(), 1)) + "%"), file=printfile)
printfile.close()
if __name__ == '__main__':
sum_process_resources()