-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitors_base.py
92 lines (74 loc) · 2.61 KB
/
monitors_base.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
from monitor import Monitor
import psutil
from time import sleep
class MonitorCPU(Monitor):
def __init__(self):
super().__init__()
self.name = "cpu"
self.type = "point"
def get_point(self):
while True:
result = []
for cpu in psutil.cpu_times_percent(interval=60, percpu=True):
result.append(cpu.__dict__)
yield result
class MonitorMem(Monitor):
def __init__(self):
super().__init__()
self.name = "mem"
self.type = "point"
def get_point(self):
while True:
sleep(60)
yield {"mem": psutil.virtual_memory().__dict__, "swap": psutil.swap_memory().__dict__}
class MonitorLoad(Monitor):
def __init__(self):
super().__init__()
self.name = "load"
self.type = "point"
def get_point(self):
while True:
sleep(60)
with open("/proc/loadavg") as loadavg:
load = loadavg.read().split(" ")[:3]
yield load
class MonitorNetwork(Monitor):
def __init__(self):
super().__init__()
self.name = "net"
self.type = "point"
def get_point(self):
counters_old = psutil.net_io_counters().__dict__
while True:
sleep(60)
conn_counters = {
"ESTABLISHED": 0,
"SYN_SENT": 0,
"SYN_RECV": 0,
"CLOSING": 0,
"LISTEN": 0,
"TIME_WAIT": 0,
"LAST_ACK": 0,
"FIN_WAIT2": 0,
"FIN_WAIT1": 0,
"CLOSE_WAIT": 0,
"NONE": 0,
}
for connection in psutil.net_connections():
conn_counters[connection.status] += 1
conn_semantic = {
"connected": conn_counters["ESTABLISHED"],
"connecting": conn_counters["SYN_SENT"] + conn_counters["SYN_RECV"],
"closing": conn_counters["CLOSING"] + conn_counters["LAST_ACK"] + conn_counters["FIN_WAIT1"] +
conn_counters["FIN_WAIT2"] + conn_counters["CLOSE_WAIT"] + conn_counters["TIME_WAIT"],
"listening": conn_counters["LISTEN"],
"unknown": conn_counters["NONE"]
}
counters = psutil.net_io_counters().__dict__
counters_delta = {key: counters[key] - counters_old.get(key, 0) for key in counters.keys()}
counters_old = counters
result = {
"counters": counters_delta,
"sockets": conn_semantic
}
yield result