Linuxfabrik / monitoring-plugins

220+ check plugins for Icinga and other Nagios-compatible monitoring applications. Each plugin is a standalone command line tool (written in Python) that provides a specific type of check.
https://linuxfabrik.ch
The Unlicense
214 stars 49 forks source link

file-size: Added the range operator, range2human function and error handling #735

Closed djmcd89 closed 6 months ago

djmcd89 commented 8 months ago

Changed the operator to range. Changed the argument type to string for the warning and critical arguments. Added function range2human, which checks for a colon in the given arguments (warning and critical).
Then splits the numeric values from the colon, checks that they contain only digits and uses the lib.human.bytes2human function to translate to a human readable format. Added an error count and adapted the output to contain the number of files which are out of scope instead of listing all files in the directory. Edited the output to contain the accepted ranges if a range is specified.

Original output:

/usr/lib64/nagios/plugins$ ./file-size_original --filename '/tmp/test/*' -w 5999999 -c 100000000
7 files are bigger than the given size thresholds (5.7MiB/95.4MiB).

* /tmp/test/ostechnix.txt: 1.9MiB
* /tmp/test/ostechnix1.txt: 1.9MiB
* /tmp/test/ostechnix10.txt: 9.5MiB [WARNING]
* /tmp/test/ostechnix20.txt: 19.1MiB [WARNING]
* /tmp/test/ostechnix3.txt: 2.9MiB
* /tmp/test/ostechnix30.txt: 28.6MiB [WARNING]
* /tmp/test/ostechnix4.txt: 3.8MiB

Updated output with error count:

/usr/lib64/nagios/plugins$ ./file-size --filename '/tmp/test/*' -w 5999999 -c 100000000
3 files are above the given size thresholds (5.7MiB/ 95.4MiB).
* /tmp/test/ostechnix.txt: 1.9MiB
* /tmp/test/ostechnix1.txt: 1.9MiB
* /tmp/test/ostechnix10.txt: 9.5MiB [WARNING]
* /tmp/test/ostechnix20.txt: 19.1MiB [WARNING]
* /tmp/test/ostechnix3.txt: 2.9MiB
* /tmp/test/ostechnix30.txt: 28.6MiB [WARNING]
* /tmp/test/ostechnix4.txt: 3.8MiB```

Output with range specified:

/usr/lib64/nagios/plugins$ ./file-size --filename '/tmp/test/*' -w 5999999:100000000 -c 100000000:
7 files are outside the given threshold ranges of:
Warning   (Accepted Range): 5.7MiB - 95.4MiB.
Critical  (Accepted Range): > 95.4MiB.

* /tmp/test/ostechnix.txt: 1.9MiB [CRITICAL]
* /tmp/test/ostechnix1.txt: 1.9MiB [CRITICAL]
* /tmp/test/ostechnix10.txt: 9.5MiB [CRITICAL]
* /tmp/test/ostechnix20.txt: 19.1MiB [CRITICAL]
* /tmp/test/ostechnix3.txt: 2.9MiB [CRITICAL]
* /tmp/test/ostechnix30.txt: 28.6MiB [CRITICAL]
* /tmp/test/ostechnix4.txt: 3.8MiB [CRITICAL]
markuslf commented 8 months ago

Thank you for your pull request :+1:, I will have a look.

KriSchu commented 7 months ago

Hi @markuslf Can you estimate when this PR may be merged? This is a nice feature, which would be very helpful.

Thanks in advance.

markuslf commented 7 months ago

Next week, I'm currently on vacation.

markuslf commented 6 months ago

Improved the code afterwards (no need for your function range2human(value), since the Nagios range support is built into the Linuxfabrik libraries). I improved the output as well.