diff --git a/plugins/check_http.c b/plugins/check_http.c
index 3ba406a848d1005cee8e5214a92450a9c54e739c..de7a2db7f7b70b70d3917874ddca0a2908df03fc 100644 (file)
--- a/plugins/check_http.c
+++ b/plugins/check_http.c
*
*****************************************************************************/
-#define PROGNAME "check_http"
+const char *progname = "check_http";
#define REVISION "$Revision$"
#define COPYRIGHT "1999-2001"
#define AUTHORS "Ethan Galstad/Karl DeBisschop"
#include "config.h"
#include "common.h"
-#include "version.h"
#include "netutils.h"
#include "utils.h"
int errcode;
#endif
+struct timeval tv;
+
#define server_type_check(server_type) \
(strcmp (server_type, "https") ? FALSE : TRUE)
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;
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);
/* initialize alarm signal handling, set socket timeout, start timer */
(void) signal (SIGALRM, socket_timeout_alarm_handler);
(void) alarm (socket_timeout);
- (void) time (&start_time);
+ gettimeofday (&tv, NULL);
#ifdef HAVE_SSL
if (use_ssl && check_cert == TRUE) {
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'},
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:nLS"
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;
exit (STATE_OK);
break;
case 'V': /* version */
- print_revision (PROGNAME, REVISION);
+ print_revision (progname, REVISION);
exit (STATE_OK);
break;
case 't': /* timeout period */
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 */
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);
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);
+ host_name = strscpy (host_name, optarg);
break;
case 'I': /* Server IP-address */
- server_address = strscpy (server_address, optarg);
+ server_address = strscpy (server_address, optarg);
break;
case 'u': /* Host or server */
server_url = strscpy (server_url, optarg);
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 *full_page = "";
char *buf = NULL;
- char *pos = 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
}
/* send a newline so the server knows we're done with the request */
- asprintf (&buf, "\r\n\r\n");
+ asprintf (&buf, "\r\n");
send (sd, buf, strlen (buf), 0);
#ifdef HAVE_SSL
}
#endif
/* fetch the page */
- pagesize = (size_t) 0;
while ((i = my_recv ()) > 0) {
- buffer[i] = '\0';
- full_page = strscat (full_page, buffer);
+ buffer[i] = '\0';
+ asprintf (&full_page, "%s%s", full_page, buffer);
pagesize += i;
}
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);
printf ("HTTP WARNING");
else if (onredirect == STATE_CRITICAL)
printf ("HTTP CRITICAL");
- time (&end_time);
- asprintf (&msg, ": %s - %d second response time %s%s|time=%d\n",
- status_line, (int) (end_time - start_time), timestamp,
- (display_html ? "</A>" : ""), (int) (end_time - start_time));
+ 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 ? "</A>" : ""), elapsed_time);
terminate (onredirect, msg);
} /* end if (strstr (status_line, "30[0-4]") */
/* check elapsed time */
- time (&end_time);
- asprintf (&msg, "HTTP problem: %s - %d second response time %s%s|time=%d\n",
- status_line, (int) (end_time - start_time), timestamp,
- (display_html ? "</A>" : ""), (int) (end_time - start_time));
- 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 ? "</A>" : ""), 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 */
if (strlen (string_expect)) {
if (strstr (page, string_expect)) {
- printf ("HTTP ok: %s - %d second response time %s%s|time=%d\n",
- status_line, (int) (end_time - start_time),
- timestamp, (display_html ? "</A>" : ""), (int) (end_time - start_time));
+ printf ("HTTP ok: %s - %7.3f second response time %s%s|time=%7.3f\n",
+ status_line, elapsed_time,
+ timestamp, (display_html ? "</A>" : ""), elapsed_time);
exit (STATE_OK);
}
else {
- printf ("HTTP CRITICAL: string not found%s|time=%d\n",
- (display_html ? "</A>" : ""), (int) (end_time - start_time));
+ printf ("HTTP CRITICAL: string not found%s|time=%7.3f\n",
+ (display_html ? "</A>" : ""), elapsed_time);
exit (STATE_CRITICAL);
}
}
if (strlen (regexp)) {
errcode = regexec (&preg, page, REGS, pmatch, 0);
if (errcode == 0) {
- printf ("HTTP ok: %s - %d second response time %s%s|time=%d\n",
- status_line, (int) (end_time - start_time),
- timestamp, (display_html ? "</A>" : ""), (int) (end_time - start_time));
+ printf ("HTTP ok: %s - %7.3f second response time %s%s|time=%7.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=%d\n",
- (display_html ? "</A>" : ""), (int) (end_time - start_time));
+ printf ("HTTP CRITICAL: pattern not found%s|time=%7.3f\n",
+ (display_html ? "</A>" : ""), elapsed_time);
exit (STATE_CRITICAL);
}
else {
#endif
/* We only get here if all tests have been passed */
- asprintf (&msg, "HTTP ok: %s - %d second response time %s%s|time=%d\n",
- status_line, (int) (end_time - start_time),
- timestamp, (display_html ? "</A>" : ""), (int) (end_time - start_time));
+ asprintf (&msg, "HTTP ok: %s - %7.3f second response time %s%s|time=%7.3f\n",
+ status_line, (float)elapsed_time,
+ timestamp, (display_html ? "</A>" : ""), elapsed_time);
terminate (STATE_OK, msg);
return STATE_UNKNOWN;
}
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) {
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);
" %s -h for detailed help\n"
" %s -V for version information\n",
#endif
- PROGNAME, OPTIONS, PROGNAME, PROGNAME);
+ progname, OPTIONS, progname, progname);
}