SNMP discovery updates continued

This commit is contained in:
Keith Smith
2026-05-26 17:43:33 -06:00
parent e59733d331
commit 6ff452a8a9
10 changed files with 554 additions and 36 deletions
+62 -3
View File
@@ -58,6 +58,10 @@ ENT_PHY_SENSOR_SCALE = (1, 3, 6, 1, 2, 1, 99, 1, 1, 1, 2)
ENT_PHY_SENSOR_PRECISION = (1, 3, 6, 1, 2, 1, 99, 1, 1, 1, 3)
ENT_PHY_SENSOR_VALUE = (1, 3, 6, 1, 2, 1, 99, 1, 1, 1, 4)
ENT_PHY_SENSOR_OPER_STATUS = (1, 3, 6, 1, 2, 1, 99, 1, 1, 1, 5)
UCD_LA_LOAD_INT = (1, 3, 6, 1, 4, 1, 2021, 10, 1, 5)
UCD_MEM_TOTAL_REAL = (1, 3, 6, 1, 4, 1, 2021, 4, 5, 0)
UCD_MEM_AVAIL_REAL = (1, 3, 6, 1, 4, 1, 2021, 4, 6, 0)
UCD_DSK_PERCENT = (1, 3, 6, 1, 4, 1, 2021, 9, 1, 9)
STATUS_LABELS = {
1: "up",
@@ -128,6 +132,61 @@ def _run_snmp_check_sync(config: SnmpCheckConfig) -> SnmpCheckResult:
metrics=[SnmpMetricValue(name="load_percent", value=float(value), unit="%")],
)
if config.item_type == "linux_load_average":
load_index = _item_index(config.item_id, "linux", position=2)
if load_index is None:
return SnmpCheckResult(status="down", response_time_ms=0, message="SNMP Linux load item was not valid")
oid = _with_index(UCD_LA_LOAD_INT, load_index)
value = _int_value(client.get_many([oid]).get(oid))
response_time_ms = int((perf_counter() - started) * 1000)
if value is None:
return SnmpCheckResult(status="down", response_time_ms=response_time_ms, message="Linux load average was not reported")
load_average = value / 100
label = config.label or "Load average"
return SnmpCheckResult(
status="up",
response_time_ms=response_time_ms,
message=f"{label} is {load_average:.2f}",
metrics=[SnmpMetricValue(name="load_average", value=load_average)],
)
if config.item_type == "linux_memory_usage":
values = client.get_many([UCD_MEM_TOTAL_REAL, UCD_MEM_AVAIL_REAL])
response_time_ms = int((perf_counter() - started) * 1000)
total_kb = _int_value(values.get(UCD_MEM_TOTAL_REAL))
available_kb = _int_value(values.get(UCD_MEM_AVAIL_REAL))
if not total_kb or available_kb is None:
return SnmpCheckResult(status="down", response_time_ms=response_time_ms, message="Linux memory was not reported")
used_kb = total_kb - available_kb
used_percent = (used_kb / total_kb) * 100
return SnmpCheckResult(
status="up",
response_time_ms=response_time_ms,
message=f"Memory is {used_percent:.1f}% used",
metrics=[
SnmpMetricValue(name="used_percent", value=used_percent, unit="%"),
SnmpMetricValue(name="used_bytes", value=float(used_kb * 1024), unit="bytes"),
SnmpMetricValue(name="total_bytes", value=float(total_kb * 1024), unit="bytes"),
],
)
if config.item_type == "linux_disk_usage":
disk_index = _item_index(config.item_id, "linux", position=2)
if disk_index is None:
return SnmpCheckResult(status="down", response_time_ms=0, message="SNMP Linux disk item was not valid")
oid = _with_index(UCD_DSK_PERCENT, disk_index)
used_percent = _int_value(client.get_many([oid]).get(oid))
response_time_ms = int((perf_counter() - started) * 1000)
if used_percent is None:
return SnmpCheckResult(status="down", response_time_ms=response_time_ms, message="Linux disk usage was not reported")
label = config.label or "Disk"
return SnmpCheckResult(
status="up",
response_time_ms=response_time_ms,
message=f"{label} is {used_percent}% used",
metrics=[SnmpMetricValue(name="used_percent", value=float(used_percent), unit="%")],
)
if config.item_type in {"memory_usage", "storage_usage"}:
storage_index = _item_index(config.item_id, "storage")
if storage_index is None:
@@ -276,12 +335,12 @@ def _interface_index(item_id: str) -> int | None:
return _item_index(item_id, "interface")
def _item_index(item_id: str, expected_prefix: str) -> int | None:
def _item_index(item_id: str, expected_prefix: str, position: int = 1) -> int | None:
parts = item_id.split(".")
if len(parts) < 3 or parts[0] != expected_prefix:
if len(parts) <= position or parts[0] != expected_prefix:
return None
try:
return int(parts[1])
return int(parts[position])
except ValueError:
return None