output: make --skip to take arguments what will be skipped

Accidental typo in usage() caused realisation making skipping to fully
controllable is good idea.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2017-11-13 20:23:12 +00:00
parent e4f7259cf6
commit 3369278fc0
No known key found for this signature in database
GPG key ID: A9553245FDE9B739
9 changed files with 216 additions and 23 deletions

View file

@ -156,7 +156,8 @@ printed.
.TP
\fB\-\-color\fR=\fIwhen\fR
Use yellow for warning, red for critical, green for suppressed by \-\-minsize
and blue when \-\-snet\-alarms is the cause of supression. The
and blue when \-\-snet\-alarms is the cause of supression or shared network
does not have any ranges. The
.I when
string can be
.BR always ,
@ -172,11 +173,17 @@ coloring thresholds can be changed, but one must also use
.B \-\-format=text
to avoid turning on alarting mode.
.TP
\fB\-\-skip\-ok
Do not print ranges that are within thresholds of
.B \-\-warning
\fB\-\-skip\fR=\fIwhen\fR
The
.I when
can be one of the following:
.IR ok ,
.IR warning ,
.IR critical ,
.IR minsize ,
or
.BR \-\-critical .
.IR suppressed .
The skipping criterias are exact match with colors in \-\-color option.
.TP
\fB\-\-warning\fR=\fIpercent\fR
Turn on alarm output format, and specify percentage number which will

View file

@ -85,6 +85,49 @@ static void prepare_memory(struct conf_t *state)
state->shared_net_head = state->shared_net_root;
}
/*! \brief The --skip option argument parser. */
static void skip_arg_parse(struct conf_t *state, char *optarg)
{
enum {
OPT_ARG_OK = 0,
OPT_ARG_WARNING,
OPT_ARG_CRITICAL,
OPT_ARG_MINSIZE,
OPT_ARG_SUPRESSED
};
char *const tokens[] = {
[OPT_ARG_OK] = "ok",
[OPT_ARG_WARNING] = "warning",
[OPT_ARG_CRITICAL] = "critical",
[OPT_ARG_MINSIZE] = "minsize",
[OPT_ARG_SUPRESSED] = "suppressed",
NULL
};
char *value;
while (*optarg != '\0') {
switch(getsubopt(&optarg, tokens, &value)) {
case OPT_ARG_OK:
state->skip_ok = 1;
break;
case OPT_ARG_WARNING:
state->skip_warning = 1;
break;
case OPT_ARG_CRITICAL:
state->skip_critical = 1;
break;
case OPT_ARG_MINSIZE:
state->skip_minsize = 1;
break;
case OPT_ARG_SUPRESSED:
state->skip_suppressed = 1;
break;
default:
error(EXIT_FAILURE, 0, "unknown --skip specifier: %s", value);
}
}
}
/*! \brief Start of execution. Parse options, and call other other
* functions one after another. At the moment adding threading support
* would be difficult, but there does not seem to be valid reason to
@ -120,7 +163,7 @@ int main(int argc, char **argv)
OPT_WARN_COUNT,
OPT_CRIT_COUNT,
OPT_COLOR,
OPT_SKIP_OK,
OPT_SKIP,
OPT_SET_IPV,
OPT_MUSTACH
};
@ -130,7 +173,7 @@ int main(int argc, char **argv)
{"config", required_argument, NULL, 'c'},
{"leases", required_argument, NULL, 'l'},
{"color", required_argument, NULL, OPT_COLOR},
{"skip-ok", no_argument, NULL, OPT_SKIP_OK},
{"skip", required_argument, NULL, OPT_SKIP},
{"format", required_argument, NULL, 'f'},
{"sort", required_argument, NULL, 's'},
{"reverse", no_argument, NULL, 'r'},
@ -223,8 +266,8 @@ int main(int argc, char **argv)
if (state.color_mode == color_unknown)
error(EXIT_FAILURE, errno, "unknown color mode: %s", quote(optarg));
break;
case OPT_SKIP_OK:
state.skip_ok = 1;
case OPT_SKIP:
skip_arg_parse(&state, optarg);
break;
case OPT_SNET_ALARMS:
state.snet_alarms = 1;

View file

@ -267,6 +267,10 @@ struct conf_t {
header_limit:4,
number_limit:3,
skip_ok:1,
skip_warning:1,
skip_critical:1,
skip_minsize:1,
skip_suppressed:1,
color_mode:2;
};

View file

@ -567,7 +567,7 @@ void __attribute__ ((__noreturn__)) usage(int status)
fputs( " J for json with active lease details\n", out);
fputs( " c for comma separated values\n", out);
#ifdef BUILD_MUSTACH
fputs( " --mustach=tmpl output using mustach template file\n", out);
fputs( " --mustach=FILE output using mustach template file\n", out);
#endif
fputs( " -s, --sort=[nimcptTe] sort ranges by\n", out);
fputs( " n name\n", out);
@ -582,9 +582,10 @@ void __attribute__ ((__noreturn__)) usage(int status)
fputs( " -o, --output=FILE output into a file\n", out);
fputs( " -L, --limit=NR output limit mask 77 - 00\n", out);
fputs( " --color=WHEN use colors 'always', 'never', or 'auto'\n", out);
fputs( " --skip=ok do not print items below alarm threshold\n", out);
fputs( " --warning=PERC set warning alarming limit\n", out);
fputs( " --critical=PERC set critical alarming limit\n", out);
fputs( " --warning=PERC set warning alarming threshold\n", out);
fputs( " --critical=PERC set critical alarming threshold\n", out);
fputs( " --skip=WHAT do not print threshold 'ok', 'warning', 'critical',\n", out);
fputs( " 'minsize', or 'ignored'\n", out);
fputs( " --warn-count=NR a number of free leases before warning raised\n", out);
fputs( " --crit-count=NR a number of free leases before critical raised\n", out);
fputs( " --minsize=size disable alarms for small ranges and shared-nets\n", out);

View file

@ -90,12 +90,19 @@ int range_output_helper(struct conf_t *state, struct output_helper_t *oh,
&& (oh->range_size - range_p->count) < state->warn_count)
oh->status = STATUS_WARN;
if (oh->status != STATUS_OK) {
if (oh->range_size <= state->minsize)
if (oh->range_size <= state->minsize) {
oh->status = STATUS_IGNORED;
else if (state->snet_alarms && range_p->shared_net != state->shared_net_root)
if (state->skip_minsize)
return 1;
} else if (state->snet_alarms && range_p->shared_net != state->shared_net_root) {
oh->status = STATUS_SUPPRESSED;
if (state->skip_suppressed)
return 1;
}
}
if (state->skip_ok && oh->status == STATUS_OK)
if ((state->skip_ok && oh->status == STATUS_OK) ||
(state->skip_warning && oh->status == STATUS_WARN) ||
(state->skip_critical && oh->status == STATUS_CRIT))
return 1;
return 0;
}
@ -111,6 +118,8 @@ int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh,
oh->tcp = NAN;
oh->bup = NAN;
oh->status = STATUS_SUPPRESSED;
if (state->skip_suppressed)
return 1;
return 0;
}
@ -120,16 +129,23 @@ int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh,
oh->bup = (double)(100 * shared_p->backups) / shared_p->available;
/* set status */
if (shared_p->available <= state->minsize)
if (shared_p->available <= state->minsize) {
oh->status = STATUS_IGNORED;
else if (state->critical < oh->percent && shared_p->used < state->crit_count)
if (state->skip_minsize)
return 1;
} else if (state->critical < oh->percent && shared_p->used < state->crit_count) {
oh->status = STATUS_CRIT;
else if (state->warning < oh->percent && shared_p->used < state->warn_count)
if (state->skip_critical)
return 1;
} else if (state->warning < oh->percent && shared_p->used < state->warn_count) {
oh->status = STATUS_WARN;
else
if (state->skip_warning)
return 1;
} else {
oh->status = STATUS_OK;
if (state->skip_ok && oh->status == STATUS_OK)
return 1;
if (state->skip_ok)
return 1;
}
return 0;
}

