snmp_notifier.py 862 B

1234567891011121314151617181920212223
  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. 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}'"
  15. os.system(cmd)
  16. except Exception as e:
  17. print(f"Failed to send SNMPv3 trap: {e}")
  18. # Singleton instance
  19. notifier = SNMPNotifier()