Skip to content

Commit

Permalink
Merge pull request #8 from cesbit/dev
Browse files Browse the repository at this point in the history
Fix exceptions and v3 SNMP
  • Loading branch information
joente authored Sep 26, 2023
2 parents bb67080 + d27855c commit 26cdb99
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion asyncsnmplib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def __init__(
try:
self._priv_proto = PRIV_PROTO[priv_proto]
except KeyError:
raise Exception('Supply valid auth_proto')
raise Exception('Supply valid priv_proto')
if self._priv_proto and not self._auth_proto:
raise Exception('Supply auth_proto')
if self._auth_proto:
Expand Down
50 changes: 25 additions & 25 deletions asyncsnmplib/utils.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import logging
from .client import Snmp, SnmpV1, SnmpV3
from .exceptions import SnmpNoConnection, SnmpNoAuthParams
from .exceptions import SnmpException, SnmpNoConnection, SnmpNoAuthParams
from .mib.utils import on_result_base
from .v3.auth import AUTH_PROTO
from .v3.encr import PRIV_PROTO


class InvalidConfigException(Exception):
pass
class InvalidCredentialsException(SnmpException):
message = 'Invalid SNMP v3 credentials.'


class MissingCredentialsException(InvalidConfigException):
pass
class InvalidClientConfigException(SnmpException):
message = 'Invalid SNMP v3 client configuration.'


class InvalidCredentialsException(InvalidConfigException):
pass
class InvalidSnmpVersionException(SnmpException):
message = 'Invalid SNMP version.'


class InvalidClientConfigException(InvalidConfigException):
pass


class ParseResultException(Exception):
pass
class ParseResultException(SnmpException):
def __init__(self, message: str):
super().__init__(message)
self.message = message


def snmpv3_credentials(config: dict):
Expand Down Expand Up @@ -80,16 +78,13 @@ async def snmp_queries(
queries: tuple):

version = config.get('version', '2c')
community = config.get('community', 'public')
if not isinstance(community, str):
try:
community = community['secret']
assert isinstance(community, str)
except KeyError:
logging.warning(f'missing snmp credentials {address}')
raise MissingCredentialsException

if version == '2c':
community = config.get('community', 'public')
if isinstance(community, dict):
community = community.get('secret')
if not isinstance(community, str):
raise TypeError('SNMP community must be a string.')
cl = Snmp(
host=address,
community=community,
Expand All @@ -109,20 +104,25 @@ async def snmp_queries(
logging.warning(f'invalid snmpv3 client config {address}: {e}')
raise InvalidClientConfigException
elif version == '1':
community = config.get('community', 'public')
if isinstance(community, dict):
community = community.get('secret')
if not isinstance(community, str):
raise TypeError('SNMP community must be a string.')
cl = SnmpV1(
host=address,
community=community,
)
else:
logging.warning(f'unsupported snmp version {address}: {version}')
raise InvalidClientConfigException
raise InvalidSnmpVersionException

try:
await cl.connect()
except SnmpNoConnection as e:
except SnmpNoConnection:
raise
except SnmpNoAuthParams:
logging.warning(f'unable to connect: failed to set auth params')
logging.warning('unable to connect: failed to set auth params')
raise
else:
results = {}
Expand All @@ -133,7 +133,7 @@ async def snmp_queries(
except Exception as e:
msg = str(e) or type(e).__name__
raise ParseResultException(
f'parse result error: {msg}')
f'Failed to parse result. Exception: {msg}')
else:
results[name] = parsed_result
return results
Expand Down

0 comments on commit 26cdb99

Please sign in to comment.