Просмотр исходного кода

millisecond timimg where supported

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@125 f882894a-f735-0410-b71e-b25c423dba1c
Karl DeBisschop 23 лет назад
Родитель
Сommit
d02fedd5aa
3 измененных файлов с 47 добавлено и 47 удалено
  1. 39 36
      plugins/check_http.c
  2. 6 6
      plugins/utils.c
  3. 2 5
      plugins/utils.h.in

+ 39 - 36
plugins/check_http.c

@@ -31,7 +31,6 @@
 
 
 #include "config.h"
 #include "config.h"
 #include "common.h"
 #include "common.h"
-#include "version.h"
 #include "netutils.h"
 #include "netutils.h"
 #include "utils.h"
 #include "utils.h"
 
 
@@ -160,6 +159,8 @@ int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE;
 int errcode;
 int errcode;
 #endif
 #endif
 
 
+struct timeval tv;
+
 #define server_type_check(server_type) \
 #define server_type_check(server_type) \
 (strcmp (server_type, "https") ? FALSE : TRUE)
 (strcmp (server_type, "https") ? FALSE : TRUE)
 
 
@@ -189,9 +190,9 @@ int server_url_length = 0;
 int server_expect_yn = 0;
 int server_expect_yn = 0;
 char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT;
 char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT;
 char string_expect[MAX_INPUT_BUFFER] = "";
 char string_expect[MAX_INPUT_BUFFER] = "";
-int warning_time = 0;
+double warning_time = 0;
 int check_warning_time = FALSE;
 int check_warning_time = FALSE;
-int critical_time = 0;
+double critical_time = 0;
 int check_critical_time = FALSE;
 int check_critical_time = FALSE;
 char user_auth[MAX_INPUT_BUFFER] = "";
 char user_auth[MAX_INPUT_BUFFER] = "";
 int display_html = FALSE;
 int display_html = FALSE;
@@ -234,7 +235,7 @@ main (int argc, char **argv)
 	/* initialize alarm signal handling, set socket timeout, start timer */
 	/* initialize alarm signal handling, set socket timeout, start timer */
 	(void) signal (SIGALRM, socket_timeout_alarm_handler);
 	(void) signal (SIGALRM, socket_timeout_alarm_handler);
 	(void) alarm (socket_timeout);
 	(void) alarm (socket_timeout);
-	(void) time (&start_time);
+	gettimeofday (&tv, NULL);
 
 
 #ifdef HAVE_SSL
 #ifdef HAVE_SSL
 	if (use_ssl && check_cert == TRUE) {
 	if (use_ssl && check_cert == TRUE) {
@@ -340,13 +341,13 @@ process_arguments (int argc, char **argv)
 		case 'c': /* critical time threshold */
 		case 'c': /* critical time threshold */
 			if (!is_intnonneg (optarg))
 			if (!is_intnonneg (optarg))
 				usage2 ("invalid critical threshold", optarg);
 				usage2 ("invalid critical threshold", optarg);
-			critical_time = atoi (optarg);
+			critical_time = strtod (optarg, NULL);
 			check_critical_time = TRUE;
 			check_critical_time = TRUE;
 			break;
 			break;
 		case 'w': /* warning time threshold */
 		case 'w': /* warning time threshold */
 			if (!is_intnonneg (optarg))
 			if (!is_intnonneg (optarg))
 				usage2 ("invalid warning threshold", optarg);
 				usage2 ("invalid warning threshold", optarg);
-			warning_time = atoi (optarg);
+			warning_time = strtod (optarg, NULL);
 			check_warning_time = TRUE;
 			check_warning_time = TRUE;
 			break;
 			break;
 		case 'L': /* show html link */
 		case 'L': /* show html link */
@@ -363,7 +364,7 @@ process_arguments (int argc, char **argv)
 			if (specify_port == FALSE)
 			if (specify_port == FALSE)
 				server_port = HTTPS_PORT;
 				server_port = HTTPS_PORT;
 			break;
 			break;
-		case 'C': /* warning time threshold */
+		case 'C': /* Check SSL cert validity */
 #ifdef HAVE_SSL
 #ifdef HAVE_SSL
 			if (!is_intnonneg (optarg))
 			if (!is_intnonneg (optarg))
 				usage2 ("invalid certificate expiration period", optarg);
 				usage2 ("invalid certificate expiration period", optarg);
@@ -389,10 +390,10 @@ process_arguments (int argc, char **argv)
 			break;
 			break;
 		/* Note: H, I, and u must be malloc'd or will fail on redirects */
 		/* Note: H, I, and u must be malloc'd or will fail on redirects */
 		case 'H': /* Host Name (virtual host) */
 		case 'H': /* Host Name (virtual host) */
-			host_name = strscpy (host_name, optarg);
+ 			host_name = strscpy (host_name, optarg);
 			break;
 			break;
 		case 'I': /* Server IP-address */
 		case 'I': /* Server IP-address */
-			server_address = strscpy (server_address, optarg);
+ 			server_address = strscpy (server_address, optarg);
 			break;
 			break;
 		case 'u': /* Host or server */
 		case 'u': /* Host or server */
 			server_url = strscpy (server_url, optarg);
 			server_url = strscpy (server_url, optarg);
@@ -534,6 +535,7 @@ check_http (void)
 	char *pos = NULL;
 	char *pos = NULL;
 	char *x = NULL;
 	char *x = NULL;
 	char *orig_url = NULL;
 	char *orig_url = NULL;
+	double elapsed_time;
 
 
 	/* try to connect to the host at the given port number */
 	/* try to connect to the host at the given port number */
 #ifdef HAVE_SSL
 #ifdef HAVE_SSL
@@ -657,10 +659,11 @@ check_http (void)
 #endif
 #endif
 
 
 	/* fetch the page */
 	/* fetch the page */
-	pagesize = (size_t) 0;
+	pagesize = (size_t) 1;
+	asprintf (&full_page, "");
 	while ((i = my_recv ()) > 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;
 		pagesize += i;
 	}
 	}
 
 
@@ -821,10 +824,10 @@ check_http (void)
 				printf ("HTTP WARNING");
 				printf ("HTTP WARNING");
 			else if (onredirect == STATE_CRITICAL)
 			else if (onredirect == STATE_CRITICAL)
 				printf ("HTTP 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 - %6.2f second response time %s%s|time=%6.2f\n",
+		                 status_line, elapsed_time, timestamp,
+	                   (display_html ? "</A>" : ""), elapsed_time);
 			terminate (onredirect, msg);
 			terminate (onredirect, msg);
 		} /* end if (strstr (status_line, "30[0-4]") */
 		} /* end if (strstr (status_line, "30[0-4]") */
 
 
