Skip to content

Commit

Permalink
file-size: Supports human-readable size qualifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
markuslf committed Mar 30, 2024
1 parent 0a6c581 commit e5341d2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
42 changes: 32 additions & 10 deletions check-plugins/file-size/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob>`_. 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 <https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob>`_. 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
Expand Down Expand Up @@ -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
Expand All @@ -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
---------------------------------------+---------+------------
Expand Down Expand Up @@ -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
------
Expand Down
31 changes: 22 additions & 9 deletions check-plugins/file-size/file-size
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down

0 comments on commit e5341d2

Please sign in to comment.