Code

still trying to fix #1094326
[nagiosplug.git] / plugins / check_http.c
index 80b83679af0f9a01e0fb53123c0c97e105820692..6e76706277d702712a92bfa3e34656b9fcb29bf5 100644 (file)
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
+ $Id$
 ******************************************************************************/
 /* splint -I. -I../../plugins -I../../lib/ -I/usr/kerberos/include/ ../../plugins/check_http.c */
 
 const char *progname = "check_http";
 const char *revision = "$Revision$";
-const char *copyright = "1999-2001";
+const char *copyright = "1999-2004";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #include "common.h"
@@ -144,7 +146,7 @@ main (int argc, char **argv)
                  clean_revstring (revision), VERSION);
 
        if (process_arguments (argc, argv) == ERROR)
-               usage (_("check_http: could not parse arguments\n"));
+               usage4 (_("Could not parse arguments"));
 
        if (strstr (timestamp, ":")) {
                if (strstr (server_url, "?"))
@@ -154,8 +156,9 @@ main (int argc, char **argv)
        }
 
        if (display_html == TRUE)
-               printf ("<A HREF=\"http://%s:%d%s\" target=\"_blank\">",
-                       host_name, server_port, server_url);
+               printf ("<A HREF=\"%s://%s:%d%s\" target=\"_blank\">", 
+                       use_ssl ? "https" : "http", host_name,
+                       server_port, server_url);
 
        /* initialize alarm signal handling, set socket timeout, start timer */
        (void) signal (SIGALRM, socket_timeout_alarm_handler);
@@ -171,7 +174,7 @@ main (int argc, char **argv)
                        X509_free (server_cert);
                }
                else {
-                       printf (_("ERROR: Cannot retrieve server certificate.\n"));
+                       printf (_("CRITICAL - Cannot retrieve server certificate.\n"));
                        result = STATE_CRITICAL;
                }
                SSL_shutdown (ssl);
@@ -187,7 +190,7 @@ main (int argc, char **argv)
 #endif
        return result;
 }
-\f
+
 
 
 /* process command-line arguments */
