-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhardware_monitor.py
More file actions
135 lines (100 loc) · 3.52 KB
/
hardware_monitor.py
File metadata and controls
135 lines (100 loc) · 3.52 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import psutil
import time
import pandas as pd
import os
import matplotlib.pyplot as plt
import colorama
from colorama import Fore, Style
colorama.init(autoreset=True)
# Clears screen.
def clear_screen():
'''
input:
None.
output:
None, but clears the prompt.
'''
if os.name == 'nt':
os.system('cls') # For Windows.
else:
os.system('clear') # For Linux and Mac.
# Chooses which color to use for display percentage.
def choose_color(device_usage):
'''
input:
device_usage: a float between (inclusive) 0 and 100
output:
None, but clears the prompt.
'''
if device_usage <= 34:
color = Fore.LIGHTCYAN_EX
elif device_usage <= 64:
color = Fore.YELLOW + Style.BRIGHT
else:
color = Fore.LIGHTRED_EX
return color
# Displayer of current usage of CPU and RAM.
def display_usage(cpu_usage, ram_usage, bars=50):
'''
input:
cpu_usage, ram_usage: float between (inclusive) 0 and a 100.
bars: int >= 1.
output:
None, but the function prints out the percent usage of RAM and CPU.
'''
cpu_percent = (cpu_usage / 100)
ram_percent = (ram_usage / 100)
cpu_bar = '█' * int(cpu_percent*bars) + '-'*int(bars - cpu_percent*bars)
ram_bar = '█' * int(ram_percent*bars) + '-'*int(bars - ram_percent*bars)
cpu_color = choose_color(cpu_usage)
ram_color = choose_color(ram_usage)
print(f'\rCPU Usage:{cpu_color} |{cpu_bar}| {cpu_usage:.2f}% ', end='')
print(f'RAM Usage:{ram_color} |{ram_bar}| {ram_usage:.2f}% ', end="\r")
# Creation of DataFrame with its respective empty columns.
columns = ['timestamp', 'CPU', 'RAM']
df = pd.DataFrame(columns=columns)
# Timer before start.
clear_screen()
for i in range(5, 0, -1):
print(f'Hardware Monitor starts in: {i}', end='\r')
time.sleep(1)
# Default interface.
clear_screen()
print('HARDWARE MONITOR\n')
start = time.time()
max_ram = 0
max_cpu = 0
i = 0
while True:
try:
# Storages current usage of CPU and RAM.
cpu_usage = psutil.cpu_percent()
ram_usage = psutil.virtual_memory().percent
# Verifies if current usage of CPU or RAM is the maximum used until the iteration.
if (max_cpu < cpu_usage):
max_cpu = cpu_usage
if (max_ram < ram_usage):
max_ram = ram_usage
# Displays usage of CPU and RAM.
display_usage(cpu_usage, ram_usage, 40)
# Saves the current usage of CPU and RAM into pandas DataFrame.
df.loc[i] = [time.time() - start, ram_usage, cpu_usage]
time.sleep(0.5)
i += 1
except KeyboardInterrupt:
print(f"\n\nCPU peak usage: {max_cpu}%")
ram_total = psutil.virtual_memory().total
print(f"RAM peak usage: {(ram_total*max_ram/100)/10**9:.1f}GB ({max_ram}%)")
# Salvation of usage of CPU and RAM into a .xlsx file when user interrupts the execution (Ctrl + C).
df.to_excel('cpu_ram_data.xlsx', index=False)
# Plots a graph using the data in 'df'.
plt.plot(df['timestamp'], df['CPU'], marker='o', color='b', linestyle='-', label='CPU')
plt.plot(df['timestamp'], df['RAM'], marker='o', color='g', linestyle='-', label='RAM')
plt.ylim(0, 100)
plt.xlabel('Timestamp')
plt.ylabel('Usage (%)')
plt.title('CPU and RAM Usage Over Time')
plt.legend()
plt.savefig('cpu_ram_plot.png')
plt.show()
break