Skip to content

Commit

Permalink
Merge pull request #4 from netty-community/feat-vendors
Browse files Browse the repository at this point in the history
fix: fix factory implement
  • Loading branch information
wangxin688 authored Aug 11, 2024
2 parents 70c4a02 + 5181e15 commit 745b98a
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 19 deletions.
1 change: 1 addition & 0 deletions netty_snmp/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class SnmpDiscoveryData(TypedDict, total=False):
stack: list[dict]
vlans: list[dict]
mac_address_table: dict[int, list[str]] | None
arp_table: dict[str, str] | None
prefixes: list[dict] | None
routes: list[dict] | None
exceptions: list[DiscoveryException]
Expand Down
13 changes: 11 additions & 2 deletions netty_snmp/factory/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

SNMP_DEFAULT_PORT = 161
SNMP_DEFAULT_COMMUNITY = "public"
SNMP_MAX_repetitions = 50
SNMP_MAX_REPETITIONS = 50
UNKNOWN_MANUFACTURER = "Unknown"
UNKNOWN_PLATFORM = "Unknown"
UNKNOWN_MODEL = "Unknown"
Expand Down Expand Up @@ -578,8 +578,17 @@ class StackRole(IntEnum):

dot1dBasePortIfIndex = SnmpItem(
name="dot1dBasePortIfIndex",
oid=".1.3.6.1.2.1.2.2.1.1",
oid=".1.3.6.1.2.1.17.1.4.1.2",
description="The ifIndex of the port.",
value_type="int",
value_mapping=None,
)

ipNetToMediaPhysAddress = SnmpItem(
name="ipNetToMediaPhysAddress",
oid=".1.3.6.1.2.1.4.22.1.2",
description="The MAC address of the port.",
value_type="str",
value_mapping=None,
to_hex=True,
)
2 changes: 1 addition & 1 deletion netty_snmp/factory/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(
version: consts.SnmpVersion = consts.SnmpVersion.v2c,
community: str | None = consts.SNMP_DEFAULT_COMMUNITY,
v3_params: SnmpV3Params | None = None,
snmp_max_repetitions: int = 20,
snmp_max_repetitions: int = consts.SNMP_MAX_REPETITIONS,
max_workers: int = 16,
) -> None:
self.prefix = self.str_to_prefix(prefix)
Expand Down
2 changes: 1 addition & 1 deletion netty_snmp/factory/manufactures/arista.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def __init__(
version: consts.SnmpVersion = consts.SnmpVersion.v2c,
community: str | None = consts.SNMP_DEFAULT_COMMUNITY,
v3_params: SnmpV3Params | None = None,
snmp_max_repetitions: int = consts.SNMP_MAX_repetitions,
snmp_max_repetitions: int = consts.SNMP_MAX_REPETITIONS,
) -> None:
super().__init__(ip, port, version, community, v3_params, snmp_max_repetitions)
2 changes: 1 addition & 1 deletion netty_snmp/factory/manufactures/aruba.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def __init__(
version: consts.SnmpVersion = consts.SnmpVersion.v2c,
community: str | None = consts.SNMP_DEFAULT_COMMUNITY,
v3_params: SnmpV3Params | None = None,
snmp_max_repetitions: int = consts.SNMP_MAX_repetitions,
snmp_max_repetitions: int = consts.SNMP_MAX_REPETITIONS,
) -> None:
super().__init__(ip, port, version, community, v3_params, snmp_max_repetitions)
6 changes: 5 additions & 1 deletion netty_snmp/factory/manufactures/cisco.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ def __init__(
version: consts.SnmpVersion = consts.SnmpVersion.v2c,
community: str | None = consts.SNMP_DEFAULT_COMMUNITY,
v3_params: SnmpV3Params | None = None,
snmp_max_repetitions: int = consts.SNMP_MAX_repetitions,
snmp_max_repetitions: int = consts.SNMP_MAX_REPETITIONS,
) -> None:
super().__init__(ip, port, version, community, v3_params, snmp_max_repetitions)

@property
def stack(self) -> dict:
"""CISCO-STACK-MIB"""

@property
def mac_address_table(self) -> dict[int, list[str]]:
"""CISCO-DOT1D-MIB, per-vlan mac-address table, cisco needs specific implementation"""
2 changes: 1 addition & 1 deletion netty_snmp/factory/manufactures/fortinet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def __init__(
version: consts.SnmpVersion = consts.SnmpVersion.v2c,
community: str | None = consts.SNMP_DEFAULT_COMMUNITY,
v3_params: SnmpV3Params | None = None,
snmp_max_repetitions: int = consts.SNMP_MAX_repetitions,
snmp_max_repetitions: int = consts.SNMP_MAX_REPETITIONS,
) -> None:
super().__init__(ip, port, version, community, v3_params, snmp_max_repetitions)
2 changes: 1 addition & 1 deletion netty_snmp/factory/manufactures/h3c.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(
version: consts.SnmpVersion = consts.SnmpVersion.v2c,
community: str | None = consts.SNMP_DEFAULT_COMMUNITY,
v3_params: SnmpV3Params | None = None,
snmp_max_repetitions: int = consts.SNMP_MAX_repetitions,
snmp_max_repetitions: int = consts.SNMP_MAX_REPETITIONS,
) -> None:
super().__init__(ip, port, version, community, v3_params, snmp_max_repetitions)