@@ -248,7 +251,7 @@ process_arguments (int argc, char **argv)
 
                switch (c) {
                case '?': /* usage */
-                       usage3 (_("unknown argument"), optopt);
+                       usage2 (_("Unknown argument"), optarg);
                        break;
                case 'h': /* help */
                        print_help ();
@@ -260,13 +263,13 @@ process_arguments (int argc, char **argv)
                        break;
                case 't': /* timeout period */
                        if (!is_intnonneg (optarg))
-                               usage2 (_("timeout interval must be a non-negative integer"), optarg);
+                               usage2 (_("Timeout interval must be a positive integer"), optarg);
                        else
                                socket_timeout = atoi (optarg);
                        break;
                case 'c': /* critical time threshold */
                        if (!is_nonnegative (optarg))
-                               usage2 (_("invalid critical threshold"), optarg);
+                               usage2 (_("Critical threshold must be integer"), optarg);
                        else {
                                critical_time = strtod (optarg, NULL);
                                check_critical_time = TRUE;
@@ -274,7 +277,7 @@ process_arguments (int argc, char **argv)
                        break;
                case 'w': /* warning time threshold */
                        if (!is_nonnegative (optarg))
-                               usage2 (_("invalid warning threshold"), optarg);
+                               usage2 (_("Warning threshold must be integer"), optarg);
                        else {
                                warning_time = strtod (optarg, NULL);
                                check_warning_time = TRUE;
@@ -294,7 +297,7 @@ process_arguments (int argc, char **argv)
                        break;
                case 'S': /* use SSL */
 #ifndef HAVE_SSL
-                       usage (_("check_http: invalid option - SSL is not available\n"));
+                       usage4 (_("Invalid option - SSL is not available"));
 #endif
                        use_ssl = TRUE;
                        if (specify_port == FALSE)
@@ -303,13 +306,13 @@ process_arguments (int argc, char **argv)
                case 'C': /* Check SSL cert validity */
 #ifdef HAVE_SSL
                        if (!is_intnonneg (optarg))
-                               usage2 (_("invalid certificate expiration period"), optarg);
+                               usage2 (_("Invalid certificate expiration period"), optarg);
                        else {
                                days_till_exp = atoi (optarg);
                                check_cert = TRUE;
                        }
 #else
-                       usage (_("check_http: invalid option - SSL is not available\n"));
+                       usage4 (_("Invalid option - SSL is not available"));
 #endif
                        break;
                case 'f': /* onredirect */
@@ -329,6 +332,8 @@ process_arguments (int argc, char **argv)
                /* Note: H, I, and u must be malloc'd or will fail on redirects */
                case 'H': /* Host Name (virtual host) */
                        host_name = strdup (optarg);
+                       if (strstr (optarg, ":"))
+                               sscanf (optarg, "%*[^:]:%d", &server_port);
                        break;
                case 'I': /* Server IP-address */
                        server_address = strdup (optarg);
@@ -339,7 +344,7 @@ process_arguments (int argc, char **argv)
                        break;
                case 'p': /* Server port */
                        if (!is_intnonneg (optarg))
-                               usage2 (_("invalid port number"), optarg);
+                               usage2 (_("Invalid port number"), optarg);
                        else {
                                server_port = atoi (optarg);
                                specify_port = TRUE;
@@ -370,7 +375,7 @@ process_arguments (int argc, char **argv)
                case 'l': /* linespan */
                case 'r': /* linespan */
                case 'R': /* linespan */
-                       usage (_("check_http: call for regex which was not a compiled option\n"));
+                       usage4 (_("Call for regex which was not a compiled option"));
                        break;
 #else
                case 'l': /* linespan */
@@ -396,7 +401,7 @@ process_arguments (int argc, char **argv)
 #ifdef USE_IPV6
                        address_family = AF_INET6;
 #else
-                       usage (_("IPv6 support not available\n"));
+                       usage4 (_("IPv6 support not available"));
 #endif
                        break;
                case 'v': /* verbose */
@@ -439,7 +444,7 @@ process_arguments (int argc, char **argv)
 
        if (server_address == NULL) {
                if (host_name == NULL)
-                       usage (_("check_http: you must specify a server address or host name\n"));
+                       usage4 (_("You must specify a server address or host name"));
                else
                        server_address = strdup (host_name);
        }
@@ -452,7 +457,7 @@ process_arguments (int argc, char **argv)
 
        return TRUE;
 }
-\f
+
 
 
 /* written by lauri alanko */
@@ -495,8 +500,6 @@ base64 (const char *bin, size_t len)
        buf[i] = '\0';
        return buf;
 }
-\f
-
 
 
 
@@ -504,8 +507,7 @@ base64 (const char *bin, size_t len)
 static int
 document_headers_done (char *full_page)
 {
-       const char *body, *s;
-       const char *end;
+       const char *body;
 
        for (body = full_page; *body; body++) {
                if (!strncmp (body, "\n\n", 2) || !strncmp (body, "\n\r\n", 3))
@@ -611,6 +613,7 @@ parse_time_string (const char *string)
 }
 
 
+
 static void
 check_document_dates (const char *headers)
 {
@@ -671,17 +674,17 @@ check_document_dates (const char *headers)
        } else if (!document_date || !*document_date) {
                die (STATE_CRITICAL, _("Document modification date unknown\n"));
        } else {
-               time_t sd = parse_time_string (server_date);
-               time_t dd = parse_time_string (document_date);
+               time_t srv_data = parse_time_string (server_date);
+               time_t doc_data = parse_time_string (document_date);
 
-               if (sd <= 0) {
+               if (srv_data <= 0) {
                        die (STATE_CRITICAL, _("CRITICAL - Server date \"%100s\" unparsable"), server_date);
-               } else if (dd <= 0) {
+               } else if (doc_data <= 0) {
                        die (STATE_CRITICAL, _("CRITICAL - Document date \"%100s\" unparsable"), document_date);
-               } else if (dd > sd + 30) {
-                       die (STATE_CRITICAL, _("CRITICAL - Document is %d seconds in the future\n"), dd - sd);
-               } else if (dd < sd - maximum_age) {
-               int n = (sd - dd);
+               } else if (doc_data > srv_data + 30) {
+                       die (STATE_CRITICAL, _("CRITICAL - Document is %d seconds in the future\n"), (int)doc_data - (int)srv_data);
+               } else if (doc_data < srv_data - maximum_age) {
+               int n = (srv_data - doc_data);
                if (n > (60 * 60 * 24 * 2))
                        die (STATE_CRITICAL,
                          _("CRITICAL - Last modified %.1f days ago\n"),
@@ -733,7 +736,7 @@ check_http (void)
                        X509_free (server_cert);
                }
                else {
-                       printf (_("ERROR: Cannot retrieve server certificate.\n"));
+                       printf (_("CRITICAL - Cannot retrieve server certificate.\n"));
                        return STATE_CRITICAL;
                }
 
@@ -816,12 +819,12 @@ check_http (void)
                        if ( sslerr == SSL_ERROR_SSL ) {
                                die (STATE_WARNING, _("Client Certificate Required\n"));
                        } else {
-                               die (STATE_CRITICAL, _("Error in recv()\n"));
+                               die (STATE_CRITICAL, _("Error on receive\n"));
                        }
                }
                else {
 #endif
-                       die (STATE_CRITICAL, _("Error in recv()\n"));
+                       die (STATE_CRITICAL, _("Error on receive\n"));
 #ifdef HAVE_SSL
                }
 #endif
@@ -841,7 +844,9 @@ check_http (void)
        page = full_page;
 
        if (verbose)
-               printf ("%s://%s:%d%s is %d characters\n", server_type, server_address, server_port, server_url, pagesize);
+               printf ("%s://%s:%d%s is %d characters\n",
+                       use_ssl ? "https" : "http", server_address,
+                       server_port, server_url, pagesize);
 
        /* find status line and null-terminate it */
        status_line = page;
@@ -946,7 +951,7 @@ check_http (void)
        microsec = deltime (tv);
        elapsed_time = (double)microsec / 1.0e6;
        asprintf (&msg,
-                 _("HTTP problem: %s - %.3f second response time %s%s|%s %s\n"),
+                 _("HTTP WARNING: %s - %.3f second response time %s%s|%s %s\n"),
                  status_line, elapsed_time, timestamp,
                  (display_html ? "</A>" : ""),
                                                perfd_time (elapsed_time), perfd_size (pagesize));
@@ -1017,7 +1022,6 @@ check_http (void)
 
 
 
-
 /* per RFC 2396 */
 #define HDR_LOCATION "%*[Ll]%*[Oo]%*[Cc]%*[Aa]%*[Tt]%*[Ii]%*[Oo]%*[Nn]: "
 #define URI_HTTP "%[HTPShtps]://"
@@ -1043,11 +1047,11 @@ redir (char *pos, char *status_line)
 
        addr = malloc (MAX_IPV4_HOSTLENGTH + 1);
        if (addr == NULL)
-               die (STATE_UNKNOWN, _("ERROR: could not allocate addr\n"));
+               die (STATE_UNKNOWN, _("Could not allocate addr\n"));
        
        url = malloc (strcspn (pos, "\r\n"));
        if (url == NULL)
-               die (STATE_UNKNOWN, _("ERROR: could not allocate url\n"));
+               die (STATE_UNKNOWN, _("Could not allocate url\n"));
 
        while (pos) {
 
@@ -1067,7 +1071,7 @@ redir (char *pos, char *status_line)
 
                url = realloc (url, strcspn (pos, "\r\n"));
                if (url == NULL)
-                       die (STATE_UNKNOWN, _("ERROR: could not allocate url\n"));
+                       die (STATE_UNKNOWN, _("could not allocate url\n"));
 
                /* URI_HTTP, URI_HOST, URI_PORT, URI_PATH */
                if (sscanf (pos, HD1, type, addr, port, url) == 4) {
@@ -1219,6 +1223,8 @@ int connect_SSL (void)
 }
 #endif
 
+
+
 #ifdef HAVE_SSL
 int
 check_certificate (X509 ** certificate)
@@ -1294,7 +1300,7 @@ check_certificate (X509 ** certificate)
        return STATE_OK;
 }
 #endif
-\f
+
 
 
 char *perfd_time (double elapsed_time)
@@ -1306,6 +1312,7 @@ char *perfd_time (double elapsed_time)
 }
 
 
+
 char *perfd_size (int page_len)
 {
        return perfdata ("size", page_len, "B",
@@ -1315,6 +1322,7 @@ char *perfd_size (int page_len)
 }
 
 
+
 int
 my_recv (void)
 {
@@ -1333,6 +1341,7 @@ my_recv (void)
 }
 
 
+
 int
 my_close (void)
 {
@@ -1353,22 +1362,19 @@ my_close (void)
 
 
 
-
-
-\f
 void
 print_help (void)
 {
        print_revision (progname, revision);
 
-       printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
-       printf (_(COPYRIGHT), copyright, email);
+       printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
+       printf (COPYRIGHT, copyright, email);
 
        printf (_("\
 This plugin tests the HTTP service on the specified host. It can test\n\
 normal (http) and secure (https) servers, follow redirects, search for\n\
 strings and regular expressions, check connection times, and report on\n\
-certificate expiration times.\n"));
+certificate expiration times.\n\n"));
 
        print_usage ();
 
@@ -1379,6 +1385,7 @@ certificate expiration times.\n"));
        printf (_("\
  -H, --hostname=ADDRESS\n\
     Host name argument for servers using host headers (virtual host)\n\
+    Append a port to include it in the header (eg: example.com:5000)\n\
  -I, --IP-address=ADDRESS\n\
    IP address or name (use numeric address if possible to bypass DNS lookup).\n\
  -p, --port=INTEGER\n\
@@ -1478,16 +1485,14 @@ the certificate is expired.\n"));
 
 
 
-
 void
 print_usage (void)
 {
-       printf (_("\
-Usage: %s (-H <vhost> | -I <IP-address>) [-u <uri>] [-p <port>]\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] [-4|-6] [-N] [-M <age>] [-A string]\n\
-  [-k string]\n"), progname);
-       printf (_(UT_HLP_VRS), progname, progname);
+       printf ("\
+Usage: %s -H <vhost> | -I <IP-address>) [-u <uri>] [-p <port>]\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] [-4|-6] [-N] [-M <age>]\n\
+                  [-A string] [-k string]\n", progname);
 }