Ver Fonte

New timeout switch format implemented for all

relevant plugins.  I am still unsure if 3 different alarms are needed,
there may be a simpler way - unifying all plugin timeouts under utils.c
functions.  New syntax for timeouts is:

-t <interval>:<state>

Interval is a positive integer.
State is case insensitive and one of:
state name: ok/warning/critical/unknown
Or stateid: 0/1/2/3

For example:

-t 5:0 = 5 seconds timeout with state ok
-t :warning = default timeout with state warning
-t 5 = 5 second timeout with default state

etc.
abrist há 11 anos atrás
pai
commit
a8cfdceca2

+ 2 - 8
plugins/check_disk.c

@@ -482,14 +482,8 @@ process_arguments (int argc, char **argv)
 
     switch (c) {
     case 't':                 /* timeout period */
-      if (is_integer (optarg)) {
-        timeout_interval = atoi (optarg);
-        break;
-      }
-      else {
-        usage2 (_("Timeout interval must be a positive integer"), optarg);
-      }
-
+      timeout_interval = parse_timeout_string(optarg);
+      break;
     /* See comments for 'c' */
     case 'w':                 /* warning threshold */
       if (strstr(optarg, "%")) {

+ 1 - 1
plugins/check_dns.c

@@ -464,7 +464,7 @@ process_arguments (int argc, char **argv)
       verbose = TRUE;
       break;
     case 't': /* timeout period */
-      timeout_interval = atoi (optarg);
+      timeout_interval = parse_timeout_string (optarg);
       break;
     case 'H': /* hostname */
       if (strlen (optarg) >= ADDRESS_LENGTH)

+ 1 - 4
plugins/check_http.c

@@ -274,10 +274,7 @@ process_arguments (int argc, char **argv)
       exit (STATE_OK);
       break;
     case 't': /* timeout period */
-      if (!is_intnonneg (optarg))
-        usage2 (_("Timeout interval must be a positive integer"), optarg);
-      else
-        socket_timeout = atoi (optarg);
+      socket_timeout = parse_socket_timeout_string(optarg);
       break;
     case 'c': /* critical time threshold */
       critical_thresholds = optarg;

+ 1 - 4
plugins/check_ldap.c

@@ -289,10 +289,7 @@ process_arguments (int argc, char **argv)
 			print_revision (progname, NP_VERSION);
 			exit (STATE_OK);
 		case 't':									/* timeout period */
-			if (!is_intnonneg (optarg))
-				usage2 (_("Timeout interval must be a positive integer"), optarg);
-			else
-				socket_timeout = atoi (optarg);
+			socket_timeout = parse_socket_timeout_string(optarg);
 			break;
 		case 'H':
 			ld_host = optarg;

+ 1 - 5
plugins/check_nagios.c

@@ -253,11 +253,7 @@ process_arguments (int argc, char **argv)
 				     _("Expiration time must be an integer (seconds)\n"));
 			break;
 		case 't':									/* timeout */
-			if (is_intnonneg (optarg))
-				timeout_interval = atoi (optarg);
-			else
-				die (STATE_UNKNOWN,
-				     _("Timeout must be an integer (seconds)\n"));
+			timeout_interval = parse_timeout_string (optarg);
 			break;
 		case 'v':
 			verbose++;

+ 0 - 3
plugins/check_nt.c

@@ -509,7 +509,6 @@ int process_arguments(int argc, char **argv){
 	{
 		{"port",     required_argument,0,'p'},
 		{"timeout",  required_argument,0,'t'},
-		{"timeoutstate",  required_argument,0,'Z'},
 		{"critical", required_argument,0,'c'},
 		{"warning",  required_argument,0,'w'},
 		{"variable", required_argument,0,'v'},
@@ -616,8 +615,6 @@ int process_arguments(int argc, char **argv){
 				break;
 			case 't': /* timeout */
 				socket_timeout = parse_socket_timeout_string(optarg);
-				if(socket_timeout<=0)
-					return ERROR;
 				break;
 			}
 

+ 2 - 2
plugins/check_ntp.c

@@ -472,7 +472,7 @@ double offset_request(const char *host, int *status){
 	}
 
 	if (one_read == 0) {
-		die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
+		die(socket_timeout_state, "%s: No response from NTP server\n", state_text(socket_timeout_state));
 	}
 
 	/* now, pick the best server from the list */
@@ -712,7 +712,7 @@ int process_arguments(int argc, char **argv){
 			server_address = strdup(optarg);
 			break;
 		case 't':
-			socket_timeout=atoi(optarg);
+			socket_timeout = parse_socket_timeout_string(optarg);
 			break;
 		case '4':
 			address_family = AF_INET;

+ 1 - 1
plugins/check_ntp_peer.c

@@ -501,7 +501,7 @@ int process_arguments(int argc, char **argv){
 			port=atoi(optarg);
 			break;
 		case 't':
-			socket_timeout=atoi(optarg);
+			socket_timeout = parse_socket_timeout_string(optarg);
 			break;
 		case '4':
 			address_family = AF_INET;

+ 2 - 2
plugins/check_ntp_time.c

@@ -418,7 +418,7 @@ double offset_request(const char *host, int *status){
 	}
 
 	if (one_read == 0) {
-		die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
+		die(socket_timeout_state, "%s: No response from NTP server\n", state_text(socket_timeout_state));
 	}
 
 	/* now, pick the best server from the list */
@@ -503,7 +503,7 @@ int process_arguments(int argc, char **argv){
 			port = strdup(optarg);
 			break;
 		case 't':
-			socket_timeout=atoi(optarg);
+			socket_timeout = parse_socket_timeout_string(optarg);
 			break;
 		case 'o':
 			time_offset=atoi(optarg);

+ 1 - 3
plugins/check_nwstat.c

@@ -1576,9 +1576,7 @@ int process_arguments(int argc, char **argv) {
 				check_critical_value=TRUE;
 				break;
 			case 't': /* timeout */
-				socket_timeout=atoi(optarg);
-				if (socket_timeout<=0)
-					return ERROR;
+				socket_timeout = parse_socket_timeout_string(optarg);
 			}
 
 	}

+ 1 - 3
plugins/check_overcr.c

@@ -395,9 +395,7 @@ process_arguments (int argc, char **argv)
 			check_critical_value = TRUE;
 			break;
 		case 't':									/* timeout */
-			socket_timeout = atoi (optarg);
-			if (socket_timeout <= 0)
-				return ERROR;
+			socket_timeout = parse_socket_timeout_string (optarg);
 		}
 
 	}

+ 1 - 4
plugins/check_pgsql.c

@@ -307,10 +307,7 @@ process_arguments (int argc, char **argv)
 			print_revision (progname, NP_VERSION);
 			exit (STATE_OK);
 		case 't':     /* timeout period */
-			if (!is_integer (optarg))
-				usage2 (_("Timeout interval must be a positive integer"), optarg);
-			else
-				timeout_interval = atoi (optarg);
+			timeout_interval = parse_timeout_string (optarg);
 			break;
 		case 'c':     /* critical time threshold */
 			if (!is_nonnegative (optarg))

+ 1 - 1
plugins/check_ping.c

@@ -231,7 +231,7 @@ process_arguments (int argc, char **argv)
 			exit (STATE_OK);
 			break;
 		case 't':	/* timeout period */
-			timeout_interval = atoi (optarg);
+			timeout_interval = parse_timeout_string(optarg);
 			break;
 		case 'v':	/* verbose mode */
 			verbose++;

+ 1 - 4
plugins/check_procs.c

@@ -433,10 +433,7 @@ process_arguments (int argc, char **argv)
 			print_revision (progname, NP_VERSION);
 			exit (STATE_OK);
 		case 't':									/* timeout period */
-			if (!is_integer (optarg))
-				usage2 (_("Timeout interval must be a positive integer"), optarg);
-			else
-				timeout_interval = atoi (optarg);
+			timeout_interval = parse_timeout_string (optarg);
 			break;
 		case 'c':									/* critical threshold */
 			critical_range = optarg;

+ 1 - 4
plugins/check_radius.c

@@ -302,10 +302,7 @@ process_arguments (int argc, char **argv)
 				usage4 (_("Number of retries must be a positive integer"));
 			break;
 		case 't':									/* timeout */
-			if (is_intpos (optarg))
-				timeout_interval = atoi (optarg);
-			else
-				usage2 (_("Timeout interval must be a positive integer"), optarg);
+			timeout_interval = parse_timeout_string (optarg);
 			break;
 		}
 	}

+ 1 - 6
plugins/check_real.c

@@ -349,12 +349,7 @@ process_arguments (int argc, char **argv)
 			verbose = TRUE;
 			break;
 		case 't':									/* timeout */
-			if (is_intnonneg (optarg)) {
-				socket_timeout = atoi (optarg);
-			}
-			else {
-				usage4 (_("Timeout interval must be a positive integer"));
-			}
+			socket_timeout = parse_socket_timeout_string (optarg);
 			break;
 		case 'V':									/* version */
 			print_revision (progname, NP_VERSION);

+ 1 - 6
plugins/check_smtp.c

@@ -574,12 +574,7 @@ process_arguments (int argc, char **argv)
 			ignore_send_quit_failure++;             /* ignore problem sending QUIT */
 			break;
 		case 't':									/* timeout */
-			if (is_intnonneg (optarg)) {
-				socket_timeout = atoi (optarg);
-			}
-			else {
-				usage4 (_("Timeout interval must be a positive integer"));
-			}
+			socket_timeout = parse_socket_timeout_string (optarg);
 			break;
 		case 'S':
 		/* starttls */

+ 1 - 1
plugins/check_snmp.c

@@ -353,7 +353,7 @@ main (int argc, char **argv)
 		external_error=1;
 	if (external_error) {
 		if ((chld_err.lines > 0) && strstr(chld_err.line[0], "Timeout")) {
-			printf (_("External command error: %s\n"), chld_err.line[0]);
+			printf (_("%s - External command error: %s\n"), state_text(runcmd_timeout_state), chld_err.line[0]);
 			for (i = 1; i < chld_err.lines; i++) {
 				printf ("%s\n", chld_err.line[i]);
 			}

+ 1 - 4
plugins/check_ssh.c

@@ -136,10 +136,7 @@ process_arguments (int argc, char **argv)
 			verbose = TRUE;
 			break;
 		case 't':									/* timeout period */
-			if (!is_integer (optarg))
-				usage2 (_("Timeout interval must be a positive integer"), optarg);
-			else
-				socket_timeout = atoi (optarg);
+			socket_timeout = parse_socket_timeout_string (optarg);
 			break;
 		case '4':
 			address_family = AF_INET;

+ 1 - 4
plugins/check_tcp.c

@@ -506,10 +506,7 @@ process_arguments (int argc, char **argv)
 			warn_codes[warn_codes_count - 1] = optarg;
 			break;
 		case 't':                 /* timeout */
-			if (!is_intpos (optarg))
-				usage4 (_("Timeout interval must be a positive integer"));
-			else
-				socket_timeout = atoi (optarg);
+			socket_timeout = parse_socket_timeout_string (optarg);
 			break;
 		case 'p':                 /* port */
 			if (!is_intpos (optarg))

+ 2 - 9
plugins/check_time.c

@@ -198,7 +198,6 @@ process_arguments (int argc, char **argv)
 		{"port", required_argument, 0, 'p'},
 		{"udp", no_argument, 0, 'u'},
 		{"timeout", required_argument, 0, 't'},
-		{"timeoutstate",  required_argument,0,'Z'},
 		{"version", no_argument, 0, 'V'},
 		{"help", no_argument, 0, 'h'},
 		{0, 0, 0, 0}
@@ -221,7 +220,7 @@ process_arguments (int argc, char **argv)
 	}
 
 	while (1) {
-		c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:uZ:", longopts,
+		c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts,
 									 &option);
 
 		if (c == -1 || c == EOF)
@@ -299,14 +298,8 @@ process_arguments (int argc, char **argv)
 				server_port = atoi (optarg);
 			break;
 		case 't':									/* timeout */
-			if (!is_intnonneg (optarg))
-				usage2 (_("Timeout interval must be a positive integer"), optarg);
-			else
-				socket_timeout = atoi (optarg);
+			socket_timeout = parse_socket_timeout_string (optarg);
 			break;
-                case 'Z':
-                        set_socket_timeout_state(optarg);
-                        break;
 		case 'u':									/* udp */
 			use_udp = TRUE;
 		}

+ 1 - 6
plugins/check_ups.c

@@ -549,12 +549,7 @@ process_arguments (int argc, char **argv)
 				usage2 (_("Unrecognized UPS variable"), optarg);
 			break;
 		case 't':									/* timeout */
-			if (is_intnonneg (optarg)) {
-				socket_timeout = atoi (optarg);
-			}
-			else {
-				usage4 (_("Timeout interval must be a positive integer"));
-			}
+			socket_timeout = parse_socket_timeout_string (optarg);
 			break;
 		case 'V':									/* version */
 			print_revision (progname, NP_VERSION);

+ 1 - 4
plugins/check_uptime.c

@@ -173,10 +173,7 @@ static int process_arguments (int argc, char **argv) {
 				warning = optarg;
 				break;
 			case 't': /* timeout period */
-				if (!is_intpos (optarg))
-					usage2 (_("Timeout interval must be a positive integer"), optarg);
-				else
-					timeout_interval = atoi (optarg);
+				timeout_interval = parse_timeout_string (optarg);
 				break;
 			} // end case
 		} // end while

