From f70bf691333d4c857184c7e59d39197c2d0f5025 Mon Sep 17 00:00:00 2001 From: Antoine Van Elstraete Date: Mon, 26 Jan 2026 17:08:16 +0100 Subject: [PATCH] Fix SNMP checker for pysnmp 7.x API Migrate from deprecated hlapi to v1arch.asyncio with Slim wrapper. Co-Authored-By: Claude Opus 4.5 --- checkers/snmp.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/checkers/snmp.py b/checkers/snmp.py index 37c4721..c470d7f 100644 --- a/checkers/snmp.py +++ b/checkers/snmp.py @@ -1,13 +1,10 @@ +import asyncio import time -from pysnmp.hlapi import ( - SnmpEngine, - CommunityData, - UdpTransportTarget, - ContextData, - ObjectType, +from pysnmp.hlapi.v1arch.asyncio import ( + Slim, ObjectIdentity, - getCmd, + ObjectType, ) from .base import BaseChecker, CheckResult @@ -15,6 +12,9 @@ from .base import BaseChecker, CheckResult class SnmpChecker(BaseChecker): def check(self) -> CheckResult: + return asyncio.run(self._async_check()) + + async def _async_check(self) -> CheckResult: host = self.config["host"] port = self.config.get("port", 161) community = self.config.get("community", "public") @@ -23,15 +23,16 @@ class SnmpChecker(BaseChecker): start = time.time() try: - iterator = getCmd( - SnmpEngine(), - CommunityData(community), - UdpTransportTarget((host, port), timeout=timeout_val, retries=1), - ContextData(), - ObjectType(ObjectIdentity(oid)) - ) + with Slim() as slim: + error_indication, error_status, error_index, var_binds = await slim.get( + community, + host, + port, + ObjectType(ObjectIdentity(oid)), + timeout=timeout_val, + retries=1 + ) - error_indication, error_status, error_index, var_binds = next(iterator) response_time = (time.time() - start) * 1000 # ms if error_indication: