Преглед изворни кода

Added new functions to set the state to utils

I have started to work the changes into each of the plugins.
abrist пре 11 година
родитељ
комит
4bff52ce32
6 измењених фајлова са 30 додато и 31 уклоњено
  1. 1 1
      plugins/check_apt.c
  2. 1 4
      plugins/check_by_ssh.c
  3. 2 8
      plugins/check_dbi.c
  4. 1 6
      plugins/check_dig.c
  5. 2 11
      plugins/check_snmp.c
  6. 23 1
      plugins/runcmd.c

+ 1 - 1
plugins/check_apt.c

@@ -168,7 +168,7 @@ int process_arguments (int argc, char **argv) {
 			verbose++;
 			break;
 		case 't':
-			timeout_interval=atoi(optarg);
+			timeout_interval=parse_runcmd_timeout_string(optarg);
 			break;
 		case 'd':
 			upgrade=DIST_UPGRADE;

+ 1 - 4
plugins/check_by_ssh.c

@@ -216,10 +216,7 @@ process_arguments (int argc, char **argv)
 			verbose = TRUE;
 			break;
 		case 't':									/* timeout period */
-			if (!is_integer (optarg))
-				usage_va(_("Timeout interval must be a positive integer"));
-			else
-				timeout_interval = atoi (optarg);
+			timeout_interval = parse_timeout_string (optarg);
 			break;
 		case 'H':									/* host */
 			host_or_die(optarg);

+ 2 - 8
plugins/check_dbi.c

@@ -35,9 +35,7 @@ const char *email = "devel@nagios-plugins.org";
 
 #include "common.h"
 #include "utils.h"
-
 #include "netutils.h"
-
 #include "regex.h"
 
 /* required for NAN */
@@ -47,9 +45,7 @@ const char *email = "devel@nagios-plugins.org";
 
 #include <assert.h>
 #include <math.h>
-
 #include <dbi/dbi.h>
-
 #include <stdarg.h>
 
 typedef enum {
@@ -420,10 +416,8 @@ process_arguments (int argc, char **argv)
 				usage2 (_("Invalid metric"), optarg);
 			break;
 		case 't':     /* timeout */
-			if (!is_intnonneg (optarg))
-				usage2 (_("Timeout interval must be a positive integer"), optarg);
-			else
-				timeout_interval = atoi (optarg);
+			timeout_interval = parse_timeout_string(optarg);
+			break;
 
 		case 'H':     /* host */
 			if (!is_host (optarg))

+ 1 - 6
plugins/check_dig.c

@@ -260,12 +260,7 @@ process_arguments (int argc, char **argv)
       }
       break;
     case 't':                 /* timeout */
-      if (is_intnonneg (optarg)) {
-        timeout_interval = atoi (optarg);
-      }
-      else {
-        usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
-      }
+      timeout_interval = parse_runcmd_timeout_string (optarg);
       break;
     case 'r':                 /* retires */
       if (is_intnonneg (optarg)) {

+ 2 - 11
plugins/check_snmp.c

@@ -667,8 +667,6 @@ process_arguments (int argc, char **argv)
 		{"output-delimiter", required_argument, 0, 'D'},
 		{"string", required_argument, 0, 's'},
 		{"timeout", required_argument, 0, 't'},
-		{"timeoutstate", required_argument, 0, 'Z'},
-		{"regex", required_argument, 0, 'r'},
 		{"ereg", required_argument, 0, 'r'},
 		{"eregi", required_argument, 0, 'R'},
 		{"label", required_argument, 0, 'l'},
@@ -706,7 +704,7 @@ process_arguments (int argc, char **argv)
 	}
 
 	while (1) {
-		c = getopt_long (argc, argv, "nhvVOt:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:L:U:a:x:A:X:Z:",
+		c = getopt_long (argc, argv, "nhvVOt:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:L:U:a:x:A:X:",
 									 longopts, &option);
 
 		if (c == -1 || c == EOF)
@@ -763,15 +761,8 @@ process_arguments (int argc, char **argv)
 			privpasswd = optarg;
 			break;
 		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_runcmd_timeout_string (optarg);
 			break;
-                case 'Z':
-                        set_runcmd_timeout_state(optarg);
-                        break;
-
 	/* Test parameters */
 		case 'c':									/* critical threshold */
 			critical_thresholds = optarg;

+ 23 - 1
plugins/runcmd.c

@@ -263,7 +263,7 @@ runcmd_timeout_alarm_handler (int signo)
 	size_t i;
 
 	if (signo == SIGALRM)
-		puts(_("CRITICAL - Plugin timed out while executing system call\n"));
+		printf("%s - Plugin timed out while executing system call\n", state_text(runcmd_timeout_state));
 
 	if(np_pids) for(i = 0; i < maxfd; i++) {
 		if(np_pids[i] != 0) kill(np_pids[i], SIGKILL);
@@ -279,6 +279,28 @@ set_runcmd_timeout_state (char *state)
                 usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
 }
 
+int
+parse_runcmd_timeout_string (char *timeout_str)
+{
+        char *seperated_str;
+        int timeout_value;
+        if ( strstr(timeout_str, ":") == NULL) {
+                return atoi(timeout_str);
+        } else {
+                seperated_str = strtok(timeout_str, ":");
+                timeout_value = atoi(seperated_str);
+                seperated_str = strtok(NULL, ":");
+
+                if (seperated_str != NULL) {
+                        set_runcmd_timeout_state(seperated_str);
+                }
+
+                if (timeout_value != NULL) {
+                        return timeout_value;
+                }
+        }
+}
+
 
 static int
 np_fetch_output(int fd, output *op, int flags)