Expand Down
10 changes: 5 additions & 5 deletions netty_snmp/factory/manufactures/huawei.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(
version: consts.SnmpVersion = consts.SnmpVersion.v2c,
community: str | None = consts.SNMP_DEFAULT_COMMUNITY,
v3_params: SnmpV3Params | None = None,
snmp_max_repetitions: int = consts.SNMP_MAX_repetitions,
snmp_max_repetitions: int = consts.SNMP_MAX_REPETITIONS,
) -> None:
super().__init__(ip, port, version, community, v3_params, snmp_max_repetitions)

Expand All @@ -25,16 +25,16 @@ def stack(self) -> list[StackMember]:
return []
try:
hw_stack_id = self.session.bulkwalk(
consts.hwMemberCurrentStackId.oid, max_repetitions=self.snmp_max_repetitions
consts.hwMemberCurrentStackId.oid, max_repetitions=self.SNMP_MAX_REPETITIONS
)
hw_stack_priority = self.session.bulkwalk(
consts.hwMemberStackPriority.oid, max_repetitions=self.snmp_max_repetitions
consts.hwMemberStackPriority.oid, max_repetitions=self.SNMP_MAX_REPETITIONS
)
hw_stack_role = self.session.bulkwalk(
consts.hwMemberStackRole.oid, max_repetitions=self.snmp_max_repetitions
consts.hwMemberStackRole.oid, max_repetitions=self.SNMP_MAX_REPETITIONS
)
hw_stack_mac_address = self.session.bulkwalk(
consts.hwMemberStackMacAddress.oid, max_repetitions=self.snmp_max_repetitions
consts.hwMemberStackMacAddress.oid, max_repetitions=self.SNMP_MAX_REPETITIONS
)

except EzSNMPError as e:
Expand Down
2 changes: 1 addition & 1 deletion netty_snmp/factory/manufactures/juniper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def __init__(
version: consts.SnmpVersion = consts.SnmpVersion.v2c,
community: str | None = consts.SNMP_DEFAULT_COMMUNITY,
v3_params: SnmpV3Params | None = None,
snmp_max_repetitions: int = consts.SNMP_MAX_repetitions,
snmp_max_repetitions: int = consts.SNMP_MAX_REPETITIONS,
) -> None:
super().__init__(ip, port, version, community, v3_params, snmp_max_repetitions)
2 changes: 1 addition & 1 deletion netty_snmp/factory/manufactures/paloalto.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def __init__(
version: consts.SnmpVersion = consts.SnmpVersion.v2c,
community: str | None = consts.SNMP_DEFAULT_COMMUNITY,
v3_params: SnmpV3Params | None = None,
snmp_max_repetitions: int = consts.SNMP_MAX_repetitions,
snmp_max_repetitions: int = consts.SNMP_MAX_REPETITIONS,
) -> None:
super().__init__(ip, port, version, community, v3_params, snmp_max_repetitions)
2 changes: 1 addition & 1 deletion netty_snmp/factory/manufactures/ruijie.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def __init__(
version: consts.SnmpVersion = consts.SnmpVersion.v2c,
community: str | None = consts.SNMP_DEFAULT_COMMUNITY,
v3_params: SnmpV3Params | None = None,
snmp_max_repetitions: int = consts.SNMP_MAX_repetitions,
snmp_max_repetitions: int = consts.SNMP_MAX_REPETITIONS,
) -> None:
super().__init__(ip, port, version, community, v3_params, snmp_max_repetitions)
13 changes: 11 additions & 2 deletions netty_snmp/factory/snmp_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
version: consts.SnmpVersion = consts.SnmpVersion.v2c,
community: str | None = consts.SNMP_DEFAULT_COMMUNITY,
v3_params: SnmpV3Params | None = None,
snmp_max_repetitions: int = consts.SNMP_MAX_repetitions,
snmp_max_repetitions: int = consts.SNMP_MAX_REPETITIONS,
) -> None:
"""
:param ip: network device ip, ipv4 or ipv6 address
Expand Down Expand Up @@ -324,7 +324,15 @@ def mac_address_table(self) -> dict[int, list[str]]:
return results

@property
def arp_table(self) -> Any: ...
def arp_table(self) -> dict[str, str]:
try:
arp_table = self.session.bulkwalk(
oids=consts.ipNetToMediaPhysAddress.oid, max_repetitions=self.snmp_max_repetitions
)
except EzSNMPError as e:
self.exceptions.append(DiscoveryException(item="arp_table", exception=str(e)))
return {}
return {".".join(x.oid_index.split(".")[-4:]): mac_address_validator(x.value, True) for x in arp_table}

def discovery(self, items: list[DiscoveryItem] | None = None) -> SnmpDiscoveryData:
"""
Expand All @@ -346,6 +354,7 @@ def discovery(self, items: list[DiscoveryItem] | None = None) -> SnmpDiscoveryDa
vlans=self.vlans,
prefixes=self.prefixes,
mac_address_table=self.mac_address_table,
arp_table=self.arp_table,
routes=self.routes,
exceptions=self.exceptions,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_dispatch():
port=161,
version=consts.SnmpVersion.v2c,
community="public",
snmp_max_repetitions=20,
snmp_max_repetitions=consts.SNMP_MAX_REPETITIONS,
max_workers=64,
)

Expand Down

0 comments on commit 745b98a

Please sign in to comment.