summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_disk.c81
-rw-r--r--plugins/t/check_disk.t31
2 files changed, 96 insertions, 16 deletions
diff --git a/plugins/check_disk.c b/plugins/check_disk.c
index 90b2248..d267409 100644
--- a/plugins/check_disk.c
+++ b/plugins/check_disk.c
@@ -447,15 +447,16 @@ int
447process_arguments (int argc, char **argv) 447process_arguments (int argc, char **argv)
448{ 448{
449 int c, err; 449 int c, err;
450 struct parameter_list *se, *se2; 450 struct parameter_list *se;
451 struct parameter_list *temp_list; 451 struct parameter_list *temp_list = NULL, *previous = NULL;
452 struct parameter_list *temp_path_select_list = NULL; 452 struct parameter_list *temp_path_select_list = NULL;
453 struct mount_entry *me; 453 struct mount_entry *me, *temp_me;
454 int result = OK; 454 int result = OK;
455 regex_t re; 455 regex_t re;
456 int cflags = REG_NOSUB | REG_EXTENDED; 456 int cflags = REG_NOSUB | REG_EXTENDED;
457 int default_cflags = cflags;
457 char errbuf[MAX_INPUT_BUFFER]; 458 char errbuf[MAX_INPUT_BUFFER];
458 bool fnd = false; 459 int fnd = 0;
459 460
460 int option = 0; 461 int option = 0;
461 static struct option longopts[] = { 462 static struct option longopts[] = {
@@ -478,9 +479,14 @@ process_arguments (int argc, char **argv)
478 {"eregi-partition", required_argument, 0, 'R'}, 479 {"eregi-partition", required_argument, 0, 'R'},
479 {"ereg-path", required_argument, 0, 'r'}, 480 {"ereg-path", required_argument, 0, 'r'},
480 {"ereg-partition", required_argument, 0, 'r'}, 481 {"ereg-partition", required_argument, 0, 'r'},
482 {"ignore-ereg-path", required_argument, 0, 'i'},
483 {"ignore-ereg-partition", required_argument, 0, 'i'},
484 {"ignore-eregi-path", required_argument, 0, 'I'},
485 {"ignore-eregi-partition", required_argument, 0, 'I'},
481 {"mountpoint", no_argument, 0, 'M'}, 486 {"mountpoint", no_argument, 0, 'M'},
482 {"errors-only", no_argument, 0, 'e'}, 487 {"errors-only", no_argument, 0, 'e'},
483 {"exact-match", no_argument, 0, 'E'}, 488 {"exact-match", no_argument, 0, 'E'},
489 {"all", no_argument, 0, 'A'},
484 {"verbose", no_argument, 0, 'v'}, 490 {"verbose", no_argument, 0, 'v'},
485 {"quiet", no_argument, 0, 'q'}, 491 {"quiet", no_argument, 0, 'q'},
486 {"clear", no_argument, 0, 'C'}, 492 {"clear", no_argument, 0, 'C'},
@@ -499,7 +505,7 @@ process_arguments (int argc, char **argv)
499 strcpy (argv[c], "-t"); 505 strcpy (argv[c], "-t");
500 506
501 while (1) { 507 while (1) {
502 c = getopt_long (argc, argv, "+?VqhveCt:c:w:K:W:u:p:x:X:mklg:R:r:ME", longopts, &option); 508 c = getopt_long (argc, argv, "+?VqhveCt:c:w:K:W:u:p:x:X:mklg:R:r:i:I:MEA", longopts, &option);
503 509
504 if (c == -1 || c == EOF) 510 if (c == -1 || c == EOF)
505 break; 511 break;
@@ -613,18 +619,13 @@ process_arguments (int argc, char **argv)
613 die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -p\n")); 619 die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -p\n"));
614 } 620 }
615 621
616 /* get the real mountdir of the specified path. np_find_parameter won't find an entry if -p is not
617 * exactly the same string as the mountdir */
618 se2 = np_add_parameter(&temp_path_select_list, optarg);
619 np_set_best_match(se2, mount_list, FALSE);
620
621
622 /* add parameter if not found. overwrite thresholds if path has already been added */ 622 /* add parameter if not found. overwrite thresholds if path has already been added */
623 if (! (se = np_find_parameter(path_select_list, optarg))) { 623 if (! (se = np_find_parameter(path_select_list, optarg))) {
624 se = np_add_parameter(&path_select_list, optarg); 624 se = np_add_parameter(&path_select_list, optarg);
625 } 625 }
626 se->group = group; 626 se->group = group;
627 set_all_thresholds(se); 627 set_all_thresholds(se);
628 np_set_best_match(se, mount_list, exact_match);
628 path_selected = true; 629 path_selected = true;
629 break; 630 break;
630 case 'x': /* exclude path or partition */ 631 case 'x': /* exclude path or partition */
@@ -644,13 +645,56 @@ process_arguments (int argc, char **argv)
644 erronly = TRUE; 645 erronly = TRUE;
645 break; 646 break;
646 case 'E': 647 case 'E':
648 if (path_selected)
649 die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set -E before selecting pathes\n"));
647 exact_match = TRUE; 650 exact_match = TRUE;
648 break; 651 break;
649 case 'g': 652 case 'g':
650 if (path_selected) 653 if (path_selected)
651 die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set group value before using -p\n")); 654 die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set group value before selecting pathes \n"));
652 group = optarg; 655 group = optarg;
653 break; 656 break;
657 case 'I':
658 cflags |= REG_ICASE;
659 case 'i':
660 if (!path_selected)
661 die (STATE_UNKNOWN, "DISK %s: %s\n", _("UNKNOWN"), _("Pathes need to be selected before using -i/-I. Use -A to select all pathes explicitly"));
662 err = regcomp(&re, optarg, cflags);
663 if (err != 0) {
664 regerror (err, &re, errbuf, MAX_INPUT_BUFFER);
665 die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), _("Could not compile regular expression"), errbuf);
666 }
667
668 temp_list = path_select_list;
669
670 previous = NULL;
671 while (temp_list) {
672 if (temp_list->best_match) {
673 if (np_regex_match_mount_entry(temp_list->best_match, &re)) {
674
675 if (verbose >=3)
676 printf("ignoring %s matching regex\n", temp_list->name);
677
678 temp_list = np_del_parameter(temp_list, previous);
679 /* pointer to first element needs to be uĆ¼dated if first item gets deleted */
680 if (previous == NULL)
681 path_select_list = temp_list;
682 } else {
683 previous = temp_list;
684 temp_list = temp_list->name_next;
685 }
686 } else {
687 previous = temp_list;
688 temp_list = temp_list->name_next;
689 }
690 }
691
692
693 cflags = default_cflags;
694 break;
695
696 case 'A':
697 optarg = strdup(".*");
654 case 'R': 698 case 'R':
655 cflags |= REG_ICASE; 699 cflags |= REG_ICASE;
656 case 'r': 700 case 'r':
@@ -669,9 +713,9 @@ process_arguments (int argc, char **argv)
669 713
670 for (me = mount_list; me; me = me->me_next) { 714 for (me = mount_list; me; me = me->me_next) {
671 if (np_regex_match_mount_entry(me, &re)) { 715 if (np_regex_match_mount_entry(me, &re)) {
672 fnd = true; 716 fnd = true;
673 if (verbose > 3) 717 if (verbose > 3)
674 printf("%s %s matching expression %s\n", me->me_devname, me->me_mountdir, optarg); 718 printf("%s %s matching expression %s\n", me->me_devname, me->me_mountdir, optarg);
675 719
676 /* add parameter if not found. overwrite thresholds if path has already been added */ 720 /* add parameter if not found. overwrite thresholds if path has already been added */
677 if (! (se = np_find_parameter(path_select_list, me->me_mountdir))) { 721 if (! (se = np_find_parameter(path_select_list, me->me_mountdir))) {
@@ -688,6 +732,9 @@ process_arguments (int argc, char **argv)
688 732
689 fnd = false; 733 fnd = false;
690 path_selected = true; 734 path_selected = true;
735 np_set_best_match(path_select_list, mount_list, exact_match);
736 cflags = default_cflags;
737
691 break; 738 break;
692 case 'M': /* display mountpoint */ 739 case 'M': /* display mountpoint */
693 display_mntp = TRUE; 740 display_mntp = TRUE;
@@ -871,10 +918,16 @@ print_help (void)
871 printf (" %s\n", _("Display the mountpoint instead of the partition")); 918 printf (" %s\n", _("Display the mountpoint instead of the partition"));
872 printf (" %s\n", "-m, --megabytes"); 919 printf (" %s\n", "-m, --megabytes");
873 printf (" %s\n", _("Same as '--units MB'")); 920 printf (" %s\n", _("Same as '--units MB'"));
921 printf (" %s\n", "-A, --all");
922 printf (" %s\n", _("Explicitly select all pathes. This is equivalent to -R '.*'"));
874 printf (" %s\n", "-R, --eregi-path=PATH, --eregi-partition=PARTITION"); 923 printf (" %s\n", "-R, --eregi-path=PATH, --eregi-partition=PARTITION");
875 printf (" %s\n", _("Case insensitive regular expression for path/partition (may be repeated)")); 924 printf (" %s\n", _("Case insensitive regular expression for path/partition (may be repeated)"));
876 printf (" %s\n", "-r, --ereg-path=PATH, --ereg-partition=PARTITION"); 925 printf (" %s\n", "-r, --ereg-path=PATH, --ereg-partition=PARTITION");
877 printf (" %s\n", _("Regular expression for path or partition (may be repeated)")); 926 printf (" %s\n", _("Regular expression for path or partition (may be repeated)"));
927 printf (" %s\n", "-I, --ignore-eregi-path=PATH, --ignore-eregi-partition=PARTITION");
928 printf (" %s\n", _("Regular expression to ignore selected path/partition (case insensitive) (may be repeated)"));
929 printf (" %s\n", "-i, --ignore-ereg-path=PATH, --ignore-ereg-partition=PARTITION");
930 printf (" %s\n", _("Regular expression to ignore selected path or partition (may be repeated)"));
878 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); 931 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
879 printf (" %s\n", "-u, --units=STRING"); 932 printf (" %s\n", "-u, --units=STRING");
880 printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)")); 933 printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)"));
diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t
index 4f5c4bc..99c434a 100644
--- a/plugins/t/check_disk.t
+++ b/plugins/t/check_disk.t
@@ -24,7 +24,7 @@ my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to anoth
24if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") { 24if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") {
25 plan skip_all => "Need 2 mountpoints to test"; 25 plan skip_all => "Need 2 mountpoints to test";
26} else { 26} else {
27 plan tests => 69; 27 plan tests => 78;
28} 28}
29 29
30$result = NPTest->testCmd( 30$result = NPTest->testCmd(
@@ -284,9 +284,15 @@ $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
284cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" ); 284cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
285cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /"); 285cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
286 286
287$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E" ); 287$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -E -p /etc " );
288cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified"); 288cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
289 289
290$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E " );
291cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -p");
292
293$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -r /etc -E " );
294cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -r");
295
290$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" ); 296$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
291cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical"); 297cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
292unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it"); 298unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
@@ -318,3 +324,24 @@ cmp_ok( $result->return_code, '==', 3, "Invalid options: -p must come after grou
318$result = NPTest->testCmd( "./check_disk -w 1 -c 1 -r '('" ); 324$result = NPTest->testCmd( "./check_disk -w 1 -c 1 -r '('" );
319cmp_ok( $result->return_code, '==', 3, "Exit UNKNOWN if regex is not compileable"); 325cmp_ok( $result->return_code, '==', 3, "Exit UNKNOWN if regex is not compileable");
320 326
327# ignore: exit unknown, if all pathes are deselected using -i
328$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '$mountpoint_valid' -i '$mountpoint2_valid'" );
329cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case sensitive)");
330
331# ignore: exit unknown, if all pathes are deselected using -I
332$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -I '".uc($mountpoint_valid)."' -I '".uc($mountpoint2_valid)."'" );
333cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case insensitive)");
334
335# ignore: exit unknown, if all pathes are deselected using -i
336$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '.*'" );
337cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored using -i '.*'");
338
339# ignore: test if ignored path is actually ignored
340$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^$mountpoint2_valid\$'");
341like( $result->output, qr/$mountpoint_valid/, "output data does have $mountpoint_valid in it");
342unlike( $result->output, qr/$mountpoint2_valid/, "output data does not have $mountpoint2_valid in it");
343
344# ignore: test if all pathes are listed when ignore regex doesn't match
345$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^barbazJodsf\$'");
346like( $result->output, qr/$mountpoint_valid/, "ignore: output data does have $mountpoint_valid when regex doesn't match");
347like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mountpoint2_valid when regex doesn't match");