فهرست منبع

check_users not correctly detecting thresholds

Fix for issue https://github.com/nagios-plugins/nagios-plugins/issues/81

check_users now uses the standard warning and critical ranges parser and
a standard perdata output routine.
John C. Frickson 9 سال پیش
والد
کامیت
a5983eda69
4فایلهای تغییر یافته به همراه75 افزوده شده و 61 حذف شده
  1. 1 0
      NEWS
  2. 22 38
      plugins/check_users.c
  3. 41 0
      plugins/utils.c
  4. 11 23
      plugins/utils.h

+ 1 - 0
NEWS

@@ -12,6 +12,7 @@ This file documents the major additions and syntax changes between releases.
 	  to force TLSv1.1 and TLSv1.2 connections, respectively
 	  to force TLSv1.1 and TLSv1.2 connections, respectively
 	The check_http -S/--ssl option now allows for specifying the desired
 	The check_http -S/--ssl option now allows for specifying the desired
 	  protocol with a "+" suffix to also accept newer versions
 	  protocol with a "+" suffix to also accept newer versions
+	check_users: add support for range thresholds (John C. Frickson)
 
 
 	FIXES
 	FIXES
 	Let check_real terminate lines with CRLF when talking to the server, as
 	Let check_real terminate lines with CRLF when talking to the server, as

+ 22 - 38
plugins/check_users.c

@@ -54,15 +54,15 @@ int process_arguments (int, char **);
 void print_help (void);
 void print_help (void);
 void print_usage (void);
 void print_usage (void);
 
 
-int wusers = -1;
-int cusers = -1;
+char *warning_range = NULL;
+char *critical_range = NULL;
+thresholds *thlds = NULL;
 
 
 int
 int
 main (int argc, char **argv)
 main (int argc, char **argv)
 {
 {
 	int users = -1;
 	int users = -1;
 	int result = STATE_UNKNOWN;
 	int result = STATE_UNKNOWN;
-	char *perf;
 #if HAVE_WTSAPI32_H
 #if HAVE_WTSAPI32_H
 	WTS_SESSION_INFO *wtsinfo;
 	WTS_SESSION_INFO *wtsinfo;
 	DWORD wtscount;
 	DWORD wtscount;
@@ -77,8 +77,6 @@ main (int argc, char **argv)
 	bindtextdomain (PACKAGE, LOCALEDIR);
 	bindtextdomain (PACKAGE, LOCALEDIR);
 	textdomain (PACKAGE);
 	textdomain (PACKAGE);
 
 
-	perf = strdup ("");
-
 	/* Parse extra opts if any */
 	/* Parse extra opts if any */
 	argv = np_extra_opts (&argc, argv, progname);
 	argv = np_extra_opts (&argc, argv, progname);
 
 
@@ -160,23 +158,15 @@ main (int argc, char **argv)
 #endif
 #endif
 
 
 	/* check the user count against warning and critical thresholds */
 	/* check the user count against warning and critical thresholds */
-	if (users > cusers)
-		result = STATE_CRITICAL;
-	else if (users > wusers)
-		result = STATE_WARNING;
-	else if (users >= 0)
-		result = STATE_OK;
+	result = get_status((double)users, thlds);
 
 
 	if (result == STATE_UNKNOWN)
 	if (result == STATE_UNKNOWN)
 		printf ("%s\n", _("Unable to read output"));
 		printf ("%s\n", _("Unable to read output"));
 	else {
 	else {
-		xasprintf (&perf, "%s", perfdata ("users", users, "",
-		  TRUE, wusers,
-		  TRUE, cusers,
-		  TRUE, 0,
-		  FALSE, 0));
-		printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
-		  users, perf);
+		printf (_("USERS %s - %d users currently logged in |%s\n"), 
+				state_text(result), users,
+				sperfdata_int("users", users, "", warning_range,
+							critical_range, TRUE, 0, FALSE, 0));
 	}
 	}
 
 
 	return result;
 	return result;
@@ -215,33 +205,27 @@ process_arguments (int argc, char **argv)
 			print_revision (progname, NP_VERSION);
 			print_revision (progname, NP_VERSION);
 			exit (STATE_UNKNOWN);
 			exit (STATE_UNKNOWN);
 		case 'c':									/* critical */
 		case 'c':									/* critical */
-			if (!is_intnonneg (optarg))
-				usage4 (_("Critical threshold must be a positive integer"));
-			else
-				cusers = atoi (optarg);
+			critical_range = optarg;
 			break;
 			break;
 		case 'w':									/* warning */
 		case 'w':									/* warning */
-			if (!is_intnonneg (optarg))
-				usage4 (_("Warning threshold must be a positive integer"));
-			else
-				wusers = atoi (optarg);
+			warning_range = optarg;
 			break;
 			break;
 		}
 		}
 	}
 	}
 
 
 	c = optind;
 	c = optind;
