Spaces:
No application file
No application file
Upload system_stats.py
Browse files- system_stats.py +34 -0
system_stats.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import psutil
|
4 |
+
import pandas as pd
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
def get_stats(file_path):
|
8 |
+
if os.path.exists(file_path):
|
9 |
+
data = pd.read_csv(file_path)
|
10 |
+
else:
|
11 |
+
columns = ['Timestamp', 'MemoryUsed(%)', 'CPU_Used(%)']
|
12 |
+
data = pd.DataFrame(columns=columns)
|
13 |
+
while True:
|
14 |
+
current_time = time.strftime("%d_%m_%y_%H_%M_%S")
|
15 |
+
|
16 |
+
# Memory stats
|
17 |
+
memory_stats = psutil.virtual_memory()
|
18 |
+
memory_used = memory_stats.percent
|
19 |
+
|
20 |
+
# CPU stats
|
21 |
+
#cpu_stats = psutil.cpu_percent(interval=1, percpu=True)
|
22 |
+
cpu_stats = np.mean(psutil.cpu_percent(interval=1, percpu=True))
|
23 |
+
# Create a new row with the timestamp, memory stats, and CPU stats
|
24 |
+
new_row = {'Timestamp': current_time, 'Memory Stats': memory_used, 'CPU Stats': cpu_stats}
|
25 |
+
|
26 |
+
# Append the row to the DataFrame
|
27 |
+
data = data.append(new_row, ignore_index=True)
|
28 |
+
|
29 |
+
# Sleep for 60 seconds
|
30 |
+
time.sleep(5)
|
31 |
+
|
32 |
+
# Save the DataFrame to a CSV file
|
33 |
+
data.to_csv(file_path, index=False)
|
34 |
+
|