From 7b1aa087d284e7266865178ae578cbd81996009d Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Tue, 6 Jan 2026 14:00:14 -0600 Subject: [PATCH] Add detailed context to SNMP error logs for better diagnostics Enhance SNMP error logging to include: - Target IP address - SNMP version (v1/v2c) - Timeout value in milliseconds - OID being queried Before: [warning] SNMP GET failed for "1.3.6.1.2.1.2.2.1.10.15729368": :timeout After: [warning] SNMP GET failed for 192.168.1.1 (v2c, timeout: 5000ms) OID "1.3.6.1.2.1.2.2.1.10.15729368": :timeout This makes it much easier to diagnose SNMP issues by immediately knowing which device is timing out and what the network configuration is. Updated all SNMP operations: GET, WALK, and GET-BULK. --- lib/towerops/snmp/client.ex | 39 +++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/towerops/snmp/client.ex b/lib/towerops/snmp/client.ex index 014830cb..f26532ea 100644 --- a/lib/towerops/snmp/client.ex +++ b/lib/towerops/snmp/client.ex @@ -45,17 +45,22 @@ defmodule Towerops.Snmp.Client do # Only log warnings for actual communication errors, not missing OIDs case reason do :no_such_object -> - Logger.debug("SNMP object not found: #{inspect(oid)}") + Logger.debug("SNMP object not found for #{target}: #{inspect(oid)}") :no_such_instance -> - Logger.debug("SNMP instance not found: #{inspect(oid)}") + Logger.debug("SNMP instance not found for #{target}: #{inspect(oid)}") :end_of_mib_view -> - Logger.debug("End of MIB view reached: #{inspect(oid)}") + Logger.debug("End of MIB view reached for #{target}: #{inspect(oid)}") _ -> # Actual errors like timeout, network issues, etc. - Logger.warning("SNMP GET failed for #{inspect(oid)}: #{inspect(reason)}") + timeout = Keyword.get(opts, :timeout, @default_timeout) + version = Keyword.fetch!(opts, :version) + + Logger.warning( + "SNMP GET failed for #{target} (v#{version}, timeout: #{timeout}ms) OID #{inspect(oid)}: #{inspect(reason)}" + ) end error @@ -123,16 +128,21 @@ defmodule Towerops.Snmp.Client do # Only log warnings for actual communication errors case reason do :no_such_object -> - Logger.debug("SNMP WALK: object not found at #{inspect(start_oid)}") + Logger.debug("SNMP WALK: object not found for #{target} at #{inspect(start_oid)}") :no_such_instance -> - Logger.debug("SNMP WALK: instance not found at #{inspect(start_oid)}") + Logger.debug("SNMP WALK: instance not found for #{target} at #{inspect(start_oid)}") :end_of_mib_view -> - Logger.debug("SNMP WALK: end of MIB view at #{inspect(start_oid)}") + Logger.debug("SNMP WALK: end of MIB view for #{target} at #{inspect(start_oid)}") _ -> - Logger.warning("SNMP WALK failed for #{inspect(start_oid)}: #{inspect(reason)}") + timeout = Keyword.get(opts, :timeout, @default_timeout) + version = Keyword.fetch!(opts, :version) + + Logger.warning( + "SNMP WALK failed for #{target} (v#{version}, timeout: #{timeout}ms) OID #{inspect(start_oid)}: #{inspect(reason)}" + ) end error @@ -170,16 +180,21 @@ defmodule Towerops.Snmp.Client do # Only log warnings for actual communication errors case reason do :no_such_object -> - Logger.debug("SNMP GET-BULK: object not found at #{inspect(start_oid)}") + Logger.debug("SNMP GET-BULK: object not found for #{target} at #{inspect(start_oid)}") :no_such_instance -> - Logger.debug("SNMP GET-BULK: instance not found at #{inspect(start_oid)}") + Logger.debug("SNMP GET-BULK: instance not found for #{target} at #{inspect(start_oid)}") :end_of_mib_view -> - Logger.debug("SNMP GET-BULK: end of MIB view at #{inspect(start_oid)}") + Logger.debug("SNMP GET-BULK: end of MIB view for #{target} at #{inspect(start_oid)}") _ -> - Logger.warning("SNMP GET-BULK failed for #{inspect(start_oid)}: #{inspect(reason)}") + timeout = Keyword.get(opts, :timeout, @default_timeout) + version = Keyword.fetch!(opts, :version) + + Logger.warning( + "SNMP GET-BULK failed for #{target} (v#{version}, timeout: #{timeout}ms) OID #{inspect(start_oid)}: #{inspect(reason)}" + ) end error