+ 30 - 18
plugins/netutils.c

@@ -53,32 +53,44 @@ socket_timeout_alarm_handler (int sig)
 	exit (socket_timeout_state);
 }
 
+void
+set_socket_timeout_state (char *state) {
+        if ((socket_timeout_state = translate_state(state)) == ERROR)
+                usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
+}
+
 int
 parse_socket_timeout_string (char *timeout_str)
 {
-        char *seperated_str;
-        int timeout_value;
-        if ( strstr(timeout_str, ":") == NULL) {
-                return atoi(timeout_str);
+	char *seperated_str;
+        char *timeout_val = "";
+	char *timeout_sta;
+        if ( strstr(timeout_str, ":" ) == NULL) {
+		timeout_val = timeout_str;
+        } else if ( strncmp(timeout_str, ":", 1 ) == 0) {
+		seperated_str = strtok(timeout_str, ":");
+                if ( seperated_str != NULL ) {
+                	timeout_sta = seperated_str;
+		}
         } else {
-                seperated_str = strtok(timeout_str, ":");
-                timeout_value = atoi(seperated_str);
+		seperated_str = strtok(timeout_str, ":");
+                timeout_val = seperated_str;
                 seperated_str = strtok(NULL, ":");
-
                 if (seperated_str != NULL) {
-                        set_socket_timeout_state(seperated_str);
-                }
-
-                if (timeout_value != NULL) {
-                        return timeout_value;
+                        timeout_sta = seperated_str;
                 }
         }
-}
-
-void
-set_socket_timeout_state (char *state) {
-        if ((socket_timeout_state = translate_state(state)) == ERROR)
-                usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
+        if ( timeout_sta != NULL ) {
+		set_socket_timeout_state(timeout_sta);
+	}
+	if (( timeout_val == NULL ) || ( timeout_val[0] == '\0' )) {
+		return socket_timeout;
+	} else if (is_intpos(timeout_val)) {
+		return atoi(timeout_val);
+	} else {
+		usage4 (_("Socket timeout value must be a positive integer"));
+		exit (STATE_UNKNOWN);
+	}
 }
 
 /* connects to a host on a specified tcp port, sends a string, and gets a

+ 25 - 13
plugins/runcmd.c

@@ -68,6 +68,7 @@
 static pid_t *np_pids = NULL;
 
 unsigned int runcmd_timeout_state = STATE_CRITICAL;
+unsigned int runcmd_timeout = DEFAULT_SOCKET_TIMEOUT;
 
 /* Try sysconf(_SC_OPEN_MAX) first, as it can be higher than OPEN_MAX.
  * If that fails and the macro isn't defined, we fall back to an educated
@@ -282,26 +283,37 @@ set_runcmd_timeout_state (char *state)
 int
 parse_runcmd_timeout_string (char *timeout_str)
 {
-        char *seperated_str;
-        int timeout_value;
-        if ( strstr(timeout_str, ":") == NULL) {
-                return atoi(timeout_str);
+	char *seperated_str;
+        char *timeout_val = "";
+	char *timeout_sta;
+        if ( strstr(timeout_str, ":" ) == NULL) {
+		timeout_val = timeout_str;
+        } else if ( strncmp(timeout_str, ":", 1 ) == 0) {
+		seperated_str = strtok(timeout_str, ":");
+                if ( seperated_str != NULL ) {
+                	timeout_sta = seperated_str;
+		}
         } else {
-                seperated_str = strtok(timeout_str, ":");
-                timeout_value = atoi(seperated_str);
+		seperated_str = strtok(timeout_str, ":");
+                timeout_val = seperated_str;
                 seperated_str = strtok(NULL, ":");
-
                 if (seperated_str != NULL) {
-                        set_runcmd_timeout_state(seperated_str);
-                }
-
-                if (timeout_value != NULL) {
-                        return timeout_value;
+                        timeout_sta = seperated_str;
                 }
         }
+        if ( timeout_sta != NULL ) {
+		set_runcmd_timeout_state(timeout_sta);
+	}
+	if (( timeout_val == NULL ) || ( timeout_val[0] == '\0' )) {
+		return runcmd_timeout;
+	} else if (is_intpos(timeout_val)) {
+		return atoi(timeout_val);
+	} else {
+		usage4 (_("Socket timeout value must be a positive integer"));
+		exit (STATE_UNKNOWN);
+	}
 }
 
-
 static int
 np_fetch_output(int fd, output *op, int flags)
 {

+ 1 - 0
plugins/runcmd.h

@@ -28,6 +28,7 @@
 #include "utils_cmd.h" /* for the "output" type */
 
 extern unsigned int runcmd_timeout_state;
+extern unsigned int runcmd_timeout;
 
 /** prototypes **/
 int np_runcmd(const char *, output *, output *, int);

+ 31 - 19
plugins/utils.c

@@ -177,32 +177,44 @@ timeout_alarm_handler (int signo)
 	}
 }
 
