summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristian Schuster <116557017+KriSchu@users.noreply.github.com>2023-02-19 21:49:30 (GMT)
committerKristian Schuster <116557017+KriSchu@users.noreply.github.com>2023-02-19 21:49:30 (GMT)
commitba78c32018658608a31c293beef89ec82b9ba9d3 (patch)
tree20ea9a99aeade63d1d996b7f05359552fa0631dc
parent9898a8ad7dabfabfe80785585a5bbc30b678bdb0 (diff)
downloadmonitoring-plugins-ba78c32.tar.gz
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.
-rw-r--r--plugins/check_disk.c103
1 files changed, 78 insertions, 25 deletions
diff --git a/plugins/check_disk.c b/plugins/check_disk.c
index 8df9e7e..c1cfb13 100644
--- a/plugins/check_disk.c
+++ b/plugins/check_disk.c
@@ -117,7 +117,7 @@ enum
117}; 117};
118 118
119#ifdef _AIX 119#ifdef _AIX
120 #pragma alloca 120#pragma alloca
121#endif 121#endif
122 122
123int process_arguments (int, char **); 123int process_arguments (int, char **);
@@ -127,7 +127,7 @@ int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, ch
127void print_help (void); 127void print_help (void);
128void print_usage (void); 128void print_usage (void);
129double calculate_percent(uintmax_t, uintmax_t); 129double calculate_percent(uintmax_t, uintmax_t);
130void stat_path (struct parameter_list *p); 130bool stat_path (struct parameter_list *p);
131void get_stats (struct parameter_list *p, struct fs_usage *fsp); 131void get_stats (struct parameter_list *p, struct fs_usage *fsp);
132void get_path_stats (struct parameter_list *p, struct fs_usage *fsp); 132void get_path_stats (struct parameter_list *p, struct fs_usage *fsp);
133 133
@@ -157,6 +157,7 @@ char *crit_usedinodes_percent = NULL;
157char *warn_freeinodes_percent = NULL; 157char *warn_freeinodes_percent = NULL;
158char *crit_freeinodes_percent = NULL; 158char *crit_freeinodes_percent = NULL;
159int path_selected = FALSE; 159int path_selected = FALSE;
160int path_ignored = FALSE;
160char *group = NULL; 161char *group = NULL;
161struct stat *stat_buf; 162struct stat *stat_buf;
162struct name_list *seen = NULL; 163struct name_list *seen = NULL;
@@ -168,10 +169,12 @@ main (int argc, char **argv)
168 int result = STATE_UNKNOWN; 169 int result = STATE_UNKNOWN;
169 int disk_result = STATE_UNKNOWN; 170 int disk_result = STATE_UNKNOWN;
170 char *output; 171 char *output;
172 char *ignored;
171 char *details; 173 char *details;
172 char *perf; 174 char *perf;
173 char *perf_ilabel; 175 char *perf_ilabel;
174 char *preamble; 176 char *preamble;
177 char *ignored_preamble;
175 char *flag_header; 178 char *flag_header;
176 int temp_result; 179 int temp_result;
177 180
@@ -183,8 +186,10 @@ main (int argc, char **argv)
183 char mountdir[32]; 186 char mountdir[32];
184#endif 187#endif
185 188
186 preamble = strdup (" - free space:"); 189 preamble = strdup (" free space:");
190 ignored_preamble = strdup (" ignored paths:");
187 output = strdup (""); 191 output = strdup ("");
192 ignored = strdup ("");
188 details = strdup (""); 193 details = strdup ("");
189 perf = strdup (""); 194 perf = strdup ("");
190 perf_ilabel = strdup (""); 195 perf_ilabel = strdup ("");
@@ -205,7 +210,7 @@ main (int argc, char **argv)
205 /* If a list of paths has not been selected, find entire 210 /* If a list of paths has not been selected, find entire
206 mount list and create list of paths 211 mount list and create list of paths
207 */ 212 */
208 if (path_selected == FALSE) { 213 if (path_selected == FALSE && path_ignored == FALSE) {
209 for (me = mount_list; me; me = me->me_next) { 214 for (me = mount_list; me; me = me->me_next) {
210 if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) { 215 if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) {
211 path = np_add_parameter(&path_select_list, me->me_mountdir); 216 path = np_add_parameter(&path_select_list, me->me_mountdir);
@@ -215,19 +220,40 @@ main (int argc, char **argv)
215 set_all_thresholds(path); 220 set_all_thresholds(path);
216 } 221 }
217 } 222 }
218 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 }
219 227
220 /* Error if no match found for specified paths */ 228 /* Error if no match found for specified paths */
221 temp_list = path_select_list; 229 temp_list = path_select_list;
222 230
223 while (temp_list) { 231 while (path_select_list) {
224 if (! temp_list->best_match && ignore_missing == 1) { 232 if (! path_select_list->best_match && ignore_missing == 1) {
225 die (STATE_OK, _("DISK %s: %s not found (ignoring)\n"), _("OK"), temp_list->name); 233 /* If the first element will be deleted, the temp_list must be updated with the new start address as well */
226 } else if (! temp_list->best_match) { 234 if (path_select_list == temp_list) {
227 die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name); 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;
228 } 247 }
248 }
249
250 path_select_list = temp_list;
229 251
230 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 }
231 } 257 }
232 258
233 /* Process for every path in list */ 259 /* Process for every path in list */
@@ -246,6 +272,10 @@ main (int argc, char **argv)
246 272
247 me = path->best_match; 273 me = path->best_match;
248 274
275 if (!me) {
276 continue;
277 }
278
249#ifdef __CYGWIN__ 279#ifdef __CYGWIN__
250 if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11) 280 if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11)
251 continue; 281 continue;
@@ -264,8 +294,12 @@ main (int argc, char **argv)
264 if (path->group == NULL) { 294 if (path->group == NULL) {
265 /* Skip remote filesystems if we're not interested in them */ 295 /* Skip remote filesystems if we're not interested in them */
266 if (me->me_remote && show_local_fs) { 296 if (me->me_remote && show_local_fs) {
267 if (stat_remote_fs) 297 if (stat_remote_fs) {
268 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 }
269 continue; 303 continue;
270 /* 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 */
271 } else if (me->me_dummy && !show_all_fs) { 305 } else if (me->me_dummy && !show_all_fs) {
@@ -284,7 +318,13 @@ main (int argc, char **argv)
284 } 318 }
285 } 319 }
286 320
287 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 }
288 get_fs_usage (me->me_mountdir, me->me_devname, &fsp); 328 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
289 329
290 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { 330 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
@@ -415,8 +455,12 @@ main (int argc, char **argv)
415 if (verbose >= 2) 455 if (verbose >= 2)
416 xasprintf (&output, "%s%s", output, details); 456 xasprintf (&output, "%s%s", output, details);
417 457
458 if (strcmp(output, "") == 0) {
459 preamble = "";
460 xasprintf (&output, " No disks were found for provided parameters;");
461 }
418 462
419 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);
420 return result; 464 return result;
421} 465}
422 466
@@ -637,12 +681,19 @@ process_arguments (int argc, char **argv)
637 /* 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 */
638 if (! (se = np_find_parameter(path_select_list, optarg))) { 682 if (! (se = np_find_parameter(path_select_list, optarg))) {
639 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 }
640 } 689 }
641 se->group = group; 690 se->group = group;
642 set_all_thresholds(se); 691 set_all_thresholds(se);
643 692
644 /* 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 */
645 stat_path(se); 694 if (!stat_path(se)) {
695 break;
696 }
646 /* 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
647 * 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
648 * 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 */
@@ -761,10 +812,11 @@ process_arguments (int argc, char **argv)
761 } 812 }
762 } 813 }
763 814
764 if (!fnd && ignore_missing == 1) 815 if (!fnd && ignore_missing == 1) {
765 die (STATE_OK, "DISK %s: %s - %s\n",_("OK"), 816 path_ignored = TRUE;
766 _("Regular expression did not match any path or disk (ignoring)"), optarg); 817 /* path_selected = TRUE;*/
767 else if (!fnd) 818 break;
819 } else if (!fnd)
768 die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), 820 die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"),
769 _("Regular expression did not match any path or disk"), optarg); 821 _("Regular expression did not match any path or disk"), optarg);
770 822
@@ -936,7 +988,7 @@ print_help (void)
936 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)"));
937 printf (" %s\n", "--ignore-missing"); 989 printf (" %s\n", "--ignore-missing");
938 printf (" %s\n", _("Return OK if no filesystem matches, filesystem does not exist or is inaccessible.")); 990 printf (" %s\n", _("Return OK if no filesystem matches, filesystem does not exist or is inaccessible."));
939 printf (" %s\n", _("(Provide this option before -r / --ereg-path if used)")); 991 printf (" %s\n", _("(Provide this option before -p / -r / --ereg-path if used)"));
940 printf (UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 992 printf (UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
941 printf (" %s\n", "-u, --units=STRING"); 993 printf (" %s\n", "-u, --units=STRING");
942 printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)")); 994 printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)"));
@@ -970,7 +1022,7 @@ print_usage (void)
970 printf ("[-t timeout] [-u unit] [-v] [-X type] [-N type]\n"); 1022 printf ("[-t timeout] [-u unit] [-v] [-X type] [-N type]\n");
971} 1023}
972 1024
973void 1025bool
974stat_path (struct parameter_list *p) 1026stat_path (struct parameter_list *p)
975{ 1027{
976 /* Stat entry to check that dir exists and is accessible */ 1028 /* Stat entry to check that dir exists and is accessible */
@@ -980,13 +1032,13 @@ stat_path (struct parameter_list *p)
980 if (verbose >= 3) 1032 if (verbose >= 3)
981 printf("stat failed on %s\n", p->name); 1033 printf("stat failed on %s\n", p->name);
982 if (ignore_missing == 1) { 1034 if (ignore_missing == 1) {
983 printf("DISK %s - ", _("OK")); 1035 return false;
984 die (STATE_OK, _("%s %s: %s\n"), p->name, _("is not accessible (ignoring)"), strerror(errno));
985 } else { 1036 } else {
986 printf("DISK %s - ", _("CRITICAL")); 1037 printf("DISK %s - ", _("CRITICAL"));
987 die (STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno)); 1038 die (STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno));
988 } 1039 }
989 } 1040 }
1041 return true;
990} 1042}
991 1043
992 1044
@@ -1006,7 +1058,8 @@ get_stats (struct parameter_list *p, struct fs_usage *fsp) {
1006 continue; 1058 continue;
1007#endif 1059#endif
1008 if (p_list->group && ! (strcmp(p_list->group, p->group))) { 1060 if (p_list->group && ! (strcmp(p_list->group, p->group))) {
1009 stat_path(p_list); 1061 if (! stat_path(p_list))
1062 continue;
1010 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);
1011 get_path_stats(p_list, &tmpfsp); 1064 get_path_stats(p_list, &tmpfsp);
1012 if (verbose >= 3) 1065 if (verbose >= 3)