This commit is contained in:
Graham McIntire 2026-01-04 13:35:54 -06:00
parent 40a329ea78
commit 1253230639
No known key found for this signature in database

View file

@ -86,14 +86,19 @@ defmodule Towerops.Snmp.PollerWorker do
poll_interval = get_poll_interval(equipment)
grace_period = 5
if !should_skip_poll?(equipment, poll_interval, grace_period) do
if should_skip_poll?(equipment, poll_interval, grace_period) do
Logger.debug("Skipping poll for #{equipment.name} - recently polled by another process")
# Update poll timestamp before polling (optimistic locking)
# Poll all sensors
# Poll all interfaces
else
Equipment.update_snmp_poll_time(equipment)
client_opts = build_client_opts(equipment)
now = DateTime.truncate(DateTime.utc_now(), :second)
# Poll all sensors
try do
poll_sensors(device.sensors, client_opts, now)
Logger.debug("Polled #{length(device.sensors)} sensors for #{equipment.name}")
@ -104,7 +109,6 @@ defmodule Towerops.Snmp.PollerWorker do
)
end
# Poll all interfaces
try do
poll_interfaces(device.interfaces, client_opts, now)
Logger.debug("Polled #{length(device.interfaces)} interfaces for #{equipment.name}")
@ -114,10 +118,6 @@ defmodule Towerops.Snmp.PollerWorker do
"Error polling interfaces for #{equipment.name}: #{inspect(error)}\n#{Exception.format_stacktrace()}"
)
end
else
Logger.debug(
"Skipping poll for #{equipment.name} - recently polled by another process"
)
end
# Schedule next poll regardless of whether we polled or skipped
@ -244,44 +244,40 @@ defmodule Towerops.Snmp.PollerWorker do
# Decode SNMP values, handling Counter64 and other binary types
defp decode_snmp_value(value) when is_number(value), do: value
# Try to decode based on byte size
defp decode_snmp_value(value) when is_binary(value) do
# Try to decode based on byte size
try do
case byte_size(value) do
8 ->
# Standard Counter64 (8 bytes, big-endian unsigned integer)
<<counter::unsigned-big-integer-size(64)>> = value
counter
case byte_size(value) do
8 ->
# Standard Counter64 (8 bytes, big-endian unsigned integer)
<<counter::unsigned-big-integer-size(64)>> = value
counter
16 ->
# Some SNMP implementations return 16-byte values
# Try reading last 8 bytes as Counter64
<<_prefix::binary-size(8), counter::unsigned-big-integer-size(64)>> = value
counter
16 ->
# Some SNMP implementations return 16-byte values
# Try reading last 8 bytes as Counter64
<<_prefix::binary-size(8), counter::unsigned-big-integer-size(64)>> = value
counter
size when size > 8 ->
# Large binary, try reading last 8 bytes
offset = size - 8
<<_prefix::binary-size(offset), counter::unsigned-big-integer-size(64)>> = value
Logger.debug("Decoded #{size}-byte SNMP value as Counter64 from last 8 bytes: #{counter}")
counter
size when size > 8 ->
# Large binary, try reading last 8 bytes
offset = size - 8
<<_prefix::binary-size(offset), counter::unsigned-big-integer-size(64)>> = value
Logger.debug("Decoded #{size}-byte SNMP value as Counter64 from last 8 bytes: #{counter}")
counter
_ ->
# Unknown binary format or too small
Logger.warning(
"Unknown SNMP binary value format, size: #{byte_size(value)}, bytes: #{inspect(value)}"
)
nil
end
rescue
error ->
Logger.error(
"Failed to decode SNMP binary value (size: #{byte_size(value)}): #{inspect(error)}, bytes: #{inspect(value)}"
)
_ ->
# Unknown binary format or too small
Logger.warning("Unknown SNMP binary value format, size: #{byte_size(value)}, bytes: #{inspect(value)}")
nil
end
rescue
error ->
Logger.error(
"Failed to decode SNMP binary value (size: #{byte_size(value)}): #{inspect(error)}, bytes: #{inspect(value)}"
)
nil
end
defp decode_snmp_value(_), do: nil