Skip to content

Commit

Permalink
git - Merge pull request #80 from DinoTools/system_uptime
Browse files Browse the repository at this point in the history
System uptime
  • Loading branch information
phibos authored May 15, 2024
2 parents 6cf6f5c + e866b97 commit a6235c4
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
39 changes: 36 additions & 3 deletions routeros_check/check/system_uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import librouteros
import librouteros.query
import nagiosplugin
from nagiosplugin.state import Ok as STATE_Ok, Warn as STATE_Warn, Critical as STATE_Critical

from ..cli import cli
from ..helper import logger
from ..helper import humanize_time, logger
from ..resource import RouterOSCheckResource


Expand Down Expand Up @@ -37,17 +38,49 @@ def probe(self):
)


class UptimeSimpleScalarContext(nagiosplugin.ScalarContext):
def describe(self, metric):
return humanize_time(metric.value)

def evaluate(self, metric, resource):
if str(self.critical) != "" and metric.value in self.critical:
return self.result_cls(
STATE_Critical,
None,
metric
)

if str(self.warning) != "" and metric.value in self.warning:
return self.result_cls(
STATE_Warn,
None,
metric
)
return self.result_cls(STATE_Ok, None, metric)


@cli.command("system.uptime")
@click.option(
"--warning",
help="State WARNING if current uptime is below this threshold",
)
@click.option(
"--critical",
help="State CRITICAL if current uptime is below this threshold",
default=None,
)
@click.pass_context
@nagiosplugin.guarded
def system_uptime(ctx):
def system_uptime(ctx, warning, critical):
"""Get Uptime of a device"""
check = nagiosplugin.Check(
SystemUptimeResource(
cmd_options=ctx.obj,
),
nagiosplugin.ScalarContext(
UptimeSimpleScalarContext(
name="uptime",
warning=float(warning) if warning else None,
critical=float(critical) if critical else None,
)
)

Expand Down
35 changes: 34 additions & 1 deletion routeros_check/helper.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# SPDX-FileCopyrightText: PhiBo DinoTools (2021)
# SPDX-License-Identifier: GPL-3.0-or-later

from datetime import timedelta
import importlib
import logging
import os
import re
from typing import List, Optional
from typing import List, Optional, Union

logger = logging.getLogger('nagiosplugin')

Expand Down Expand Up @@ -90,6 +91,38 @@ def escape_filename(value):
return re.sub(r"[-\s]+", '-', value)


def humanize_time(time: Union[float, int, timedelta]):
interval_mappings = {
1: {
"short": "s",
},
60: {
"short": "m",
},
3600: {
"short": "h",
},
86400: {
"short": "d",
}
}

if isinstance(time, timedelta):
time = time.total_seconds()

intervals: List[int] = sorted(interval_mappings.keys())
intervals.reverse()

results: List[str] = []
for interval in intervals:
v = int(time // interval)
if v or len(results):
results.append(f"{v}{interval_mappings[interval]['short']}")
time = time % interval

return " ".join(results)


def load_modules(pkg_names: Optional[List] = None):
if pkg_names is None:
pkg_names = [".check"]
Expand Down
22 changes: 22 additions & 0 deletions tests/helpers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ def test_compare_routeros_version_lt(self):
for item in cmp_items:
assert not helper.RouterOSVersion(item[0]) < helper.RouterOSVersion(item[1])

def test_humanize_time_with_int(self):
assert helper.humanize_time(10) == "10s"
assert helper.humanize_time(75) == "1m 15s"
assert helper.humanize_time(3601) == "1h 0m 1s"
assert helper.humanize_time(
60 * 60 * 24 +
60 * 60 * 1 +
60 * 13 +
44
) == "1d 1h 13m 44s"

def test_humanize_time_with_timedelta(self):
from datetime import timedelta
assert helper.humanize_time(
timedelta(
days=2,
hours=12,
minutes=33,
seconds=56
)
) == "2d 12h 33m 56s"

def test_parse_routeros_version(self):
a = helper.RouterOSVersion("7.8")
assert a.major == 7
Expand Down

0 comments on commit a6235c4

Please sign in to comment.