@@ -833,13 +836,13 @@ check_http (void)
 
 
 		
 		
 	/* check elapsed time */
 	/* 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 - %6.2f second response time %s%s|time=%6.2f\n",
+	               status_line, elapsed_time, timestamp,
+	               (display_html ? "</A>" : ""), elapsed_time);
+	if (check_critical_time == TRUE && elapsed_time > critical_time)
 		terminate (STATE_CRITICAL, msg);
 		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);
 		terminate (STATE_WARNING, msg);
 
 
 	/* Page and Header content checks go here */
 	/* Page and Header content checks go here */
@@ -847,14 +850,14 @@ check_http (void)
 
 
 	if (strlen (string_expect)) {
 	if (strlen (string_expect)) {
 		if (strstr (page, 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 - %6.2f second response time %s%s|time=%6.2f\n",
+			        status_line, elapsed_time,
+			        timestamp, (display_html ? "</A>" : ""), elapsed_time);
 			exit (STATE_OK);
 			exit (STATE_OK);
 		}
 		}
 		else {
 		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=%6.2f\n",
+			        (display_html ? "</A>" : ""), elapsed_time);
 			exit (STATE_CRITICAL);
 			exit (STATE_CRITICAL);
 		}
 		}
 	}
 	}
@@ -862,15 +865,15 @@ check_http (void)
 	if (strlen (regexp)) {
 	if (strlen (regexp)) {
 		errcode = regexec (&preg, page, REGS, pmatch, 0);
 		errcode = regexec (&preg, page, REGS, pmatch, 0);
 		if (errcode == 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 - %6.2f second response time %s%s|time=%6.2f\n",
+			        status_line, elapsed_time,
+			        timestamp, (display_html ? "</A>" : ""), elapsed_time);
 			exit (STATE_OK);
 			exit (STATE_OK);
 		}
 		}
 		else {
 		else {
 			if (errcode == REG_NOMATCH) {
 			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=%6.2f\n",
+				        (display_html ? "</A>" : ""), elapsed_time);
 				exit (STATE_CRITICAL);
 				exit (STATE_CRITICAL);
 			}
 			}
 			else {
 			else {
@@ -883,9 +886,9 @@ check_http (void)
 #endif
 #endif
 
 
 	/* We only get here if all tests have been passed */
 	/* 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 - %6.2f second response time %s%s|time=%6.2f\n",
+	                status_line, (float)elapsed_time,
+	                timestamp, (display_html ? "</A>" : ""), elapsed_time);
 	terminate (STATE_OK, msg);
 	terminate (STATE_OK, msg);
 	return STATE_UNKNOWN;
 	return STATE_UNKNOWN;
 }
 }
@@ -915,7 +918,7 @@ int connect_SSL (void)
 	alarm (socket_timeout);
 	alarm (socket_timeout);
 
 
 	/* Save start time */
 	/* Save start time */
-	time (&start_time);
+	gettimeofday (&tv, NULL);
 
 
 	/* Make TCP connection */
 	/* Make TCP connection */
 	if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK) {
 	if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK) {

+ 6 - 6
plugins/utils.c

@@ -317,14 +317,14 @@ is_option (char *str)
 
 
 
 
 double
 double
-delta_time (struct timeval *tv)
+delta_time (struct timeval tv)
 {
 {
-	struct timeval *pt;
-	struct timezone *tz;
+	struct timeval now;
+	struct timezone tz;
+	double et;
 
 
-	gettimeofday (pt, tz);
-
-	return (pt->tv_sec - tv->tv_sec + (pt->tv_usec - tv->tv_usec) / 1000000);
+	gettimeofday (&now, NULL);
+	return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
 }
 }
 
 
 
 

+ 2 - 5
plugins/utils.h.in

@@ -52,17 +52,14 @@ struct timeval {
 	long tv_usec;  /* microseconds */
 	long tv_usec;  /* microseconds */
 };
 };
 
 
-struct timezone {
-	int  tz_minuteswest; /* minutes W of Greenwich */
-	int  tz_dsttime;     /* type of dst correction */
-};
-
 #define gettimeofday (tvp,tz) {\
 #define gettimeofday (tvp,tz) {\
  tvp->tv_usec=0;\
  tvp->tv_usec=0;\
  tvp->tv_sec=(long)time();\
  tvp->tv_sec=(long)time();\
 }
 }
 #endif
 #endif
 
 
+double delta_time (struct timeval tv);
+
 /* Handle strings safely */
 /* Handle strings safely */
 
 
 void strip (char *buffer);
 void strip (char *buffer);