Преглед на файлове

Fix snmp_notifier to use snmptrap cli

lanfr144 преди 2 седмици
родител
ревизия
c7eda9a94d
променени са 1 файла, в които са добавени 6 реда и са изтрити 20 реда
  1. 6 20
      snmp_notifier.py

+ 6 - 20
snmp_notifier.py

@@ -1,6 +1,5 @@
-import time
+import os
 import socket
-from pysnmp.hlapi import *
 
 class SNMPNotifier:
     def __init__(self, target_host='192.168.130.170', target_port=162):
@@ -12,26 +11,13 @@ class SNMPNotifier:
 
     def send_alert(self, message):
         try:
-            errorIndication, errorStatus, errorIndex, varBinds = next(
-                sendNotification(
-                    SnmpEngine(),
-                    UsmUserData(self.user, self.auth_key, self.priv_key,
-                                authProtocol=usmHMACSHAAuthProtocol,
-                                privProtocol=usmAesCfb128Protocol),
-                    UdpTransportTarget((self.target_host, self.target_port)),
-                    ContextData(),
-                    'trap',
-                    NotificationType(
-                        ObjectIdentity('1.3.6.1.4.1.8072.3.2.10') # SNMP trap OID
-                    ).addVarBinds(
-                        ('1.3.6.1.2.1.1.1.0', OctetString(f"[{socket.gethostname()}] {message}"))
-                    )
-                )
-            )
-            if errorIndication:
-                print(f"SNMP Trap Failed: {errorIndication}")
+            # Using the standard snmptrap CLI which is more stable than pysnmp v7
+            hostname = socket.gethostname()
+            cmd = f"snmptrap -v3 -l authPriv -u {self.user} -a SHA -A {self.auth_key} -x AES -X {self.priv_key} {self.target_host}:{self.target_port} '' 1.3.6.1.4.1.8072.3.2.10 1.3.6.1.2.1.1.1.0 s '[{hostname}] {message}'"
+            os.system(cmd)
         except Exception as e:
             print(f"Failed to send SNMPv3 trap: {e}")
 
 # Singleton instance
 notifier = SNMPNotifier()
+