-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_tcp.py
More file actions
358 lines (283 loc) · 13.6 KB
/
monitor_tcp.py
File metadata and controls
358 lines (283 loc) · 13.6 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
monitor_tcp.py - Monitor de parâmetros TCP simplificado
"""
import time
import socket
import threading
import json
from collections import deque
from datetime import datetime
class MonitorTCP:
def __init__(self, nome_componente="Monitor"):
self.nome_componente = nome_componente
# Contadores básicos
self.bytes_enviados = 0
self.bytes_recebidos = 0
self.mensagens_enviadas = 0
self.mensagens_recebidas = 0
self.mensagens_erro = 0
self.conexoes_estabelecidas = 0
# Timestamps
self.inicio_monitor = time.time()
self.inicio_conexao_atual = None
self.ultima_atividade = time.time()
# Histórico para throughput
self.historico_bytes_enviados = deque(maxlen=60)
self.historico_bytes_recebidos = deque(maxlen=60)
self.historico_timestamps = deque(maxlen=60)
# Latência
self.latencias = deque(maxlen=100)
self.pendentes_ping = {}
# Estado da conexão
self.socket_ativo = None
self.endereco_remoto = None
self.porta_local = None
self.porta_remota = None
self.estado_conexao = "Desligado"
# Thread para coleta contínua
self.a_monitorizar = True
self.thread_monitor = threading.Thread(target=self.coleta_continua, daemon=True)
self.thread_monitor.start()
print(f"Monitor TCP inicializado para {nome_componente}")
def definir_socket_ativo(self, socket_tcp, endereco_remoto=None):
self.socket_ativo = socket_tcp
self.endereco_remoto = endereco_remoto
self.inicio_conexao_atual = time.time()
self.estado_conexao = "Conectado"
self.conexoes_estabelecidas += 1
if socket_tcp:
try:
endereco_local = socket_tcp.getsockname()
self.porta_local = endereco_local[1]
if endereco_remoto:
self.porta_remota = endereco_remoto[1] if isinstance(endereco_remoto, tuple) else None
print(f"Monitor TCP: Conexão estabelecida {endereco_local} <-> {endereco_remoto}")
except Exception as e:
print(f"Erro ao obter informações do socket: {e}")
def conexao_perdida(self):
self.socket_ativo = None
self.estado_conexao = "Desligado"
self.endereco_remoto = None
print(f"Monitor TCP: Conexão perdida")
def registrar_envio(self, tamanho_bytes, sucesso=True):
if sucesso:
self.bytes_enviados += tamanho_bytes
self.mensagens_enviadas += 1
else:
self.mensagens_erro += 1
self.ultima_atividade = time.time()
def registrar_recepcao(self, tamanho_bytes, sucesso=True):
if sucesso:
self.bytes_recebidos += tamanho_bytes
self.mensagens_recebidas += 1
else:
self.mensagens_erro += 1
self.ultima_atividade = time.time()
def iniciar_medicao_latencia(self, id_ping):
self.pendentes_ping[id_ping] = time.time()
def finalizar_medicao_latencia(self, id_ping):
if id_ping in self.pendentes_ping:
inicio = self.pendentes_ping.pop(id_ping)
latencia_ms = (time.time() - inicio) * 1000
self.latencias.append(latencia_ms)
return latencia_ms
return None
def obter_throughput(self):
agora = time.time()
# Remove entradas antigas
while (self.historico_timestamps and
agora - self.historico_timestamps[0] > 60):
self.historico_timestamps.popleft()
if self.historico_bytes_enviados:
self.historico_bytes_enviados.popleft()
if self.historico_bytes_recebidos:
self.historico_bytes_recebidos.popleft()
if len(self.historico_timestamps) < 2:
return 0.0, 0.0
tempo_decorrido = self.historico_timestamps[-1] - self.historico_timestamps[0]
if tempo_decorrido <= 0:
return 0.0, 0.0
bytes_enviados_periodo = sum(self.historico_bytes_enviados)
bytes_recebidos_periodo = sum(self.historico_bytes_recebidos)
throughput_envio = bytes_enviados_periodo / tempo_decorrido
throughput_recepcao = bytes_recebidos_periodo / tempo_decorrido
return throughput_envio, throughput_recepcao
def obter_latencia_media(self):
if not self.latencias:
return 0.0
return sum(self.latencias) / len(self.latencias)
def obter_parametros_socket(self):
parametros = {
'window_size': None,
'buffer_size_envio': None,
'buffer_size_recepcao': None,
'keepalive': None,
'nodelay': None,
'timeout': None
}
if not self.socket_ativo:
return parametros
try:
parametros['buffer_size_recepcao'] = self.socket_ativo.getsockopt(
socket.SOL_SOCKET, socket.SO_RCVBUF
)
parametros['buffer_size_envio'] = self.socket_ativo.getsockopt(
socket.SOL_SOCKET, socket.SO_SNDBUF
)
parametros['keepalive'] = bool(self.socket_ativo.getsockopt(
socket.SOL_SOCKET, socket.SO_KEEPALIVE
))
parametros['nodelay'] = bool(self.socket_ativo.getsockopt(
socket.IPPROTO_TCP, socket.TCP_NODELAY
))
parametros['timeout'] = self.socket_ativo.gettimeout()
parametros['window_size'] = parametros['buffer_size_recepcao']
except Exception as e:
print(f"Erro ao obter parâmetros do socket: {e}")
return parametros
def obter_estatisticas_conexao(self):
agora = time.time()
tempo_ativo = agora - self.inicio_monitor
tempo_conexao = agora - self.inicio_conexao_atual if self.inicio_conexao_atual else 0
throughput_envio, throughput_recepcao = self.obter_throughput()
latencia_media = self.obter_latencia_media()
parametros_socket = self.obter_parametros_socket()
total_mensagens = self.mensagens_enviadas + self.mensagens_recebidas
taxa_sucesso = 0.0
if total_mensagens > 0:
taxa_sucesso = ((total_mensagens - self.mensagens_erro) / total_mensagens) * 100
estatisticas = {
'componente': self.nome_componente,
'estado_ligacao': self.estado_conexao,
'tempo_ativo_total': tempo_ativo,
'tempo_conexao_atual': tempo_conexao,
'endereco_local': f"localhost:{self.porta_local}" if self.porta_local else "N/A",
'endereco_remoto': f"{self.endereco_remoto[0]}:{self.endereco_remoto[1]}" if self.endereco_remoto else "N/A",
'bytes_enviados': self.bytes_enviados,
'bytes_recebidos': self.bytes_recebidos,
'mensagens_enviadas': self.mensagens_enviadas,
'mensagens_recebidas': self.mensagens_recebidas,
'mensagens_erro': self.mensagens_erro,
'conexoes_estabelecidas': self.conexoes_estabelecidas,
'throughput_envio_bps': throughput_envio,
'throughput_recepcao_bps': throughput_recepcao,
'latencia_media_ms': latencia_media,
'taxa_sucesso_percent': taxa_sucesso,
'parametros_tcp': parametros_socket,
'ultima_atividade': self.ultima_atividade,
'inicio_monitor': self.inicio_monitor
}
return estatisticas
def coleta_continua(self):
ultimo_bytes_enviados = 0
ultimo_bytes_recebidos = 0
while self.a_monitorizar:
try:
agora = time.time()
bytes_enviados_delta = self.bytes_enviados - ultimo_bytes_enviados
bytes_recebidos_delta = self.bytes_recebidos - ultimo_bytes_recebidos
self.historico_timestamps.append(agora)
self.historico_bytes_enviados.append(bytes_enviados_delta)
self.historico_bytes_recebidos.append(bytes_recebidos_delta)
ultimo_bytes_enviados = self.bytes_enviados
ultimo_bytes_recebidos = self.bytes_recebidos
if self.socket_ativo:
try:
self.socket_ativo.setblocking(False)
try:
self.socket_ativo.recv(0)
except socket.error:
pass
finally:
self.socket_ativo.setblocking(True)
except:
self.conexao_perdida()
time.sleep(1)
except Exception as e:
print(f"Erro na coleta contínua: {e}")
time.sleep(1)
def formatar_bytes(self, bytes_valor):
if bytes_valor < 1024:
return f"{bytes_valor} B"
elif bytes_valor < 1024 * 1024:
return f"{bytes_valor / 1024:.1f} KB"
elif bytes_valor < 1024 * 1024 * 1024:
return f"{bytes_valor / (1024 * 1024):.1f} MB"
else:
return f"{bytes_valor / (1024 * 1024 * 1024):.1f} GB"
def formatar_tempo(self, segundos):
if segundos < 60:
return f"{segundos:.0f}s"
elif segundos < 3600:
minutos = int(segundos // 60)
segundos_rest = int(segundos % 60)
return f"{minutos:02d}:{segundos_rest:02d}"
else:
horas = int(segundos // 3600)
minutos = int((segundos % 3600) // 60)
segundos_rest = int(segundos % 60)
return f"{horas:02d}:{minutos:02d}:{segundos_rest:02d}"
def gerar_relatorio_detalhado(self):
stats = self.obter_estatisticas_conexao()
relatorio = []
relatorio.append("=" * 60)
relatorio.append(f"PARÂMETROS TCP - {stats['componente'].upper()}")
relatorio.append("=" * 60)
relatorio.append("INFORMAÇÕES DA CONEXÃO:")
relatorio.append(f" Estado: {stats['estado_ligacao']}")
relatorio.append(f" Endereço local: {stats['endereco_local']}")
relatorio.append(f" Endereço remoto: {stats['endereco_remoto']}")
relatorio.append(f" Tempo ativo: {self.formatar_tempo(stats['tempo_ativo_total'])}")
if stats['tempo_conexao_atual'] > 0:
relatorio.append(f" Conexão atual: {self.formatar_tempo(stats['tempo_conexao_atual'])}")
relatorio.append("")
relatorio.append("ESTATÍSTICAS DE TRÁFEGO:")
relatorio.append(f" Bytes enviados: {self.formatar_bytes(stats['bytes_enviados'])}")
relatorio.append(f" Bytes recebidos: {self.formatar_bytes(stats['bytes_recebidos'])}")
relatorio.append(f" Mensagens enviadas: {stats['mensagens_enviadas']}")
relatorio.append(f" Mensagens recebidas: {stats['mensagens_recebidas']}")
relatorio.append(f" Mensagens com erro: {stats['mensagens_erro']}")
relatorio.append(f" Conexões estabelecidas: {stats['conexoes_estabelecidas']}")
relatorio.append("")
relatorio.append("MÉTRICAS DE PERFORMANCE:")
relatorio.append(f" Throughput envio: {self.formatar_bytes(stats['throughput_envio_bps'])}/s")
relatorio.append(f" Throughput recepção: {self.formatar_bytes(stats['throughput_recepcao_bps'])}/s")
relatorio.append(f" Latência média: {stats['latencia_media_ms']:.1f} ms")
relatorio.append(f" Taxa de sucesso: {stats['taxa_sucesso_percent']:.1f}%")
relatorio.append("")
relatorio.append("PARÂMETROS TCP:")
tcp_params = stats['parametros_tcp']
if tcp_params['window_size']:
relatorio.append(f" Window Size: {self.formatar_bytes(tcp_params['window_size'])}")
if tcp_params['buffer_size_envio']:
relatorio.append(f" Buffer envio: {self.formatar_bytes(tcp_params['buffer_size_envio'])}")
if tcp_params['buffer_size_recepcao']:
relatorio.append(f" Buffer recepção: {self.formatar_bytes(tcp_params['buffer_size_recepcao'])}")
if tcp_params['keepalive'] is not None:
relatorio.append(f" Keep Alive: {'Ativo' if tcp_params['keepalive'] else 'Inativo'}")
if tcp_params['nodelay'] is not None:
relatorio.append(f" TCP No Delay: {'Ativo' if tcp_params['nodelay'] else 'Inativo'}")
if tcp_params['timeout']:
relatorio.append(f" Timeout: {tcp_params['timeout']:.1f}s")
relatorio.append("=" * 60)
return "\n".join(relatorio)
def parar(self):
self.a_monitorizar = False
print(f"Monitor TCP parado para {self.nome_componente}")
def testar_monitor():
print("Testando Monitor TCP...")
monitor = MonitorTCP("Teste")
monitor.registrar_envio(100)
monitor.registrar_recepcao(80)
monitor.registrar_envio(200)
monitor.registrar_recepcao(150)
monitor.iniciar_medicao_latencia("ping1")
time.sleep(0.001)
monitor.finalizar_medicao_latencia("ping1")
time.sleep(2)
print(monitor.gerar_relatorio_detalhado())
monitor.parar()
if __name__ == "__main__":
testar_monitor()