From 9da86617de19ce6d94ca4f23fd14e7d29aa14951 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Thu, 16 May 2024 10:22:42 +0200 Subject: [PATCH 1/3] src - Add initial disk check --- routeros_check/check/system_disk.py | 149 ++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 routeros_check/check/system_disk.py diff --git a/routeros_check/check/system_disk.py b/routeros_check/check/system_disk.py new file mode 100644 index 0000000..762d1f3 --- /dev/null +++ b/routeros_check/check/system_disk.py @@ -0,0 +1,149 @@ +# SPDX-FileCopyrightText: PhiBo DinoTools (2021) +# SPDX-License-Identifier: GPL-3.0-or-later + +from typing import List, Optional + +import click +import librouteros +import librouteros.query +import nagiosplugin + +from ..cli import cli +from ..context import ScalarPercentContext +from ..helper import logger, RouterOSVersion +from ..resource import RouterOSCheckResource + + +class SystemDiskResource(RouterOSCheckResource): + name = "DISK" + + def __init__(self, cmd_options): + super().__init__(cmd_options=cmd_options) + + self.total_hdd_space: Optional[int] = None + self._routeros_metric_values = [ + {"name": "write-sect-since-reboot", "type": int, "min": 0}, + {"name": "write-sect-total", "type": int, "min": 0}, + {"name": "bad-blocks", "type": float, "min": 0, "max": 100, "missing_ok": True}, + ] + + def probe(self): + api = self._connect_api() + + logger.info("Fetching data ...") + call = api.path( + "/system/resource" + ).select( + librouteros.query.Key("free-hdd-space"), + librouteros.query.Key("total-hdd-space"), + *self.get_routeros_select_keys() + ) + if self.routeros_version < RouterOSVersion("7"): + api_result_items = tuple(call) + api_result_items = self._convert_v6_list_to_v7(api_result_items) + else: + api_result_items = tuple(call) + + free_hdd_space = api_result_items[0]["free-hdd-space"] + self.total_hdd_space = api_result_items[0]["total-hdd-space"] + results = self.get_routeros_metric_item(api_result_items[0]) + + results.append( + nagiosplugin.Metric( + name="free", + value=free_hdd_space, + uom="B", + min=0, + max=self.total_hdd_space, + ) + ) + + results.append( + nagiosplugin.Metric( + name="used", + value=self.total_hdd_space - free_hdd_space, + uom="B", + min=0, + max=self.total_hdd_space, + ) + ) + + return results + + +class SystemDiskSummary(nagiosplugin.summary.Summary): + def __init__(self, result_names: List[str]): + super().__init__() + self._result_names = result_names + + def ok(self, results): + msgs = [] + for result_name in self._result_names: + msgs.append(str(results[result_name])) + return " ".join(msgs) + + +@cli.command("system.disk") +@click.option( + "--used/--free", + is_flag=True, + default=True, + help="Set if used or free memory should be checked. (Default: used)", +) +@click.option( + "--warning", + required=True, + help="Warning threshold in % or MB. Example (20% oder 20 = 20MB)", +) +@click.option( + "--critical", + required=True, + help="Critical threshold in % or MB. Example (20% oder 20 = 20MB)", +) +@click.pass_context +@nagiosplugin.guarded +def system_memory(ctx, used, warning, critical): + check = nagiosplugin.Check( + SystemDiskResource( + cmd_options=ctx.obj, + ) + ) + + if used: + check.add(nagiosplugin.ScalarContext( + name="free", + )) + check.add(ScalarPercentContext( + name="used", + total_name="total_hdd_space", + warning=warning, + critical=critical + )) + else: + check.add(ScalarPercentContext( + name="free", + total_name="total_hdd_space", + warning=f"{warning}:", + critical=f"{critical}:" + )) + check.add(nagiosplugin.ScalarContext( + name="used", + )) + + check.add(nagiosplugin.ScalarContext( + name="bad-blocks", + )) + + check.add(nagiosplugin.ScalarContext( + name="write-sect-since-reboot", + )) + + check.add(nagiosplugin.ScalarContext( + name="write-sect-total", + )) + + check.add(SystemDiskSummary( + result_names=["used"] if used else ["free"] + )) + + check.main(verbose=ctx.obj["verbose"]) From 0d197dab0d9db43ab8372631d103cc793980bbb9 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Thu, 16 May 2024 10:40:56 +0200 Subject: [PATCH 2/3] src - Add bad blocks thresholds for disk check --- routeros_check/check/system_disk.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/routeros_check/check/system_disk.py b/routeros_check/check/system_disk.py index 762d1f3..1d14f77 100644 --- a/routeros_check/check/system_disk.py +++ b/routeros_check/check/system_disk.py @@ -10,7 +10,7 @@ from ..cli import cli from ..context import ScalarPercentContext -from ..helper import logger, RouterOSVersion +from ..helper import logger from ..resource import RouterOSCheckResource @@ -38,11 +38,7 @@ def probe(self): librouteros.query.Key("total-hdd-space"), *self.get_routeros_select_keys() ) - if self.routeros_version < RouterOSVersion("7"): - api_result_items = tuple(call) - api_result_items = self._convert_v6_list_to_v7(api_result_items) - else: - api_result_items = tuple(call) + api_result_items = tuple(call) free_hdd_space = api_result_items[0]["free-hdd-space"] self.total_hdd_space = api_result_items[0]["total-hdd-space"] @@ -100,9 +96,17 @@ def ok(self, results): required=True, help="Critical threshold in % or MB. Example (20% oder 20 = 20MB)", ) +@click.option( + "--bad-blocks-warning", + help="Warning threshold for bad blocks. Example: 20 -> 20% bad blocks", +) +@click.option( + "--bad-blocks-critical", + help="Critical threshold for bad blocks", +) @click.pass_context @nagiosplugin.guarded -def system_memory(ctx, used, warning, critical): +def system_disk(ctx, used, warning, critical, bad_blocks_warning, bad_blocks_critical): check = nagiosplugin.Check( SystemDiskResource( cmd_options=ctx.obj, @@ -132,6 +136,8 @@ def system_memory(ctx, used, warning, critical): check.add(nagiosplugin.ScalarContext( name="bad-blocks", + warning=bad_blocks_warning, + critical=bad_blocks_critical, )) check.add(nagiosplugin.ScalarContext( From 7c05df1932d28d3ab57395a64fede2379176f6ca Mon Sep 17 00:00:00 2001 From: PhiBo Date: Thu, 16 May 2024 10:44:13 +0200 Subject: [PATCH 3/3] cfg - Add icinga2 config for disk check --- config/check_routeros.icinga2.conf | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/config/check_routeros.icinga2.conf b/config/check_routeros.icinga2.conf index 7fbb24c..94640c4 100644 --- a/config/check_routeros.icinga2.conf +++ b/config/check_routeros.icinga2.conf @@ -238,6 +238,27 @@ object CheckCommand "routeros_system_cpu" { vars.routeros_command = "system.cpu" } +object CheckCommand "routeros_system_disk" { + import "routeros_command" + + arguments += { + "--warning" = { + value = "$routeros_system_disk_warning$" + } + "--critical" = { + value = "$routeros_system_disk_critical$" + } + "--bad-blocks-warning" = { + value = "$routeros_system_disk_bad_blocks_warning$" + } + "--bad-blocks-critical" = { + value = "$routeros_system_disk_bad_blocks_critical$" + } + } + + vars.routeros_command = "system.disk" +} + object CheckCommand "routeros_system_fan" { import "routeros_command"