From 117cd8e4b826e471e795536228628d817df33f5a Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Fri, 23 Feb 2024 18:24:28 +0100 Subject: check_disk increase alert precision (#1989) * check_disk increase alert precision Free disk percentage value was rounded to a full integer meaning it alerted about ~1% percent too early. This is about 10GB on a 1TB disk. The warning and critical thresholds already support float values, so just the percentage calculation needs to be improved. old: ./check_disk -w 35% -c 20% -p / -f -vvv Thresholds(pct) for / warn: 35.000000 crit 20.000000 calling stat on / For /, used_pct=65 free_pct=35 used_units=286451 free_units=156651 total_units=443102 used_inodes_pct=11 free_inodes_pct=89 fsp.fsu_blocksize=4096 mult=1048576 Freespace_units result=0 Freespace% result=1 Usedspace_units result=0 Usedspace_percent result=0 Usedinodes_percent result=0 Freeinodes_percent result=0 DISK WARNING - free space: WARNING [ / 156651MiB (35% inode=89%)];| /=300365643776B;302006979788;371700898201;0;464626122752 new: ./check_disk -w 35% -c 20% -p / -f -vvv Thresholds(pct) for / warn: 35.000000 crit 20.000000 calling stat on / For /, used_pct=64.649722 free_pct=35.350278 used_units=286464 free_units=156637 total_units=443102 used_inodes_pct=10.016183 free_inodes_pct=89.983817 fsp.fsu_blocksize=4096 mult=1048576 Freespace_units result=0 Freespace% result=0 Usedspace_units result=0 Usedspace_percent result=0 Usedinodes_percent result=0 Freeinodes_percent result=0 DISK OK - free space: / 156637MiB (35.4% inode=90%);| /=300379275264B;302006979788;371700898201;0;464626122752 * check_disk: adjust test case to support float precision --- plugins/t/check_disk.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/t/check_disk.t') diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t index bf8dd362..e0dd70eb 100644 --- a/plugins/t/check_disk.t +++ b/plugins/t/check_disk.t @@ -44,7 +44,7 @@ my @perf_data = sort(split(/ /, $result->perf_output)); # Calculate avg_free free on mountpoint1 and mountpoint2 # because if you check in the middle, you should get different errors $_ = $result->output; -my ($free_on_mp1, $free_on_mp2) = (m/\((\d+)%.*\((\d+)%/); +my ($free_on_mp1, $free_on_mp2) = (m/\((\d+\.\d+)%.*\((\d+\.\d+)%/); die "Cannot parse output: $_" unless ($free_on_mp1 && $free_on_mp2); my $avg_free = ceil(($free_on_mp1+$free_on_mp2)/2); my ($more_free, $less_free); -- cgit v1.2.3-74-g34f1 From d3faf13961c61b3ad69b47960124d3269c5f335e Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 27 Mar 2024 00:36:41 +0100 Subject: check_disk: Fail on missing arguments for --warning and --critical and fix a test case (#1935) * check_disk: Fail on missing arguments for --warning and --critical * Add new test function for percentage expressions and use it in check_disk * Add error abort in tests if they fail to parse output * Fix typo in test which probably broke the test since forever :-( --- plugins/check_disk.c | 8 ++++++++ plugins/t/check_disk.t | 8 ++++++-- plugins/utils.c | 31 ++++++++++++++++++++++++++++++- plugins/utils.h | 1 + 4 files changed, 45 insertions(+), 3 deletions(-) (limited to 'plugins/t/check_disk.t') diff --git a/plugins/check_disk.c b/plugins/check_disk.c index 0d84ecd2..24de2d45 100644 --- a/plugins/check_disk.c +++ b/plugins/check_disk.c @@ -550,6 +550,10 @@ process_arguments (int argc, char **argv) /* See comments for 'c' */ case 'w': /* warning threshold */ + if (!is_percentage_expression(optarg) && !is_numeric(optarg)) { + die(STATE_UNKNOWN, "Argument for --warning invalid or missing: %s\n", optarg); + } + if (strstr(optarg, "%")) { if (*optarg == '@') { warn_freespace_percent = optarg; @@ -571,6 +575,10 @@ process_arguments (int argc, char **argv) force @ at the beginning of the range, so that it is backwards compatible */ case 'c': /* critical threshold */ + if (!is_percentage_expression(optarg) && !is_numeric(optarg)) { + die(STATE_UNKNOWN, "Argument for --critical invalid or missing: %s\n", optarg); + } + if (strstr(optarg, "%")) { if (*optarg == '@') { crit_freespace_percent = optarg; diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t index e0dd70eb..9eb77ce4 100644 --- a/plugins/t/check_disk.t +++ b/plugins/t/check_disk.t @@ -119,8 +119,12 @@ like ( $result->only_output, qr/$more_free/, "Have disk name in text"); $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free" ); cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free and $less_free"); + $_ = $result->output; + my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+)MiB .* (\d+)MiB /g); +die "Cannot parse output: $_" unless ($free_mb_on_mp1 && $free_mb_on_mp2); + my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2; @@ -311,8 +315,8 @@ $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -C -w 0% -c 0% -p $mountpoi like( $result->output, '/;.*;\|/', "-C selects partitions if -p is not given"); # grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit -$result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all + 1) ." -c ". ($free_mb_on_all + 1) ."-g group -p $mountpoint_valid -p $mountpoint2_valid" ); -cmp_ok( $result->return_code, '==', 2, "grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit"); +$result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all + 1) ." -c ". ($free_mb_on_all + 1) ." -g group -p $mountpoint_valid -p $mountpoint2_valid" ); +cmp_ok( $result->return_code, '==', 2, "grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit\nInstead received: " . $result->output); # grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c $result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all + 1) ." -c ". ($free_mb_on_all - 1) ." -g group -p $mountpoint_valid -p $mountpoint2_valid" ); diff --git a/plugins/utils.c b/plugins/utils.c index aff17906..77d6a6f9 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -23,13 +23,15 @@ *****************************************************************************/ #include "common.h" -#include "utils.h" +#include "./utils.h" #include "utils_base.h" #include #include #include #include +#include + #include extern void print_usage (void); @@ -188,6 +190,33 @@ bool is_percentage (char *number) { return false; } +bool is_percentage_expression (const char str[]) { + if (!str) { + return false; + } + + size_t len = strlen(str); + + if (str[len-1] != '%') { + return false; + } + + char *foo = calloc(sizeof(char), len + 1); + + if (!foo) { + die (STATE_UNKNOWN, _("calloc failed \n")); + } + + strcpy(foo, str); + foo[len-1] = '\0'; + + bool result = is_numeric(foo); + + free(foo); + + return result; +} + bool is_integer (char *number) { long int n; diff --git a/plugins/utils.h b/plugins/utils.h index 62e489be..f939e337 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -49,6 +49,7 @@ bool is_positive (char *); bool is_negative (char *); bool is_nonnegative (char *); bool is_percentage (char *); +bool is_percentage_expression (const char[]); bool is_option (char *); -- cgit v1.2.3-74-g34f1