From 3369278fc02ff8765824e08fb3eb68dc544bdb2f Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Mon, 13 Nov 2017 20:23:12 +0000 Subject: [PATCH] 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 --- man/dhcpd-pools.1.in | 17 +++++++--- src/dhcpd-pools.c | 51 +++++++++++++++++++++++++--- src/dhcpd-pools.h | 4 +++ src/other.c | 9 ++--- src/output.c | 34 ++++++++++++++----- tests/Makemodule.am | 1 + tests/expected/skip | 80 ++++++++++++++++++++++++++++++++++++++++++++ tests/mustach | 2 +- tests/skip | 41 +++++++++++++++++++++++ 9 files changed, 216 insertions(+), 23 deletions(-) create mode 100644 tests/expected/skip create mode 100755 tests/skip diff --git a/man/dhcpd-pools.1.in b/man/dhcpd-pools.1.in index 753d6df..b56e883 100644 --- a/man/dhcpd-pools.1.in +++ b/man/dhcpd-pools.1.in @@ -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 diff --git a/src/dhcpd-pools.c b/src/dhcpd-pools.c index 681ec55..64c853f 100644 --- a/src/dhcpd-pools.c +++ b/src/dhcpd-pools.c @@ -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; diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index 258649a..183d3ae 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -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; }; diff --git a/src/other.c b/src/other.c index 3ffadc7..8ebe627 100644 --- a/src/other.c +++ b/src/other.c @@ -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); diff --git a/src/output.c b/src/output.c index ebd841f..e60aa97 100644 --- a/src/output.c +++ b/src/output.c @@ -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; } diff --git a/tests/Makemodule.am b/tests/Makemodule.am index 4256ae1..1372324 100644 --- a/tests/Makemodule.am +++ b/tests/Makemodule.am @@ -24,6 +24,7 @@ TESTS = \ tests/range6 \ tests/same-twice \ tests/simple \ + tests/skip \ tests/sorts \ tests/v6 \ tests/v6-perfdata diff --git a/tests/expected/skip b/tests/expected/skip new file mode 100644 index 0000000..4553da8 --- /dev/null +++ b/tests/expected/skip @@ -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 diff --git a/tests/mustach b/tests/mustach index af53900..f99ef81 100755 --- a/tests/mustach +++ b/tests/mustach @@ -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 diff --git a/tests/skip b/tests/skip new file mode 100755 index 0000000..7578cf9 --- /dev/null +++ b/tests/skip @@ -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 $?