From b8e2850c1add8031a11d951bec1459b203582299 Mon Sep 17 00:00:00 2001 From: tonvoon Date: Wed, 23 Jun 2010 15:56:29 +0000 Subject: Added option to invert search results --- plugins/check_snmp.c | 20 ++++++++++++++------ plugins/tests/check_snmp.t | 18 +++++++++++++++++- plugins/tests/check_snmp_agent.pl | 6 +++--- 3 files changed, 34 insertions(+), 10 deletions(-) (limited to 'plugins') 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"; /* Longopts only arguments */ #define L_CALCULATE_RATE CHAR_MAX+1 #define L_RATE_MULTIPLIER CHAR_MAX+2 +#define L_INVERT_SEARCH CHAR_MAX+3 /* Gobble to string - stop incrementing c when c[0] match one of the * characters in s */ @@ -115,6 +116,7 @@ char *units; char *port; char *snmpcmd; char string_value[MAX_INPUT_BUFFER] = ""; +int invert_search=0; char **labels = NULL; char **unitv = NULL; size_t nlabels = 0; @@ -433,16 +435,16 @@ main (int argc, char **argv) /* Process this block for string matching */ else if (eval_method[i] & CRIT_STRING) { if (strcmp (show, string_value)) - iresult = STATE_CRITICAL; + iresult = (invert_search==0) ? STATE_CRITICAL : STATE_OK; else - iresult = STATE_OK; + iresult = (invert_search==0) ? STATE_OK : STATE_CRITICAL; } /* Process this block for regex matching */ else if (eval_method[i] & CRIT_REGEX) { excode = regexec (&preg, response, 10, pmatch, eflags); if (excode == 0) { - iresult = STATE_OK; + iresult = (invert_search==0) ? STATE_OK : STATE_CRITICAL; } else if (excode != REG_NOMATCH) { regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER); @@ -450,7 +452,7 @@ main (int argc, char **argv) exit (STATE_CRITICAL); } else { - iresult = STATE_CRITICAL; + iresult = (invert_search==0) ? STATE_CRITICAL : STATE_OK; } } @@ -584,6 +586,7 @@ process_arguments (int argc, char **argv) {"next", no_argument, 0, 'n'}, {"rate", no_argument, 0, L_CALCULATE_RATE}, {"rate-multiplier", required_argument, 0, L_RATE_MULTIPLIER}, + {"invert-search", no_argument, 0, L_INVERT_SEARCH}, {0, 0, 0, 0} }; @@ -796,6 +799,9 @@ process_arguments (int argc, char **argv) if(!is_integer(optarg)||(rate_multiplier=atoi(optarg)<=0)) usage2(_("Rate multiplier must be a positive integer"),optarg); break; + case L_INVERT_SEARCH: + invert_search=1; + break; } } @@ -1044,10 +1050,12 @@ print_help (void) printf (" %s\n", _("Return OK state (for that OID) if extended regular expression REGEX matches")); printf (" %s\n", "-R, --eregi=REGEX"); printf (" %s\n", _("Return OK state (for that OID) if case-insensitive extended REGEX matches")); - printf (" %s\n", "-l, --label=STRING"); - printf (" %s\n", _("Prefix label for output from plugin (default -l 'SNMP')")); + printf (" %s\n", "--invert-search"); + printf (" %s\n", _("Invert search result (CRITICAL if found)")); /* Output Formatting */ + printf (" %s\n", "-l, --label=STRING"); + printf (" %s\n", _("Prefix label for output from plugin")); printf (" %s\n", "-u, --units=STRING"); printf (" %s\n", _("Units label(s) for output data (e.g., 'sec.').")); 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") { } } -my $tests = 21; +my $tests = 29; if (-x "./check_snmp") { plan tests => $tests; } else { @@ -141,5 +141,21 @@ is($res->return_code, 0, "OK as no thresholds" ); is($res->output, "SNMP RATE OK - inoctets 333 | inoctets-rate=333 ", "Check rate decreases due to longer interval"); +$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\"'" ); +is($res->return_code, 0, "OK as string matches" ); +is($res->output, 'SNMP OK - "stringtests" | ', "Good string match" ); + +$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" ); +is($res->return_code, 2, "CRITICAL as string doesn't match (though is a substring)" ); +is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Failed string match" ); + +$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\"'" ); +is($res->return_code, 2, "CRITICAL as string matches but inverted" ); +is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Inverted string match" ); + +$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" ); +is($res->return_code, 0, "OK as string doesn't match but inverted" ); +is($res->output, 'SNMP OK - "stringtests" | ', "OK as inverted string no match" ); + 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:\\'; my $multilin5 = 'And now have fun with with this: "C:\\" because we\'re not done yet!'; -my @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); -my @values = ($multiline, $multilin2, $multilin3, $multilin4, $multilin5, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)), 64000); -my @incrts = (undef, undef, undef, undef, undef, 1000, -500, 1000, 100000, undef, 666); +my @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); +my @values = ($multiline, $multilin2, $multilin3, $multilin4, $multilin5, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)), 64000, "stringtests"); +my @incrts = (undef, undef, undef, undef, undef, 1000, -500, 1000, 100000, undef, 666, undef); # Number of elements in our OID my $oidelts; -- cgit v1.2.3