Skip to content

Commit

Permalink
git - Merge pull request #84 from DinoTools/check_interface
Browse files Browse the repository at this point in the history
Fix issue with interface speed in interface check
  • Loading branch information
phibos authored Jun 5, 2024
2 parents 512d187 + 26b6875 commit 05ddc02
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 31 deletions.
32 changes: 18 additions & 14 deletions config/check_routeros.icinga2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,21 @@ object CheckCommand "routeros_interface" {
"--cookie-filename" = {
value = "$routeros_interface_cookie_filename$"
}
"--value-default" = {
value = "$routeros_interface_value_default$"
repeat_key = true
}
"--value-override" = {
value = "$routeros_interface_value_override$"
repeat_key = true
repeat_key = true
}
"--value-warning" = {
value = "$routeros_interface_value_warning$"
repeat_key = true
repeat_key = true
}
"--value-critical" = {
value = "$routeros_interface_value_critical$"
repeat_key = true
repeat_key = true
}
}

Expand All @@ -127,7 +131,7 @@ object CheckCommand "routeros_interface_gre" {
arguments += {
"--name" = {
value = "$routeros_interface_gre_name$"
repeat_key = true
repeat_key = true
}
"--regex" = {
set_if = "$routeros_interface_gre_regex$"
Expand Down Expand Up @@ -166,7 +170,7 @@ object CheckCommand "routeros_routing_bgp_peer" {
arguments += {
"--name" = {
value = "$routeros_routing_bgp_peer_name$"
repeat_key = true
repeat_key = true
}
"--regex" = {
set_if = "$routeros_routing_bgp_peer_regex$"
Expand Down Expand Up @@ -224,11 +228,11 @@ object CheckCommand "routeros_system_cpu" {
}
"--value-warning" = {
value = "$routeros_system_cpu_value_warning$"
repeat_key = true
repeat_key = true
}
"--value-critical" = {
value = "$routeros_system_cpu_value_critical$"
repeat_key = true
repeat_key = true
}
"--regex" = {
set_if = "$routeros_system_cpu_regex$"
Expand Down Expand Up @@ -265,11 +269,11 @@ object CheckCommand "routeros_system_fan" {
arguments += {
"--value-warning" = {
value = "$routeros_system_fan_value_warning$"
repeat_key = true
repeat_key = true
}
"--value-critical" = {
value = "$routeros_system_fan_value_critical$"
repeat_key = true
repeat_key = true
}
"--regex" = {
set_if = "$routeros_system_fan_regex$"
Expand Down Expand Up @@ -297,7 +301,7 @@ object CheckCommand "routeros_system_license" {
}
"--level" = {
value = "$routeros_system_license_levels$"
repeat_key = true
repeat_key = true
}
}

Expand Down Expand Up @@ -373,11 +377,11 @@ object CheckCommand "routeros_system_psu" {
arguments += {
"--value-warning" = {
value = "$routeros_system_psu_value_warning$"
repeat_key = true
repeat_key = true
}
"--value-critical" = {
value = "$routeros_system_psu_value_critical$"
repeat_key = true
repeat_key = true
}
}

Expand All @@ -390,11 +394,11 @@ object CheckCommand "routeros_system_temperature" {
arguments += {
"--value-warning" = {
value = "$routeros_system_temperature_value_warning$"
repeat_key = true
repeat_key = true
}
"--value-critical" = {
value = "$routeros_system_temperature_value_critical$"
repeat_key = true
repeat_key = true
}
"--regex" = {
set_if = "$routeros_system_temperature_regex$"
Expand Down
60 changes: 43 additions & 17 deletions routeros_check/check/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(
cookie_filename: str,
warning_values: List[str],
critical_values: List[str],
default_values: List[str],
override_values: List[str],
):
super().__init__(cmd_options=cmd_options)
Expand All @@ -46,6 +47,7 @@ def __init__(

self._parsed_warning_values: Dict[str, str] = self.prepare_thresholds(warning_values)
self._parsed_critical_values: Dict[str, str] = self.prepare_thresholds(critical_values)
self._parsed_default_values: Dict[str, str] = self.prepare_override_values(default_values)
self._parsed_override_values: Dict[str, str] = self.prepare_override_values(override_values)

self._routeros_metric_values = [
Expand Down Expand Up @@ -151,14 +153,14 @@ def __init__(
"min": 0,
"uom": "c",
"rate": True,
"rate_percent_total_name": "speed-byte",
},
{
"name": "tx-byte",
"type": int,
"min": 0,
"uom": "B",
"rate": True,
"rate_percent_total_name": "speed-byte",
},
{
"name": "tx-drop",
Expand Down Expand Up @@ -248,14 +250,19 @@ def fetch_data(self) -> Dict[str, Dict]:

logger.info("Fetching data ...")
interface_ethernet_data = {}
call = api.path(
"/interface/ethernet"
)
call_results = tuple(call)
for result in call_results:
interface_ethernet_data[result["name"]] = {
"speed": result["speed"],
interface_count = len(tuple(api.path("/interface/ethernet")))
call_results = tuple(api(
"/interface/ethernet/monitor",
**{
"once": "",
"numbers": f"{','.join([str(i) for i in range(interface_count)])}"
}
))
for result in call_results:
if "rate" in result:
interface_ethernet_data[result["name"]] = {
"speed": result["rate"],
}

call = api.path(
"/interface"
Expand All @@ -267,19 +274,23 @@ def fetch_data(self) -> Dict[str, Dict]:
if self.ignore_disabled and result["disabled"]:
continue

if result["name"] in interface_ethernet_data:
result.update(interface_ethernet_data[result["name"]])
interface_data = dict(self._parsed_default_values.items())
interface_data.update(result)

if interface_data["name"] in interface_ethernet_data:
interface_data.update(interface_ethernet_data[interface_data["name"]])

result.update(self._parsed_override_values)
interface_data.update(self._parsed_override_values)

if len(self.names) == 0:
self._interface_data[result["name"]] = result
self._interface_data[interface_data["name"]] = interface_data
elif self.regex:
for name in self.names:
if name.match(result["name"]):
self._interface_data[result["name"]] = result
elif result["name"] in self.names:
self._interface_data[result["name"]] = result
if name.match(interface_data["name"]):
self._interface_data[interface_data["name"]] = interface_data
elif interface_data["name"] in self.names:
self._interface_data[interface_data["name"]] = interface_data

return self._interface_data

@property
Expand Down Expand Up @@ -379,6 +390,19 @@ def evaluate(self, metric, resource: InterfaceResource):
"(Default: /tmp/check_routeros_interface_{name}.data)"
),
)
@click.option(
"default_values",
"--value-default",
multiple=True,
help=(
"Set a default value if the value is not provided by RouterOS. "
"Format of the value must be compatible with RouterOS values. "
"Example: Set the default speed value for interfaces: "
"--value-override speed:10Gbps "
"Looks like there is a bug where RouterOS does not report the current "
"speed of the interface (RouterOS 7.8 - 7.14.2?). "
)
)
@click.option(
"override_values",
"--value-override",
Expand Down Expand Up @@ -414,7 +438,8 @@ def evaluate(self, metric, resource: InterfaceResource):
)
@click.pass_context
def interface(
ctx, names, regex, single, ignore_disabled, cookie_filename, warning_values, critical_values, override_values
ctx, names, regex, single, ignore_disabled, cookie_filename, warning_values, critical_values, default_values,
override_values
):
"""Check the state and the stats of interfaces"""
check = nagiosplugin.Check()
Expand All @@ -426,6 +451,7 @@ def interface(
single_interface=single,
ignore_disabled=ignore_disabled,
cookie_filename=cookie_filename,
default_values=default_values,
warning_values=warning_values,
critical_values=critical_values,
override_values=override_values,
Expand Down

0 comments on commit 05ddc02

Please sign in to comment.