+void
+set_timeout_state (char *state) {
+        if ((timeout_state = translate_state(state)) == ERROR)
+                usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
+}
+
 int
 parse_timeout_string (char *timeout_str)
 {
 	char *seperated_str;
-	int timeout_value;
-	if ( strstr(timeout_str, ":") == NULL) {
-		return atoi(timeout_str);
-	} else {
+        char *timeout_val = "";
+	char *timeout_sta;
+        if ( strstr(timeout_str, ":" ) == NULL) {
+		timeout_val = timeout_str;
+        } else if ( strncmp(timeout_str, ":", 1 ) == 0) {
 		seperated_str = strtok(timeout_str, ":");
-		timeout_value = atoi(seperated_str);
-		seperated_str = strtok(NULL, ":");
-
-		if (seperated_str != NULL) {
-			set_timeout_state(seperated_str);
-		}
-
-		if (timeout_value != NULL) {
-			return timeout_value;
+                if ( seperated_str != NULL ) {
+                	timeout_sta = seperated_str;
 		}
+        } else {
+		seperated_str = strtok(timeout_str, ":");
+                timeout_val = seperated_str;
+                seperated_str = strtok(NULL, ":");
+                if (seperated_str != NULL) {
+                        timeout_sta = seperated_str;
+                }
+        }
+        if ( timeout_sta != NULL ) {
+		set_timeout_state(timeout_sta);
+	}
+	if (( timeout_val == NULL ) || ( timeout_val[0] == '\0' )) {
+		return timeout_interval;
+	} else if (is_intpos(timeout_val)) {
+		return atoi(timeout_val);
+	} else {
+		usage4 (_("Timeout value must be a positive integer"));
+		exit (STATE_UNKNOWN);
 	}
-}
-
-void
-set_timeout_state (char *state) {
-	if ((timeout_state = translate_state(state)) == ERROR)
-                usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
 }
 
 int