diff options
author | Lorenz <12514511+RincewindsHat@users.noreply.github.com> | 2023-03-11 09:27:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-11 09:27:43 (GMT) |
commit | 357787868b5201ec3e874e7a225b1c944cbbdb4d (patch) | |
tree | b15068501b8f115269b6e5d52a6fb5e3308aa87e /plugins/check_disk.c | |
parent | 269718094177fb8a7e3d3005d1310495009fe8c4 (diff) | |
parent | 3e7da5f970d73df91fad32f4dce259d30cdbbd65 (diff) | |
download | monitoring-plugins-3577878.tar.gz |
Merge pull request #1801 from KriSchu/feature_check_disk_add_ignore_missing_option
check_disk: add ignore-missing option to return OK for missing fs
Diffstat (limited to 'plugins/check_disk.c')
-rw-r--r-- | plugins/check_disk.c | 112 |
1 files changed, 91 insertions, 21 deletions
diff --git a/plugins/check_disk.c b/plugins/check_disk.c index 935acce..bd84c82 100644 --- a/plugins/check_disk.c +++ b/plugins/check_disk.c | |||
@@ -112,11 +112,12 @@ enum | |||
112 | { | 112 | { |
113 | SYNC_OPTION = CHAR_MAX + 1, | 113 | SYNC_OPTION = CHAR_MAX + 1, |
114 | NO_SYNC_OPTION, | 114 | NO_SYNC_OPTION, |
115 | BLOCK_SIZE_OPTION | 115 | BLOCK_SIZE_OPTION, |
116 | IGNORE_MISSING | ||
116 | }; | 117 | }; |
117 | 118 | ||
118 | #ifdef _AIX | 119 | #ifdef _AIX |
119 | #pragma alloca | 120 | #pragma alloca |
120 | #endif | 121 | #endif |
121 | 122 | ||
122 | int process_arguments (int, char **); | 123 | int process_arguments (int, char **); |
@@ -126,7 +127,7 @@ int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, ch | |||
126 | void print_help (void); | 127 | void print_help (void); |
127 | void print_usage (void); | 128 | void print_usage (void); |
128 | double calculate_percent(uintmax_t, uintmax_t); | 129 | double calculate_percent(uintmax_t, uintmax_t); |
129 | void stat_path (struct parameter_list *p); | 130 | bool stat_path (struct parameter_list *p); |
130 | void get_stats (struct parameter_list *p, struct fs_usage *fsp); | 131 | void get_stats (struct parameter_list *p, struct fs_usage *fsp); |
131 | void get_path_stats (struct parameter_list *p, struct fs_usage *fsp); | 132 | void get_path_stats (struct parameter_list *p, struct fs_usage *fsp); |
132 | 133 | ||
@@ -140,6 +141,7 @@ int verbose = 0; | |||
140 | int erronly = FALSE; | 141 | int erronly = FALSE; |
141 | int display_mntp = FALSE; | 142 | int display_mntp = FALSE; |
142 | int exact_match = FALSE; | 143 | int exact_match = FALSE; |
144 | bool ignore_missing = false; | ||
143 | int freespace_ignore_reserved = FALSE; | 145 | int freespace_ignore_reserved = FALSE; |
144 | int display_inodes_perfdata = FALSE; | 146 | int display_inodes_perfdata = FALSE; |
145 | char *warn_freespace_units = NULL; | 147 | char *warn_freespace_units = NULL; |
@@ -155,6 +157,7 @@ char *crit_usedinodes_percent = NULL; | |||
155 | char *warn_freeinodes_percent = NULL; | 157 | char *warn_freeinodes_percent = NULL; |
156 | char *crit_freeinodes_percent = NULL; | 158 | char *crit_freeinodes_percent = NULL; |
157 | int path_selected = FALSE; | 159 | int path_selected = FALSE; |
160 | bool path_ignored = false; | ||
158 | char *group = NULL; | 161 | char *group = NULL; |
159 | struct stat *stat_buf; | 162 | struct stat *stat_buf; |
160 | struct name_list *seen = NULL; | 163 | struct name_list *seen = NULL; |
@@ -166,10 +169,12 @@ main (int argc, char **argv) | |||
166 | int result = STATE_UNKNOWN; | 169 | int result = STATE_UNKNOWN; |
167 | int disk_result = STATE_UNKNOWN; | 170 | int disk_result = STATE_UNKNOWN; |
168 | char *output; | 171 | char *output; |
172 | char *ignored; | ||
169 | char *details; | 173 | char *details; |
170 | char *perf; | 174 | char *perf; |
171 | char *perf_ilabel; | 175 | char *perf_ilabel; |
172 | char *preamble; | 176 | char *preamble = " - free space:"; |
177 | char *ignored_preamble = " - ignored paths:"; | ||
173 | char *flag_header; | 178 | char *flag_header; |
174 | int temp_result; | 179 | int temp_result; |
175 | 180 | ||
@@ -181,8 +186,8 @@ main (int argc, char **argv) | |||
181 | char mountdir[32]; | 186 | char mountdir[32]; |
182 | #endif | 187 | #endif |
183 | 188 | ||
184 | preamble = strdup (" - free space:"); | ||
185 | output = strdup (""); | 189 | output = strdup (""); |
190 | ignored = strdup (""); | ||
186 | details = strdup (""); | 191 | details = strdup (""); |
187 | perf = strdup (""); | 192 | perf = strdup (""); |
188 | perf_ilabel = strdup (""); | 193 | perf_ilabel = strdup (""); |
@@ -203,7 +208,7 @@ main (int argc, char **argv) | |||
203 | /* If a list of paths has not been selected, find entire | 208 | /* If a list of paths has not been selected, find entire |
204 | mount list and create list of paths | 209 | mount list and create list of paths |
205 | */ | 210 | */ |
206 | if (path_selected == FALSE) { | 211 | if (path_selected == FALSE && path_ignored == false) { |
207 | for (me = mount_list; me; me = me->me_next) { | 212 | for (me = mount_list; me; me = me->me_next) { |
208 | if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) { | 213 | if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) { |
209 | path = np_add_parameter(&path_select_list, me->me_mountdir); | 214 | path = np_add_parameter(&path_select_list, me->me_mountdir); |
@@ -213,17 +218,40 @@ main (int argc, char **argv) | |||
213 | set_all_thresholds(path); | 218 | set_all_thresholds(path); |
214 | } | 219 | } |
215 | } | 220 | } |
216 | np_set_best_match(path_select_list, mount_list, exact_match); | 221 | |
222 | if (path_ignored == false) { | ||
223 | np_set_best_match(path_select_list, mount_list, exact_match); | ||
224 | } | ||
217 | 225 | ||
218 | /* Error if no match found for specified paths */ | 226 | /* Error if no match found for specified paths */ |
219 | temp_list = path_select_list; | 227 | temp_list = path_select_list; |
220 | 228 | ||
221 | while (temp_list) { | 229 | while (path_select_list) { |
222 | if (! temp_list->best_match) { | 230 | if (! path_select_list->best_match && ignore_missing == true) { |
223 | die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name); | 231 | /* If the first element will be deleted, the temp_list must be updated with the new start address as well */ |
232 | if (path_select_list == temp_list) { | ||
233 | temp_list = path_select_list->name_next; | ||
234 | } | ||
235 | /* Add path argument to list of ignored paths to inform about missing paths being ignored and not alerted */ | ||
236 | xasprintf (&ignored, "%s %s;", ignored, path_select_list->name); | ||
237 | /* Delete the path from the list so that it is not stat-checked later in the code. */ | ||
238 | path_select_list = np_del_parameter(path_select_list, path_select_list->name_prev); | ||
239 | } else if (! path_select_list->best_match) { | ||
240 | /* Without --ignore-missing option, exit with Critical state. */ | ||
241 | die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), path_select_list->name); | ||
242 | } else { | ||
243 | /* Continue jumping through the list */ | ||
244 | path_select_list = path_select_list->name_next; | ||
224 | } | 245 | } |
246 | } | ||
247 | |||
248 | path_select_list = temp_list; | ||
225 | 249 | ||
226 | temp_list = temp_list->name_next; | 250 | if (! path_select_list && ignore_missing == true) { |
251 | result = STATE_OK; | ||
252 | if (verbose >= 2) { | ||
253 | printf ("None of the provided paths were found\n"); | ||
254 | } | ||
227 | } | 255 | } |
228 | 256 | ||
229 | /* Process for every path in list */ | 257 | /* Process for every path in list */ |
@@ -242,6 +270,10 @@ main (int argc, char **argv) | |||
242 | 270 | ||
243 | me = path->best_match; | 271 | me = path->best_match; |
244 | 272 | ||
273 | if (!me) { | ||
274 | continue; | ||
275 | } | ||
276 | |||
245 | #ifdef __CYGWIN__ | 277 | #ifdef __CYGWIN__ |
246 | if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11) | 278 | if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11) |
247 | continue; | 279 | continue; |
@@ -260,8 +292,12 @@ main (int argc, char **argv) | |||
260 | if (path->group == NULL) { | 292 | if (path->group == NULL) { |
261 | /* Skip remote filesystems if we're not interested in them */ | 293 | /* Skip remote filesystems if we're not interested in them */ |
262 | if (me->me_remote && show_local_fs) { | 294 | if (me->me_remote && show_local_fs) { |
263 | if (stat_remote_fs) | 295 | if (stat_remote_fs) { |
264 | stat_path(path); | 296 | if (!stat_path(path) && ignore_missing == true) { |
297 | result = STATE_OK; | ||
298 | xasprintf (&ignored, "%s %s;", ignored, path->name); | ||
299 | } | ||
300 | } | ||
265 | continue; | 301 | continue; |
266 | /* Skip pseudo fs's if we haven't asked for all fs's */ | 302 | /* Skip pseudo fs's if we haven't asked for all fs's */ |
267 | } else if (me->me_dummy && !show_all_fs) { | 303 | } else if (me->me_dummy && !show_all_fs) { |
@@ -280,7 +316,13 @@ main (int argc, char **argv) | |||
280 | } | 316 | } |
281 | } | 317 | } |
282 | 318 | ||
283 | stat_path(path); | 319 | if (!stat_path(path)) { |
320 | if (ignore_missing == true) { | ||
321 | result = STATE_OK; | ||
322 | xasprintf (&ignored, "%s %s;", ignored, path->name); | ||
323 | } | ||
324 | continue; | ||
325 | } | ||
284 | get_fs_usage (me->me_mountdir, me->me_devname, &fsp); | 326 | get_fs_usage (me->me_mountdir, me->me_devname, &fsp); |
285 | 327 | ||
286 | if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { | 328 | if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { |
@@ -411,8 +453,12 @@ main (int argc, char **argv) | |||
411 | if (verbose >= 2) | 453 | if (verbose >= 2) |
412 | xasprintf (&output, "%s%s", output, details); | 454 | xasprintf (&output, "%s%s", output, details); |
413 | 455 | ||
456 | if (strcmp(output, "") == 0 && ! erronly) { | ||
457 | preamble = ""; | ||
458 | xasprintf (&output, " - No disks were found for provided parameters;"); | ||
459 | } | ||
414 | 460 | ||
415 | printf ("DISK %s%s%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf); | 461 | 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); |
416 | return result; | 462 | return result; |
417 | } | 463 | } |
418 | 464 | ||
@@ -481,6 +527,7 @@ process_arguments (int argc, char **argv) | |||
481 | {"ignore-ereg-partition", required_argument, 0, 'i'}, | 527 | {"ignore-ereg-partition", required_argument, 0, 'i'}, |
482 | {"ignore-eregi-path", required_argument, 0, 'I'}, | 528 | {"ignore-eregi-path", required_argument, 0, 'I'}, |
483 | {"ignore-eregi-partition", required_argument, 0, 'I'}, | 529 | {"ignore-eregi-partition", required_argument, 0, 'I'}, |
530 | {"ignore-missing", no_argument, 0, IGNORE_MISSING}, | ||
484 | {"local", no_argument, 0, 'l'}, | 531 | {"local", no_argument, 0, 'l'}, |
485 | {"stat-remote-fs", no_argument, 0, 'L'}, | 532 | {"stat-remote-fs", no_argument, 0, 'L'}, |
486 | {"iperfdata", no_argument, 0, 'P'}, | 533 | {"iperfdata", no_argument, 0, 'P'}, |
@@ -632,12 +679,19 @@ process_arguments (int argc, char **argv) | |||
632 | /* add parameter if not found. overwrite thresholds if path has already been added */ | 679 | /* add parameter if not found. overwrite thresholds if path has already been added */ |
633 | if (! (se = np_find_parameter(path_select_list, optarg))) { | 680 | if (! (se = np_find_parameter(path_select_list, optarg))) { |
634 | se = np_add_parameter(&path_select_list, optarg); | 681 | se = np_add_parameter(&path_select_list, optarg); |
682 | |||
683 | if (stat(optarg, &stat_buf[0]) && ignore_missing == true) { | ||
684 | path_ignored = true; | ||
685 | break; | ||
686 | } | ||
635 | } | 687 | } |
636 | se->group = group; | 688 | se->group = group; |
637 | set_all_thresholds(se); | 689 | set_all_thresholds(se); |
638 | 690 | ||
639 | /* With autofs, it is required to stat() the path before re-populating the mount_list */ | 691 | /* With autofs, it is required to stat() the path before re-populating the mount_list */ |
640 | stat_path(se); | 692 | if (!stat_path(se)) { |
693 | break; | ||
694 | } | ||
641 | /* NB: We can't free the old mount_list "just like that": both list pointers and struct | 695 | /* NB: We can't free the old mount_list "just like that": both list pointers and struct |
642 | * pointers are copied around. One of the reason it wasn't done yet is that other parts | 696 | * pointers are copied around. One of the reason it wasn't done yet is that other parts |
643 | * of check_disk need the same kind of cleanup so it'd better be done as a whole */ | 697 | * of check_disk need the same kind of cleanup so it'd better be done as a whole */ |
@@ -718,6 +772,9 @@ process_arguments (int argc, char **argv) | |||
718 | cflags = default_cflags; | 772 | cflags = default_cflags; |
719 | break; | 773 | break; |
720 | 774 | ||
775 | case IGNORE_MISSING: | ||
776 | ignore_missing = true; | ||
777 | break; | ||
721 | case 'A': | 778 | case 'A': |
722 | optarg = strdup(".*"); | 779 | optarg = strdup(".*"); |
723 | // Intentional fallthrough | 780 | // Intentional fallthrough |
@@ -753,7 +810,11 @@ process_arguments (int argc, char **argv) | |||
753 | } | 810 | } |
754 | } | 811 | } |
755 | 812 | ||
756 | if (!fnd) | 813 | if (!fnd && ignore_missing == true) { |
814 | path_ignored = true; | ||
815 | /* path_selected = TRUE;*/ | ||
816 | break; | ||
817 | } else if (!fnd) | ||
757 | die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), | 818 | die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), |
758 | _("Regular expression did not match any path or disk"), optarg); | 819 | _("Regular expression did not match any path or disk"), optarg); |
759 | 820 | ||
@@ -923,6 +984,9 @@ print_help (void) | |||
923 | printf (" %s\n", _("Regular expression to ignore selected path/partition (case insensitive) (may be repeated)")); | 984 | printf (" %s\n", _("Regular expression to ignore selected path/partition (case insensitive) (may be repeated)")); |
924 | printf (" %s\n", "-i, --ignore-ereg-path=PATH, --ignore-ereg-partition=PARTITION"); | 985 | printf (" %s\n", "-i, --ignore-ereg-path=PATH, --ignore-ereg-partition=PARTITION"); |
925 | printf (" %s\n", _("Regular expression to ignore selected path or partition (may be repeated)")); | 986 | printf (" %s\n", _("Regular expression to ignore selected path or partition (may be repeated)")); |
987 | printf (" %s\n", "--ignore-missing"); | ||
988 | printf (" %s\n", _("Return OK if no filesystem matches, filesystem does not exist or is inaccessible.")); | ||
989 | printf (" %s\n", _("(Provide this option before -p / -r / --ereg-path if used)")); | ||
926 | printf (UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 990 | printf (UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
927 | printf (" %s\n", "-u, --units=STRING"); | 991 | printf (" %s\n", "-u, --units=STRING"); |
928 | printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)")); | 992 | printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)")); |
@@ -956,7 +1020,7 @@ print_usage (void) | |||
956 | printf ("[-t timeout] [-u unit] [-v] [-X type] [-N type]\n"); | 1020 | printf ("[-t timeout] [-u unit] [-v] [-X type] [-N type]\n"); |
957 | } | 1021 | } |
958 | 1022 | ||
959 | void | 1023 | bool |
960 | stat_path (struct parameter_list *p) | 1024 | stat_path (struct parameter_list *p) |
961 | { | 1025 | { |
962 | /* Stat entry to check that dir exists and is accessible */ | 1026 | /* Stat entry to check that dir exists and is accessible */ |
@@ -965,9 +1029,14 @@ stat_path (struct parameter_list *p) | |||
965 | if (stat (p->name, &stat_buf[0])) { | 1029 | if (stat (p->name, &stat_buf[0])) { |
966 | if (verbose >= 3) | 1030 | if (verbose >= 3) |
967 | printf("stat failed on %s\n", p->name); | 1031 | printf("stat failed on %s\n", p->name); |
968 | printf("DISK %s - ", _("CRITICAL")); | 1032 | if (ignore_missing == true) { |
969 | die (STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno)); | 1033 | return false; |
1034 | } else { | ||
1035 | printf("DISK %s - ", _("CRITICAL")); | ||
1036 | die (STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno)); | ||
1037 | } | ||
970 | } | 1038 | } |
1039 | return true; | ||
971 | } | 1040 | } |
972 | 1041 | ||
973 | 1042 | ||
@@ -987,7 +1056,8 @@ get_stats (struct parameter_list *p, struct fs_usage *fsp) { | |||
987 | continue; | 1056 | continue; |
988 | #endif | 1057 | #endif |
989 | if (p_list->group && ! (strcmp(p_list->group, p->group))) { | 1058 | if (p_list->group && ! (strcmp(p_list->group, p->group))) { |
990 | stat_path(p_list); | 1059 | if (! stat_path(p_list)) |
1060 | continue; | ||
991 | get_fs_usage (p_list->best_match->me_mountdir, p_list->best_match->me_devname, &tmpfsp); | 1061 | get_fs_usage (p_list->best_match->me_mountdir, p_list->best_match->me_devname, &tmpfsp); |
992 | get_path_stats(p_list, &tmpfsp); | 1062 | get_path_stats(p_list, &tmpfsp); |
993 | if (verbose >= 3) | 1063 | if (verbose >= 3) |