diff options
| author | Ton Voon <tonvoon@users.sourceforge.net> | 2006-05-25 15:34:54 +0000 |
|---|---|---|
| committer | Ton Voon <tonvoon@users.sourceforge.net> | 2006-05-25 15:34:54 +0000 |
| commit | fbf2154c5a68915dc6197e7a2ff56e56ef924220 (patch) | |
| tree | 9092d9452f4e4d310a746ccdde7d14adf7374fe5 | |
| parent | 5fd2550d4c96318b2de4a4a44e15e3c50c268e79 (diff) | |
| download | monitoring-plugins-fbf2154c5a68915dc6197e7a2ff56e56ef924220.tar.gz | |
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
| -rw-r--r-- | plugins/check_http.c | 41 | ||||
| -rw-r--r-- | 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 1869837e..8eadc62e 100644 --- a/plugins/check_http.c +++ b/plugins/check_http.c | |||
| @@ -62,6 +62,7 @@ char regexp[MAX_RE_SIZE]; | |||
| 62 | char errbuf[MAX_INPUT_BUFFER]; | 62 | char errbuf[MAX_INPUT_BUFFER]; |
| 63 | int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE; | 63 | int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE; |
| 64 | int errcode; | 64 | int errcode; |
| 65 | int invert_regex = 0; | ||
| 65 | 66 | ||
| 66 | struct timeval tv; | 67 | struct timeval tv; |
| 67 | 68 | ||
| @@ -156,6 +157,10 @@ process_arguments (int argc, char **argv) | |||
| 156 | { | 157 | { |
| 157 | int c = 1; | 158 | int c = 1; |
| 158 | 159 | ||
| 160 | enum { | ||
| 161 | INVERT_REGEX = CHAR_MAX + 1 | ||
| 162 | }; | ||
| 163 | |||
| 159 | int option = 0; | 164 | int option = 0; |
| 160 | static struct option longopts[] = { | 165 | static struct option longopts[] = { |
| 161 | STD_LONG_OPTS, | 166 | STD_LONG_OPTS, |
| @@ -180,6 +185,7 @@ process_arguments (int argc, char **argv) | |||
| 180 | {"max-age", required_argument, 0, 'M'}, | 185 | {"max-age", required_argument, 0, 'M'}, |
| 181 | {"content-type", required_argument, 0, 'T'}, | 186 | {"content-type", required_argument, 0, 'T'}, |
| 182 | {"pagesize", required_argument, 0, 'm'}, | 187 | {"pagesize", required_argument, 0, 'm'}, |
| 188 | {"invert-regex", no_argument, NULL, INVERT_REGEX}, | ||
| 183 | {"use-ipv4", no_argument, 0, '4'}, | 189 | {"use-ipv4", no_argument, 0, '4'}, |
| 184 | {"use-ipv6", no_argument, 0, '6'}, | 190 | {"use-ipv6", no_argument, 0, '6'}, |
| 185 | {0, 0, 0, 0} | 191 | {0, 0, 0, 0} |
| @@ -346,6 +352,9 @@ process_arguments (int argc, char **argv) | |||
| 346 | return ERROR; | 352 | return ERROR; |
| 347 | } | 353 | } |
| 348 | break; | 354 | break; |
| 355 | case INVERT_REGEX: | ||
| 356 | invert_regex = 1; | ||
| 357 | break; | ||
| 349 | case '4': | 358 | case '4': |
| 350 | address_family = AF_INET; | 359 | address_family = AF_INET; |
| 351 | break; | 360 | break; |
| @@ -985,25 +994,29 @@ check_http (void) | |||
| 985 | 994 | ||
| 986 | if (strlen (regexp)) { | 995 | if (strlen (regexp)) { |
| 987 | errcode = regexec (&preg, page, REGS, pmatch, 0); | 996 | errcode = regexec (&preg, page, REGS, pmatch, 0); |
| 988 | if (errcode == 0) { | 997 | if ((errcode == 0 && invert_regex == 0) || (errcode == REG_NOMATCH && invert_regex == 1)) { |
| 989 | printf (_("HTTP OK %s - %.3f second response time %s%s|%s %s\n"), | 998 | printf (_("HTTP OK %s - %.3f second response time %s%s|%s %s\n"), |
| 990 | status_line, elapsed_time, | 999 | status_line, elapsed_time, |
| 991 | timestamp, (display_html ? "</A>" : ""), | 1000 | timestamp, (display_html ? "</A>" : ""), |
| 992 | perfd_time (elapsed_time), perfd_size (pagesize)); | 1001 | perfd_time (elapsed_time), perfd_size (pagesize)); |
| 993 | exit (STATE_OK); | 1002 | exit (STATE_OK); |
| 994 | } | 1003 | } |
| 1004 | else if ((errcode == REG_NOMATCH && invert_regex == 0) || (errcode == 0 && invert_regex == 1)) { | ||
| 1005 | if (invert_regex == 0) | ||
| 1006 | msg = strdup(_("pattern not found")); | ||
| 1007 | else | ||
| 1008 | msg = strdup(_("pattern found")); | ||
| 1009 | printf (_("%s - %s%s|%s %s\n"), | ||
| 1010 | _("CRITICAL"), | ||
| 1011 | msg, | ||
| 1012 | (display_html ? "</A>" : ""), | ||
| 1013 | perfd_time (elapsed_time), perfd_size (pagesize)); | ||
| 1014 | exit (STATE_CRITICAL); | ||
| 1015 | } | ||
| 995 | else { | 1016 | else { |
| 996 | if (errcode == REG_NOMATCH) { | 1017 | regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); |
| 997 | printf (_("CRITICAL - pattern not found%s|%s %s\n"), | 1018 | printf (_("CRITICAL - Execute Error: %s\n"), errbuf); |
| 998 | (display_html ? "</A>" : ""), | 1019 | exit (STATE_CRITICAL); |
| 999 | perfd_time (elapsed_time), perfd_size (pagesize)); | ||
| 1000 | exit (STATE_CRITICAL); | ||
| 1001 | } | ||
| 1002 | else { | ||
| 1003 | regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); | ||
| 1004 | printf (_("CRITICAL - Execute Error: %s\n"), errbuf); | ||
| 1005 | exit (STATE_CRITICAL); | ||
| 1006 | } | ||
| 1007 | } | 1020 | } |
| 1008 | } | 1021 | } |
| 1009 | 1022 | ||
| @@ -1265,7 +1278,9 @@ certificate expiration times.")); | |||
| 1265 | -r, --regex, --ereg=STRING\n\ | 1278 | -r, --regex, --ereg=STRING\n\ |
| 1266 | Search page for regex STRING\n\ | 1279 | Search page for regex STRING\n\ |
| 1267 | -R, --eregi=STRING\n\ | 1280 | -R, --eregi=STRING\n\ |
| 1268 | Search page for case-insensitive regex STRING\n")); | 1281 | Search page for case-insensitive regex STRING\n\ |
| 1282 | --invert-regex\n\ | ||
| 1283 | Return CRITICAL if found, OK if not\n")); | ||
| 1269 | 1284 | ||
| 1270 | printf (_("\ | 1285 | printf (_("\ |
| 1271 | -a, --authorization=AUTH_PAIR\n\ | 1286 | -a, --authorization=AUTH_PAIR\n\ |
diff --git a/plugins/t/check_http.t b/plugins/t/check_http.t index 3154151f..4e3c06cb 100644 --- a/plugins/t/check_http.t +++ b/plugins/t/check_http.t | |||
| @@ -9,7 +9,7 @@ use strict; | |||
| 9 | use Test::More; | 9 | use Test::More; |
| 10 | use NPTest; | 10 | use NPTest; |
| 11 | 11 | ||
| 12 | plan tests => 14; | 12 | plan tests => 21; |
| 13 | 13 | ||
| 14 | my $successOutput = '/OK.*HTTP.*second/'; | 14 | my $successOutput = '/OK.*HTTP.*second/'; |
| 15 | 15 | ||
| @@ -79,4 +79,19 @@ $res = NPTest->testCmd( | |||
| 79 | ); | 79 | ); |
| 80 | cmp_ok( $res->return_code, "==", 0, "Can read https for www.e-paycobalt.com (uses AES certificate)" ); | 80 | cmp_ok( $res->return_code, "==", 0, "Can read https for www.e-paycobalt.com (uses AES certificate)" ); |
| 81 | 81 | ||
| 82 | 82 | $res = NPTest->testCmd( "./check_http -H altinity.com -r 'nagios'" ); | |
| 83 | cmp_ok( $res->return_code, "==", 0, "Got a reference to 'nagios'"); | ||
| 84 | |||
| 85 | $res = NPTest->testCmd( "./check_http -H altinity.com -r 'nAGiOs'" ); | ||
| 86 | cmp_ok( $res->return_code, "==", 2, "Not got 'nAGiOs'"); | ||
| 87 | like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'"); | ||
| 88 | |||
| 89 | $res = NPTest->testCmd( "./check_http -H altinity.com -R 'nAGiOs'" ); | ||
| 90 | cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'nAGiOs'"); | ||
| 91 | |||
| 92 | $res = NPTest->testCmd( "./check_http -H altinity.com -r 'nagios' --invert-regex" ); | ||
| 93 | cmp_ok( $res->return_code, "==", 2, "Invert results work when found"); | ||
| 94 | like ( $res->output, "/pattern found/", "Error message says 'pattern found'"); | ||
| 95 | |||
| 96 | $res = NPTest->testCmd( "./check_http -H altinity.com -r 'nAGiOs' --invert-regex" ); | ||
| 97 | cmp_ok( $res->return_code, "==", 0, "And also when not found"); | ||
