Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exceptions and v3 SNMP #8

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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