From e5341d2b87db92fcd065ca2fe81657303536e9c5 Mon Sep 17 00:00:00 2001 From: Markus Frei Date: Sat, 30 Mar 2024 17:52:43 +0100 Subject: [PATCH] file-size: Supports human-readable size qualifiers --- CHANGELOG.md | 1 + check-plugins/file-size/README.rst | 42 ++++++++++++++----- check-plugins/file-size/file-size | 31 ++++++++++---- .../icingaweb2-module-director/file-size.json | 8 ++-- 4 files changed, 59 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05fc2a4b..0fe8c9b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ Icinga Director: Monitoring Plugins: +* file-size: Note that the plugin now requires a size qualifier when specifying parameters, e.g. ``--warning=10K`` for 10 KiB (instead of ``--warning=10000`` as in previous versions). * librenms-alerts: Rewritten from scratch to fetch from LibreNMS MySQL/MariaDB database (therefore the check comes with new parameters) * librenms-health: Rewritten from scratch to fetch from LibreNMS MySQL/MariaDB database (therefore the check comes with new parameters) * uptime: Use the plugin to warn about recent reboots ([#722](https://github.com/Linuxfabrik/monitoring-plugins/issues/722)). Note that the plugin now requires a time qualifier when specifying parameters, e.g. ``--warning=180D`` for 180 days (instead of ``--warning=180`` as in previous versions). diff --git a/check-plugins/file-size/README.rst b/check-plugins/file-size/README.rst index 68e87873..4eb34eda 100644 --- a/check-plugins/file-size/README.rst +++ b/check-plugins/file-size/README.rst @@ -6,7 +6,7 @@ Overview Checks the size of files in bytes, ignoring directories as the size of a directory is not defined consistently across file systems and is never the size of the contents. This check supports both Nagios ranges and Samba shares. -The plugin can follow symbolic links. Depending on the file and user (e.g. running as *icinga*), sudo (sudoers) may be required. It supports globs according to `Python 3 `_. Note that using recursive globs can cause high memory usage. +The plugin can follow symbolic links. Depending on the file and user (e.g. running as *icinga*), sudo (sudoers) may be required. It supports globs according to `Python 3 `_. Note that using recursive globs can cause high memory usage. Also note that the plugin requires a valid qualifier when specifying parameters, e.g. ``--warning=10K`` for 10 KiB (instead of ``--warning=10000`` as in previous versions). Fact Sheet @@ -38,8 +38,11 @@ Help -V, --version show program's version number and exit --always-ok Always returns OK. -c CRIT, --critical CRIT - Threshold for the file size in Bytes. Supports Nagios - ranges. Default: 1073741824 + Threshold for the file size in a human readable format + (base is always 1024; valid qualifiers are b, + k/kb/kib, m/mb/mib, g/gb/gib etc.). Supports Nagios + ranges. Example: `:1G` alerts if size is greater than + 1 GiB.Default: 1G --filename FILENAME File (or directory) name to check. Supports glob in accordance with https://docs.python.org/2.7/library/glob.html. Note @@ -57,26 +60,39 @@ Help with `--filename`. --username USERNAME SMB Username. -w WARN, --warning WARN - Threshold for the file size in Bytes. Supports Nagios - ranges. Default: 26214400 + Threshold for the file size in a human readable format + (base is always 1024; valid qualifiers are b, + k/kb/kib, m/mb/mib, g/gb/gib etc.). Supports Nagios + ranges. Example: `:1G` alerts if size is greater than + 1 GiB.Default: 25M Usage Examples -------------- +Warn if file is greater than 25M, crit if it is greater than 1G: + .. code-block:: bash - # warn if files are not within 6 to 10 KB, crit if files are larger than 14 KB - ./file-size --filename '/path/to/m*.png*' --warning 6000:10000 --critical :14000 + ./file-size --filename=/var/log/coolwsd.log --warning=25M --critical=1G + +Output: - # the same as above, but recursive (might use a lot of memory) - ./file-size --filename '/path/to/**/m*.png*' --warning 6000:10000 --critical :14000 +.. code-block:: text + + 1 file checked. It is within the given size thresholds (25M/1G). Checked /var/log/coolwsd.log: 119.9KiB + +Warn if files are not within 6 to 10 KB, crit if files are larger than 14 KB (plus showing the various formats): + +.. code-block:: bash + + ./file-size --filename '/path/to/m*.png*' --warning '6 KiB:10k' --critical ':14 KB' Output: .. code-block:: text - 28 files checked. 22 are outside the given size thresholds (6000:10000/:14000). + 28 files checked. 21 are outside the given size thresholds (6 KiB:10k/:14 KB). File ! Size ! State ---------------------------------------+---------+------------ @@ -109,6 +125,12 @@ Output: mysql-user-security.png ! 16.3KiB ! [CRITICAL] mysql-version.png ! 10.3KiB ! [WARNING] +The same as above, but recursive (might use a lot of memory): + +.. code-block:: bash + + ./file-size --filename '/path/to/**/m*.png*' --warning 6000B:10K --critical :14KB + States ------ diff --git a/check-plugins/file-size/file-size b/check-plugins/file-size/file-size index f34f637b..1878c5bb 100755 --- a/check-plugins/file-size/file-size +++ b/check-plugins/file-size/file-size @@ -32,15 +32,15 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413 STATE_UNKNOWN, STATE_WARN) __author__ = 'Linuxfabrik GmbH, Zurich/Switzerland' -__version__ = '2024033002' +__version__ = '2024033003' DESCRIPTION = 'Checks the size for a file (in bytes).' -DEFAULT_CRIT = str(1024*1024*1024) # 1G (Gibibyte) +DEFAULT_CRIT = '1G' DEFAULT_PATTERN = '*' DEFAULT_TIMEOUT = 3 -DEFAULT_WARN = str(25*1024*1024) # 25M (Mebibyte) +DEFAULT_WARN = '25M' def parse_args(): @@ -64,7 +64,11 @@ def parse_args(): parser.add_argument( '-c', '--critical', - help='Threshold for the file size in Bytes. Supports Nagios ranges. ' + help='Threshold for the file size in a human readable format ' + '(base is always 1024; valid qualifiers are ' + 'b, k/kb/kib, m/mb/mib, g/gb/gib etc.). ' + 'Supports Nagios ranges. ' + 'Example: `:1G` alerts if size is greater than 1 GiB.' 'Default: %(default)s', dest='CRIT', default=DEFAULT_CRIT, @@ -120,7 +124,11 @@ def parse_args(): parser.add_argument( '-w', '--warning', - help='Threshold for the file size in Bytes. Supports Nagios ranges. ' + help='Threshold for the file size in a human readable format ' + '(base is always 1024; valid qualifiers are ' + 'b, k/kb/kib, m/mb/mib, g/gb/gib etc.). ' + 'Supports Nagios ranges. ' + 'Example: `:1G` alerts if size is greater than 1 GiB.' 'Default: %(default)s', dest='WARN', default=DEFAULT_WARN, @@ -148,6 +156,11 @@ def main(): table_data = [] alert_count = 0 + # convert human readable nagios ranges to something that the Linuxfabrik libraries + # can understand + CRIT = lib.human.humanrange2bytes(args.CRIT) + WARN = lib.human.humanrange2bytes(args.WARN) + # fetch data from local if args.FILENAME: for item in sorted(glob.iglob(args.FILENAME)): @@ -190,8 +203,8 @@ def main(): table_data[i]['size_hr'] = lib.human.bytes2human(table_data[i]['size']) table_data[i]['state'] = lib.base.get_state( item['size'], - args.WARN, - args.CRIT, + WARN, + CRIT, _operator='range', ) table_data[i]['state_hr'] = lib.base.state2str(table_data[i]['state'], empty_ok=False) @@ -205,13 +218,13 @@ def main(): lib.txt.pluralize('file', len(table_data)), ) if state == STATE_OK: - msg += '{} within the given size thresholds ({}/{} Bytes).'.format( + msg += '{} within the given size thresholds ({}/{}).'.format( lib.txt.pluralize('', len(table_data), 'It is,All are'), args.WARN, args.CRIT, ) else: - msg += '{} {} outside the given size thresholds ({}/{} Bytes).'.format( + msg += '{} {} outside the given size thresholds ({}/{}).'.format( alert_count, lib.txt.pluralize('', alert_count, 'is,are'), args.WARN, diff --git a/check-plugins/file-size/icingaweb2-module-director/file-size.json b/check-plugins/file-size/icingaweb2-module-director/file-size.json index 1512d272..035f6a7d 100644 --- a/check-plugins/file-size/icingaweb2-module-director/file-size.json +++ b/check-plugins/file-size/icingaweb2-module-director/file-size.json @@ -149,10 +149,10 @@ "vars": { "criticality": "C", "file_size_always_ok": false, - "file_size_critical": "1073741824", + "file_size_critical": "1G", "file_size_pattern": "*", "file_size_timeout": 3, - "file_size_warning": "26214400" + "file_size_warning": "25M" }, "volatile": null, "zone": null, @@ -217,7 +217,7 @@ "2": { "varname": "file_size_critical", "caption": "File Size: Critical", - "description": "Threshold for the file size in Bytes. Supports Nagios ranges.", + "description": "Threshold for the file size in a human readable format (base is always 1024; valid qualifiers are b, k/kb/kib, m/mb/mib, g/gb/gib etc.). Supports Nagios ranges. Example: `:1G` alerts if size is greater than 1 GiB.Default: %(default)s", "datatype": "Icinga\\Module\\Director\\DataType\\DataTypeString", "format": null, "settings": { @@ -294,7 +294,7 @@ "9": { "varname": "file_size_warning", "caption": "File Size: Warning", - "description": "Threshold for the file size in Bytes. Supports Nagios ranges.", + "description": "Threshold for the file size in a human readable format (base is always 1024; valid qualifiers are b, k/kb/kib, m/mb/mib, g/gb/gib etc.). Supports Nagios ranges. Example: `:1G` alerts if size is greater than 1 GiB.Default: %(default)s", "datatype": "Icinga\\Module\\Director\\DataType\\DataTypeString", "format": null, "settings": {