X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fcheck_http.c;h=506a1ec720ff18b88baf7ddeb265a595e8e84f0c;hb=9452b46d955a3e99837214c77ad48dae4a7bf7cd;hp=db5d50dd1e1decb0bc4b29eede663732110f3d07;hpb=44a321cb8a42d6c0ea2d96a1086a17f2134c89cc;p=nagiosplug.git diff --git a/plugins/check_http.c b/plugins/check_http.c index db5d50d..506a1ec 100644 --- a/plugins/check_http.c +++ b/plugins/check_http.c @@ -23,7 +23,7 @@ * *****************************************************************************/ -#define PROGNAME "check_http" +const char *progname = "check_http"; #define REVISION "$Revision$" #define COPYRIGHT "1999-2001" #define AUTHORS "Ethan Galstad/Karl DeBisschop" @@ -31,7 +31,6 @@ #include "config.h" #include "common.h" -#include "version.h" #include "netutils.h" #include "utils.h" @@ -45,7 +44,7 @@ certificate expiration times.\n" \(-H | -I ) [-u ] [-p ]\n\ [-w ] [-c ] [-t ] [-L]\n\ [-a auth] [-f ] [-e ]\n\ - [-s string] [-r | -R ]\n\ + [-s string] [-l] [-r | -R ]\n\ [-P string]" #define LONGOPTIONS "\ @@ -54,7 +53,8 @@ certificate expiration times.\n" -I, --IP-address=ADDRESS\n\ IP address or name (use numeric address if possible to bypass DNS lookup).\n\ -e, --expect=STRING\n\ - String to expect in first line of server response (default: %s)\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\ -s, --string=STRING\n\ String to expect in the content\n\ -u, --url=PATH\n\ @@ -74,7 +74,7 @@ certificate expiration times.\n" -L, --link=URL\n\ Wrap output in HTML link (obsoleted by urlize)\n\ -f, --onredirect=\n\ - How to handle redirected pages\n%s\ + How to handle redirected pages\n%s%s\ -v, --verbose\n\ Show details for command-line debugging (do not use with nagios server)\n\ -h, --help\n\ @@ -93,6 +93,18 @@ certificate expiration times.\n" #define SSLOPTIONS "" #endif +#ifdef HAVE_REGEX_H +#define REGOPTIONS "\ + -l, --linespan\n\ + Allow regex to span newlines (must precede -r or -R)\n\ + -r, --regex, --ereg=STRING\n\ + Search page for regex STRING\n\ + -R, --eregi=STRING\n\ + Search page for case-insensitive regex STRING\n" +#else +#define REGOPTIONS "" +#endif + #define DESCRIPTION "\ This plugin will attempt to open an HTTP connection with the host. Successul\n\ connects return STATE_OK, refusals and timeouts return STATE_CRITICAL, other\n\ @@ -159,6 +171,8 @@ int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE; int errcode; #endif +struct timeval tv; + #define server_type_check(server_type) \ (strcmp (server_type, "https") ? FALSE : TRUE) @@ -175,22 +189,23 @@ int errcode; #define HTTPS_PORT 443 #define HTTP_EXPECT "HTTP/1." #define HTTP_URL "/" +#define CRLF "\r\n" -time_t start_time, end_time; -char timestamp[10] = ""; +char timestamp[17] = ""; int specify_port = FALSE; int server_port = HTTP_PORT; char server_port_text[6] = ""; char server_type[6] = "http"; -char *server_address = NULL; -char *host_name = NULL; -char *server_url = NULL; -int server_url_length = 0; +char *server_address = ""; +char *host_name = ""; +char *server_url = HTTP_URL; +int server_url_length = 1; +int server_expect_yn = 0; char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT; char string_expect[MAX_INPUT_BUFFER] = ""; -int warning_time = 0; +double warning_time = 0; int check_warning_time = FALSE; -int critical_time = 0; +double critical_time = 0; int check_critical_time = FALSE; char user_auth[MAX_INPUT_BUFFER] = ""; int display_html = FALSE; @@ -198,14 +213,13 @@ int onredirect = STATE_OK; int use_ssl = FALSE; int verbose = FALSE; int sd; -char *http_method = NULL; -char *http_post_data = NULL; +char *http_method = "GET"; +char *http_post_data = ""; char buffer[MAX_INPUT_BUFFER]; void print_usage (void); void print_help (void); int process_arguments (int, char **); -int call_getopt (int, char **); static char *base64 (char *bin, int len); int check_http (void); int my_recv (void); @@ -221,9 +235,9 @@ main (int argc, char **argv) if (strstr (timestamp, ":")) { if (strstr (server_url, "?")) - sprintf (server_url, "%s&%s", server_url, timestamp); + asprintf (&server_url, "%s&%s", server_url, timestamp); else - sprintf (server_url, "%s?%s", server_url, timestamp); + asprintf (&server_url, "%s?%s", server_url, timestamp); } if (display_html == TRUE) @@ -231,9 +245,9 @@ main (int argc, char **argv) host_name, server_port, server_url); /* initialize alarm signal handling, set socket timeout, start timer */ - signal (SIGALRM, socket_timeout_alarm_handler); - alarm (socket_timeout); - time (&start_time); + (void) signal (SIGALRM, socket_timeout_alarm_handler); + (void) alarm (socket_timeout); + gettimeofday (&tv, NULL); #ifdef HAVE_SSL if (use_ssl && check_cert == TRUE) { @@ -268,13 +282,13 @@ main (int argc, char **argv) int process_arguments (int argc, char **argv) { - int c, i = 1; - char optchars[MAX_INPUT_BUFFER]; + int c = 1; #ifdef HAVE_GETOPT_H int option_index = 0; static struct option long_options[] = { - STD_OPTS_LONG, + STD_LONG_OPTS, + {"file",required_argument,0,'F'}, {"link", no_argument, 0, 'L'}, {"nohtml", no_argument, 0, 'n'}, {"ssl", no_argument, 0, 'S'}, @@ -285,6 +299,7 @@ process_arguments (int argc, char **argv) {"regex", required_argument, 0, 'r'}, {"ereg", required_argument, 0, 'r'}, {"eregi", required_argument, 0, 'R'}, + {"linespan", no_argument, 0, 'l'}, {"onredirect", required_argument, 0, 'f'}, {"certificate", required_argument, 0, 'C'}, {0, 0, 0, 0} @@ -307,14 +322,13 @@ process_arguments (int argc, char **argv) strcpy (argv[c], "-n"); } - snprintf (optchars, MAX_INPUT_BUFFER, "%s%s", STD_OPTS, - "P:I:a:e:p:s:R:r:u:f:C:nLS"); +#define OPTCHARS "Vvht:c:w:H:P:I:a:e:p:s:R:r:u:f:C:nlLS" while (1) { #ifdef HAVE_GETOPT_H - c = getopt_long (argc, argv, optchars, long_options, &option_index); + c = getopt_long (argc, argv, OPTCHARS, long_options, &option_index); #else - c = getopt (argc, argv, optchars); + c = getopt (argc, argv, OPTCHARS); #endif if (c == -1 || c == EOF) break; @@ -328,7 +342,7 @@ process_arguments (int argc, char **argv) exit (STATE_OK); break; case 'V': /* version */ - print_revision (PROGNAME, REVISION); + print_revision (progname, REVISION); exit (STATE_OK); break; case 't': /* timeout period */ @@ -339,13 +353,13 @@ process_arguments (int argc, char **argv) case 'c': /* critical time threshold */ if (!is_intnonneg (optarg)) usage2 ("invalid critical threshold", optarg); - critical_time = atoi (optarg); + critical_time = strtod (optarg, NULL); check_critical_time = TRUE; break; case 'w': /* warning time threshold */ if (!is_intnonneg (optarg)) usage2 ("invalid warning threshold", optarg); - warning_time = atoi (optarg); + warning_time = strtod (optarg, NULL); check_warning_time = TRUE; break; case 'L': /* show html link */ @@ -362,7 +376,7 @@ process_arguments (int argc, char **argv) if (specify_port == FALSE) server_port = HTTPS_PORT; break; - case 'C': /* warning time threshold */ + case 'C': /* Check SSL cert validity */ #ifdef HAVE_SSL if (!is_intnonneg (optarg)) usage2 ("invalid certificate expiration period", optarg); @@ -383,17 +397,19 @@ process_arguments (int argc, char **argv) onredirect = STATE_WARNING; if (!strcmp (optarg, "critical")) onredirect = STATE_CRITICAL; + if (verbose) + printf("option f:%d \n", onredirect); break; /* Note: H, I, and u must be malloc'd or will fail on redirects */ case 'H': /* Host Name (virtual host) */ - host_name = strscpy (host_name, optarg); + asprintf (&host_name, "%s", optarg); break; case 'I': /* Server IP-address */ - server_address = strscpy (server_address, optarg); + asprintf (&server_address, "%s", optarg); break; case 'u': /* Host or server */ - server_url = strscpy (server_url, optarg); - server_url_length = strlen (optarg); + asprintf (&server_url, "%s", optarg); + server_url_length = strlen (server_url); break; case 'p': /* Host or server */ if (!is_intnonneg (optarg)) @@ -406,8 +422,8 @@ process_arguments (int argc, char **argv) user_auth[MAX_INPUT_BUFFER - 1] = 0; break; case 'P': /* HTTP POST data in URL encoded format */ - http_method = strscpy (http_method, "POST"); - http_post_data = strscpy (http_post_data, optarg); + asprintf (&http_method, "%s", "POST"); + asprintf (&http_post_data, "%s", optarg); break; case 's': /* string or substring */ strncpy (string_expect, optarg, MAX_INPUT_BUFFER - 1); @@ -416,28 +432,31 @@ process_arguments (int argc, char **argv) case 'e': /* string or substring */ strncpy (server_expect, optarg, MAX_INPUT_BUFFER - 1); server_expect[MAX_INPUT_BUFFER - 1] = 0; + server_expect_yn = 1; break; - case 'R': /* regex */ -#ifdef HAVE_REGEX_H - cflags = REG_ICASE; -#else +#ifndef HAVE_REGEX_H + case 'l': /* linespan */ + case 'r': /* linespan */ + case 'R': /* linespan */ usage ("check_http: call for regex which was not a compiled option\n"); -#endif + break; +#else + case 'l': /* linespan */ + cflags &= ~REG_NEWLINE; + break; + case 'R': /* regex */ + cflags |= REG_ICASE; case 'r': /* regex */ -#ifdef HAVE_REGEX_H - cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; - strncpy (regexp, optarg, MAX_INPUT_BUFFER - 1); - regexp[MAX_INPUT_BUFFER - 1] = 0; + strncpy (regexp, optarg, MAX_RE_SIZE - 1); + regexp[MAX_RE_SIZE - 1] = 0; errcode = regcomp (&preg, regexp, cflags); if (errcode != 0) { - regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); + (void) regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); printf ("Could Not Compile Regular Expression: %s", errbuf); return ERROR; } -#else - usage ("check_http: call for regex which was not a compiled option\n"); -#endif break; +#endif case 'v': /* verbose */ verbose = TRUE; break; @@ -446,26 +465,17 @@ process_arguments (int argc, char **argv) c = optind; - if (server_address == NULL && host_name == NULL) { - server_address = strscpy (NULL, argv[c]); - host_name = strscpy (NULL, argv[c++]); - } - - if (server_address == NULL && host_name == NULL) - usage ("check_http: you must specify a host name\n"); + if (strcmp (server_address, "") == 0 && c < argc) + asprintf (&server_address, "%s", argv[c++]); - if (server_address == NULL) - server_address = strscpy (NULL, host_name); + if (strcmp (host_name, "") == 0 && c < argc) + asprintf (&host_name, "%s", argv[c++]); - if (host_name == NULL) - host_name = strscpy (NULL, server_address); - - if (http_method == NULL) - http_method = strscpy (http_method, "GET"); - - if (server_url == NULL) { - server_url = strscpy (NULL, "/"); - server_url_length = strlen(HTTP_URL); + if (strcmp (server_address ,"") == 0) { + if (strcmp (host_name, "") == 0) + usage ("check_http: you must specify a server address or host name\n"); + else + asprintf (&server_address, "%s", host_name); } return TRUE; @@ -482,9 +492,7 @@ base64 (char *bin, int len) int i = 0, j = 0; char BASE64_END = '='; - char base64_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; + char base64_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; while (j < len - 2) { buf[i++] = base64_table[bin[j] >> 2]; @@ -521,23 +529,25 @@ int check_http (void) { char *msg = NULL; - char *status_line = NULL; + char *status_line = ""; char *header = NULL; - char *page = NULL; + char *page = ""; char *auth = NULL; int i = 0; size_t pagesize = 0; - char *full_page = NULL; - char *pos = NULL; + char *full_page = ""; + char *buf = NULL; + char *pos = ""; + char *x = NULL; + char *orig_url = NULL; + double elapsed_time; /* try to connect to the host at the given port number */ #ifdef HAVE_SSL if (use_ssl == TRUE) { - if (connect_SSL () != OK) { - msg = ssprintf (msg, "Unable to open TCP socket"); - terminate (STATE_CRITICAL, msg); - } + if (connect_SSL () != OK) + terminate (STATE_CRITICAL, "Unable to open TCP socket"); if ((server_cert = SSL_get_peer_certificate (ssl)) != NULL) { X509_free (server_cert); @@ -547,25 +557,25 @@ check_http (void) return STATE_CRITICAL; } - sprintf (buffer, "%s %s HTTP/1.0\r\n", http_method, server_url); - if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { + asprintf (&buf, "%s %s HTTP/1.0\r\n", http_method, server_url); + if (SSL_write (ssl, buf, strlen (buf)) == -1) { ERR_print_errors_fp (stderr); return STATE_CRITICAL; } /* optionally send the host header info (not clear if it's usable) */ if (strcmp (host_name, "")) { - sprintf (buffer, "Host: %s\r\n", host_name); - if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { + asprintf (&buf, "Host: %s\r\n", host_name); + if (SSL_write (ssl, buf, strlen (buf)) == -1) { ERR_print_errors_fp (stderr); return STATE_CRITICAL; } } /* send user agent */ - sprintf (buffer, "User-Agent: check_http/%s (nagios-plugins %s)\r\n", + asprintf (&buf, "User-Agent: check_http/%s (nagios-plugins %s)\r\n", clean_revstring (REVISION), PACKAGE_VERSION); - if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { + if (SSL_write (ssl, buf, strlen (buf)) == -1) { ERR_print_errors_fp (stderr); return STATE_CRITICAL; } @@ -573,90 +583,94 @@ check_http (void) /* optionally send the authentication info */ if (strcmp (user_auth, "")) { auth = base64 (user_auth, strlen (user_auth)); - sprintf (buffer, "Authorization: Basic %s\r\n", auth); - if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { + asprintf (&buf, "Authorization: Basic %s\r\n", auth); + if (SSL_write (ssl, buf, strlen (buf)) == -1) { ERR_print_errors_fp (stderr); return STATE_CRITICAL; } } - /* optionally send http POST data */ - if (http_post_data) { - sprintf (buffer, "Content-Type: application/x-www-form-urlencoded\r\n"); - if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { + /* either send http POST data */ + if (strlen (http_post_data)) { + asprintf (&buf, "Content-Type: application/x-www-form-urlencoded\r\n"); + if (SSL_write (ssl, buf, strlen (buf)) == -1) { ERR_print_errors_fp (stderr); return STATE_CRITICAL; } - sprintf (buffer, "Content-Length: %i\r\n\r\n", strlen (http_post_data)); - if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { + asprintf (&buf, "Content-Length: %i\r\n\r\n", strlen (http_post_data)); + if (SSL_write (ssl, buf, strlen (buf)) == -1) { ERR_print_errors_fp (stderr); return STATE_CRITICAL; } - http_post_data = strscat (http_post_data, "\r\n"); if (SSL_write (ssl, http_post_data, strlen (http_post_data)) == -1) { ERR_print_errors_fp (stderr); return STATE_CRITICAL; } + asprintf (&buf, CRLF); + if (SSL_write (ssl, buf, strlen (buf)) == -1) { + ERR_print_errors_fp (stderr); + return STATE_CRITICAL; + } } - - /* send a newline so the server knows we're done with the request */ - sprintf (buffer, "\r\n\r\n"); - if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { - ERR_print_errors_fp (stderr); - return STATE_CRITICAL; + else { + /* or just a newline so the server knows we're done with the request */ + asprintf (&buf, "\r\n"); + if (SSL_write (ssl, buf, strlen (buf)) == -1) { + ERR_print_errors_fp (stderr); + return STATE_CRITICAL; + } } } else { #endif - if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) { - msg = ssprintf (msg, "Unable to open TCP socket"); - terminate (STATE_CRITICAL, msg); - } - sprintf (buffer, "%s %s HTTP/1.0\r\n", http_method, server_url); - send (sd, buffer, strlen (buffer), 0); + if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) + terminate (STATE_CRITICAL, "Unable to open TCP socket"); + asprintf (&buf, "%s %s HTTP/1.0\r\n", http_method, server_url); + send (sd, buf, strlen (buf), 0); /* optionally send the host header info */ if (strcmp (host_name, "")) { - sprintf (buffer, "Host: %s\r\n", host_name); - send (sd, buffer, strlen (buffer), 0); + asprintf (&buf, "Host: %s\r\n", host_name); + send (sd, buf, strlen (buf), 0); } /* send user agent */ - sprintf (buffer, + asprintf (&buf, "User-Agent: check_http/%s (nagios-plugins %s)\r\n", clean_revstring (REVISION), PACKAGE_VERSION); - send (sd, buffer, strlen (buffer), 0); + send (sd, buf, strlen (buf), 0); /* optionally send the authentication info */ if (strcmp (user_auth, "")) { auth = base64 (user_auth, strlen (user_auth)); - sprintf (buffer, "Authorization: Basic %s\r\n", auth); - send (sd, buffer, strlen (buffer), 0); + asprintf (&buf, "Authorization: Basic %s\r\n", auth); + send (sd, buf, strlen (buf), 0); } - /* optionally send http POST data */ + /* either send http POST data */ /* written by Chris Henesy */ - if (http_post_data) { - sprintf (buffer, "Content-Type: application/x-www-form-urlencoded\r\n"); - send (sd, buffer, strlen (buffer), 0); - sprintf (buffer, "Content-Length: %i\r\n\r\n", strlen (http_post_data)); - send (sd, buffer, strlen (buffer), 0); - http_post_data = strscat (http_post_data, "\r\n"); + if (strlen (http_post_data)) { + asprintf (&buf, "Content-Type: application/x-www-form-urlencoded\r\n"); + send (sd, buf, strlen (buf), 0); + asprintf (&buf, "Content-Length: %i\r\n\r\n", strlen (http_post_data)); + send (sd, buf, strlen (buf), 0); send (sd, http_post_data, strlen (http_post_data), 0); + send (sd, CRLF, strlen (CRLF), 0); + } + else { + /* or just a newline so the server knows we're done with the request */ + asprintf (&buf, "\r\n"); + send (sd, buf, strlen (buf), 0); } - - /* send a newline so the server knows we're done with the request */ - sprintf (buffer, "\r\n\r\n"); - send (sd, buffer, strlen (buffer), 0); #ifdef HAVE_SSL } #endif /* fetch the page */ - pagesize = (size_t) 0; while ((i = my_recv ()) > 0) { - full_page = strscat (full_page, buffer); + buffer[i] = '\0'; + asprintf (&full_page, "%s%s", full_page, buffer); pagesize += i; } @@ -684,7 +698,7 @@ check_http (void) page += (size_t) strcspn (page, "\r\n"); pos = page; page += (size_t) strspn (page, "\r\n"); - status_line[pos - status_line] = 0; + status_line[strcspn(status_line, "\r\n")] = 0; strip (status_line); if (verbose) printf ("STATUS: %s\n", status_line); @@ -708,114 +722,134 @@ check_http (void) /* make sure the status line matches the response we are looking for */ if (!strstr (status_line, server_expect)) { if (server_port == HTTP_PORT) - msg = ssprintf (msg, "Invalid HTTP response received from host\n"); + asprintf (&msg, "Invalid HTTP response received from host\n"); else - msg = ssprintf (msg, + asprintf (&msg, "Invalid HTTP response received from host on port %d\n", server_port); terminate (STATE_CRITICAL, msg); } - /* check the return code */ - /* server errors result in a critical state */ - if (strstr (status_line, "500") || - strstr (status_line, "501") || - strstr (status_line, "502") || - strstr (status_line, "503")) { - msg = ssprintf (msg, "HTTP CRITICAL: %s\n", status_line); - terminate (STATE_CRITICAL, msg); - } - /* client errors result in a warning state */ - if (strstr (status_line, "400") || - strstr (status_line, "401") || - strstr (status_line, "402") || - strstr (status_line, "403") || - strstr (status_line, "404")) { - msg = ssprintf (msg, "HTTP WARNING: %s\n", status_line); - terminate (STATE_WARNING, msg); + /* Exit here if server_expect was set by user and not default */ + if ( server_expect_yn ) { + asprintf (&msg, "HTTP OK: Status line output matched \"%s\"\n", + server_expect); + if (verbose) + printf ("%s\n",msg); + } + else { + + + /* check the return code */ + /* server errors result in a critical state */ + if (strstr (status_line, "500") || + strstr (status_line, "501") || + strstr (status_line, "502") || + strstr (status_line, "503")) { + terminate (STATE_CRITICAL, "HTTP CRITICAL: %s\n", status_line); + } - /* check redirected page if specified */ - if (strstr (status_line, "300") || - strstr (status_line, "301") || - strstr (status_line, "302") || - strstr (status_line, "303") || - strstr (status_line, "304")) { - if (onredirect == STATE_DEPENDENT) { - - pos = header; - while (pos) { - server_address = realloc (server_address, MAX_IPV4_HOSTLENGTH); - if (server_address == NULL) - terminate (STATE_UNKNOWN, - "HTTP UNKNOWN: could not allocate server_address"); - if (strspn (pos, "\r\n") > server_url_length) { - server_url = realloc (server_url, strspn (pos, "\r\n")); - if (server_url == NULL) - terminate (STATE_UNKNOWN, - "HTTP UNKNOWN: could not allocate server_url"); - server_url_length = strspn (pos, "\r\n"); - } - if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PORT URI_PATH, server_type, server_address, server_port_text, server_url) == 4) { - host_name = strscpy (host_name, server_address); - use_ssl = server_type_check (server_type); - server_port = atoi (server_port_text); - check_http (); - } - else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PATH, server_type, server_address, server_url) == 3) { - host_name = strscpy (host_name, server_address); - use_ssl = server_type_check (server_type); - server_port = server_port_check (use_ssl); - check_http (); - } - else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PORT, server_type, server_address, server_port_text) == 3) { - host_name = strscpy (host_name, server_address); - strcpy (server_url, "/"); - use_ssl = server_type_check (server_type); - server_port = atoi (server_port_text); - check_http (); - } - else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST, server_type, server_address) == 2) { - host_name = strscpy (host_name, server_address); - strcpy (server_url, "/"); - use_ssl = server_type_check (server_type); - server_port = server_port_check (use_ssl); - check_http (); - } - else if (sscanf (pos, HDR_LOCATION URI_PATH, server_url) == 1) { - check_http (); - } - 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", - status_line, (display_html ? "" : "")); - exit (STATE_UNKNOWN); - } /* end if (onredirect == STATE_DEPENDENT) */ - else if (onredirect == STATE_UNKNOWN) - printf ("HTTP UNKNOWN"); - else if (onredirect == STATE_OK) - printf ("HTTP ok"); - else if (onredirect == STATE_WARNING) - printf ("HTTP WARNING"); - else if (onredirect == STATE_CRITICAL) - printf ("HTTP CRITICAL"); - time (&end_time); - msg = ssprintf (msg, ": %s - %d second response time %s%s\n", - status_line, (int) (end_time - start_time), - timestamp, (display_html ? "" : "")); - terminate (onredirect, msg); - } /* end if (strstr (status_line, "30[0-4]") */ + /* client errors result in a warning state */ + if (strstr (status_line, "400") || + strstr (status_line, "401") || + strstr (status_line, "402") || + strstr (status_line, "403") || + strstr (status_line, "404")) { + terminate (STATE_WARNING, "HTTP WARNING: %s\n", status_line); + } + /* check redirected page if specified */ + if (strstr (status_line, "300") || + strstr (status_line, "301") || + strstr (status_line, "302") || + strstr (status_line, "303") || + strstr (status_line, "304")) { + if (onredirect == STATE_DEPENDENT) { + + asprintf (&orig_url, "%s", server_url); + pos = header; + while (pos) { + server_address = realloc (server_address, MAX_IPV4_HOSTLENGTH); + if (server_address == NULL) + terminate (STATE_UNKNOWN, + "HTTP UNKNOWN: could not allocate server_address"); + if (strcspn (pos, "\r\n") > server_url_length) { + server_url = realloc (server_url, strcspn (pos, "\r\n")); + if (server_url == NULL) + terminate (STATE_UNKNOWN, + "HTTP UNKNOWN: could not allocate server_url"); + server_url_length = strcspn (pos, "\r\n"); + } + if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PORT URI_PATH, server_type, server_address, server_port_text, server_url) == 4) { + asprintf (&host_name, "%s", server_address); + use_ssl = server_type_check (server_type); + server_port = atoi (server_port_text); + check_http (); + } + else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PATH, server_type, server_address, server_url) == 3 ) { + asprintf (&host_name, "%s", server_address); + use_ssl = server_type_check (server_type); + server_port = server_port_check (use_ssl); + check_http (); + } + else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PORT, server_type, server_address, server_port_text) == 3) { + asprintf (&host_name, "%s", server_address); + strcpy (server_url, "/"); + use_ssl = server_type_check (server_type); + server_port = atoi (server_port_text); + check_http (); + } + else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST, server_type, server_address) == 2) { + asprintf (&host_name, "%s", server_address); + strcpy (server_url, "/"); + use_ssl = server_type_check (server_type); + server_port = server_port_check (use_ssl); + check_http (); + } + else if (sscanf (pos, HDR_LOCATION URI_PATH, server_url) == 1) { + if ((server_url[0] != '/') && (x = strrchr(orig_url, '/'))) { + *x = '\0'; + asprintf (&server_url, "%s/%s", orig_url, server_url); + } + check_http (); + } + 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", + status_line, (display_html ? "" : "")); + exit (STATE_UNKNOWN); + } /* end if (onredirect == STATE_DEPENDENT) */ + + else if (onredirect == STATE_UNKNOWN) + printf ("HTTP UNKNOWN"); + else if (onredirect == STATE_OK) + printf ("HTTP ok"); + else if (onredirect == STATE_WARNING) + printf ("HTTP WARNING"); + else if (onredirect == STATE_CRITICAL) + printf ("HTTP CRITICAL"); + elapsed_time = delta_time (tv); + asprintf (&msg, ": %s - %7.3f second response time %s%s|time=%7.3f\n", + status_line, elapsed_time, timestamp, + (display_html ? "" : ""), elapsed_time); + terminate (onredirect, msg); + } /* end if (strstr (status_line, "30[0-4]") */ + + + } /* end else (server_expect_yn) */ + + /* check elapsed time */ - time (&end_time); - msg = ssprintf (msg, "HTTP problem: %s - %d second response time %s%s\n", - status_line, (int) (end_time - start_time), - timestamp, (display_html ? "" : "")); - if (check_critical_time == TRUE && (end_time - start_time) > critical_time) + elapsed_time = delta_time (tv); + asprintf (&msg, "HTTP problem: %s - %7.3f second response time %s%s|time=%7.3f\n", + status_line, elapsed_time, timestamp, + (display_html ? "" : ""), elapsed_time); + if (check_critical_time == TRUE && elapsed_time > critical_time) terminate (STATE_CRITICAL, msg); - if (check_warning_time == TRUE && (end_time - start_time) > warning_time) + if (check_warning_time == TRUE && elapsed_time > warning_time) terminate (STATE_WARNING, msg); /* Page and Header content checks go here */ @@ -823,14 +857,14 @@ check_http (void) if (strlen (string_expect)) { if (strstr (page, string_expect)) { - printf ("HTTP ok: %s - %d second response time %s%s\n", - status_line, (int) (end_time - start_time), - timestamp, (display_html ? "" : "")); + printf ("HTTP ok: %s - %7.3f second response time %s%s|time=%7.3f\n", + status_line, elapsed_time, + timestamp, (display_html ? "" : ""), elapsed_time); exit (STATE_OK); } else { - printf ("HTTP CRITICAL: string not found%s\n", - (display_html ? "" : "")); + printf ("HTTP CRITICAL: string not found%s|time=%7.3f\n", + (display_html ? "" : ""), elapsed_time); exit (STATE_CRITICAL); } } @@ -838,15 +872,15 @@ check_http (void) if (strlen (regexp)) { errcode = regexec (&preg, page, REGS, pmatch, 0); if (errcode == 0) { - printf ("HTTP ok: %s - %d second response time %s%s\n", - status_line, (int) (end_time - start_time), - timestamp, (display_html ? "" : "")); + printf ("HTTP ok: %s - %7.3f second response time %s%s|time=%7.3f\n", + status_line, elapsed_time, + timestamp, (display_html ? "" : ""), elapsed_time); exit (STATE_OK); } else { if (errcode == REG_NOMATCH) { - printf ("HTTP CRITICAL: pattern not found%s\n", - (display_html ? "" : "")); + printf ("HTTP CRITICAL: pattern not found%s|time=%7.3f\n", + (display_html ? "" : ""), elapsed_time); exit (STATE_CRITICAL); } else { @@ -859,22 +893,21 @@ check_http (void) #endif /* We only get here if all tests have been passed */ - msg = ssprintf (msg, "HTTP ok: %s - %d second response time %s%s\n", - status_line, (int) (end_time - start_time), - timestamp, (display_html ? "" : "")); + asprintf (&msg, "HTTP ok: %s - %7.3f second response time %s%s|time=%7.3f\n", + status_line, (float)elapsed_time, + timestamp, (display_html ? "" : ""), elapsed_time); terminate (STATE_OK, msg); return STATE_UNKNOWN; } - + #ifdef HAVE_SSL -int -connect_SSL (void) +int connect_SSL (void) { SSL_METHOD *meth; - randbuff = strscpy (NULL, "qwertyuiopasdfghjkl"); + asprintf (randbuff, "%s", "qwertyuiopasdfghjkl"); RAND_seed (randbuff, strlen (randbuff)); /* Initialize SSL context */ SSLeay_add_ssl_algorithms (); @@ -892,7 +925,7 @@ connect_SSL (void) alarm (socket_timeout); /* Save start time */ - time (&start_time); + gettimeofday (&tv, NULL); /* Make TCP connection */ if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK) { @@ -925,8 +958,6 @@ check_certificate (X509 ** certificate) int offset; struct tm stamp; int days_left; - /* int result = STATE_OK; */ - /* char timestamp[14]; */ /* Retrieve timestamp of certificate */ @@ -970,8 +1001,8 @@ check_certificate (X509 ** certificate) stamp.tm_isdst = -1; days_left = (mktime (&stamp) - time (NULL)) / 86400; - sprintf - (timestamp, "%02d/%02d/%04d %02d:%02d", + snprintf + (timestamp, 16, "%02d/%02d/%04d %02d:%02d", stamp.tm_mon + 1, stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min); @@ -1038,14 +1069,14 @@ my_close (void) void print_help (void) { - print_revision (PROGNAME, REVISION); + print_revision (progname, REVISION); printf ("Copyright (c) %s %s <%s>\n\n%s\n", COPYRIGHT, AUTHORS, EMAIL, SUMMARY); print_usage (); printf ("NOTE: One or both of -H and -I must be specified\n"); printf ("\nOptions:\n" LONGOPTIONS "\n", HTTP_EXPECT, HTTP_PORT, - DEFAULT_SOCKET_TIMEOUT, SSLOPTIONS); + DEFAULT_SOCKET_TIMEOUT, SSLOPTIONS, REGOPTIONS); #ifdef HAVE_SSL printf (SSLDESCRIPTION); #endif @@ -1063,5 +1094,5 @@ print_usage (void) " %s -h for detailed help\n" " %s -V for version information\n", #endif - PROGNAME, OPTIONS, PROGNAME, PROGNAME); + progname, OPTIONS, progname, progname); }