fix String.replace crash when YAML index is numeric

YAML parses bare numbers like `index: 0` as integers. coerce
index_template to string before calling String.replace in both
build_table_sensor and build_state_sensor.
This commit is contained in:
Graham McIntire 2026-02-11 13:27:46 -06:00
parent 9e36d5030a
commit 4e290b1938
No known key found for this signature in database
2 changed files with 41 additions and 2 deletions

View file

@ -324,7 +324,8 @@ defmodule Towerops.Snmp.Profiles.Dynamic do
template ->
# Replace {{ $index }} with the actual OID index
String.replace(template, "{{ $index }}", oid_index)
# Coerce to string since YAML parses bare numbers (e.g. `index: 0`) as integers
template |> to_string() |> String.replace("{{ $index }}", oid_index)
end
%{
@ -387,7 +388,8 @@ defmodule Towerops.Snmp.Profiles.Dynamic do
template ->
# Replace {{ $index }} with the actual OID index
String.replace(template, "{{ $index }}", oid_index)
# Coerce to string since YAML parses bare numbers (e.g. `index: 0`) as integers
template |> to_string() |> String.replace("{{ $index }}", oid_index)
end
%{

View file

@ -1110,5 +1110,42 @@ defmodule Towerops.Snmp.Profiles.DynamicTest do
indices = count_sensors |> Enum.map(& &1.sensor_index) |> Enum.sort()
assert indices == ["count_1", "count_2"]
end
test "handles numeric index_template from YAML without crashing" do
# YAML parses bare numbers like `index: 0` as integers, not strings.
# This must not crash String.replace/4.
profile =
base_profile(%{
count_sensor_oids: [
%{
base_oid: "1.3.6.1.4.1.99999.1.1",
sensor_type: "count",
sensor_descr: "Memory Usage",
sensor_unit: "",
sensor_divisor: 1,
descr_oid: nil,
index_template: 0
}
]
})
stub(SnmpMock, :get, fn _, _, _ -> {:error, :no_such_object} end)
stub(SnmpMock, :walk, fn _, oid, _ ->
case oid do
"1.3.6.1.4.1.99999.1.1" ->
{:ok, [%{oid: "1.3.6.1.4.1.99999.1.1.1", value: {:integer, 42}}]}
_ ->
{:ok, []}
end
end)
assert {:ok, sensors} = Dynamic.discover_sensors(profile, @client_opts)
count_sensors = Enum.filter(sensors, &(&1.sensor_type == "count"))
assert length(count_sensors) == 1
assert hd(count_sensors).sensor_index == "0"
end
end
end