-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocesses_diagnostics.py
More file actions
93 lines (77 loc) · 2.78 KB
/
processes_diagnostics.py
File metadata and controls
93 lines (77 loc) · 2.78 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import time
import psutil
import os
from prettytable import PrettyTable
import sys
def Create_Process_List():
proc = []
for p in psutil.process_iter(['pid', 'name']):
try:
p.cpu_percent()
proc.append(p)
except Exception as e:
pass
return proc
def run_process_diagnostics():
try:
while True:
os.system('cls' if os.name == 'nt' else 'clear')
process_cpu_table = PrettyTable(
['PID', 'PNAME', 'STATUS', 'CPU%', 'NUM_THREADS', 'MEMORY(MB)']
)
process_mem_table = PrettyTable(
['PID', 'PNAME', 'STATUS', 'CPU%', 'NUM_THREADS', 'MEMORY(MB)']
)
topcpu = {}
topmem = {}
time.sleep(0.5)
for p in Create_Process_List():
if p.pid != 0:
try:
topcpu[p] = p.cpu_percent() / psutil.cpu_count()
topmem[p] = p.memory_info().rss
except Exception:
continue
top_cpu_list = sorted(topcpu.items(), key=lambda x: x[1], reverse=True)[:10]
top_mem_list = sorted(topmem.items(), key=lambda x: x[1], reverse=True)[:10]
for p, cpu_percent in top_cpu_list:
try:
with p.oneshot():
process_cpu_table.add_row([
str(p.pid),
p.name(),
p.status(),
f"{cpu_percent:.2f}%",
p.num_threads(),
f"{p.memory_info().rss / 1e6:.3f}"
])
except Exception:
pass
for p, mem in top_mem_list:
try:
with p.oneshot():
process_mem_table.add_row([
str(p.pid),
p.name(),
p.status(),
f"{p.cpu_percent() / psutil.cpu_count():.2f}%",
p.num_threads(),
f"{mem / 1e6:.3f}"
])
except Exception:
pass
print("\n" + "="*70)
print("TOP 10 PROCESSES BY CPU UTILIZATION")
print("="*70)
print(process_cpu_table)
print("\n" + "="*70)
print("TOP 10 PROCESSES BY MEMORY USAGE")
print("="*70)
print(process_mem_table)
time.sleep(1)
except KeyboardInterrupt:
print("\n\nStopping process monitor...")
sys.exit(0)
except Exception as e:
print(f"An error occurred: {e}")
sys.exit(1)