From fbf2154c5a68915dc6197e7a2ff56e56ef924220 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Thu, 25 May 2006 15:34:54 +0000 Subject: [PATCH] Option to invert results from a regexp pattern match git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1404 f882894a-f735-0410-b71e-b25c423dba1c --- plugins/check_http.c | 41 ++++++++++++++++++++++++++++------------- plugins/t/check_http.t | 19 +++++++++++++++++-- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/plugins/check_http.c b/plugins/check_http.c index 1869837..8eadc62 100644 --- a/plugins/check_http.c +++ b/plugins/check_http.c @@ -62,6 +62,7 @@ char regexp[MAX_RE_SIZE]; char errbuf[MAX_INPUT_BUFFER]; int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE; int errcode; +int invert_regex = 0; struct timeval tv; @@ -156,6 +157,10 @@ process_arguments (int argc, char **argv) { int c = 1; + enum { + INVERT_REGEX = CHAR_MAX + 1 + }; + int option = 0; static struct option longopts[] = { STD_LONG_OPTS, @@ -180,6 +185,7 @@ process_arguments (int argc, char **argv) {"max-age", required_argument, 0, 'M'}, {"content-type", required_argument, 0, 'T'}, {"pagesize", required_argument, 0, 'm'}, + {"invert-regex", no_argument, NULL, INVERT_REGEX}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {0, 0, 0, 0} @@ -346,6 +352,9 @@ process_arguments (int argc, char **argv) return ERROR; } break; + case INVERT_REGEX: + invert_regex = 1; + break; case '4': address_family = AF_INET; break; @@ -985,25 +994,29 @@ check_http (void) if (strlen (regexp)) { errcode = regexec (&preg, page, REGS, pmatch, 0); - if (errcode == 0) { + if ((errcode == 0 && invert_regex == 0) || (errcode == REG_NOMATCH && invert_regex == 1)) { printf (_("HTTP OK %s - %.3f second response time %s%s|%s %s\n"), status_line, elapsed_time, timestamp, (display_html ? "" : ""), perfd_time (elapsed_time), perfd_size (pagesize)); exit (STATE_OK); } + else if ((errcode == REG_NOMATCH && invert_regex == 0) || (errcode == 0 && invert_regex == 1)) { + if (invert_regex == 0) + msg = strdup(_("pattern not found")); + else + msg = strdup(_("pattern found")); + printf (_("%s - %s%s|%s %s\n"), + _("CRITICAL"), + msg, + (display_html ? "" : ""), + perfd_time (elapsed_time), perfd_size (pagesize)); + exit (STATE_CRITICAL); + } else { - if (errcode == REG_NOMATCH) { - printf (_("CRITICAL - pattern not found%s|%s %s\n"), - (display_html ? "" : ""), - perfd_time (elapsed_time), perfd_size (pagesize)); - exit (STATE_CRITICAL); - } - else { - regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); - printf (_("CRITICAL - Execute Error: %s\n"), errbuf); - exit (STATE_CRITICAL); - } + regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); + printf (_("CRITICAL - Execute Error: %s\n"), errbuf); + exit (STATE_CRITICAL); } } @@ -1265,7 +1278,9 @@ certificate expiration times.")); -r, --regex, --ereg=STRING\n\ Search page for regex STRING\n\ -R, --eregi=STRING\n\ - Search page for case-insensitive regex STRING\n")); + Search page for case-insensitive regex STRING\n\ + --invert-regex\n\ + Return CRITICAL if found, OK if not\n")); printf (_("\ -a, --authorization=AUTH_PAIR\n\ diff --git a/plugins/t/check_http.t b/plugins/t/check_http.t index 3154151..4e3c06c 100644 --- a/plugins/t/check_http.t +++ b/plugins/t/check_http.t @@ -9,7 +9,7 @@ use strict; use Test::More; use NPTest; -plan tests => 14; +plan tests => 21; my $successOutput = '/OK.*HTTP.*second/'; @@ -79,4 +79,19 @@ $res = NPTest->testCmd( ); cmp_ok( $res->return_code, "==", 0, "Can read https for www.e-paycobalt.com (uses AES certificate)" ); - +$res = NPTest->testCmd( "./check_http -H altinity.com -r 'nagios'" ); +cmp_ok( $res->return_code, "==", 0, "Got a reference to 'nagios'"); + +$res = NPTest->testCmd( "./check_http -H altinity.com -r 'nAGiOs'" ); +cmp_ok( $res->return_code, "==", 2, "Not got 'nAGiOs'"); +like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'"); + +$res = NPTest->testCmd( "./check_http -H altinity.com -R 'nAGiOs'" ); +cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'nAGiOs'"); + +$res = NPTest->testCmd( "./check_http -H altinity.com -r 'nagios' --invert-regex" ); +cmp_ok( $res->return_code, "==", 2, "Invert results work when found"); +like ( $res->output, "/pattern found/", "Error message says 'pattern found'"); + +$res = NPTest->testCmd( "./check_http -H altinity.com -r 'nAGiOs' --invert-regex" ); +cmp_ok( $res->return_code, "==", 0, "And also when not found"); -- 2.30.2