Linuxfabrik / monitoring-plugins

200+ 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
207 stars 48 forks source link

Improve file-size plugin #734

Closed djmcd89 closed 6 months ago

djmcd89 commented 6 months ago

The requested use case is that the file size of files within a given directory should not subceed a specified threshold.

Updated file-size plugin with an option to check for file sizes below the given threshold. Added an optional --check-direction argument, which determines whether the file-size should be smaller or larger than the given threshold. Values that can be used are "smaller" and "larger", larger remains the default value if none is set.

Additionally added an error count and edited the output to include the number of files above/below the threshold instead of the number of all files contained in the directory.

/usr/lib64/nagios/plugins/file-size$ ./file-size_original --filename '/tmp/test/*' -w 10000000 -c 100000000
7 files are bigger than the given size thresholds (9.5MiB/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
/usr/lib64/nagios/plugins/file-size$ ./file-size_updated --filename '/tmp/test/*' -w 10000000 -c 100000000
3 files are above the given size thresholds (9.5MiB/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
/usr/lib64/nagios/plugins/file-size$ ./file-size_updated --filename '/tmp/test/*' --check-direction smaller -w 10000000 -c 100000000
7 files are below the given size thresholds (9.5MiB/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
slalomsk8er commented 6 months ago

Why not use a the standard range thresholds as default? Why translate --check-direction to check_operator of get_state and not expose check_operator directly as a --check-operator argument?

djmcd89 commented 6 months ago

Thanks for the suggestion, I hadn't seen the range option since the specific use case was for a check regarding smaller than threshold. Definitely a good idea, I'll take a look at it again and come back with another PR if I manage to get anywhere. :-) The --check-direction translation was actually an effort to make it a little more human readable and user friendly.