aboutsummaryrefslogtreecommitdiff
path: root/plugins/check_users.c
diff options
context:
space:
mode:
authorGravatar John C. Frickson <jfrickson@nagios.com> 2016-11-07 13:06:05 -0600
committerGravatar Sven Nierlein <sven@nierlein.de> 2016-11-07 21:36:25 +0100
commita5983eda69b442a90495909803724901669b50fb (patch)
treefa3442fe3bc20309245188d88d77c6cdd15c17d9 /plugins/check_users.c
parent3a12034805caf6c4ad21a8f86c8e4a43ff62576c (diff)
downloadmonitoring-plugins-a5983eda69b442a90495909803724901669b50fb.tar.gz
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.
Diffstat (limited to 'plugins/check_users.c')
-rw-r--r--plugins/check_users.c60
1 files changed, 22 insertions, 38 deletions
diff --git a/plugins/check_users.c b/plugins/check_users.c
index 54415a48..a10249c4 100644
--- a/plugins/check_users.c
+++ b/plugins/check_users.c
@@ -54,15 +54,15 @@ int process_arguments (int, char **);
void print_help (void);
void print_usage (void);
-int wusers = -1;
-int cusers = -1;
+char *warning_range = NULL;
+char *critical_range = NULL;
+thresholds *thlds = NULL;
int
main (int argc, char **argv)
{
int users = -1;
int result = STATE_UNKNOWN;
- char *perf;
#if HAVE_WTSAPI32_H
WTS_SESSION_INFO *wtsinfo;
DWORD wtscount;
@@ -77,8 +77,6 @@ main (int argc, char **argv)
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
- perf = strdup ("");
-
/* Parse extra opts if any */
argv = np_extra_opts (&argc, argv, progname);
@@ -160,23 +158,15 @@ main (int argc, char **argv)
#endif
/* 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)
printf ("%s\n", _("Unable to read output"));
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;
@@ -215,33 +205,27 @@ process_arguments (int argc, char **argv)
print_revision (progname, NP_VERSION);
exit (STATE_UNKNOWN);
case 'c': /* critical */
- if (!is_intnonneg (optarg))
- usage4 (_("Critical threshold must be a positive integer"));
- else
- cusers = atoi (optarg);
+ critical_range = optarg;
break;
case 'w': /* warning */
- if (!is_intnonneg (optarg))
- usage4 (_("Warning threshold must be a positive integer"));
- else
- wusers = atoi (optarg);
+ warning_range = optarg;
break;
}
}
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;
}