View file

@ -24,6 +24,7 @@ TESTS = \
tests/range6 \
tests/same-twice \
tests/simple \
tests/skip \
tests/sorts \
tests/v6 \
tests/v6-perfdata

80
tests/expected/skip Normal file
View file

@ -0,0 +1,80 @@
--- skip ok ---
Ranges:
shared net name first ip last ip max cur percent touch t+c t+c perc bu bu perc
example1 10.0.0.1 - 10.0.0.20 20 11 55.000 0 11 55.000 1 5.000
example1 10.1.0.1 - 10.1.0.20 20 10 50.000 0 10 50.000 0 0.000
example2 10.3.0.1 - 10.3.0.20 20 9 45.000 0 9 45.000 0 0.000
Shared networks:
name max cur percent touch t+c t+c perc bu bu perc
example1 40 21 52.500 0 21 52.500 1 2.500
Sum of all ranges:
name max cur percent touch t+c t+c perc bu bu perc
All networks 100 43 43.000 0 43 43.000 2 2.000
--- skip warning ---
Ranges:
shared net name first ip last ip max cur percent touch t+c t+c perc bu bu perc
example1 10.0.0.1 - 10.0.0.20 20 11 55.000 0 11 55.000 1 5.000
example2 10.2.0.1 - 10.2.0.20 20 8 40.000 0 8 40.000 0 0.000
All networks 10.4.0.1 - 10.4.0.20 20 5 25.000 0 5 25.000 1 5.000
Shared networks:
name max cur percent touch t+c t+c perc bu bu perc
example1 40 21 52.500 0 21 52.500 1 2.500
example2 40 17 42.500 0 17 42.500 0 0.000
Sum of all ranges:
name max cur percent touch t+c t+c perc bu bu perc
All networks 100 43 43.000 0 43 43.000 2 2.000
--- skip critical ok ---
Ranges:
shared net name first ip last ip max cur percent touch t+c t+c perc bu bu perc
example1 10.1.0.1 - 10.1.0.20 20 10 50.000 0 10 50.000 0 0.000
example2 10.3.0.1 - 10.3.0.20 20 9 45.000 0 9 45.000 0 0.000
Shared networks:
name max cur percent touch t+c t+c perc bu bu perc
Sum of all ranges:
name max cur percent touch t+c t+c perc bu bu perc
All networks 100 43 43.000 0 43 43.000 2 2.000
--- skip suppressed ---
Ranges:
shared net name first ip last ip max cur percent touch t+c t+c perc bu bu perc
All networks 10.4.0.1 - 10.4.0.20 20 5 25.000 0 5 25.000 1 5.000
Shared networks:
name max cur percent touch t+c t+c perc bu bu perc
example1 40 21 52.500 0 21 52.500 1 2.500
example2 40 17 42.500 0 17 42.500 0 0.000
Sum of all ranges:
name max cur percent touch t+c t+c perc bu bu perc
All networks 100 43 43.000 0 43 43.000 2 2.000
--- skip minsize ---
Ranges:
shared net name first ip last ip max cur percent touch t+c t+c perc bu bu perc
Shared networks:
name max cur percent touch t+c t+c perc bu bu perc
example1 40 21 52.500 0 21 52.500 1 2.500
example2 40 17 42.500 0 17 42.500 0 0.000
Sum of all ranges:
name max cur percent touch t+c t+c perc bu bu perc
All networks 100 43 43.000 0 43 43.000 2 2.000
--- skip count ok ---
Ranges:
shared net name first ip last ip max cur percent touch t+c t+c perc bu bu perc
example1 10.0.0.1 - 10.0.0.20 20 11 55.000 0 11 55.000 1 5.000
example1 10.1.0.1 - 10.1.0.20 20 10 50.000 0 10 50.000 0 0.000
example2 10.2.0.1 - 10.2.0.20 20 8 40.000 0 8 40.000 0 0.000
example2 10.3.0.1 - 10.3.0.20 20 9 45.000 0 9 45.000 0 0.000
Shared networks:
name max cur percent touch t+c t+c perc bu bu perc
Sum of all ranges:
name max cur percent touch t+c t+c perc bu bu perc
All networks 100 43 43.000 0 43 43.000 2 2.000

