summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_disk.c112
-rw-r--r--plugins/t/check_disk.t27
2 files changed, 118 insertions, 21 deletions
diff --git a/plugins/check_disk.c b/plugins/check_disk.c
index 935acce..d32841d 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
122int process_arguments (int, char **); 123int process_arguments (int, char **);
@@ -126,7 +127,7 @@ int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, ch
126void print_help (void); 127void print_help (void);
127void print_usage (void); 128void print_usage (void);
128double calculate_percent(uintmax_t, uintmax_t); 129double calculate_percent(uintmax_t, uintmax_t);
129void stat_path (struct parameter_list *p); 130bool stat_path (struct parameter_list *p);
130void get_stats (struct parameter_list *p, struct fs_usage *fsp); 131void get_stats (struct parameter_list *p, struct fs_usage *fsp);
131void get_path_stats (struct parameter_list *p, struct fs_usage *fsp); 132void get_path_stats (struct parameter_list *p, struct fs_usage *fsp);
132 133
@@ -140,6 +141,7 @@ int verbose = 0;
140int erronly = FALSE; 141int erronly = FALSE;
141int display_mntp = FALSE; 142int display_mntp = FALSE;
142int exact_match = FALSE; 143int exact_match = FALSE;
144int ignore_missing = FALSE;
143int freespace_ignore_reserved = FALSE; 145int freespace_ignore_reserved = FALSE;
144int display_inodes_perfdata = FALSE; 146int display_inodes_perfdata = FALSE;
145char *warn_freespace_units = NULL; 147char *warn_freespace_units = NULL;
@@ -155,6 +157,7 @@ char *crit_usedinodes_percent = NULL;
155char *warn_freeinodes_percent = NULL; 157char *warn_freeinodes_percent = NULL;
156char *crit_freeinodes_percent = NULL; 158char *crit_freeinodes_percent = NULL;
157int path_selected = FALSE; 159int path_selected = FALSE;
160int path_ignored = FALSE;
158char *group = NULL; 161char *group = NULL;
159struct stat *stat_buf; 162struct stat *stat_buf;
160struct name_list *seen = NULL; 163struct 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;
177 char *ignored_preamble;
173 char *flag_header; 178 char *flag_header;
174 int temp_result; 179 int temp_result;
175 180
@@ -181,8 +186,10 @@ main (int argc, char **argv)
181 char mountdir[32]; 186 char mountdir[32];
182#endif 187#endif
183 188
184 preamble = strdup (" - free space:"); 189 preamble = strdup (" free space:");
190 ignored_preamble = strdup (" ignored paths:");
185 output = strdup (""); 191 output = strdup ("");
192 ignored = strdup ("");
186 details = strdup (""); 193 details = strdup ("");
187 perf = strdup (""); 194 perf = strdup ("");
188 perf_ilabel = strdup (""); 195 perf_ilabel = strdup ("");
@@ -203,7 +210,7 @@ main (int argc, char **argv)
203 /* If a list of paths has not been selected, find entire 210 /* If a list of paths has not been selected, find entire
204 mount list and create list of paths 211 mount list and create list of paths
205 */ 212 */
206 if (path_selected == FALSE) { 213 if (path_selected == FALSE && path_ignored == FALSE) {
207 for (me = mount_list; me; me = me->me_next) { 214 for (me = mount_list; me; me = me->me_next) {
208 if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) { 215 if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) {
209 path = np_add_parameter(&path_select_list, me->me_mountdir); 216 path = np_add_parameter(&path_select_list, me->me_mountdir);
@@ -213,17 +220,40 @@ main (int argc, char **argv)
213 set_all_thresholds(path); 220 set_all_thresholds(path);
214 } 221 }
215 } 222 }
216 np_set_best_match(path_select_list, mount_list, exact_match); 223
224 if (path_ignored == FALSE) {
225 np_set_best_match(path_select_list, mount_list, exact_match);
226 }
217 227
218 /* Error if no match found for specified paths */ 228 /* Error if no match found for specified paths */
219 temp_list = path_select_list; 229 temp_list = path_select_list;
220 230
221 while (temp_list) { 231 while (path_select_list) {
222 if (! temp_list->best_match) { 232 if (! path_select_list->best_match && ignore_missing == 1) {
223 die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name); 233 /* If the first element will be deleted, the temp_list must be updated with the new start address as well */
234 if (path_select_list == temp_list) {
235 temp_list = path_select_list->name_next;
236 }
237 /* Add path argument to list of ignored paths to inform about missing paths being ignored and not alerted */
238 xasprintf (&ignored, "%s %s;", ignored, path_select_list->name);
239 /* Delete the path from the list so that it is not stat-checked later in the code. */
240 path_select_list = np_del_parameter(path_select_list, path_select_list->name_prev);
241 } else if (! path_select_list->best_match) {
242 /* Without --ignore-missing option, exit with Critical state. */
243 die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), path_select_list->name);
244 } else {
245 /* Continue jumping through the list */
246 path_select_list = path_select_list->name_next;
224 } 247 }
248 }
249
250 path_select_list = temp_list;
225 251
226 temp_list = temp_list->name_next; 252 if (! path_select_list && ignore_missing == 1) {
253 result = STATE_OK;
254 if (verbose >= 2) {
255 printf ("None of the provided paths were found\n");
256 }
227 } 257 }
228 258
229 /* Process for every path in list */ 259 /* Process for every path in list */
@@ -242,6 +272,10 @@ main (int argc, char **argv)
242 272
243 me = path->best_match; 273 me = path->best_match;
244 274
275 if (!me) {
276 continue;
277 }
278
245#ifdef __CYGWIN__ 279#ifdef __CYGWIN__
246 if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11) 280 if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11)
247 continue; 281 continue;
@@ -260,8 +294,12 @@ main (int argc, char **argv)
260 if (path->group == NULL) { 294 if (path->group == NULL) {
261 /* Skip remote filesystems if we're not interested in them */ 295 /* Skip remote filesystems if we're not interested in them */
262 if (me->me_remote && show_local_fs) { 296 if (me->me_remote && show_local_fs) {
263 if (stat_remote_fs) 297 if (stat_remote_fs) {
264 stat_path(path); 298 if (!stat_path(path) && ignore_missing == 1) {
299 result = STATE_OK;
300 xasprintf (&ignored, "%s %s;", ignored, path->name);
301 }
302 }
265 continue; 303 continue;
266 /* Skip pseudo fs's if we haven't asked for all fs's */ 304 /* Skip pseudo fs's if we haven't asked for all fs's */
267 } else if (me->me_dummy && !show_all_fs) { 305 } else if (me->me_dummy && !show_all_fs) {
@@ -280,7 +318,13 @@ main (int argc, char **argv)
280 } 318 }
281 } 319 }
282 320
283 stat_path(path); 321 if (!stat_path(path)) {
322 if (ignore_missing == 1) {
323 result = STATE_OK;
324 xasprintf (&ignored, "%s %s;", ignored, path->name);
325 }
326 continue;
327 }
284 get_fs_usage (me->me_mountdir, me->me_devname, &fsp); 328 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
285 329
286 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { 330 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
@@ -411,8 +455,12 @@ main (int argc, char **argv)
411 if (verbose >= 2) 455 if (verbose >= 2)
412 xasprintf (&output, "%s%s", output, details); 456 xasprintf (&output, "%s%s", output, details);
413 457
458 if (strcmp(output, "") == 0) {
459 preamble = "";
460 xasprintf (&output, " No disks were found for provided parameters;");
461 }
414 462
415 printf ("DISK %s%s%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf); 463 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; 464 return result;
417} 465}
418 466
@@ -481,6 +529,7 @@ process_arguments (int argc, char **argv)
481 {"ignore-ereg-partition", required_argument, 0, 'i'}, 529 {"ignore-ereg-partition", required_argument, 0, 'i'},
482 {"ignore-eregi-path", required_argument, 0, 'I'}, 530 {"ignore-eregi-path", required_argument, 0, 'I'},
483 {"ignore-eregi-partition", required_argument, 0, 'I'}, 531 {"ignore-eregi-partition", required_argument, 0, 'I'},
532 {"ignore-missing", no_argument, 0, IGNORE_MISSING},
484 {"local", no_argument, 0, 'l'}, 533 {"local", no_argument, 0, 'l'},
485 {"stat-remote-fs", no_argument, 0, 'L'}, 534 {"stat-remote-fs", no_argument, 0, 'L'},
486 {"iperfdata", no_argument, 0, 'P'}, 535 {"iperfdata", no_argument, 0, 'P'},
@@ -632,12 +681,19 @@ process_arguments (int argc, char **argv)
632 /* add parameter if not found. overwrite thresholds if path has already been added */ 681 /* add parameter if not found. overwrite thresholds if path has already been added */
633 if (! (se = np_find_parameter(path_select_list, optarg))) { 682 if (! (se = np_find_parameter(path_select_list, optarg))) {
634 se = np_add_parameter(&path_select_list, optarg); 683 se = np_add_parameter(&path_select_list, optarg);
684
685 if (stat(optarg, &stat_buf[0]) && ignore_missing == 1) {
686 path_ignored = TRUE;
687 break;
688 }
635 } 689 }
636 se->group = group; 690 se->group = group;
637 set_all_thresholds(se); 691 set_all_thresholds(se);
638 692
639 /* With autofs, it is required to stat() the path before re-populating the mount_list */ 693 /* With autofs, it is required to stat() the path before re-populating the mount_list */
640 stat_path(se); 694 if (!stat_path(se)) {
695 break;
696 }
641 /* NB: We can't free the old mount_list "just like that": both list pointers and struct 697 /* 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 698 * 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 */ 699 * of check_disk need the same kind of cleanup so it'd better be done as a whole */
@@ -718,6 +774,9 @@ process_arguments (int argc, char **argv)
718 cflags = default_cflags; 774 cflags = default_cflags;
719 break; 775 break;
720 776
777 case IGNORE_MISSING:
778 ignore_missing = 1;
779 break;
721 case 'A': 780 case 'A':
722 optarg = strdup(".*"); 781 optarg = strdup(".*");
723 // Intentional fallthrough 782 // Intentional fallthrough
@@ -753,7 +812,11 @@ process_arguments (int argc, char **argv)
753 } 812 }
754 } 813 }
755 814
756 if (!fnd) 815 if (!fnd && ignore_missing == 1) {
816 path_ignored = TRUE;
817 /* path_selected = TRUE;*/
818 break;
819 } else if (!fnd)
757 die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), 820 die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"),
758 _("Regular expression did not match any path or disk"), optarg); 821 _("Regular expression did not match any path or disk"), optarg);
759 822
@@ -923,6 +986,9 @@ print_help (void)
923 printf (" %s\n", _("Regular expression to ignore selected path/partition (case insensitive) (may be repeated)")); 986 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"); 987 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)")); 988 printf (" %s\n", _("Regular expression to ignore selected path or partition (may be repeated)"));
989 printf (" %s\n", "--ignore-missing");
990 printf (" %s\n", _("Return OK if no filesystem matches, filesystem does not exist or is inaccessible."));
991 printf (" %s\n", _("(Provide this option before -p / -r / --ereg-path if used)"));
926 printf (UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 992 printf (UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
927 printf (" %s\n", "-u, --units=STRING"); 993 printf (" %s\n", "-u, --units=STRING");
928 printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)")); 994 printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)"));
@@ -956,7 +1022,7 @@ print_usage (void)
956 printf ("[-t timeout] [-u unit] [-v] [-X type] [-N type]\n"); 1022 printf ("[-t timeout] [-u unit] [-v] [-X type] [-N type]\n");
957} 1023}
958 1024
959void 1025bool
960stat_path (struct parameter_list *p) 1026stat_path (struct parameter_list *p)
961{ 1027{
962 /* Stat entry to check that dir exists and is accessible */ 1028 /* Stat entry to check that dir exists and is accessible */
@@ -965,9 +1031,14 @@ stat_path (struct parameter_list *p)
965 if (stat (p->name, &stat_buf[0])) { 1031 if (stat (p->name, &stat_buf[0])) {
966 if (verbose >= 3) 1032 if (verbose >= 3)
967 printf("stat failed on %s\n", p->name); 1033 printf("stat failed on %s\n", p->name);
968 printf("DISK %s - ", _("CRITICAL")); 1034 if (ignore_missing == 1) {
969 die (STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno)); 1035 return false;
1036 } else {
1037 printf("DISK %s - ", _("CRITICAL"));
1038 die (STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno));
1039 }
970 } 1040 }
1041 return true;
971} 1042}
972 1043
973 1044
@@ -987,7 +1058,8 @@ get_stats (struct parameter_list *p, struct fs_usage *fsp) {
987 continue; 1058 continue;
988#endif 1059#endif
989 if (p_list->group && ! (strcmp(p_list->group, p->group))) { 1060 if (p_list->group && ! (strcmp(p_list->group, p->group))) {
990 stat_path(p_list); 1061 if (! stat_path(p_list))
1062 continue;
991 get_fs_usage (p_list->best_match->me_mountdir, p_list->best_match->me_devname, &tmpfsp); 1063 get_fs_usage (p_list->best_match->me_mountdir, p_list->best_match->me_devname, &tmpfsp);
992 get_path_stats(p_list, &tmpfsp); 1064 get_path_stats(p_list, &tmpfsp);
993 if (verbose >= 3) 1065 if (verbose >= 3)
diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t
index ec527e7..275db70 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
23if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") { 23if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") {
24 plan skip_all => "Need 2 mountpoints to test"; 24 plan skip_all => "Need 2 mountpoints to test";
25} else { 25} else {
26 plan tests => 78; 26 plan tests => 86;
27} 27}
28 28
29$result = NPTest->testCmd( 29$result = NPTest->testCmd(
@@ -351,3 +351,28 @@ unlike( $result->output, qr/$mountpoint2_valid/, "output data does not have $mou
351$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^barbazJodsf\$'"); 351$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^barbazJodsf\$'");
352like( $result->output, qr/$mountpoint_valid/, "ignore: output data does have $mountpoint_valid when regex doesn't match"); 352like( $result->output, qr/$mountpoint_valid/, "ignore: output data does have $mountpoint_valid when regex doesn't match");
353like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mountpoint2_valid when regex doesn't match"); 353like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mountpoint2_valid when regex doesn't match");
354
355# ignore-missing: exit okay, when fs is not accessible
356$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p /bob");
357cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for not existing filesystem /bob");
358like( $result->output, '/^DISK OK - No disks were found for provided parameters; ignored paths: /bob;.*$/', 'Output OK');
359
360# ignore-missing: exit okay, when regex does not match
361$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r /bob");
362cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching");
363like( $result->output, '/^DISK OK - No disks were found for provided parameters;.*$/', 'Output OK');
364
365# ignore-missing: exit okay, when fs with exact match (-E) is not found
366$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -E -p /etc");
367cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay when exact match does not find fs");
368like( $result->output, '/^DISK OK - No disks were found for provided parameters; ignored paths: /etc;.*$/', 'Output OK');
369
370# ignore-missing: exit okay, when checking one existing fs and one non-existing fs (regex)
371$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r '/bob' -r '^/$'");
372cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching");
373like( $result->output, '/^DISK OK - free space: / .*$/', 'Output OK');
374
375# ignore-missing: exit okay, when checking one existing fs and one non-existing fs (path)
376$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p '/bob' -p '/'");
377cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching");
378like( $result->output, '/^DISK OK - free space: / .*; ignored paths: /bob;.*$/', 'Output OK'); \ No newline at end of file