-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
51 lines (41 loc) · 1.54 KB
/
monitor.py
File metadata and controls
51 lines (41 loc) · 1.54 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
import psutil
import time
import os
def clear_screen():
os.system('clear' if os.name == 'posix' else 'cls')
def get_cpu_usage():
return psutil.cpu_percent(interval=1)
def get_memory_usage():
mem = psutil.virtual_memory()
return mem.used / (1024 ** 2), mem.total / (1024 ** 2), mem.percent
def get_disk_usage():
disk = psutil.disk_usage('/')
return disk.used / (1024 ** 3), disk.total / (1024 ** 3), disk.percent
def get_network_usage():
net1 = psutil.net_io_counters()
time.sleep(1)
net2 = psutil.net_io_counters()
upload_speed = (net2.bytes_sent - net1.bytes_sent) / 1024
download_speed = (net2.bytes_recv - net1.bytes_recv) / 1024
return upload_speed, download_speed
def main():
while True:
clear_screen()
print("="*40)
print(" Linux System Resource Monitor")
print("="*40)
cpu = get_cpu_usage()
used_mem, total_mem, mem_percent = get_memory_usage()
used_disk, total_disk, disk_percent = get_disk_usage()
upload, download = get_network_usage()
print(f"CPU Usage: {cpu:.2f}%")
print(f"Memory Usage: {used_mem:.2f}MB / {total_mem:.2f}MB ({mem_percent:.2f}%)")
print(f"Disk Usage: {used_disk:.2f}GB / {total_disk:.2f}GB ({disk_percent:.2f}%)")
print(f"Network Speed: ↑ {upload:.2f} KB/s ↓ {download:.2f} KB/s")
print("\nPress Ctrl+C to exit.")
time.sleep(1)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nExiting monitor...")