View file

@ -14,7 +14,7 @@ dhcpd-pools -c $top_srcdir/samples/dhcpd.conf -l $top_srcdir/samples/dhcpd.lease
echo "--- skip ok ---" >> tests/outputs/$IAM
dhcpd-pools -c $top_srcdir/samples/dhcpd.conf -l $top_srcdir/samples/dhcpd.leases \
--mustach $top_srcdir/samples/mustach.template --skip-ok --warning 44 |
--mustach $top_srcdir/samples/mustach.template --skip=ok --warning 44 |
sed '/gettimeofday:/d; /lease_file_mtime:/d' >> tests/outputs/$IAM
diff -u $top_srcdir/tests/expected/$IAM tests/outputs/$IAM

41
tests/skip Executable file
View file

@ -0,0 +1,41 @@
#!/bin/sh
#
# --skip option tests
IAM=$(basename $0)
if [ ! -d tests/outputs ]; then
mkdir tests/outputs
fi
echo "--- skip ok ---" >| tests/outputs/$IAM
dhcpd-pools -c $top_srcdir/samples/dhcpd.conf -l $top_srcdir/samples/dhcpd.leases \
--color=always -ft --skip=ok --warning 44 >> tests/outputs/$IAM
echo "--- skip warning ---" >> tests/outputs/$IAM
dhcpd-pools -c $top_srcdir/samples/dhcpd.conf -l $top_srcdir/samples/dhcpd.leases \
--color=always --format text --skip=warning --warning 44 --critical=50 \
>> tests/outputs/$IAM
echo "--- skip critical ok ---" >> tests/outputs/$IAM
dhcpd-pools -c $top_srcdir/samples/dhcpd.conf -l $top_srcdir/samples/dhcpd.leases \
--color=always --format text --skip=ok,critical --warning 44 --critical=50 \
>> tests/outputs/$IAM
echo "--- skip suppressed ---" >> tests/outputs/$IAM
dhcpd-pools -c $top_srcdir/samples/dhcpd.conf -l $top_srcdir/samples/dhcpd.leases \
--color=always --format text --critical 1 --snet-alarms --skip=suppressed \
>> tests/outputs/$IAM
echo "--- skip minsize ---" >> tests/outputs/$IAM
dhcpd-pools -c $top_srcdir/samples/dhcpd.conf -l $top_srcdir/samples/dhcpd.leases \
--color=always --format text --critical=1 --minsize 20 --skip=minsize \
>> tests/outputs/$IAM
echo "--- skip count ok ---" >> tests/outputs/$IAM
dhcpd-pools -c $top_srcdir/samples/dhcpd.conf -l $top_srcdir/samples/dhcpd.leases \
--color=always --format=t --skip=ok --critical=1 --crit-count=12 --warning=1 \
--warn-count=15 >> tests/outputs/$IAM
diff -u $top_srcdir/tests/expected/$IAM tests/outputs/$IAM
exit $?