4
4
5
5
import asyncio
6
6
import json
7
- import logging
8
7
import socket
9
8
import threading
10
9
from datetime import datetime
@@ -128,6 +127,7 @@ def start_consume(self):
128
127
daemon = True
129
128
)
130
129
self .trap_thread .start ()
130
+ self .logger .info (f"SNMP trap receiver thread started successfully on { self .authentication_config .listen_address } :{ self .authentication_config .port } " )
131
131
132
132
return {"status" : "SNMP trap receiver started" }
133
133
@@ -152,7 +152,7 @@ def _start_trap_receiver(self):
152
152
# Configure community string for SNMP v1 and v2c
153
153
config .addV1System (
154
154
self .snmp_engine ,
155
- 'my-area ' ,
155
+ 'keep-snmp-security-domain ' ,
156
156
self .authentication_config .community
157
157
)
158
158
@@ -406,14 +406,38 @@ def get_alert_schema() -> Dict[str, Any]:
406
406
}
407
407
408
408
def dispose (self ):
409
- """Clean up resources."""
410
- if self .running :
411
- self .logger .info ("Stopping SNMP trap receiver" )
412
- self .running = False
409
+ """Clean up resources and release all ports used by the SNMP trap receiver."""
410
+ if not self .running :
411
+ return
413
412
414
- if self .snmp_engine :
415
- self .snmp_engine .transportDispatcher .jobFinished (1 )
413
+ self .logger .info ("Stopping SNMP trap receiver" )
414
+ self .running = False
415
+
416
+ if self .snmp_engine :
417
+ try :
418
+ transport_dispatcher = self .snmp_engine .transportDispatcher
419
+
420
+ transport_dispatcher .jobFinished (1 )
421
+
422
+ transport_dispatcher .closeDispatcher ()
423
+
424
+ self .logger .info (f"SNMP engine transport dispatcher stopped, port { self .authentication_config .port } released" )
425
+
426
+ except Exception as e :
427
+ self .logger .error (f"Error during SNMP engine cleanup: { e } " )
428
+ finally :
416
429
self .snmp_engine = None
430
+
431
+
432
+ if self .trap_thread and self .trap_thread .is_alive ():
433
+ try :
434
+ self .trap_thread .join (timeout = 5.0 )
435
+ if self .trap_thread .is_alive ():
436
+ self .logger .warning ("SNMP trap thread did not stop gracefully within timeout" )
437
+ except Exception as e :
438
+ self .logger .error (f"Error joining SNMP trap thread: { e } " )
439
+ finally :
440
+ self .trap_thread = None
417
441
418
442
@property
419
443
def is_consumer (self ) -> bool :
@@ -422,4 +446,4 @@ def is_consumer(self) -> bool:
422
446
423
447
def status (self ) -> bool :
424
448
"""Check if the SNMP trap receiver is running."""
425
- return self .running
449
+ return self .running
0 commit comments