Code

Fixed tests (Mathew Ericson - 738608)
[nagiosplug.git] / plugins / check_http.c
index c947bd8723b8569d78b2673fd135ba6cf2a4d001..91cc48cda6470a390f8995ffdefdc25b4c9304fb 100644 (file)
@@ -189,7 +189,7 @@ struct timeval tv;
 #define URI_PATH "%[/a-zA-Z0-9._-=@,]"
 
 enum {
-       MAX_IPV4_HOSTLENGTH = 64,
+       MAX_IPV4_HOSTLENGTH = 255,
        HTTP_PORT = 80,
        HTTPS_PORT = 443
 };
@@ -486,6 +486,9 @@ process_arguments (int argc, char **argv)
                        asprintf (&server_address, "%s", host_name);
        }
 
+       if (check_critical_time && critical_time>(double)socket_timeout)
+               socket_timeout = (int)critical_time + 1;
+
        return TRUE;
 }
 \f
@@ -627,16 +630,21 @@ check_http (void)
                pagesize += i;
        }
 
-       if (i < 0) {
+       if (i < 0 && errno != ECONNRESET) {
 #ifdef HAVE_SSL
-               sslerr=SSL_get_error(ssl, i);
-               if ( sslerr == SSL_ERROR_SSL ) {
-                       terminate (STATE_WARNING, "Client Certificate Required\n");
-               } else {
+               if (use_ssl) {
+                       sslerr=SSL_get_error(ssl, i);
+                       if ( sslerr == SSL_ERROR_SSL ) {
+                               terminate (STATE_WARNING, "Client Certificate Required\n");
+                       } else {
+                               terminate (STATE_CRITICAL, "Error in recv()");
+                       }
+               }
+               else {
+#endif
                        terminate (STATE_CRITICAL, "Error in recv()");
+#ifdef HAVE_SSL
                }
-#else
-               terminate (STATE_CRITICAL, "Error in recv()");
 #endif
        }
 
@@ -734,7 +742,7 @@ check_http (void)
                                asprintf (&orig_url, "%s", server_url);
                                pos = header;
                                while (pos) {
-                                       server_address = realloc (server_address, MAX_IPV4_HOSTLENGTH);
+                                       server_address = realloc (server_address, MAX_IPV4_HOSTLENGTH + 1);
                                        if (server_address == NULL)
                                                terminate (STATE_UNKNOWN,
                                                                                 "HTTP UNKNOWN: could not allocate server_address");
@@ -781,21 +789,21 @@ check_http (void)
                                        pos += (size_t) strcspn (pos, "\r\n");
                                        pos += (size_t) strspn (pos, "\r\n");
                                } /* end while (pos) */
-                               printf ("HTTP UNKNOWN: Could not find redirect location - %s%s",
+                               printf ("UNKNOWN - Could not find redirect location - %s%s",
                                        status_line, (display_html ? "</A>" : ""));
                                exit (STATE_UNKNOWN);
                        } /* end if (onredirect == STATE_DEPENDENT) */
                        
                        else if (onredirect == STATE_UNKNOWN)
-                               printf ("HTTP UNKNOWN");
+                               printf ("UNKNOWN");
                        else if (onredirect == STATE_OK)
-                               printf ("HTTP ok");
+                               printf ("OK");
                        else if (onredirect == STATE_WARNING)
-                               printf ("HTTP WARNING");
+                               printf ("WARNING");
                        else if (onredirect == STATE_CRITICAL)
-                               printf ("HTTP CRITICAL");
+                               printf ("CRITICAL");
                        elapsed_time = delta_time (tv);
-                       asprintf (&msg, ": %s - %7.3f second response time %s%s|time=%7.3f\n",
+                       asprintf (&msg, " - %s - %.3f second response time %s%s|time=%.3f\n",
                                 status_line, elapsed_time, timestamp,
                           (display_html ? "</A>" : ""), elapsed_time);
                        terminate (onredirect, msg);
@@ -807,7 +815,7 @@ check_http (void)
                
        /* check elapsed time */
        elapsed_time = delta_time (tv);
-       asprintf (&msg, "HTTP problem: %s - %7.3f second response time %s%s|time=%7.3f\n",
+       asprintf (&msg, "HTTP problem: %s - %.3f second response time %s%s|time=%.3f\n",
                       status_line, elapsed_time, timestamp,
                       (display_html ? "</A>" : ""), elapsed_time);
        if (check_critical_time == TRUE && elapsed_time > critical_time)
@@ -820,13 +828,13 @@ check_http (void)
 
        if (strlen (string_expect)) {
                if (strstr (page, string_expect)) {
-                       printf ("HTTP OK %s - %7.3f second response time %s%s|time=%7.3f\n",
+                       printf ("HTTP OK %s - %.3f second response time %s%s|time=%.3f\n",
                                status_line, elapsed_time,
                                timestamp, (display_html ? "</A>" : ""), elapsed_time);
                        exit (STATE_OK);
                }
                else {
-                       printf ("HTTP CRITICAL: string not found%s|time=%7.3f\n",
+                       printf ("CRITICAL - string not found%s|time=%.3f\n",
                                (display_html ? "</A>" : ""), elapsed_time);
                        exit (STATE_CRITICAL);
                }
@@ -835,20 +843,20 @@ check_http (void)
        if (strlen (regexp)) {
                errcode = regexec (&preg, page, REGS, pmatch, 0);
                if (errcode == 0) {
-                       printf ("HTTP OK %s - %7.3f second response time %s%s|time=%7.3f\n",
+                       printf ("HTTP OK %s - %.3f second response time %s%s|time=%.3f\n",
                                status_line, elapsed_time,
                                timestamp, (display_html ? "</A>" : ""), elapsed_time);
                        exit (STATE_OK);
                }
                else {
                        if (errcode == REG_NOMATCH) {
-                               printf ("HTTP CRITICAL: pattern not found%s|time=%7.3f\n",
+                               printf ("CRITICAL - pattern not found%s|time=%.3f\n",
                                        (display_html ? "</A>" : ""), elapsed_time);
                                exit (STATE_CRITICAL);
                        }
                        else {
                                regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
-                               printf ("Execute Error: %s\n", errbuf);
+                               printf ("CRITICAL - Execute Error: %s\n", errbuf);
                                exit (STATE_CRITICAL);
                        }
                }
@@ -863,7 +871,7 @@ check_http (void)
                exit (STATE_WARNING);
        }
        /* We only get here if all tests have been passed */
-       asprintf (&msg, "HTTP OK %s - %7.3f second response time %s%s|time=%7.3f\n",
+       asprintf (&msg, "HTTP OK %s - %.3f second response time %s%s|time=%.3f\n",
                        status_line, (float)elapsed_time,
                        timestamp, (display_html ? "</A>" : ""), elapsed_time);
        terminate (STATE_OK, msg);
@@ -887,7 +895,7 @@ int connect_SSL (void)
        meth = SSLv23_client_method ();
        SSL_load_error_strings ();
        if ((ctx = SSL_CTX_new (meth)) == NULL) {
-               printf ("ERROR: Cannot create SSL context.\n");
+               printf ("CRITICAL -  Cannot create SSL context.\n");
                return STATE_CRITICAL;
        }
 
@@ -911,7 +919,7 @@ int connect_SSL (void)
                        ERR_print_errors_fp (stderr);
                }
                else {
-                       printf ("ERROR: Cannot initiate SSL handshake.\n");
+                       printf ("CRITICAL - Cannot initiate SSL handshake.\n");
                }
                SSL_free (ssl);
        }
@@ -939,7 +947,7 @@ check_certificate (X509 ** certificate)
        /* Generate tm structure to process timestamp */
        if (tm->type == V_ASN1_UTCTIME) {
                if (tm->length < 10) {
-                       printf ("ERROR: Wrong time format in certificate.\n");
+                       printf ("CRITICAL - Wrong time format in certificate.\n");
                        return STATE_CRITICAL;
                }
                else {
@@ -951,7 +959,7 @@ check_certificate (X509 ** certificate)
        }
        else {
                if (tm->length < 12) {
-                       printf ("ERROR: Wrong time format in certificate.\n");
+                       printf ("CRITICAL - Wrong time format in certificate.\n");
                        return STATE_CRITICAL;
                }
                else {
@@ -980,20 +988,20 @@ check_certificate (X509 ** certificate)
                 stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
 
        if (days_left > 0 && days_left <= days_till_exp) {
-               printf ("Certificate expires in %d day(s) (%s).\n", days_left, timestamp);
+               printf ("WARNING - Certificate expires in %d day(s) (%s).\n", days_left, timestamp);
                return STATE_WARNING;
        }
        if (days_left < 0) {
-               printf ("Certificate expired on %s.\n", timestamp);
+               printf ("CRITICAL - Certificate expired on %s.\n", timestamp);
                return STATE_CRITICAL;
        }
 
        if (days_left == 0) {
-               printf ("Certificate expires today (%s).\n", timestamp);
+               printf ("WARNING - Certificate expires today (%s).\n", timestamp);
                return STATE_WARNING;
        }
 
-       printf ("Certificate will expire on %s.\n", timestamp);
+       printf ("OK - Certificate will expire on %s.\n", timestamp);
 
        return STATE_OK;
 }
@@ -1059,8 +1067,10 @@ print_help (void)
 void
 print_usage (void)
 {
-       printf ("Usage:\n" " %s %s\n"
-               " %s (-h | --help) for detailed help\n"
-               " %s (-V | --version) for version information\n",
+       printf ("\
+Usage:\n\
+ %s %s\n\
+ %s (-h | --help) for detailed help\n\
+ %s (-V | --version) for version information\n",
        progname, OPTIONS, progname, progname);
 }