diff --git a/README.md b/README.md index d42dc73bba8abbfd8e09acce456a1e8b88f072dd..240c521c400ae2df4dd789176f10e1fb510c37cf 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[PACKAGE]: ../../raw/master/mkp/inv_ipv4_routes-0.0.1-20231227.mkp "inv_ipv4_routes-0.0.1-20231227.mkp" +[PACKAGE]: ../../raw/master/mkp/inv_ipv4_routes-0.0.2-20240407.mkp "inv_ipv4_routes-0.0.2-20240407.mkp" # Inventory of IPv4 routes The plugin adds the IPv4 routes of a device monitored via SNMP to the inventory. diff --git a/mkp/inv_ipv4_routes-0.0.2-20240407.mkp b/mkp/inv_ipv4_routes-0.0.2-20240407.mkp new file mode 100644 index 0000000000000000000000000000000000000000..5ffca2ef8e9f8231f0bef316ac67bcbede95bfb2 Binary files /dev/null and b/mkp/inv_ipv4_routes-0.0.2-20240407.mkp differ diff --git a/source/agent_based/inv_ipv4_routes.py b/source/agent_based/inv_ipv4_routes.py index 81c85eeac65dcaadf920f7e64300e18e47f2e027..a302379b1d60155c2ef817a7de98df9d07513acb 100644 --- a/source/agent_based/inv_ipv4_routes.py +++ b/source/agent_based/inv_ipv4_routes.py @@ -9,10 +9,13 @@ # File : inv_ipv4_route.py # # inventory of IPv4 routing information +# +# 2024.04.07: fixed missing/bad netmask (ThX bitwiz@froum.checkmk.com) +# improved validation if SNMP input data -import ipaddress from dataclasses import dataclass +from ipaddress import AddressValueError, IPv4Address, IPv4Network, NetmaskValueError from typing import List from cmk.base.plugins.agent_based.agent_based_api.v1 import ( @@ -71,7 +74,7 @@ class Ipv4Route: age: int | None cidr: int destination: str - if_index: int + if_index: int | None if_name: str netmask: str next_hop: str @@ -80,23 +83,42 @@ class Ipv4Route: type: str -def parse_inv_ipv4_routes(string_table: List[StringTable]) -> List[Ipv4Route]: - routes, if_info = string_table +def parse_inv_ipv4_routes(string_table: List[StringTable]) -> List[Ipv4Route] | None: + try: + routes, if_info = string_table + except ValueError: + return - interface_by_index = {if_index: if_name for if_index, if_name in if_info} + try: + interface_by_index = {if_index: if_name for if_index, if_name in if_info} + except ValueError: + interface_by_index = {} ipv4_routes = [] - for destination, netmask, next_hop, if_index, route_type, protocol, age, status in routes: - ipv4 = ipaddress.IPv4Network(address=f'{destination}/{netmask}', strict=False) + + for entry in routes: + try: + destination, netmask, next_hop, if_index, route_type, protocol, age, status = entry + except ValueError: + continue + try: + ipv4 = IPv4Network(address=f'{destination}/{netmask}', strict=True) + except (AddressValueError, NetmaskValueError, ValueError): + continue + try: + next_hop = IPv4Address(next_hop) + except AddressValueError: + continue + ipv4_routes.append( Ipv4Route( age=int(age) if age.isdigit() else None, cidr=int(ipv4.prefixlen), - destination=str(destination), - if_index=int(if_index), - if_name=str(interface_by_index.get(if_index, f'unknown ({if_index})')), - netmask=str(netmask), - next_hop=str(next_hop), + destination=str(ipv4.network_address), + if_index=int(if_index) if if_index.isdigit() else None, + if_name=str(interface_by_index.get(if_index, if_index)), + netmask=str(ipv4.netmask), + next_hop=str(next_hop.exploded), protocol=_route_protocol.get(protocol, f'unknown ({protocol})'), status=_route_status.get(status, f'unknown ({status})'), type=_route_type.get(route_type, f'unknown ({route_type})'), @@ -115,14 +137,12 @@ def inventory_ipv4_routes(section: List[Ipv4Route]) -> InventoryResult: 'gateway': ipv4_route.next_hop, } inventory_columns = { - # 'age': render.timespan(ipv4_route.age), + **({"age": render.timespan(ipv4_route.age)} if ipv4_route.age else {}), 'device': ipv4_route.if_name, 'protocol': ipv4_route.protocol, 'status': ipv4_route.status, 'type': ipv4_route.type, } - if ipv4_route.age: - inventory_columns['age'] = render.timespan(ipv4_route.age) yield TableRow( path=path, diff --git a/source/packages/inv_ipv4_routes b/source/packages/inv_ipv4_routes index 136e1b1afeb9e401c857d4bdffe8646c48526d69..6225df18b1bd90f92eab65925e063f8eb2234db1 100644 --- a/source/packages/inv_ipv4_routes +++ b/source/packages/inv_ipv4_routes @@ -6,7 +6,7 @@ 'web': ['plugins/views/inv_ipv4_routes.py']}, 'name': 'inv_ipv4_routes', 'title': 'Inventory of IPv4 routes', - 'version': '0.0.1-20231227', + 'version': '0.0.2-20240407', 'version.min_required': '2.2.0b1', - 'version.packaged': '2.2.0p17', + 'version.packaged': '2.2.0p24', 'version.usable_until': '2.3.0p1'}