[nagiosplug] added ability to check regex against http ...

Nagios Plugin Development nagios-plugins at users.sourceforge.net
Sun Aug 18 18:40:22 CEST 2013


    Module: nagiosplug
    Branch: master
    Commit: 2a8eab6b31e0f461e52b504776caad6bb93b981f
    Author: linkslice <sparctacus at gmail.com>
 Committer: Holger Weiss <holger at zedat.fu-berlin.de>
      Date: Wed Jun  6 21:35:34 2012 -0700
       URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=2a8eab6

added ability to check regex against http headers.  booyah!

---

 plugins/check_http.c       |   24 ++++++++++++++++++++++--
 plugins/tests/check_http.t |   11 +++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/plugins/check_http.c b/plugins/check_http.c
index 6db38e8..5204028 100644
--- a/plugins/check_http.c
+++ b/plugins/check_http.c
@@ -100,7 +100,9 @@ char *user_agent;
 int server_url_length;
 int server_expect_yn = 0;
 char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT;
+char header_expect[MAX_INPUT_BUFFER] = "";
 char string_expect[MAX_INPUT_BUFFER] = "";
+char output_header_search[30] = "";
 char output_string_search[30] = "";
 char *warning_thresholds = NULL;
 char *critical_thresholds = NULL;
@@ -205,6 +207,7 @@ process_arguments (int argc, char **argv)
     {"port", required_argument, 0, 'p'},
     {"authorization", required_argument, 0, 'a'},
     {"proxy_authorization", required_argument, 0, 'b'},
+    {"header_string", required_argument, 0, 'd'},
     {"string", required_argument, 0, 's'},
     {"expect", required_argument, 0, 'e'},
     {"regex", required_argument, 0, 'r'},
@@ -243,7 +246,7 @@ process_arguments (int argc, char **argv)
   }
 
   while (1) {
-    c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:b:e:p:s:R:r:u:f:C:nlLS::m:M:N:E", longopts, &option);
+    c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:b:d:e:p:s:R:r:u:f:C:nlLS::m:M:N:E", longopts, &option);
     if (c == -1 || c == EOF)
       break;
 
@@ -392,6 +395,10 @@ process_arguments (int argc, char **argv)
         free(http_method);
       http_method = strdup (optarg);
       break;
+    case 'd': /* string or substring */
+      strncpy (header_expect, optarg, MAX_INPUT_BUFFER - 1);
+      header_expect[MAX_INPUT_BUFFER - 1] = 0;
+      break;
     case 's': /* string or substring */
       strncpy (string_expect, optarg, MAX_INPUT_BUFFER - 1);
       string_expect[MAX_INPUT_BUFFER - 1] = 0;
@@ -1085,6 +1092,17 @@ check_http (void)
   }
 
   /* Page and Header content checks go here */
+  if (strlen (header_expect)) {
+    if (!strstr (header, header_expect)) {
+      strncpy(&output_header_search[0],header_expect,sizeof(output_header_search));
+      if(output_header_search[sizeof(output_header_search)-1]!='\0') {
+        bcopy("...",&output_header_search[sizeof(output_header_search)-4],4);
+      }
+      asprintf (&msg, _("%sheader '%s' not found on '%s://%s:%d%s', "), msg, output_header_search, use_ssl ? "https" : "http", host_name ? host_name : server_address, server_port, server_url);
+      result = STATE_CRITICAL;
+    }
+  }
+
 
   if (strlen (string_expect)) {
     if (!strstr (page, string_expect)) {
@@ -1434,6 +1452,8 @@ print_help (void)
   printf ("    %s", _("the first (status) line of the server response (default: "));
   printf ("%s)\n", HTTP_EXPECT);
   printf ("    %s\n", _("If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)"));
+  printf (" %s\n", "-d, --header-string=STRING");
+  printf ("    %s\n", _("String to expect in the response headers"));
   printf (" %s\n", "-s, --string=STRING");
   printf ("    %s\n", _("String to expect in the content"));
   printf (" %s\n", "-u, --url=PATH");
@@ -1535,7 +1555,7 @@ print_usage (void)
   printf (" %s -H <vhost> | -I <IP-address> [-u <uri>] [-p <port>]\n",progname);
   printf ("       [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L] [-a auth]\n");
   printf ("       [-b proxy_auth] [-f <ok|warning|critcal|follow|sticky|stickyport>]\n");
-  printf ("       [-e <expect>] [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]\n");
+  printf ("       [-e <expect>] [-d string] [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]\n");
   printf ("       [-P string] [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>]\n");
   printf ("       [-A string] [-k string] [-S <version>] [--sni] [-C <warn_age>[,<crit_age>]]\n");
   printf ("       [-T <content-type>] [-j method]\n");
diff --git a/plugins/tests/check_http.t b/plugins/tests/check_http.t
index 9f97abd..873fd6e 100755
--- a/plugins/tests/check_http.t
+++ b/plugins/tests/check_http.t
@@ -151,6 +151,10 @@ sub run_server {
 				unshift @persist, $c;
 				delete($persist[1000]);
 				next MAINLOOP;
+			} elsif ($r->url->path eq "/header_check") {
+				$c->send_basic_header;
+				$c->send_header('foo');
+				$c->send_crlf;
 			} else {
 				$c->send_error(HTTP::Status->RC_FORBIDDEN);
 			}
@@ -223,6 +227,13 @@ sub run_common_tests {
 	is( $result->return_code, 2, "Missing string check");
 	like( $result->output, qr%HTTP CRITICAL: HTTP/1\.1 200 OK - string 'NonRootWithOver30charsAndM...' not found on 'https?://127\.0\.0\.1:\d+/file/root'%, "Shows search string and location");
 
+	$result = NPTest->testCmd( "$command -u /header_check -d foo" );
+	is( $result->return_code, 0, "header_check search for string");
+	like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
+
+	$result = NPTest->testCmd( "$command -u /header_check -d foobar" );
+	is( $result->return_code, 2, "Missing header string check");
+	like( $result->output, qr%^HTTP CRITICAL: HTTP/1\.1 200 OK - string 'foobar' not found on 'https?://127\.0\.0\.1:\d+/header_check/root'%, "Shows search string and location");
 
 	my $cmd;
 	$cmd = "$command -u /slow";





More information about the Commits mailing list