summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authortonvoon <ton.voon@opsview.com>2010-06-23 15:56:29 +0000
committertonvoon <ton.voon@opsview.com>2010-06-23 15:56:29 +0000
commitb8e2850c1add8031a11d951bec1459b203582299 (patch)
tree174f6b7d59a3f7156504218e3c235e6d95c9a00a /plugins
parent1252195ed5cdf7b5f5fdc1fd5f2b09827a46f6ce (diff)
downloadmonitoring-plugins-b8e2850c1add8031a11d951bec1459b203582299.tar.gz
Added option to invert search results
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_snmp.c20
-rwxr-xr-xplugins/tests/check_snmp.t18
-rw-r--r--plugins/tests/check_snmp_agent.pl6
3 files changed, 34 insertions, 10 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c
index bf210222..d3871040 100644
--- a/plugins/check_snmp.c
+++ b/plugins/check_snmp.c
@@ -62,6 +62,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
62/* Longopts only arguments */ 62/* Longopts only arguments */
63#define L_CALCULATE_RATE CHAR_MAX+1 63#define L_CALCULATE_RATE CHAR_MAX+1
64#define L_RATE_MULTIPLIER CHAR_MAX+2 64#define L_RATE_MULTIPLIER CHAR_MAX+2
65#define L_INVERT_SEARCH CHAR_MAX+3
65 66
66/* Gobble to string - stop incrementing c when c[0] match one of the 67/* Gobble to string - stop incrementing c when c[0] match one of the
67 * characters in s */ 68 * characters in s */
@@ -115,6 +116,7 @@ char *units;
115char *port; 116char *port;
116char *snmpcmd; 117char *snmpcmd;
117char string_value[MAX_INPUT_BUFFER] = ""; 118char string_value[MAX_INPUT_BUFFER] = "";
119int invert_search=0;
118char **labels = NULL; 120char **labels = NULL;
119char **unitv = NULL; 121char **unitv = NULL;
120size_t nlabels = 0; 122size_t nlabels = 0;
@@ -433,16 +435,16 @@ main (int argc, char **argv)
433 /* Process this block for string matching */ 435 /* Process this block for string matching */
434 else if (eval_method[i] & CRIT_STRING) { 436 else if (eval_method[i] & CRIT_STRING) {
435 if (strcmp (show, string_value)) 437 if (strcmp (show, string_value))
436 iresult = STATE_CRITICAL; 438 iresult = (invert_search==0) ? STATE_CRITICAL : STATE_OK;
437 else 439 else
438 iresult = STATE_OK; 440 iresult = (invert_search==0) ? STATE_OK : STATE_CRITICAL;
439 } 441 }
440 442
441 /* Process this block for regex matching */ 443 /* Process this block for regex matching */
442 else if (eval_method[i] & CRIT_REGEX) { 444 else if (eval_method[i] & CRIT_REGEX) {
443 excode = regexec (&preg, response, 10, pmatch, eflags); 445 excode = regexec (&preg, response, 10, pmatch, eflags);
444 if (excode == 0) { 446 if (excode == 0) {
445 iresult = STATE_OK; 447 iresult = (invert_search==0) ? STATE_OK : STATE_CRITICAL;
446 } 448 }
447 else if (excode != REG_NOMATCH) { 449 else if (excode != REG_NOMATCH) {
448 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER); 450 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
@@ -450,7 +452,7 @@ main (int argc, char **argv)
450 exit (STATE_CRITICAL); 452 exit (STATE_CRITICAL);
451 } 453 }
452 else { 454 else {
453 iresult = STATE_CRITICAL; 455 iresult = (invert_search==0) ? STATE_CRITICAL : STATE_OK;
454 } 456 }
455 } 457 }
456 458
@@ -584,6 +586,7 @@ process_arguments (int argc, char **argv)
584 {"next", no_argument, 0, 'n'}, 586 {"next", no_argument, 0, 'n'},
585 {"rate", no_argument, 0, L_CALCULATE_RATE}, 587 {"rate", no_argument, 0, L_CALCULATE_RATE},
586 {"rate-multiplier", required_argument, 0, L_RATE_MULTIPLIER}, 588 {"rate-multiplier", required_argument, 0, L_RATE_MULTIPLIER},
589 {"invert-search", no_argument, 0, L_INVERT_SEARCH},
587 {0, 0, 0, 0} 590 {0, 0, 0, 0}
588 }; 591 };
589 592
@@ -796,6 +799,9 @@ process_arguments (int argc, char **argv)
796 if(!is_integer(optarg)||(rate_multiplier=atoi(optarg)<=0)) 799 if(!is_integer(optarg)||(rate_multiplier=atoi(optarg)<=0))
797 usage2(_("Rate multiplier must be a positive integer"),optarg); 800 usage2(_("Rate multiplier must be a positive integer"),optarg);
798 break; 801 break;
802 case L_INVERT_SEARCH:
803 invert_search=1;
804 break;
799 } 805 }
800 } 806 }
801 807
@@ -1044,10 +1050,12 @@ print_help (void)
1044 printf (" %s\n", _("Return OK state (for that OID) if extended regular expression REGEX matches")); 1050 printf (" %s\n", _("Return OK state (for that OID) if extended regular expression REGEX matches"));
1045 printf (" %s\n", "-R, --eregi=REGEX"); 1051 printf (" %s\n", "-R, --eregi=REGEX");
1046 printf (" %s\n", _("Return OK state (for that OID) if case-insensitive extended REGEX matches")); 1052 printf (" %s\n", _("Return OK state (for that OID) if case-insensitive extended REGEX matches"));
1047 printf (" %s\n", "-l, --label=STRING"); 1053 printf (" %s\n", "--invert-search");
1048 printf (" %s\n", _("Prefix label for output from plugin (default -l 'SNMP')")); 1054 printf (" %s\n", _("Invert search result (CRITICAL if found)"));
1049 1055
1050 /* Output Formatting */ 1056 /* Output Formatting */
1057 printf (" %s\n", "-l, --label=STRING");
1058 printf (" %s\n", _("Prefix label for output from plugin"));
1051 printf (" %s\n", "-u, --units=STRING"); 1059 printf (" %s\n", "-u, --units=STRING");
1052 printf (" %s\n", _("Units label(s) for output data (e.g., 'sec.').")); 1060 printf (" %s\n", _("Units label(s) for output data (e.g., 'sec.')."));
1053 printf (" %s\n", "-D, --output-delimiter=STRING"); 1061 printf (" %s\n", "-D, --output-delimiter=STRING");
diff --git a/plugins/tests/check_snmp.t b/plugins/tests/check_snmp.t
index 17420792..a7a8d2cf 100755
--- a/plugins/tests/check_snmp.t
+++ b/plugins/tests/check_snmp.t
@@ -51,7 +51,7 @@ if ($ARGV[0] && $ARGV[0] eq "-d") {
51 } 51 }
52} 52}
53 53
54my $tests = 21; 54my $tests = 29;
55if (-x "./check_snmp") { 55if (-x "./check_snmp") {
56 plan tests => $tests; 56 plan tests => $tests;
57} else { 57} else {
@@ -141,5 +141,21 @@ is($res->return_code, 0, "OK as no thresholds" );
141is($res->output, "SNMP RATE OK - inoctets 333 | inoctets-rate=333 ", "Check rate decreases due to longer interval"); 141is($res->output, "SNMP RATE OK - inoctets 333 | inoctets-rate=333 ", "Check rate decreases due to longer interval");
142 142
143 143
144$res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 -s '\"stringtests\"'" );
145is($res->return_code, 0, "OK as string matches" );
146is($res->output, 'SNMP OK - "stringtests" | ', "Good string match" );
147
148$res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 -s ring" );
149is($res->return_code, 2, "CRITICAL as string doesn't match (though is a substring)" );
150is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Failed string match" );
151
152$res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 --invert-search -s '\"stringtests\"'" );
153is($res->return_code, 2, "CRITICAL as string matches but inverted" );
154is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Inverted string match" );
155
156$res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 --invert-search -s ring" );
157is($res->return_code, 0, "OK as string doesn't match but inverted" );
158is($res->output, 'SNMP OK - "stringtests" | ', "OK as inverted string no match" );
159
144 160
145 161
diff --git a/plugins/tests/check_snmp_agent.pl b/plugins/tests/check_snmp_agent.pl
index 425caeb3..8784ab12 100644
--- a/plugins/tests/check_snmp_agent.pl
+++ b/plugins/tests/check_snmp_agent.pl
@@ -33,9 +33,9 @@ ends with with this: C:\\';
33my $multilin5 = 'And now have fun with with this: "C:\\" 33my $multilin5 = 'And now have fun with with this: "C:\\"
34because we\'re not done yet!'; 34because we\'re not done yet!';
35 35
36my @fields = (ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_UNSIGNED, ASN_UNSIGNED, ASN_COUNTER, ASN_COUNTER64, ASN_UNSIGNED, ASN_COUNTER); 36my @fields = (ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_UNSIGNED, ASN_UNSIGNED, ASN_COUNTER, ASN_COUNTER64, ASN_UNSIGNED, ASN_COUNTER, ASN_OCTET_STR);
37my @values = ($multiline, $multilin2, $multilin3, $multilin4, $multilin5, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)), 64000); 37my @values = ($multiline, $multilin2, $multilin3, $multilin4, $multilin5, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)), 64000, "stringtests");
38my @incrts = (undef, undef, undef, undef, undef, 1000, -500, 1000, 100000, undef, 666); 38my @incrts = (undef, undef, undef, undef, undef, 1000, -500, 1000, 100000, undef, 666, undef);
39 39
40# Number of elements in our OID 40# Number of elements in our OID
41my $oidelts; 41my $oidelts;