1
0

snmp_notifier.py 941 B

123456789101112131415161718192021222324
  1. import os
  2. import socket
  3. class SNMPNotifier:
  4. def __init__(self, target_host='192.168.130.170', target_port=162):
  5. self.target_host = target_host
  6. self.target_port = target_port
  7. self.user = 'zabbix_snmp'
  8. self.auth_key = 'authkey123'
  9. self.priv_key = 'privkey123'
  10. def send_alert(self, message):
  11. try:
  12. # Using the standard snmptrap CLI which is more stable than pysnmp v7
  13. hostname = socket.gethostname()
  14. safe_message = str(message).replace("'", "").replace('"', '')
  15. 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}] {safe_message}'"
  16. os.system(cmd)
  17. except Exception as e:
  18. print(f"Failed to send SNMPv3 trap: {e}")
  19. # Singleton instance
  20. notifier = SNMPNotifier()