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: