-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsystem_monitoring.py
111 lines (96 loc) · 2.82 KB
/
system_monitoring.py
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
from dataclasses import dataclass
import time
from typing import (
Any,
)
import psutil
import sqlite3
from paths import full_path_mkdir_p
import settings
@dataclass
class SystemStats:
event_time: float
cpu_utilization_total: float
@classmethod
def build_table(cls, conn):
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS system_stats (
event_time real,
cpu_utilization_total real
);
"""
)
cursor.execute(
"""
CREATE INDEX IF NOT EXISTS system_stats_idx on system_stats (event_time);
"""
)
conn.commit()
def log_stat(self, conn):
cursor = conn.cursor()
cursor.execute(
f"""
INSERT INTO system_stats VALUES (
{self.event_time},
{self.cpu_utilization_total}
)
"""
)
conn.commit()
print("logged:", self.event_time, self.cpu_utilization_total)
@classmethod
def gather_stats(cls):
event_time = time.time()
cpu_utilization_total = sum(psutil.cpu_percent(interval=0.2, percpu=True))
return cls(
event_time=event_time,
cpu_utilization_total=cpu_utilization_total,
)
@dataclass
class SystemMonitor:
db_connection: Any = None
sample_every: float = 1.0 # seconds to sample
def __post_init__(self):
# Create/stash connection
db_path = settings.MONITORING_DB_PATH
full_path_mkdir_p(db_path)
self.db_connection = sqlite3.connect(db_path)
# Ensure tables are created
# - All table/index creates are re-runnable
SystemStats.build_table(self.db_connection)
def query_utilization(
self,
start_time,
end_time,
):
cursor = self.db_connection.cursor()
query = f"""
SELECT
event_time
, cpu_utilization_total
from system_stats
where
event_time >= {start_time}
AND event_time <= {end_time}
"""
num_events = 0
total_utilization = 0
last_time = start_time
for row in cursor.execute(query):
event_time, sum_cpu_util = row
event_time = float(event_time)
num_events += 1
elapsed = float(event_time) - last_time
total_utilization += (elapsed * sum_cpu_util)
last_time = event_time
return (total_utilization, num_events)
def run(self):
while True:
stat_event = SystemStats.gather_stats()
stat_event.log_stat(self.db_connection)
time.sleep(self.sample_every)
if __name__ == "__main__":
m = SystemMonitor()
m.run()