forked from vityaman-edu/coroed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_vmstat.py
More file actions
79 lines (60 loc) · 2.12 KB
/
visualize_vmstat.py
File metadata and controls
79 lines (60 loc) · 2.12 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
import pandas as pd
import matplotlib.pyplot as plt
import sys
import os
def parse_vmstat(log_file):
with open(log_file, 'r') as f:
lines = f.readlines()
header_idx = None
for i, line in enumerate(lines):
if line.startswith('procs'):
header_idx = i + 1
break
if header_idx is None:
raise ValueError("Не удалось найти заголовок в файле vmstat.log")
headers = lines[header_idx].strip().split()
if len(headers) >= 2 and headers[0] == 'r' and headers[1] == 'b':
headers[0] = 'procs_r'
headers[1] = 'procs_b'
data = []
for line in lines[header_idx+1:]:
if line.strip() == '':
continue
parts = line.strip().split()
if len(parts) != len(headers):
continue
data.append(parts)
df = pd.DataFrame(data, columns=headers)
numeric_cols = headers.copy()
for col in numeric_cols:
df[col] = pd.to_numeric(df[col], errors='coerce')
df['time'] = pd.to_datetime(df.index, unit='s')
return df
def plot_vmstat(df, output_dir):
plt.figure(figsize=(12, 8))
plt.plot(df['time'], df['us'], label='User')
plt.plot(df['time'], df['sy'], label='System')
plt.plot(df['time'], df['id'], label='Idle')
plt.xlabel('Время')
plt.ylabel('Процент')
plt.title('CPU Usage Over Time')
plt.legend()
plt.grid(True)
plt.savefig(os.path.join(output_dir, "cpu_usage.png"))
plt.close()
print(f"График сохранен в директорию {output_dir}")
def main():
if len(sys.argv) != 2:
print("Использование: python visualize_vmstat.py <vmstat_log_file>")
sys.exit(1)
log_file = sys.argv[1]
output_dir = "log"
os.makedirs(output_dir, exist_ok=True)
try:
df = parse_vmstat(log_file)
except ValueError as e:
print(f"Ошибка при парсинге vmstat логов: {e}")
sys.exit(1)
plot_vmstat(df, output_dir)
if __name__ == "__main__":
main()