From 8cf31437e99167ad9c260e6677b4d1ed31a34d56 Mon Sep 17 00:00:00 2001 From: Kristian Schuster <116557017+KriSchu@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:29:53 +0200 Subject: check_disk: add ignore-missing option to return OK for missing fs There a situations where UNKNOWN or CRITICAL services are not wanted when a filesystem is missing, a regex does not match or the filesystem is inaccessible on a system. This new option helps to have the service in state OK. --- plugins/check_disk.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'plugins') diff --git a/plugins/check_disk.c b/plugins/check_disk.c index 7018c6fd..8df9e7ec 100644 --- a/plugins/check_disk.c +++ b/plugins/check_disk.c @@ -112,7 +112,8 @@ enum { SYNC_OPTION = CHAR_MAX + 1, NO_SYNC_OPTION, - BLOCK_SIZE_OPTION + BLOCK_SIZE_OPTION, + IGNORE_MISSING }; #ifdef _AIX @@ -140,6 +141,7 @@ int verbose = 0; int erronly = FALSE; int display_mntp = FALSE; int exact_match = FALSE; +int ignore_missing = FALSE; int freespace_ignore_reserved = FALSE; int display_inodes_perfdata = FALSE; char *warn_freespace_units = NULL; @@ -219,7 +221,9 @@ main (int argc, char **argv) temp_list = path_select_list; while (temp_list) { - if (! temp_list->best_match) { + if (! temp_list->best_match && ignore_missing == 1) { + die (STATE_OK, _("DISK %s: %s not found (ignoring)\n"), _("OK"), temp_list->name); + } else if (! temp_list->best_match) { die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name); } @@ -481,6 +485,7 @@ process_arguments (int argc, char **argv) {"ignore-ereg-partition", required_argument, 0, 'i'}, {"ignore-eregi-path", required_argument, 0, 'I'}, {"ignore-eregi-partition", required_argument, 0, 'I'}, + {"ignore-missing", no_argument, 0, IGNORE_MISSING}, {"local", no_argument, 0, 'l'}, {"stat-remote-fs", no_argument, 0, 'L'}, {"iperfdata", no_argument, 0, 'P'}, @@ -718,6 +723,9 @@ process_arguments (int argc, char **argv) cflags = default_cflags; break; + case IGNORE_MISSING: + ignore_missing = 1; + break; case 'A': optarg = strdup(".*"); // Intentional fallthrough @@ -753,7 +761,10 @@ process_arguments (int argc, char **argv) } } - if (!fnd) + if (!fnd && ignore_missing == 1) + die (STATE_OK, "DISK %s: %s - %s\n",_("OK"), + _("Regular expression did not match any path or disk (ignoring)"), optarg); + else if (!fnd) die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), _("Regular expression did not match any path or disk"), optarg); @@ -923,6 +934,9 @@ print_help (void) printf (" %s\n", _("Regular expression to ignore selected path/partition (case insensitive) (may be repeated)")); printf (" %s\n", "-i, --ignore-ereg-path=PATH, --ignore-ereg-partition=PARTITION"); printf (" %s\n", _("Regular expression to ignore selected path or partition (may be repeated)")); + printf (" %s\n", "--ignore-missing"); + printf (" %s\n", _("Return OK if no filesystem matches, filesystem does not exist or is inaccessible.")); + printf (" %s\n", _("(Provide this option before -r / --ereg-path if used)")); printf (UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); printf (" %s\n", "-u, --units=STRING"); printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)")); @@ -965,8 +979,13 @@ stat_path (struct parameter_list *p) if (stat (p->name, &stat_buf[0])) { if (verbose >= 3) printf("stat failed on %s\n", p->name); - printf("DISK %s - ", _("CRITICAL")); - die (STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno)); + if (ignore_missing == 1) { + printf("DISK %s - ", _("OK")); + die (STATE_OK, _("%s %s: %s\n"), p->name, _("is not accessible (ignoring)"), strerror(errno)); + } else { + printf("DISK %s - ", _("CRITICAL")); + die (STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno)); + } } } -- cgit v1.2.3-74-g34f1 From 0d562a356f45f645014c3908178fc13876006f6e Mon Sep 17 00:00:00 2001 From: Kristian Schuster <116557017+KriSchu@users.noreply.github.com> Date: Tue, 25 Oct 2022 20:49:51 +0200 Subject: check_disk: add tests for new option --ignore-missing --- plugins/t/check_disk.t | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'plugins') diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t index ec527e7f..bea34a4c 100644 --- a/plugins/t/check_disk.t +++ b/plugins/t/check_disk.t @@ -351,3 +351,18 @@ unlike( $result->output, qr/$mountpoint2_valid/, "output data does not have $mou $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^barbazJodsf\$'"); like( $result->output, qr/$mountpoint_valid/, "ignore: output data does have $mountpoint_valid when regex doesn't match"); like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mountpoint2_valid when regex doesn't match"); + +# ignore-missing: exit okay, when fs is not accessible +$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p /bob"); +cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for not existing filesystem /bob"); +like( $result->output, '/^DISK OK - /bob is not accessible .*$/', 'Output OK'); + +# ignore-missing: exit okay, when regex does not match +$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r /bob"); +cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); +like( $result->output, '/^DISK OK: Regular expression did not match any path or disk.*$/', 'Output OK'); + +# ignore-missing: exit okay, when fs with exact match (-E) is not found +$result = NPTest->testCmd( "./check_disk --ignore-missing -E -w 0% -c 0% -p /etc"); +cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay when exact match does not find fs"); +like( $result->output, '/^DISK OK: /etc not found.*$/', 'Output OK'); -- cgit v1.2.3-74-g34f1 From bacacd2cb38c7d7a695a6f75f699168d9df0132d Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Wed, 26 Oct 2022 14:03:22 +0200 Subject: check_disk: adjust test plan --- plugins/t/check_disk.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t index bea34a4c..a534fd4a 100644 --- a/plugins/t/check_disk.t +++ b/plugins/t/check_disk.t @@ -23,7 +23,7 @@ my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to anoth if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") { plan skip_all => "Need 2 mountpoints to test"; } else { - plan tests => 78; + plan tests => 84; } $result = NPTest->testCmd( -- cgit v1.2.3-74-g34f1 From ba78c32018658608a31c293beef89ec82b9ba9d3 Mon Sep 17 00:00:00 2001 From: Kristian Schuster <116557017+KriSchu@users.noreply.github.com> Date: Sun, 19 Feb 2023 22:49:30 +0100 Subject: check_disk: still allow check of available disks with ignore-missing param used Also add reporting of ignored paths. When paths are provided by -p and/ or -r and one path does not match a mounted disk, checking available disks is still possible. Paths provided by -p are reported as ignored, when not available. Due to code structure, this is not possible for -r unfortunately. --- plugins/check_disk.c | 103 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 78 insertions(+), 25 deletions(-) (limited to 'plugins') diff --git a/plugins/check_disk.c b/plugins/check_disk.c index 8df9e7ec..c1cfb13c 100644 --- a/plugins/check_disk.c +++ b/plugins/check_disk.c @@ -117,7 +117,7 @@ enum }; #ifdef _AIX - #pragma alloca +#pragma alloca #endif int process_arguments (int, char **); @@ -127,7 +127,7 @@ int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, ch void print_help (void); void print_usage (void); double calculate_percent(uintmax_t, uintmax_t); -void stat_path (struct parameter_list *p); +bool stat_path (struct parameter_list *p); void get_stats (struct parameter_list *p, struct fs_usage *fsp); void get_path_stats (struct parameter_list *p, struct fs_usage *fsp); @@ -157,6 +157,7 @@ char *crit_usedinodes_percent = NULL; char *warn_freeinodes_percent = NULL; char *crit_freeinodes_percent = NULL; int path_selected = FALSE; +int path_ignored = FALSE; char *group = NULL; struct stat *stat_buf; struct name_list *seen = NULL; @@ -168,10 +169,12 @@ main (int argc, char **argv) int result = STATE_UNKNOWN; int disk_result = STATE_UNKNOWN; char *output; + char *ignored; char *details; char *perf; char *perf_ilabel; char *preamble; + char *ignored_preamble; char *flag_header; int temp_result; @@ -183,8 +186,10 @@ main (int argc, char **argv) char mountdir[32]; #endif - preamble = strdup (" - free space:"); + preamble = strdup (" free space:"); + ignored_preamble = strdup (" ignored paths:"); output = strdup (""); + ignored = strdup (""); details = strdup (""); perf = strdup (""); perf_ilabel = strdup (""); @@ -205,7 +210,7 @@ main (int argc, char **argv) /* If a list of paths has not been selected, find entire mount list and create list of paths */ - if (path_selected == FALSE) { + if (path_selected == FALSE && path_ignored == FALSE) { for (me = mount_list; me; me = me->me_next) { if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) { path = np_add_parameter(&path_select_list, me->me_mountdir); @@ -215,19 +220,40 @@ main (int argc, char **argv) set_all_thresholds(path); } } - np_set_best_match(path_select_list, mount_list, exact_match); + + if (path_ignored == FALSE) { + np_set_best_match(path_select_list, mount_list, exact_match); + } /* Error if no match found for specified paths */ temp_list = path_select_list; - while (temp_list) { - if (! temp_list->best_match && ignore_missing == 1) { - die (STATE_OK, _("DISK %s: %s not found (ignoring)\n"), _("OK"), temp_list->name); - } else if (! temp_list->best_match) { - die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name); + while (path_select_list) { + if (! path_select_list->best_match && ignore_missing == 1) { + /* If the first element will be deleted, the temp_list must be updated with the new start address as well */ + if (path_select_list == temp_list) { + temp_list = path_select_list->name_next; + } + /* Add path argument to list of ignored paths to inform about missing paths being ignored and not alerted */ + xasprintf (&ignored, "%s %s;", ignored, path_select_list->name); + /* Delete the path from the list so that it is not stat-checked later in the code. */ + path_select_list = np_del_parameter(path_select_list, path_select_list->name_prev); + } else if (! path_select_list->best_match) { + /* Without --ignore-missing option, exit with Critical state. */ + die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), path_select_list->name); + } else { + /* Continue jumping through the list */ + path_select_list = path_select_list->name_next; } + } + + path_select_list = temp_list; - temp_list = temp_list->name_next; + if (! path_select_list && ignore_missing == 1) { + result = STATE_OK; + if (verbose >= 2) { + printf ("None of the provided paths were found\n"); + } } /* Process for every path in list */ @@ -246,6 +272,10 @@ main (int argc, char **argv) me = path->best_match; + if (!me) { + continue; + } + #ifdef __CYGWIN__ if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11) continue; @@ -264,8 +294,12 @@ main (int argc, char **argv) if (path->group == NULL) { /* Skip remote filesystems if we're not interested in them */ if (me->me_remote && show_local_fs) { - if (stat_remote_fs) - stat_path(path); + if (stat_remote_fs) { + if (!stat_path(path) && ignore_missing == 1) { + result = STATE_OK; + xasprintf (&ignored, "%s %s;", ignored, path->name); + } + } continue; /* Skip pseudo fs's if we haven't asked for all fs's */ } else if (me->me_dummy && !show_all_fs) { @@ -284,7 +318,13 @@ main (int argc, char **argv) } } - stat_path(path); + if (!stat_path(path)) { + if (ignore_missing == 1) { + result = STATE_OK; + xasprintf (&ignored, "%s %s;", ignored, path->name); + } + continue; + } get_fs_usage (me->me_mountdir, me->me_devname, &fsp); if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { @@ -415,8 +455,12 @@ main (int argc, char **argv) if (verbose >= 2) xasprintf (&output, "%s%s", output, details); + if (strcmp(output, "") == 0) { + preamble = ""; + xasprintf (&output, " No disks were found for provided parameters;"); + } - printf ("DISK %s%s%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf); + printf ("DISK %s -%s%s%s%s|%s\n", state_text (result), ((erronly && result==STATE_OK)) ? "" : preamble, output, (strcmp(ignored, "") == 0) ? "" : ignored_preamble, ignored, perf); return result; } @@ -637,12 +681,19 @@ process_arguments (int argc, char **argv) /* add parameter if not found. overwrite thresholds if path has already been added */ if (! (se = np_find_parameter(path_select_list, optarg))) { se = np_add_parameter(&path_select_list, optarg); + + if (stat(optarg, &stat_buf[0]) && ignore_missing == 1) { + path_ignored = TRUE; + break; + } } se->group = group; set_all_thresholds(se); /* With autofs, it is required to stat() the path before re-populating the mount_list */ - stat_path(se); + if (!stat_path(se)) { + break; + } /* NB: We can't free the old mount_list "just like that": both list pointers and struct * pointers are copied around. One of the reason it wasn't done yet is that other parts * of check_disk need the same kind of cleanup so it'd better be done as a whole */ @@ -761,10 +812,11 @@ process_arguments (int argc, char **argv) } } - if (!fnd && ignore_missing == 1) - die (STATE_OK, "DISK %s: %s - %s\n",_("OK"), - _("Regular expression did not match any path or disk (ignoring)"), optarg); - else if (!fnd) + if (!fnd && ignore_missing == 1) { + path_ignored = TRUE; + /* path_selected = TRUE;*/ + break; + } else if (!fnd) die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), _("Regular expression did not match any path or disk"), optarg); @@ -936,7 +988,7 @@ print_help (void) printf (" %s\n", _("Regular expression to ignore selected path or partition (may be repeated)")); printf (" %s\n", "--ignore-missing"); printf (" %s\n", _("Return OK if no filesystem matches, filesystem does not exist or is inaccessible.")); - printf (" %s\n", _("(Provide this option before -r / --ereg-path if used)")); + printf (" %s\n", _("(Provide this option before -p / -r / --ereg-path if used)")); printf (UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); printf (" %s\n", "-u, --units=STRING"); printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)")); @@ -970,7 +1022,7 @@ print_usage (void) printf ("[-t timeout] [-u unit] [-v] [-X type] [-N type]\n"); } -void +bool stat_path (struct parameter_list *p) { /* Stat entry to check that dir exists and is accessible */ @@ -980,13 +1032,13 @@ stat_path (struct parameter_list *p) if (verbose >= 3) printf("stat failed on %s\n", p->name); if (ignore_missing == 1) { - printf("DISK %s - ", _("OK")); - die (STATE_OK, _("%s %s: %s\n"), p->name, _("is not accessible (ignoring)"), strerror(errno)); + return false; } else { printf("DISK %s - ", _("CRITICAL")); die (STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno)); } } + return true; } @@ -1006,7 +1058,8 @@ get_stats (struct parameter_list *p, struct fs_usage *fsp) { continue; #endif if (p_list->group && ! (strcmp(p_list->group, p->group))) { - stat_path(p_list); + if (! stat_path(p_list)) + continue; get_fs_usage (p_list->best_match->me_mountdir, p_list->best_match->me_devname, &tmpfsp); get_path_stats(p_list, &tmpfsp); if (verbose >= 3) -- cgit v1.2.3-74-g34f1 From ca3d59cd6918c9e2739e783b721d4c1122640fd3 Mon Sep 17 00:00:00 2001 From: Kristian Schuster <116557017+KriSchu@users.noreply.github.com> Date: Sun, 19 Feb 2023 23:00:21 +0100 Subject: check_disk: add new tests for new ignore-missing feature --- plugins/t/check_disk.t | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'plugins') diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t index a534fd4a..275db70d 100644 --- a/plugins/t/check_disk.t +++ b/plugins/t/check_disk.t @@ -23,7 +23,7 @@ my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to anoth if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") { plan skip_all => "Need 2 mountpoints to test"; } else { - plan tests => 84; + plan tests => 86; } $result = NPTest->testCmd( @@ -355,14 +355,24 @@ like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mo # ignore-missing: exit okay, when fs is not accessible $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p /bob"); cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for not existing filesystem /bob"); -like( $result->output, '/^DISK OK - /bob is not accessible .*$/', 'Output OK'); +like( $result->output, '/^DISK OK - No disks were found for provided parameters; ignored paths: /bob;.*$/', 'Output OK'); # ignore-missing: exit okay, when regex does not match $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r /bob"); cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); -like( $result->output, '/^DISK OK: Regular expression did not match any path or disk.*$/', 'Output OK'); +like( $result->output, '/^DISK OK - No disks were found for provided parameters;.*$/', 'Output OK'); # ignore-missing: exit okay, when fs with exact match (-E) is not found -$result = NPTest->testCmd( "./check_disk --ignore-missing -E -w 0% -c 0% -p /etc"); +$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -E -p /etc"); cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay when exact match does not find fs"); -like( $result->output, '/^DISK OK: /etc not found.*$/', 'Output OK'); +like( $result->output, '/^DISK OK - No disks were found for provided parameters; ignored paths: /etc;.*$/', 'Output OK'); + +# ignore-missing: exit okay, when checking one existing fs and one non-existing fs (regex) +$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r '/bob' -r '^/$'"); +cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); +like( $result->output, '/^DISK OK - free space: / .*$/', 'Output OK'); + +# ignore-missing: exit okay, when checking one existing fs and one non-existing fs (path) +$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p '/bob' -p '/'"); +cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); +like( $result->output, '/^DISK OK - free space: / .*; ignored paths: /bob;.*$/', 'Output OK'); \ No newline at end of file -- cgit v1.2.3-74-g34f1 From a58293a0c288ee0e050c79715073da9fbdfc4c58 Mon Sep 17 00:00:00 2001 From: Kristian Schuster <116557017+KriSchu@users.noreply.github.com> Date: Mon, 20 Feb 2023 01:27:23 +0100 Subject: check_disk: fix tests by setting correct test number and escaping line end regex --- plugins/t/check_disk.t | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'plugins') diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t index 275db70d..73f1e374 100644 --- a/plugins/t/check_disk.t +++ b/plugins/t/check_disk.t @@ -23,7 +23,7 @@ my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to anoth if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") { plan skip_all => "Need 2 mountpoints to test"; } else { - plan tests => 86; + plan tests => 88; } $result = NPTest->testCmd( @@ -126,7 +126,7 @@ my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2; $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" ); -is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs"); +is( $result->only_output, "DISK OK - No disks were found for provided parameters;", "No print out of disks with -e for OKs"); $result = NPTest->testCmd( "./check_disk 100 100 $more_free" ); cmp_ok( $result->return_code, '==', 0, "Old syntax okay" ); @@ -368,9 +368,9 @@ cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay when exact m like( $result->output, '/^DISK OK - No disks were found for provided parameters; ignored paths: /etc;.*$/', 'Output OK'); # ignore-missing: exit okay, when checking one existing fs and one non-existing fs (regex) -$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r '/bob' -r '^/$'"); +$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r '/bob' -r '^/\$'"); cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); -like( $result->output, '/^DISK OK - free space: / .*$/', 'Output OK'); +like( $result->output, '/^DISK OK - free space: \/ .*$/', 'Output OK'); # ignore-missing: exit okay, when checking one existing fs and one non-existing fs (path) $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p '/bob' -p '/'"); -- cgit v1.2.3-74-g34f1 From e102b8a49e857a474db516455d2e871e6834ae34 Mon Sep 17 00:00:00 2001 From: Kristian Schuster <116557017+KriSchu@users.noreply.github.com> Date: Mon, 20 Feb 2023 02:03:01 +0100 Subject: check_disk: fix ugly output with -e option and adapt tests accordingly --- plugins/check_disk.c | 10 +++++----- plugins/t/check_disk.t | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'plugins') diff --git a/plugins/check_disk.c b/plugins/check_disk.c index d32841d8..c52d1df4 100644 --- a/plugins/check_disk.c +++ b/plugins/check_disk.c @@ -186,8 +186,8 @@ main (int argc, char **argv) char mountdir[32]; #endif - preamble = strdup (" free space:"); - ignored_preamble = strdup (" ignored paths:"); + preamble = strdup (" - free space:"); + ignored_preamble = strdup (" - ignored paths:"); output = strdup (""); ignored = strdup (""); details = strdup (""); @@ -455,12 +455,12 @@ main (int argc, char **argv) if (verbose >= 2) xasprintf (&output, "%s%s", output, details); - if (strcmp(output, "") == 0) { + if (strcmp(output, "") == 0 && ! erronly) { preamble = ""; - xasprintf (&output, " No disks were found for provided parameters;"); + xasprintf (&output, " - No disks were found for provided parameters;"); } - printf ("DISK %s -%s%s%s%s|%s\n", state_text (result), ((erronly && result==STATE_OK)) ? "" : preamble, output, (strcmp(ignored, "") == 0) ? "" : ignored_preamble, ignored, perf); + printf ("DISK %s%s%s%s%s|%s\n", state_text (result), ((erronly && result==STATE_OK)) ? "" : preamble, output, (strcmp(ignored, "") == 0) ? "" : ignored_preamble, ignored, perf); return result; } diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t index 73f1e374..c8f08f51 100644 --- a/plugins/t/check_disk.t +++ b/plugins/t/check_disk.t @@ -126,7 +126,7 @@ my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2; $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" ); -is( $result->only_output, "DISK OK - No disks were found for provided parameters;", "No print out of disks with -e for OKs"); +is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs"); $result = NPTest->testCmd( "./check_disk 100 100 $more_free" ); cmp_ok( $result->return_code, '==', 0, "Old syntax okay" ); @@ -355,7 +355,7 @@ like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mo # ignore-missing: exit okay, when fs is not accessible $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p /bob"); cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for not existing filesystem /bob"); -like( $result->output, '/^DISK OK - No disks were found for provided parameters; ignored paths: /bob;.*$/', 'Output OK'); +like( $result->output, '/^DISK OK - No disks were found for provided parameters; - ignored paths: /bob;.*$/', 'Output OK'); # ignore-missing: exit okay, when regex does not match $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r /bob"); @@ -365,7 +365,7 @@ like( $result->output, '/^DISK OK - No disks were found for provided parameters; # ignore-missing: exit okay, when fs with exact match (-E) is not found $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -E -p /etc"); cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay when exact match does not find fs"); -like( $result->output, '/^DISK OK - No disks were found for provided parameters; ignored paths: /etc;.*$/', 'Output OK'); +like( $result->output, '/^DISK OK - No disks were found for provided parameters; - ignored paths: /etc;.*$/', 'Output OK'); # ignore-missing: exit okay, when checking one existing fs and one non-existing fs (regex) $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r '/bob' -r '^/\$'"); @@ -375,4 +375,4 @@ like( $result->output, '/^DISK OK - free space: \/ .*$/', 'Output OK'); # ignore-missing: exit okay, when checking one existing fs and one non-existing fs (path) $result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p '/bob' -p '/'"); cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); -like( $result->output, '/^DISK OK - free space: / .*; ignored paths: /bob;.*$/', 'Output OK'); \ No newline at end of file +like( $result->output, '/^DISK OK - free space: / .*; - ignored paths: /bob;.*$/', 'Output OK'); \ No newline at end of file -- cgit v1.2.3-74-g34f1 From 3e7da5f970d73df91fad32f4dce259d30cdbbd65 Mon Sep 17 00:00:00 2001 From: Kristian Schuster <116557017+KriSchu@users.noreply.github.com> Date: Mon, 6 Mar 2023 14:03:10 +0100 Subject: check_disk: use cleaner code for ignore-missing option - use datatype bool for new vars ignore_missing and path_ignored instead of int - directly initialize preamble and ignored_preamble with their strings --- plugins/check_disk.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'plugins') diff --git a/plugins/check_disk.c b/plugins/check_disk.c index c52d1df4..bd84c825 100644 --- a/plugins/check_disk.c +++ b/plugins/check_disk.c @@ -141,7 +141,7 @@ int verbose = 0; int erronly = FALSE; int display_mntp = FALSE; int exact_match = FALSE; -int ignore_missing = FALSE; +bool ignore_missing = false; int freespace_ignore_reserved = FALSE; int display_inodes_perfdata = FALSE; char *warn_freespace_units = NULL; @@ -157,7 +157,7 @@ char *crit_usedinodes_percent = NULL; char *warn_freeinodes_percent = NULL; char *crit_freeinodes_percent = NULL; int path_selected = FALSE; -int path_ignored = FALSE; +bool path_ignored = false; char *group = NULL; struct stat *stat_buf; struct name_list *seen = NULL; @@ -173,8 +173,8 @@ main (int argc, char **argv) char *details; char *perf; char *perf_ilabel; - char *preamble; - char *ignored_preamble; + char *preamble = " - free space:"; + char *ignored_preamble = " - ignored paths:"; char *flag_header; int temp_result; @@ -186,8 +186,6 @@ main (int argc, char **argv) char mountdir[32]; #endif - preamble = strdup (" - free space:"); - ignored_preamble = strdup (" - ignored paths:"); output = strdup (""); ignored = strdup (""); details = strdup (""); @@ -210,7 +208,7 @@ main (int argc, char **argv) /* If a list of paths has not been selected, find entire mount list and create list of paths */ - if (path_selected == FALSE && path_ignored == FALSE) { + if (path_selected == FALSE && path_ignored == false) { for (me = mount_list; me; me = me->me_next) { if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) { path = np_add_parameter(&path_select_list, me->me_mountdir); @@ -221,7 +219,7 @@ main (int argc, char **argv) } } - if (path_ignored == FALSE) { + if (path_ignored == false) { np_set_best_match(path_select_list, mount_list, exact_match); } @@ -229,7 +227,7 @@ main (int argc, char **argv) temp_list = path_select_list; while (path_select_list) { - if (! path_select_list->best_match && ignore_missing == 1) { + if (! path_select_list->best_match && ignore_missing == true) { /* If the first element will be deleted, the temp_list must be updated with the new start address as well */ if (path_select_list == temp_list) { temp_list = path_select_list->name_next; @@ -249,7 +247,7 @@ main (int argc, char **argv) path_select_list = temp_list; - if (! path_select_list && ignore_missing == 1) { + if (! path_select_list && ignore_missing == true) { result = STATE_OK; if (verbose >= 2) { printf ("None of the provided paths were found\n"); @@ -295,7 +293,7 @@ main (int argc, char **argv) /* Skip remote filesystems if we're not interested in them */ if (me->me_remote && show_local_fs) { if (stat_remote_fs) { - if (!stat_path(path) && ignore_missing == 1) { + if (!stat_path(path) && ignore_missing == true) { result = STATE_OK; xasprintf (&ignored, "%s %s;", ignored, path->name); } @@ -319,7 +317,7 @@ main (int argc, char **argv) } if (!stat_path(path)) { - if (ignore_missing == 1) { + if (ignore_missing == true) { result = STATE_OK; xasprintf (&ignored, "%s %s;", ignored, path->name); } @@ -682,8 +680,8 @@ process_arguments (int argc, char **argv) if (! (se = np_find_parameter(path_select_list, optarg))) { se = np_add_parameter(&path_select_list, optarg); - if (stat(optarg, &stat_buf[0]) && ignore_missing == 1) { - path_ignored = TRUE; + if (stat(optarg, &stat_buf[0]) && ignore_missing == true) { + path_ignored = true; break; } } @@ -775,7 +773,7 @@ process_arguments (int argc, char **argv) break; case IGNORE_MISSING: - ignore_missing = 1; + ignore_missing = true; break; case 'A': optarg = strdup(".*"); @@ -812,8 +810,8 @@ process_arguments (int argc, char **argv) } } - if (!fnd && ignore_missing == 1) { - path_ignored = TRUE; + if (!fnd && ignore_missing == true) { + path_ignored = true; /* path_selected = TRUE;*/ break; } else if (!fnd) @@ -1031,7 +1029,7 @@ stat_path (struct parameter_list *p) if (stat (p->name, &stat_buf[0])) { if (verbose >= 3) printf("stat failed on %s\n", p->name); - if (ignore_missing == 1) { + if (ignore_missing == true) { return false; } else { printf("DISK %s - ", _("CRITICAL")); -- cgit v1.2.3-74-g34f1