-	if (wusers == -1 && argc > c) {
-		if (is_intnonneg (argv[c]) == FALSE)
-			usage4 (_("Warning threshold must be a positive integer"));
-		else
-			wusers = atoi (argv[c++]);
-	}
-	if (cusers == -1 && argc > c) {
-		if (is_intnonneg (argv[c]) == FALSE)
-			usage4 (_("Warning threshold must be a positive integer"));
-		else
-			cusers = atoi (argv[c]);
-	}
+	if (warning_range == NULL && argc > c)
+		warning_range = argv[c++];
+	if (critical_range == NULL && argc > c)
+		critical_range = argv[c++];
+
+	/* this will abort in case of invalid ranges */
+	set_thresholds (&thlds, warning_range, critical_range);
+
+	if (thlds->warning->end <= 0)
+		usage4 (_("Warning threshold must be a positive integer"));
+	if (thlds->critical->end <= 0)
+		usage4 (_("Critical threshold must be a positive integer"));
 
 
 	return OK;
 	return OK;
 }
 }

+ 41 - 0
plugins/utils.c

@@ -668,3 +668,44 @@ char *sperfdata (const char *label,
 
 
 	return data;
 	return data;
 }
 }
+
+char *sperfdata_int (const char *label,
+ int val,
+ const char *uom,
+ char *warn,
+ char *crit,
+ int minp,
+ int minv,
+ int maxp,
+ int maxv)
+{
+	char *data = NULL;
+	if (strpbrk (label, "'= "))
+		xasprintf (&data, "'%s'=", label);
+	else
+		xasprintf (&data, "%s=", label);
+
+	xasprintf (&data, "%s%d", data, val);
+	xasprintf (&data, "%s%s;", data, uom);
+
+	if (warn!=NULL)
+		xasprintf (&data, "%s%s", data, warn);
+
+	xasprintf (&data, "%s;", data);
+
+	if (crit!=NULL)
+		xasprintf (&data, "%s%s", data, crit);
+
+	xasprintf (&data, "%s;", data);
+
+	if (minp)
+		xasprintf (&data, "%s%d", data, minv);
+
+	if (maxp) {
+		xasprintf (&data, "%s;", data);
+		xasprintf (&data, "%s%d", data, maxv);
+	}
+
+	return data;
+}
+

+ 11 - 23
plugins/utils.h

@@ -94,29 +94,17 @@ const char *state_text (int);
 #define max(a,b) (((a)>(b))?(a):(b))
 #define max(a,b) (((a)>(b))?(a):(b))
 #define min(a,b) (((a)<(b))?(a):(b))
 #define min(a,b) (((a)<(b))?(a):(b))
 
 
-char *perfdata (const char *,
- long int,
- const char *,
- int,
- long int,
- int,
- long int,
- int,
- long int,
- int,
- long int);
-
-char *fperfdata (const char *,
- double,
- const char *,
- int,
- double,
- int,
- double,
- int,
- double,
- int,
- double);
+char *perfdata (const char *, long int, const char *, int, long int,
+                int, long int, int, long int, int, long int);
+
+char *fperfdata (const char *, double, const char *, int, double,
+                 int, double, int, double, int, double);
+
+char *sperfdata (const char *, double, const char *, char *, char *,
+                 int, double, int, double);
+
+char *sperfdata_int (const char *, int, const char *, char *, char *,
+                     int, int, int, int);
 
 
 /* The idea here is that, although not every plugin will use all of these, 
 /* The idea here is that, although not every plugin will use all of these, 
    most will or should.  Therefore, for consistency, these very common 
    most will or should.  Therefore, for consistency, these very common