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 unintended breakdown of MIB object sub-indexes into symbolic representations #389

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions changelog.d/389.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed unintended symbolic translations in the SNMP abstraction layer, and removed now obsolete workarounds for the problem.
5 changes: 5 additions & 0 deletions src/zino/oid.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def __new__(cls, oid):
return oid
return tuple.__new__(cls, oid)

def __init__(self, oid):
super().__init__()
if not all(isinstance(i, int) for i in self):
raise TypeError("Not all arguments could be converted to `int`")

def __str__(self):
return SEPARATOR + SEPARATOR.join([str(i) for i in self])

Expand Down
10 changes: 8 additions & 2 deletions src/zino/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,15 @@ def udp_transport_target(self) -> Union[UdpTransportTarget, Udp6TransportTarget]

def _convert_varbind(ident: ObjectIdentity, value: ObjectType) -> SNMPVarBind:
"""Converts a PySNMP varbind pair to an Identifier/value pair"""
mib, obj, row_index = ident.getMibSymbol()
mib, obj, indices = ident.getMibSymbol()
value = _mib_value_to_python(value)
return Identifier(mib, obj, OID(row_index)), value

prefix = SNMP._oid_to_object_type(mib, obj)
SNMP._resolve_object(prefix)
prefix = OID(prefix[0])
row_index = OID(ident).strip_prefix(prefix)

return Identifier(mib, obj, row_index), value


def _mib_value_to_python(value: SupportedTypes) -> Union[str, int, OID]:
Expand Down
14 changes: 3 additions & 11 deletions src/zino/tasks/bgpstatemonitortask.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ async def _get_cisco_bgp_info(self) -> Optional[list[BaseBGPRow]]:
return None

for oid, result in cisco_bgp_info.items():
result["cbgpPeer2RemoteAddr"] = ip_address(oid)
_addr_type, _addr_len, addr = oid[0], oid[1], oid[2:]
result["cbgpPeer2RemoteAddr"] = ip_address(bytes(addr))

cisco_bgp_info = self._transform_variables_from_specific_to_general(
bgp_info=cisco_bgp_info, bgp_style=BGPStyle.CISCO
Expand Down Expand Up @@ -191,16 +192,7 @@ async def _get_bgp_info(self, mib_name: str, variables: Iterable[str]) -> Option
*((mib_name, var) for var in variables),
max_repetitions=3,
)

cleaned_bgp_info = dict()

for oid, entry in bgp_info.items():
if len(oid) == 1:
cleaned_bgp_info[oid[0].prettyPrint()] = entry
else:
cleaned_bgp_info[oid[1].prettyPrint()] = entry

return cleaned_bgp_info
return bgp_info

def _transform_variables_from_specific_to_general(
self, bgp_info: SparseWalkResponse, bgp_style: BGPStyle
Expand Down
7 changes: 7 additions & 0 deletions tests/oid_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from zino.oid import OID


Expand Down Expand Up @@ -38,3 +40,8 @@ def test_can_create_oid_from_bytestring():
oid_bytestring = b".1.2.3.4"
oid = OID(oid_bytestring)
assert str(oid) == ".1.2.3.4"


def test_when_oid_contains_non_numeric_elements_it_should_raise_typeerror():
with pytest.raises(TypeError):
OID((1, 2, 3, "foo", 4))
Loading