summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2024-03-26 23:36:41 (GMT)
committerGitHub <noreply@github.com>2024-03-26 23:36:41 (GMT)
commitd3faf13961c61b3ad69b47960124d3269c5f335e (patch)
treed626f2d7d234b194af8fe536dd5b2cf318a7adf6
parent66f62dd336832702a1e4f60cbfc4258de2c931cf (diff)
downloadmonitoring-plugins-d3faf13961c61b3ad69b47960124d3269c5f335e.tar.gz
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 :-(
-rw-r--r--plugins/check_disk.c8
-rw-r--r--plugins/t/check_disk.t8
-rw-r--r--plugins/utils.c31
-rw-r--r--plugins/utils.h1
4 files changed, 45 insertions, 3 deletions
diff --git a/plugins/check_disk.c b/plugins/check_disk.c
index 0d84ecd..24de2d4 100644
--- a/plugins/check_disk.c
+++ b/plugins/check_disk.c
@@ -550,6 +550,10 @@ process_arguments (int argc, char **argv)
550 550
551 /* See comments for 'c' */ 551 /* See comments for 'c' */
552 case 'w': /* warning threshold */ 552 case 'w': /* warning threshold */
553 if (!is_percentage_expression(optarg) && !is_numeric(optarg)) {
554 die(STATE_UNKNOWN, "Argument for --warning invalid or missing: %s\n", optarg);
555 }
556
553 if (strstr(optarg, "%")) { 557 if (strstr(optarg, "%")) {
554 if (*optarg == '@') { 558 if (*optarg == '@') {
555 warn_freespace_percent = optarg; 559 warn_freespace_percent = optarg;
@@ -571,6 +575,10 @@ process_arguments (int argc, char **argv)
571 force @ at the beginning of the range, so that it is backwards compatible 575 force @ at the beginning of the range, so that it is backwards compatible
572 */ 576 */
573 case 'c': /* critical threshold */ 577 case 'c': /* critical threshold */
578 if (!is_percentage_expression(optarg) && !is_numeric(optarg)) {
579 die(STATE_UNKNOWN, "Argument for --critical invalid or missing: %s\n", optarg);
580 }
581
574 if (strstr(optarg, "%")) { 582 if (strstr(optarg, "%")) {
575 if (*optarg == '@') { 583 if (*optarg == '@') {
576 crit_freespace_percent = optarg; 584 crit_freespace_percent = optarg;
diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t
index e0dd70e..9eb77ce 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");
119 119
120$result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free" ); 120$result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free" );
121cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free and $less_free"); 121cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free and $less_free");
122
122$_ = $result->output; 123$_ = $result->output;
124
123my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+)MiB .* (\d+)MiB /g); 125my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+)MiB .* (\d+)MiB /g);
126die "Cannot parse output: $_" unless ($free_mb_on_mp1 && $free_mb_on_mp2);
127
124my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2; 128my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2;
125 129
126 130
@@ -311,8 +315,8 @@ $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -C -w 0% -c 0% -p $mountpoi
311like( $result->output, '/;.*;\|/', "-C selects partitions if -p is not given"); 315like( $result->output, '/;.*;\|/', "-C selects partitions if -p is not given");
312 316
313# grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit 317# grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit
314$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" ); 318$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" );
315cmp_ok( $result->return_code, '==', 2, "grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit"); 319cmp_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);
316 320
317# grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c 321# grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c
318$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" ); 322$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 aff1790..77d6a6f 100644
--- a/plugins/utils.c
+++ b/plugins/utils.c
@@ -23,13 +23,15 @@
23*****************************************************************************/ 23*****************************************************************************/
24 24
25#include "common.h" 25#include "common.h"
26#include "utils.h" 26#include "./utils.h"
27#include "utils_base.h" 27#include "utils_base.h"
28#include <stdarg.h> 28#include <stdarg.h>
29#include <limits.h> 29#include <limits.h>
30#include <string.h> 30#include <string.h>
31#include <errno.h> 31#include <errno.h>
32 32
33#include <stdbool.h>
34
33#include <arpa/inet.h> 35#include <arpa/inet.h>
34 36
35extern void print_usage (void); 37extern void print_usage (void);
@@ -188,6 +190,33 @@ bool is_percentage (char *number) {
188 return false; 190 return false;
189} 191}
190 192
193bool is_percentage_expression (const char str[]) {
194 if (!str) {
195 return false;
196 }
197
198 size_t len = strlen(str);
199
200 if (str[len-1] != '%') {
201 return false;
202 }
203
204 char *foo = calloc(sizeof(char), len + 1);
205
206 if (!foo) {
207 die (STATE_UNKNOWN, _("calloc failed \n"));
208 }
209
210 strcpy(foo, str);
211 foo[len-1] = '\0';
212
213 bool result = is_numeric(foo);
214
215 free(foo);
216
217 return result;
218}
219
191bool is_integer (char *number) { 220bool is_integer (char *number) {
192 long int n; 221 long int n;
193 222
diff --git a/plugins/utils.h b/plugins/utils.h
index 62e489b..f939e33 100644
--- a/plugins/utils.h
+++ b/plugins/utils.h
@@ -49,6 +49,7 @@ bool is_positive (char *);
49bool is_negative (char *); 49bool is_negative (char *);
50bool is_nonnegative (char *); 50bool is_nonnegative (char *);
51bool is_percentage (char *); 51bool is_percentage (char *);
52bool is_percentage_expression (const char[]);
52 53
53bool is_option (char *); 54bool is_option (char *);
54 55