Code

terminate in error_scan() so meaningfull errors can be sent
[nagiosplug.git] / plugins / check_http.c
index c947bd8723b8569d78b2673fd135ba6cf2a4d001..2f693328a7e597d549b9813cd3a4721675f280b4 100644 (file)
@@ -45,13 +45,17 @@ certificate expiration times.\n"
             [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L]\n\
             [-a auth] [-f <ok | warn | critcal | follow>] [-e <expect>]\n\
             [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]\n\
-            [-P string] [-m min_pg_size]"
+            [-P string] [-m min_pg_size] [-4|-6]"
 
 #define LONGOPTIONS "\
  -H, --hostname=ADDRESS\n\
     Host name argument for servers using host headers (virtual host)\n\
  -I, --IP-address=ADDRESS\n\
    IP address or name (use numeric address if possible to bypass DNS lookup).\n\
+ -4, --use-ipv4\n\
+   Use IPv4 protocol\n\
+ -6, --use-ipv6\n\
+   Use IPv6 protocol\n\
  -e, --expect=STRING\n\
    String to expect in first (status) line of server response (default: %s)\n\
    If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)\n\
@@ -182,14 +186,16 @@ struct timeval tv;
 
 #define server_port_check(use_ssl) (use_ssl ? HTTPS_PORT : HTTP_PORT)
 
+/* per RFC 2396 */
 #define HDR_LOCATION "%*[Ll]%*[Oo]%*[Cc]%*[Aa]%*[Tt]%*[Ii]%*[Oo]%*[Nn]: "
 #define URI_HTTP "%[HTPShtps]://"
-#define URI_HOST "%[a-zA-Z0-9.-]"
-#define URI_PORT ":%[0-9]"
-#define URI_PATH "%[/a-zA-Z0-9._-=@,]"
+#define URI_HOST "%[-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]"
+#define URI_PORT ":%[0123456789]"
+#define URI_PATH "%[-_.!~*'();/?:@&=+$,%#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]"
 
 enum {
-       MAX_IPV4_HOSTLENGTH = 64,
+       MAX_IPV4_HOSTLENGTH = 255,
        HTTP_PORT = 80,
        HTTPS_PORT = 443
 };
@@ -314,6 +320,8 @@ process_arguments (int argc, char **argv)
                {"onredirect", required_argument, 0, 'f'},
                {"certificate", required_argument, 0, 'C'},
                {"min", required_argument, 0, 'm'},
+               {"use-ipv4", no_argument, 0, '4'},
+               {"use-ipv6", no_argument, 0, '6'},
                {0, 0, 0, 0}
        };
 
@@ -334,13 +342,13 @@ process_arguments (int argc, char **argv)
        }
 
        while (1) {
-               c = getopt_long (argc, argv, "Vvht:c:w:H:P:I:a:e:p:s:R:r:u:f:C:nlLSm:", long_options, &option_index);
+               c = getopt_long (argc, argv, "Vvh46t:c:w:H:P:I:a:e:p:s:R:r:u:f:C:nlLSm:", long_options, &option_index);
                if (c == -1 || c == EOF)
                        break;
 
                switch (c) {
                case '?': /* usage */
-                       usage2 ("unknown argument", optarg);
+                       usage3 ("unknown argument", optopt);
                        break;
                case 'h': /* help */
                        print_help ();
@@ -462,6 +470,16 @@ process_arguments (int argc, char **argv)
                        }
                        break;
 #endif
+               case '4':
+                       address_family = AF_INET;
+                       break;
+               case '6':
+#ifdef USE_IPV6
+                       address_family = AF_INET6;
+#else
+                       usage ("IPv6 support not available\n");
+#endif
+                       break;
                case 'v': /* verbose */
                        verbose = TRUE;
                        break;
@@ -486,6 +504,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 +648,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 +760,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 +807,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 +833,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 +846,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 +861,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 +889,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 +913,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 +937,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 +965,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 +977,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 +1006,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 +1085,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);
 }