-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetplot.py
216 lines (176 loc) · 7.19 KB
/
netplot.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from flask import Flask, render_template, json, request, url_for
import psutil
import time
from threading import Thread
import os
def bytes2human(n):
# http://code.activestate.com/recipes/578019
# >>> bytes2human(10000)
# '9.8K'
# >>> bytes2human(100001221)
# '95.4M'
symbols = ("K", "M", "G", "T", "P", "E", "Z", "Y")
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
value = float(n) / prefix[s]
return "%.1f%s" % (value, s)
return "%sB" % n
class TFilter:
# переменные для фильтра калмана
varVolt = 0.25 # среднее отклонение (ищем в excel)
varProcess = 0.1 # скорость реакции на изменение (подбирается вручную)
Pc = 0.0
G = 0.0
P = 1.0
Xp = 0.0
Zp = 0.0
Xe = 0.0
def filter(self, val): # функция фильтрации
self.Pc = self.P + self.varProcess
self.G = self.Pc / (self.Pc + self.varVolt)
self.P = (1 - self.G) * self.Pc
self.Xp = self.Xe
self.Zp = self.Xp
self.Xe = self.G * (val - self.Zp) + self.Xp # "фильтрованное" значение
return self.Xe
# класс получающий текущее количество переданных и принятых байт
class TByteCounter:
old_num_bytes = {}
filters = {}
# конструктор
def __init__(self):
interfaces_list = self.get_all_interfaces()
for interface in interfaces_list:
self.old_num_bytes[interface] = {"in": 0, "out": 0}
filterIn = TFilter()
filterOut = TFilter()
self.filters[interface] = {"in": filterIn, "out": filterOut}
# возвращает словарь с значениями переданных
# и принятых байт считая от последнего запроса
def get_num_bytes(self):
iostat = psutil.net_io_counters(pernic=True)
new_num_bytes = {}
for interface in iostat:
current = {"in": iostat[interface][1], "out": iostat[interface][0]}
new_num_bytes[interface] = {
"in": iostat[interface].bytes_recv - self.old_num_bytes[interface]["in"],
"out": iostat[interface].bytes_sent
- self.old_num_bytes[interface]["out"],
}
self.old_num_bytes[interface] = current
return new_num_bytes
# возвращает все интерфейсы системы
def get_all_interfaces(self):
iostat = psutil.net_io_counters(pernic=True)
interfaces_list = []
for key in iostat:
interfaces_list.append(key)
return interfaces_list
# функция для бесконечного потока с опросом скорости
def main_counter(interval=1):
global CURRENT_SPEED_MBit
t0 = 0
byte_counter = TByteCounter()
MBit = 125000 # константа-делитель для перевода байт в мегабиты
while True:
num_bites = byte_counter.get_num_bytes()
interfaces_list = byte_counter.get_all_interfaces()
t1 = time.perf_counter() - t0
for interface in interfaces_list:
in_speed_mbit = round(num_bites[interface]["in"] / t1 / MBit, 3)
out_speed_mbit = round(num_bites[interface]["out"] / t1 / MBit, 3)
CURRENT_SPEED_MBit[interface] = {
"Incoming": in_speed_mbit,
"Outgoing": out_speed_mbit,
}
# print (t1)
# print(CURRENT_SPEED_MBit)
t0 = time.perf_counter() # засекаем время
time.sleep(interval) # засыпаем
# функция для бесконечного потока с опросом дисков
def main_partitions(interval=600):
global CURRENT_PARTITIONS
while True:
array_partitions = []
for part in psutil.disk_partitions(all=False):
if os.name == "nt":
if "cdrom" in part.opts or part.fstype == "":
# skip cd-rom drives with no disk in it; they may raise
# ENOENT, pop-up a Windows GUI error for a non-ready
# partition or just hang.
continue
usage = psutil.disk_usage(part.mountpoint)
array_partitions.append(
{
"partition": part.device + " " + bytes2human(usage.total) + "",
"used": usage.percent,
"free": 100 - usage.percent,
}
)
CURRENT_PARTITIONS["partitions"] = array_partitions
print(CURRENT_PARTITIONS)
time.sleep(interval)
def get_partitions_info():
p = psutil.disk_partitions()
return p
#########################################################################################
# словарь для храения текущей скорости всех интерфейсов
CURRENT_SPEED_MBit = {}
CURRENT_PARTITIONS = {}
# описываем и стартуем поток опроса скорости интерфейсов
speed_thrd = Thread(target=main_counter, daemon=True, args=(0.3,))
speed_thrd.start()
# описываем и стартуем поток опроса дисков
partitions_thrd = Thread(target=main_partitions, daemon=True, args=(6,))
partitions_thrd.start()
app = Flask(__name__)
@app.route("/")
def plot_graph():
interf = request.args.get("interface")
# если активный интерфейс не задан явно,
# то делаем активынм самый загруженный интерфейс
if interf is None:
x = 0
keys = list(CURRENT_SPEED_MBit.keys())
active_interface = keys[0]
for interface in CURRENT_SPEED_MBit.keys():
y = (
CURRENT_SPEED_MBit[interface]["Incoming"]
+ CURRENT_SPEED_MBit[interface]["Outgoing"]
)
print(y)
if y > x:
active_interface = interface
x = y
else:
active_interface = interf
# -----------------------
CURRENT_PARTITIONS["id"] = "part"
return render_template(
"plot.html",
active_interface=active_interface,
interfaces=CURRENT_SPEED_MBit.keys(),
partitions=CURRENT_PARTITIONS,
)
# возвращает JSON со значением текущей скорости заданного интерфейса
@app.route("/get_speed", methods=["GET"])
def get_speed():
interf = request.args.get("i")
CURRENT_SPEED_MBit[interf]["id"] = "speed"
return (
json.dumps(CURRENT_SPEED_MBit[interf]),
200,
{"Content-Type": "application/json"},
)
# возвращает массив с дисками
@app.route("/get_partitions", methods=["GET"])
def get_partition():
CURRENT_PARTITIONS["id"] = "part"
return json.dumps(CURRENT_PARTITIONS), 200, {"Content-Type": "application/json"}
if __name__ == "__main__":
app.run(host="0.0.0.0", port=88)
# from waitress import serve
# serve(app, host="0.0.0.0", port=88)