Skip to content

Commit

Permalink
dhcp-scope-usage: Ignore PercentageInUse fractions
Browse files Browse the repository at this point in the history
  • Loading branch information
markuslf committed Sep 10, 2024
1 parent cd6003d commit 413dd07
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Monitoring Plugins:
* about-me: Determines date of birth of cloud VMs more accurately
* about-me: Add Mastodon detection
* about-me: Add Moodle detection
* dhcp-scope-usage: Ignore PercentageInUse fractions
* disk-io: Re-add support for Windows after last rewrite
* librenms-alerts, librenms-health: Compact output is the new default and shows non-OK only
* nextcloud-security-scan: Handle error on https://scan.nextcloud.com/
Expand Down
8 changes: 7 additions & 1 deletion check-plugins/dhcp-scope-usage/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ Remote usage, for example on a Linux server:

.. code-block:: bash
./dhcp-scope-usage3 --hostname=dhcp01.example.com --winrm-hostname=10.80.32.246 --winrm-username=Administrator --winrm-password=password --winrm-domain=EXAMPLE.COM --winrm-transport=ntlm
./dhcp-scope-usage3 \
--hostname=dhcp01.example.com \
--winrm-hostname=10.80.32.246 \
--winrm-username=Administrator \
--winrm-password=linuxfabrik \
--winrm-domain=EXAMPLE.COM \
--winrm-transport=ntlm
Output:

Expand Down
15 changes: 6 additions & 9 deletions check-plugins/dhcp-scope-usage/dhcp-scope-usage
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""

import argparse # pylint: disable=C0413
import locale # pylint: disable=C0413
import re # pylint: disable=C0413
import sys # pylint: disable=C0413

import lib.args # pylint: disable=C0413
Expand All @@ -24,7 +24,7 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
STATE_UNKNOWN, STATE_WARN)

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2023112901'
__version__ = '2024091001'

DESCRIPTION = """Checks the IPv4 scope usage for a Windows DHCP server service."""

Expand Down Expand Up @@ -172,14 +172,11 @@ def main():

scope = line.split()
scope_id = scope[0]

# "PercentageInUse" is a floating point number and uses the user's preferred locale
# number format, so this needs to be parsed locale-aware

# use user's preferred locale
locale.setlocale(locale.LC_ALL, '')
# convert the string to a floating point number, following the LC_NUMERIC settings
scope_used = locale.atof(scope[3])
# number format. we don't care about the fraction part, separated depending on
# the user's locale by `.`, `,` or whatever - we simply cut it off.
scope_used = re.split(r'\D+', scope[3])[0]

scope_state = lib.base.get_state(scope_used, args.WARN, args.CRIT)
state = lib.base.get_worst(scope_state, state)
Expand Down
12 changes: 9 additions & 3 deletions check-plugins/dhcp-scope-usage/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class TestCheck(unittest.TestCase):
def test_if_check_runs_EXAMPLE01(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --test=stdout/EXAMPLE01,,0'))
self.assertIn('There are one or more criticals.', stdout)
self.assertIn('* 192.0.2.120: 0.0% used', stdout)
self.assertIn('* 192.0.2.121: 83.0% used [WARNING]', stdout)
self.assertIn('* 192.0.2.122: 91.0% used [CRITICAL]', stdout)
self.assertIn('* 192.0.2.120: 0% used', stdout)
self.assertIn('* 192.0.2.121: 83% used [WARNING]', stdout)
self.assertIn('* 192.0.2.122: 91% used [CRITICAL]', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_CRIT)

Expand All @@ -43,6 +43,12 @@ class TestCheck(unittest.TestCase):
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_OK)

def test_if_check_runs_EXAMPLE03(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --test=stdout/EXAMPLE03,,0'))
self.assertIn('Everything is ok.', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_OK)


if __name__ == '__main__':
unittest.main()
12 changes: 12 additions & 0 deletions check-plugins/dhcp-scope-usage/unit-test/stdout/EXAMPLE03
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ScopeId Free InUse PercentageInUse Reserved Pending SuperscopeName
------- ---- ----- --------------- -------- ------- --------------
192.168.20.0 40 11 21,56863 6 0 Servernetz
192.168.30.0 38 33 46,47887 12 0 Managementnetz
192.168.40.0 176 77 30,43478 2 0 VoIP
192.168.10.0 217 33 13,2 6 0 Hospiz
192.168.15.0 198 2 1 2 0 Buchhaltung
192.168.20.0 151 49 24,5 10 0 Verwaltungsnetz
192.168.30.0 200 0 0 0 0 WLAN
192.168.70.0 242 8 3,2 2 0
192.168.80.0 249 1 0,4 1 0
192.168.90.0 233 17 6,8 5 0

0 comments on commit 413dd07

Please sign in to comment.