Skip to content

Commit

Permalink
Merge pull request #11 from infrasonar/lldp
Browse files Browse the repository at this point in the history
New LLDP check
  • Loading branch information
joente authored Jan 9, 2025
2 parents 49e8e17 + 8cef0e0 commit fd94b22
Show file tree
Hide file tree
Showing 6 changed files with 954 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/check/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from libprobe.exceptions import CheckException
from ..snmpclient import get_snmp_client
from ..snmpquery import snmpquery
from ..utils import InterfaceLookup

QUERIES = (
MIB_INDEX['IF-MIB']['ifEntry'],
Expand Down Expand Up @@ -161,6 +162,10 @@ async def check_interface(

counts = Counter()
itms = state_data.get('if', [])

# lookup is used by lldp check
InterfaceLookup.set(asset.id, itms)

items = []
if_x_entry = {i.pop('name'): i for i in state_data.pop('ifX', [])}
for item in itms:
Expand Down
45 changes: 45 additions & 0 deletions lib/check/lldp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from asyncsnmplib.mib.mib_index import MIB_INDEX
from libprobe.asset import Asset
from ..snmpclient import get_snmp_client
from ..snmpquery import snmpquery
from ..utils import InterfaceLookup


QUERIES = (
MIB_INDEX['IF-MIB']['ifEntry'],
MIB_INDEX['BRIDGE-MIB']['dot1dStpPortEntry'],
MIB_INDEX['BRIDGE-MIB']['dot1dBasePortEntry']
)


async def check_lldp(
asset: Asset,
asset_config: dict,
check_config: dict):

snmp = get_snmp_client(asset, asset_config, check_config)

if_entry = InterfaceLookup.get(asset.id)
if if_entry is None:
state_data = await snmpquery(snmp, QUERIES, True)
if_entry = InterfaceLookup.set(asset.id, state_data.get('if', []))
else:
state_data = await snmpquery(snmp, QUERIES[1:], True)

itms = state_data.get('dot1dStpPort', [])
base_port_entry = {
i.pop('name'): i
for i in state_data.get('dot1dBasePort', [])}
for item in itms:
key = item['name']

try:
base_port_item = base_port_entry[key]
if_item = if_entry[base_port_item['IfIndex']]
item['Interface'] = if_item['Descr']
except Exception:
continue

return {
'lldp': itms
}
20 changes: 20 additions & 0 deletions lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import ipaddress
import time
from typing import Union
from .exceptions import ParseKeyException


class InterfaceLookup:
_lk = {}
_MAX_AGE = 900

@classmethod
def get(cls, asset_id: int) -> Union[dict[int, str], None]:
ts, data = cls._lk.get(asset_id, (None, None))
if ts is None or time.time() - ts > cls._MAX_AGE:
return
return data

@classmethod
def set(cls, asset_id: int, rows: list) -> dict[int, str]:
data = {i['Index']: i for i in rows}
cls._lk[asset_id] = (time.time(), data)
return data


def addr_ipv4(octets):
n = octets[0]
assert len(octets) == n + 1 == 5
Expand Down
2 changes: 1 addition & 1 deletion lib/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Version string. Examples:
# '3.0.0'
# '3.0.0-alpha9'
__version__ = '3.1.1'
__version__ = '3.1.2-alpha0'
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from lib.check.ip import check_ip
from lib.check.ip_address import check_ip_address
from lib.check.ip_forward import check_ip_forward
from lib.check.lldp import check_lldp
from lib.check.power_ethernet import check_power_ethernet
from lib.check.process import check_process
from lib.check.processor import check_processor
Expand All @@ -29,6 +30,7 @@
'ip': check_ip,
'ipAddress': check_ip_address,
'ipForward': check_ip_forward,
'lldp': check_lldp,
'powerEthernet': check_power_ethernet,
'process': check_process,
'processor': check_processor,
Expand Down
Loading

0 comments on commit fd94b22

Please sign in to comment.