Selaa lähdekoodia

check_http: -e breaks -f

Fix for issue https://github.com/nagios-plugins/nagios-plugins/issues/91

Checking status codes and following redirects makes for compicated
coding and testing. But this change seems to handle all situations
properly.

It is important to note, though, that anything specified by `-e`
will only be compared agains the final page. So if you use `-e 301`
expecting a redirect, it will error out because the status of the
last page would be `200` (or some other status).
John C. Frickson 9 vuotta sitten
vanhempi
commit
657e3b6447
2 muutettua tiedostoa jossa 50 lisäystä ja 41 poistoa
  1. 4 0
      NEWS
  2. 46 41
      plugins/check_http.c

+ 4 - 0
NEWS

@@ -1,5 +1,9 @@
 This file documents the major additions and syntax changes between releases.
 
+2.x.x xxxx-xx-xx
+	FIXES
+	check_http: -e breaks -f
+
 2.1.4 2016-11-17
 	FIXES
 	check_http: Don't include default Accept header if one is provided

+ 46 - 41
plugins/check_http.c

@@ -982,6 +982,7 @@ check_http (void)
   int page_len = 0;
   int result = STATE_OK;
   char *force_host_header = NULL;
+	int bad_response = FALSE;
 
   /* try to connect to the host at the given port number */
   gettimeofday (&tv_temp, NULL);
@@ -1235,58 +1236,62 @@ check_http (void)
       xasprintf (&msg,
                 _("Invalid HTTP response received from host on port %d: %s\n"),
                 server_port, status_line);
-    die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg);
+    bad_response = TRUE;
   }
 
   /* Bypass normal status line check if server_expect was set by user and not default */
   /* NOTE: After this if/else block msg *MUST* be an asprintf-allocated string */
-  if ( server_expect_yn  )  {
-    xasprintf (&msg,
-              _("Status line output matched \"%s\" - "), server_expect);
-    if (verbose)
-      printf ("%s\n",msg);
+  if ( !bad_response ) {
+    if ( server_expect_yn  )  {
+      xasprintf (&msg,
+                _("Status line output matched \"%s\" - "), server_expect);
+      if (verbose)
+        printf ("%s\n",msg);
+    } else
+      xasprintf (&msg, "");
   }
-  else {
-    /* Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */
-    /* HTTP-Version   = "HTTP" "/" 1*DIGIT "." 1*DIGIT */
-    /* Status-Code = 3 DIGITS */
 
-    status_code = strchr (status_line, ' ') + sizeof (char);
-    if (strspn (status_code, "1234567890") != 3)
-      die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status Line (%s)\n"), status_line);
+  /* Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */
+  /* HTTP-Version   = "HTTP" "/" 1*DIGIT "." 1*DIGIT */
+  /* Status-Code = 3 DIGITS */
 
-    http_status = atoi (status_code);
+  status_code = strchr (status_line, ' ') + sizeof (char);
+  if (strspn (status_code, "1234567890") != 3)
+    die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status Line (%s)\n"), status_line);
 
-    /* check the return code */
+  http_status = atoi (status_code);
 
-    if (http_status >= 600 || http_status < 100) {
-      die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status (%s)\n"), status_line);
-    }
-    /* server errors result in a critical state */
-    else if (http_status >= 500) {
-      xasprintf (&msg, _("%s - "), status_line);
-      result = STATE_CRITICAL;
-    }
-    /* client errors result in a warning state */
-    else if (http_status >= 400) {
-      xasprintf (&msg, _("%s - "), status_line);
-      result = max_state_alt(STATE_WARNING, result);
-    }
-    /* check redirected page if specified */
-    else if (http_status >= 300) {
+  /* check the return code */
 
-      if (onredirect == STATE_DEPENDENT)
-        redir (header, status_line);
-      else
-        result = max_state_alt(onredirect, result);
-      xasprintf (&msg, _("%s - "), status_line);
-    } /* end if (http_status >= 300) */
-    else {
-      /* Print OK status anyway */
-      xasprintf (&msg, _("%s - "), status_line);
-    }
+  if (http_status >= 600 || http_status < 100) {
+    die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status (%s)\n"), status_line);
+  }
+  /* server errors result in a critical state */
+  else if (http_status >= 500) {
+    xasprintf (&msg, _("%s%s - "), msg, status_line);
+    result = STATE_CRITICAL;
+  }
+  /* client errors result in a warning state */
+  else if (http_status >= 400) {
+    xasprintf (&msg, _("%s%s - "), msg, status_line);
+    result = max_state_alt(STATE_WARNING, result);
+  }
+  /* check redirected page if specified */
+  else if (http_status >= 300) {
 
-  } /* end else (server_expect_yn)  */
+    if (onredirect == STATE_DEPENDENT)
+      redir (header, status_line);
+    else
+      result = max_state_alt(onredirect, result);
+    xasprintf (&msg, _("%s%s - "), msg, status_line);
+  } /* end if (http_status >= 300) */
+  else if (!bad_response) {
+    /* Print OK status anyway */
+    xasprintf (&msg, _("%s%s - "), msg, status_line);
+  }
+
+  if (bad_response) 
+    die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg);
 
   /* reset the alarm - must be called *after* redir or we'll never die on redirects! */
   alarm (0);