Skip to content

Instantly share code, notes, and snippets.

@WojRep
Created August 3, 2022 09:19
Show Gist options
  • Select an option

  • Save WojRep/6710b21ecefd2a3f4420b13fc4a44b3b to your computer and use it in GitHub Desktop.

Select an option

Save WojRep/6710b21ecefd2a3f4420b13fc4a44b3b to your computer and use it in GitHub Desktop.
Inventory use of network interfaces in MikroTik devices.
#!/usr/bin/env python3
# -*- encoding: utf-8; py-indent-offset: 4 -*-
#
# Author: Wojciech Repiński (wrepinski [at] gmail.com)
#
# Requirements: CheckMK > 2.0
# File location: ~/local/lib/python3/cmk/base/plugins/agent_based/mikrotik_interfaces.py
#
# You define what names your interface types have.
#
IF_TYPE_NAME = ['ether', 'combo', 'sfp', 'sfp+',]
#
# Define your MikroTik models. You assign identifiers returned by snmp as if_index.
#
BOARD_NAME = {
'CRS112-8G-4S': {
'ether': [1,2,3,4,5,6,7,8],
'combo': [],
'sfp': [9,10,11,12],
'sfp+': [],
},
'CRS112-8P-4S': {
'ether': [1,2,3,4,5,6,7,8],
'combo': [],
'sfp': [9,10,11,12],
'sfp+': [],
},
'CRS309-1G-8S+': {
'ether': [9] ,
'combo': [],
'sfp': [],
'sfp+': [1,2,3,4,5,6,7,8],
},
'CRS328-4C-20S-4S+': {
'ether': [],
'combo': [21,22,23,24],
'sfp': [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
'sfp+': [25,26,27,28],
},
}
#
# If MikroTik has a different type of interface like "6", add it here.
# ifType: OID 1.3.6.1.2.1.2.2.1.3
#
INTERFACE_TYPE = ['6']
###########################################################################
from .agent_based_api.v1 import *
from .agent_based_api.v1.type_defs import *
from typing import Dict, List
from cmk.utils import debug
from pprint import pprint
SNMP_BASE = '.1.3.6.1'
SNMP_DETECT = startswith('.1.3.6.1.2.1.1.2.0', '.1.3.6.1.4.1.14988.1')
OIDs = [
'2.1.2.2.1.1', # if index
'2.1.2.2.1.3', # if_type
'2.1.2.2.1.2', # if_name
'2.1.2.2.1.5', # if_speed
'4.1.14988.1.1.19.1.1.2', # if_name
'4.1.14988.1.1.19.1.1.11', # Vendor
]
def _search_if_type(model, if_index):
for key, value in model.items():
if int(if_index) in value:
return key
return 'unknown'
def parse_mikrotik_interfaces(string_table):
interface_list = []
model = string_table[0][0][0].replace('RouterOS','').strip()
if model in BOARD_NAME.keys():
if_list = string_table[1]
for interface in if_list:
if_index, if_type, if_name, if_speed, sfp_name, sfp_vendor = interface
if if_type in INTERFACE_TYPE:
model_interface_list = BOARD_NAME.get(model)
if_type_name = _search_if_type(model_interface_list, if_index)
if if_speed == '0' and sfp_vendor == '':
if_in_use = False
else:
if_in_use = True
interface_list.append([if_index, if_name, if_type_name, if_in_use] )
return interface_list
def discover_mikrotik_interfaces(section):
if section:
yield Service(item="Interface Summary")
def check_mikrotik_interfaces(item, params, section):
if not section:
yield Result(state=State.UNKNOWN, summary="No data")
return
state = State.OK
summary = None
notice = None
details = ""
interfaces = {}
for line in section:
if_index, if_name, if_type_name, if_in_use = line
if_no = interfaces.get(if_type_name) if interfaces.get(if_type_name) else [0,0]
if_no[0] = if_no[0] + 1
if if_in_use:
if_no[1] = if_no[1] + 1
interfaces.update({if_type_name: if_no})
for iftypename in IF_TYPE_NAME:
value = interfaces.get(iftypename) if interfaces.get(iftypename) else [0,0]
if_used_no = int(value[0])
if_free_no = if_used_no - int(value[1])
details = f'{iftypename}: {if_used_no}/{if_free_no} '
summary = details
yield Result(state=state, summary=summary, details=details)
return
register.snmp_section(
name='mikrotik_interfaces',
fetch = [
SNMPTree(
base = SNMP_BASE,
oids = ['2.1.1.1'],
),
SNMPTree(
base = SNMP_BASE,
oids = OIDs,
),
],
detect = SNMP_DETECT,
parse_function = parse_mikrotik_interfaces,
)
register.check_plugin(
name = 'mikrotik_interfaces',
sections=['mikrotik_interfaces'],
service_name = "%s",
discovery_function = discover_mikrotik_interfaces,
check_default_parameters={},
check_ruleset_name='mikrotik_interfaces',
check_function = check_mikrotik_interfaces,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment