check_procs: Use the range/threshold support functions from libnagiosplug.

This adds support for @<range> and makes stuff a bit simpler by removing code
duplications.

Note: Previously, the compatibility code for 'check_procs <warn> <max>'
accepted something like 'check_procs -w 10:-1 -c 10:-1 20 50' as well
(treating it as if '-w 10:20 -c 10:50' was specified). This is no longer the
case ... additional arguments are only used as warn/crit thresholds in case
-w/-c is not specified at all.
This commit is contained in:
Sebastian Harl 2011-04-08 10:18:48 +02:00 committed by Holger Weiss
parent 217a6a7643
commit 2bac48c027

View file

@ -45,15 +45,13 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
int process_arguments (int, char **);
int validate_arguments (void);
int check_thresholds (int);
int convert_to_seconds (char *);
void print_help (void);
void print_usage (void);
int wmax = -1;
int cmax = -1;
int wmin = -1;
int cmin = -1;
char *warning_range = NULL;
char *critical_range = NULL;
thresholds *procs_thresholds = NULL;
int options = 0; /* bitmask of filter criteria to test against */
#define ALL 1
@ -238,14 +236,14 @@ main (int argc, char **argv)
}
if (metric == METRIC_VSZ)
i = check_thresholds (procvsz);
i = get_status ((double)procvsz, procs_thresholds);
else if (metric == METRIC_RSS)
i = check_thresholds (procrss);
i = get_status ((double)procrss, procs_thresholds);
/* TODO? float thresholds for --metric=CPU */
else if (metric == METRIC_CPU)
i = check_thresholds ((int)procpcpu);
i = get_status (procpcpu, procs_thresholds);
else if (metric == METRIC_ELAPSED)
i = check_thresholds (procseconds);
i = get_status ((double)procseconds, procs_thresholds);
if (metric != METRIC_PROCS) {
if (i == STATE_WARNING) {
@ -276,7 +274,7 @@ main (int argc, char **argv)
/* Needed if procs found, but none match filter */
if ( metric == METRIC_PROCS ) {
result = max_state (result, check_thresholds (procs) );
result = max_state (result, get_status ((double)procs, procs_thresholds) );
}
if ( result == STATE_OK ) {
@ -368,28 +366,10 @@ process_arguments (int argc, char **argv)
timeout_interval = atoi (optarg);
break;
case 'c': /* critical threshold */
if (is_integer (optarg))
cmax = atoi (optarg);
else if (sscanf (optarg, ":%d", &cmax) == 1)
break;
else if (sscanf (optarg, "%d:%d", &cmin, &cmax) == 2)
break;
else if (sscanf (optarg, "%d:", &cmin) == 1)
break;
else
usage4 (_("Critical Process Count must be an integer!"));
critical_range = optarg;
break;
case 'w': /* warning threshold */
if (is_integer (optarg))
wmax = atoi (optarg);
else if (sscanf (optarg, ":%d", &wmax) == 1)
break;
else if (sscanf (optarg, "%d:%d", &wmin, &wmax) == 2)
break;
else if (sscanf (optarg, "%d:", &wmin) == 1)
break;
else
usage4 (_("Warning Process Count must be an integer!"));
warning_range = optarg;
break;
case 'p': /* process id */
if (sscanf (optarg, "%d%[^0-9]", &ppid, tmp) == 1) {
@ -518,16 +498,19 @@ process_arguments (int argc, char **argv)
}
c = optind;
if (wmax == -1 && argv[c])
wmax = atoi (argv[c++]);
if (cmax == -1 && argv[c])
cmax = atoi (argv[c++]);
if ((! warning_range) && argv[c])
warning_range = argv[c++];
if ((! critical_range) && argv[c])
critical_range = argv[c++];
if (statopts == NULL && argv[c]) {
xasprintf (&statopts, "%s", argv[c++]);
xasprintf (&fmt, _("%s%sSTATE = %s"), (fmt ? fmt : ""), (options ? ", " : ""), statopts);
options |= STAT;
}
/* this will abort in case of invalid ranges */
set_thresholds (&procs_thresholds, warning_range, critical_range);
return validate_arguments ();
}
@ -536,27 +519,6 @@ process_arguments (int argc, char **argv)
int
validate_arguments ()
{
if (wmax >= 0 && wmin == -1)
wmin = 0;
if (cmax >= 0 && cmin == -1)
cmin = 0;
if (wmax >= wmin && cmax >= cmin) { /* standard ranges */
if (wmax > cmax && cmax != -1) {
printf (_("wmax (%d) cannot be greater than cmax (%d)\n"), wmax, cmax);
return ERROR;
}
if (cmin > wmin && wmin != -1) {
printf (_("wmin (%d) cannot be less than cmin (%d)\n"), wmin, cmin);
return ERROR;
}
}
/* if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) { */
/* printf ("At least one threshold must be set\n"); */
/* return ERROR; */
/* } */
if (options == 0)
options = ALL;
@ -579,40 +541,6 @@ validate_arguments ()
}
/* Check thresholds against value */
int
check_thresholds (int value)
{
if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) {
return OK;
}
else if (cmax >= 0 && cmin >= 0 && cmax < cmin) {
if (value > cmax && value < cmin)
return STATE_CRITICAL;
}
else if (cmax >= 0 && value > cmax) {
return STATE_CRITICAL;
}
else if (cmin >= 0 && value < cmin) {
return STATE_CRITICAL;
}
if (wmax >= 0 && wmin >= 0 && wmax < wmin) {
if (value > wmax && value < wmin) {
return STATE_WARNING;
}
}
else if (wmax >= 0 && value > wmax) {
return STATE_WARNING;
}
else if (wmin >= 0 && value < wmin) {
return STATE_WARNING;
}
return STATE_OK;
}
/* convert the elapsed time to seconds */
int
convert_to_seconds(char *etime) {