Ver código fonte

fix: auto-create target tables and sanitize snmp notifications

lanfr144 1 semana atrás
pai
commit
1b9e8b1fab
2 arquivos alterados com 3 adições e 1 exclusões
  1. 1 0
      ingest_csv.py
  2. 2 1
      snmp_notifier.py

+ 1 - 0
ingest_csv.py

@@ -86,6 +86,7 @@ def ingest_file(filename, engine):
                 
                 # INSERT IGNORE into final table
                 with engine.begin() as conn:
+                    conn.execute(text(f"CREATE TABLE IF NOT EXISTS {table_name} LIKE {temp_name}"))
                     cols_str = ", ".join([f"`{c}`" for c in columns])
                     conn.execute(text(f"INSERT IGNORE INTO {table_name} ({cols_str}) SELECT {cols_str} FROM {temp_name}"))
                     conn.execute(text(f"DROP TABLE IF EXISTS {temp_name}"))

+ 2 - 1
snmp_notifier.py

@@ -13,7 +13,8 @@ class SNMPNotifier:
         try:
             # 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}'"
+            safe_message = str(message).replace("'", "").replace('"', '')
+            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}'"
             os.system(cmd)
         except Exception as e:
             print(f"Failed to send SNMPv3 trap: {e}")