output: add output helper functions

There is too much repetative confusing maths near printouts.  Move that
stuff to a function.

This change also fixes --snet-alarms option counting issue in range that
were not part of any shared network were ignored.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2017-11-05 15:05:35 +00:00
parent 05f8208518
commit 48962004b8
No known key found for this signature in database
GPG key ID: A9553245FDE9B739
3 changed files with 184 additions and 126 deletions

View file

@ -121,6 +121,27 @@ struct range_t {
double touched;
double backups;
};
/*! \enum count_status_t
* \brief Enumeration of possible range and shared net statuses.
*/
enum count_status_t {
STATUS_OK,
STATUS_WARN,
STATUS_CRIT,
STATUS_IGNORED,
STATUS_SUPPRESSED
};
/*! \struct output_helper_t
* \brief Various per range and shared net temporary calculation results.
*/
struct output_helper_t {
int status;
double range_size;
double percent;
double tc;
double tcp;
double bup;
};
/*! \enum isc_conf_parser
* \brief Configuration file parsing state flags.
*/

View file

@ -58,13 +58,79 @@
#include "dhcpd-pools.h"
/*! \brief Calculate range percentages and such. */
static void range_output_helper(struct output_helper_t *oh, struct range_t *range_p)
{
/* counts and calculations */
oh->range_size = get_range_size(range_p);
oh->percent = (double)(100 * range_p->count) / oh->range_size;
oh->tc = range_p->touched + range_p->count;
oh->tcp = (double)(100 * oh->tc) / oh->range_size;
if (config.backups_found == 1) {
oh->bup = (double)(100 * range_p->backups) / oh->range_size;
}
/* set status */
if (config.snet_alarms && range_p->shared_net != shared_networks) {
oh->status = STATUS_SUPPRESSED;
return;
}
if (oh->range_size <= config.minsize) {
oh->status = STATUS_IGNORED;
return;
}
if (config.critical < oh->percent
&& (oh->range_size - range_p->count) < config.crit_count) {
oh->status = STATUS_CRIT;
return;
}
if (config.warning < oh->percent
&& (oh->range_size - range_p->count) < config.warn_count) {
oh->status = STATUS_WARN;
return;
}
oh->status = STATUS_OK;
}
/*! \brief Calculate shared network percentages and such. */
static void shnet_output_helper(struct output_helper_t *oh, struct shared_network_t *shared_p)
{
/* counts and calculations */
oh->tc = shared_p->touched + shared_p->used;
if (shared_p->available == 0) {
oh->percent = NAN;
oh->tcp = NAN;
oh->bup = NAN;
} else {
oh->percent = (double)(100 * shared_p->used) / shared_p->available;
oh->tcp =
(double)((100 * (shared_p->touched + shared_p->used)) / shared_p->available);
if (config.backups_found == 1) {
oh->bup = (double)(100 * shared_p->backups) / shared_p->available;
}
}
/* set status */
if (oh->percent == NAN || shared_p->available <= config.minsize) {
oh->status = STATUS_IGNORED;
return;
}
if (config.critical < oh->percent && shared_p->used < config.crit_count) {
oh->status = STATUS_CRIT;
return;
}
if (config.warning < oh->percent && shared_p->used < config.warn_count) {
oh->status = STATUS_WARN;
return;
}
oh->status = STATUS_OK;
}
/*! \brief Text output format, which is the default. */
static int output_txt(void)
{
unsigned int i;
struct range_t *range_p;
double range_size;
struct shared_network_t *shared_p;
struct output_helper_t oh;
int ret;
FILE *outfile;
int max_ipaddr_length = config.ip_version == IPv6 ? 39 : 16;
@ -79,7 +145,6 @@ static int output_txt(void)
}
range_p = ranges;
range_size = get_range_size(range_p);
shared_p = shared_networks;
if (config.header_limit & R_BIT) {
@ -99,6 +164,7 @@ static int output_txt(void)
}
if (config.number_limit & R_BIT) {
for (i = 0; i < num_ranges; i++) {
range_output_helper(&oh, range_p);
if (range_p->shared_net) {
fprintf(outfile, "%-20s", range_p->shared_net->name);
} else {
@ -113,20 +179,18 @@ static int output_txt(void)
" - %-*s %5g %5g %10.3f %5g %5g %9.3f",
max_ipaddr_length,
ntop_ipaddr(&range_p->last_ip),
range_size,
oh.range_size,
range_p->count,
(float)(100 * range_p->count) / range_size,
oh.percent,
range_p->touched,
range_p->touched + range_p->count,
(float)(100 * (range_p->touched + range_p->count)) / range_size);
oh.tc,
oh.tcp);
if (config.backups_found == 1) {
fprintf(outfile, "%7g %8.3f",
range_p->backups,
(float)(100 * range_p->backups) / range_size);
range_p->backups, oh.bup);
}
fprintf(outfile, "\n");
range_p++;
range_size = get_range_size(range_p);
}
}
if (config.number_limit & R_BIT && config.header_limit & S_BIT) {
@ -144,20 +208,20 @@ static int output_txt(void)
if (config.number_limit & S_BIT) {
for (i = 0; i < num_shared_networks; i++) {
shared_p++;
shnet_output_helper(&oh, shared_p);
fprintf(outfile,
"%-20s %5g %5g %10.3f %7g %6g %9.3f",
shared_p->name, shared_p->available,
shared_p->name,
shared_p->available,
shared_p->used,
shared_p->available ==
0 ? NAN : (float)(100 * shared_p->used) / shared_p->available,
shared_p->touched, shared_p->touched + shared_p->used,
shared_p->available ==
0 ? NAN : ((float)(100 * (shared_p->touched + shared_p->used)) /
shared_p->available));
oh.percent,
shared_p->touched,
oh.tc,
oh.tcp);
if (config.backups_found == 1) {
fprintf(outfile, "%7g %8.3f",
shared_p->backups,
(float)(100 * shared_p->backups) / shared_p->available);
oh.bup);
}
fprintf(outfile, "\n");
@ -177,24 +241,20 @@ static int output_txt(void)
fprintf(outfile, "\n");
}
if (config.number_limit & A_BIT) {
shnet_output_helper(&oh, shared_networks);
fprintf(outfile, "%-20s %5g %5g %10.3f %7g %6g %9.3f",
shared_networks->name,
shared_networks->available,
shared_networks->used,
shared_networks->available ==
0 ? NAN : (float)(100 * shared_networks->used) /
shared_networks->available, shared_networks->touched,
shared_networks->touched + shared_networks->used,
shared_networks->available ==
0 ? NAN : (float)(100 *
(shared_networks->touched +
shared_networks->used)) / shared_networks->available);
oh.percent,
shared_networks->touched,
oh.tc,
oh.tcp);
if (config.backups_found == 1) {
fprintf(outfile, "%7g %8.3f",
shared_networks->available == 0 ? NAN : shared_networks->backups,
(float)(100 * shared_networks->backups) /
shared_networks->available);
shared_networks->backups,
oh.bup);
}
fprintf(outfile, "\n");
}
@ -599,8 +659,8 @@ static int output_html(void)
{
unsigned int i;
struct range_t *range_p;
double range_size;
struct shared_network_t *shared_p;
struct output_helper_t oh;
int ret;
FILE *outfile;
@ -614,7 +674,6 @@ static int output_html(void)
}
range_p = ranges;
range_size = get_range_size(range_p);
shared_p = shared_networks;
html_header(outfile);
newsection(outfile, "Sum of all");
@ -639,27 +698,17 @@ static int output_html(void)
if (config.number_limit & A_BIT) {
start_tag(outfile, "tbody");
start_tag(outfile, "tr");
shnet_output_helper(&oh, shared_networks);
output_line(outfile, "td", shared_networks->name);
output_double(outfile, "td", shared_networks->available);
output_double(outfile, "td", shared_networks->used);
output_float(outfile, "td",
shared_networks->available ==
0 ? NAN : (float)(100 * shared_networks->used) /
shared_networks->available);
output_float(outfile, "td", oh.percent);
output_double(outfile, "td", shared_networks->touched);
output_double(outfile, "td", shared_networks->touched + shared_networks->used);
output_float(outfile, "td",
shared_networks->available == 0 ? NAN : (float)(100 *
(shared_networks->touched
+
shared_networks->used))
/ shared_networks->available);
output_double(outfile, "td", oh.tc);
output_float(outfile, "td", oh.tcp);
if (config.backups_found == 1) {
output_double(outfile, "td", shared_networks->backups);
output_float(outfile, "td",
shared_networks->available == 0 ? NAN : (float)(100 *
shared_networks->backups)
/ shared_networks->available);
output_float(outfile, "td", oh.tcp);
}
end_tag(outfile, "tr");
end_tag(outfile, "tbody");
@ -688,27 +737,18 @@ static int output_html(void)
start_tag(outfile, "tbody");
for (i = 0; i < num_shared_networks; i++) {
shared_p++;
shnet_output_helper(&oh, shared_networks);
start_tag(outfile, "tr");
output_line(outfile, "td", shared_p->name);
output_double(outfile, "td", shared_p->available);
output_double(outfile, "td", shared_p->used);
output_float(outfile, "td",
shared_p->available ==
0 ? NAN : (float)(100 * shared_p->used) /
shared_p->available);
output_float(outfile, "td", oh.percent);
output_double(outfile, "td", shared_p->touched);
output_double(outfile, "td", shared_p->touched + shared_p->used);
output_float(outfile, "td",
shared_p->available == 0 ? NAN : (float)(100 *
(shared_p->touched +
shared_p->used)) /
shared_p->available);
output_double(outfile, "td", oh.tc);
output_float(outfile, "td", oh.tcp);
if (config.backups_found == 1) {
output_double(outfile, "td", shared_p->backups);
output_float(outfile, "td",
shared_p->available == 0 ? NAN : (float)(100 *
shared_p->backups)
/ shared_p->available);
output_float(outfile, "td", oh.bup);
}
end_tag(outfile, "tr");
}
@ -739,6 +779,7 @@ static int output_html(void)
if (config.number_limit & R_BIT) {
start_tag(outfile, "tbody");
for (i = 0; i < num_ranges; i++) {
range_output_helper(&oh, range_p);
start_tag(outfile, "tr");
if (range_p->shared_net) {
output_line(outfile, "td", range_p->shared_net->name);
@ -747,22 +788,18 @@ static int output_html(void)
}
output_line(outfile, "td", ntop_ipaddr(&range_p->first_ip));
output_line(outfile, "td", ntop_ipaddr(&range_p->last_ip));
output_double(outfile, "td", range_size);
output_double(outfile, "td", oh.range_size);
output_double(outfile, "td", range_p->count);
output_float(outfile, "td", (float)(100 * range_p->count) / range_size);
output_float(outfile, "td", oh.percent);
output_double(outfile, "td", range_p->touched);
output_double(outfile, "td", range_p->touched + range_p->count);
output_float(outfile, "td",
(float)(100 *
(range_p->touched + range_p->count)) / range_size);
output_double(outfile, "td", oh.tc);
output_float(outfile, "td", oh.tcp);
if (config.backups_found == 1) {
output_double(outfile, "td", range_p->backups);
output_float(outfile, "td",
(float)(100 * range_p->backups) / range_size);
output_float(outfile, "td", oh.bup);
}
end_tag(outfile, "tr");
range_p++;
range_size = get_range_size(range_p);
}
end_tag(outfile, "tbody");
}
@ -787,8 +824,8 @@ static int output_csv(void)
{
unsigned int i;
struct range_t *range_p;
double range_size;
struct shared_network_t *shared_p;
struct output_helper_t oh;
FILE *outfile;
int ret;
@ -802,7 +839,6 @@ static int output_csv(void)
}
range_p = ranges;
range_size = get_range_size(range_p);
shared_p = shared_networks;
if (config.header_limit & R_BIT) {
fprintf(outfile, "\"Ranges:\"\n");
@ -816,6 +852,7 @@ static int output_csv(void)
}
if (config.number_limit & R_BIT) {
for (i = 0; i < num_ranges; i++) {
range_output_helper(&oh, range_p);
if (range_p->shared_net) {
fprintf(outfile, "\"%s\",", range_p->shared_net->name);
} else {
@ -824,21 +861,21 @@ static int output_csv(void)
fprintf(outfile, "\"%s\",", ntop_ipaddr(&range_p->first_ip));
fprintf(outfile,
"\"%s\",\"%g\",\"%g\",\"%.3f\",\"%g\",\"%g\",\"%.3f\"",
ntop_ipaddr(&range_p->last_ip), range_size,
ntop_ipaddr(&range_p->last_ip),
oh.range_size,
range_p->count,
(float)(100 * range_p->count) / range_size,
oh.percent,
range_p->touched,
range_p->touched + range_p->count,
(float)(100 * (range_p->touched + range_p->count)) / range_size);
oh.tc,
oh.tcp);
if (config.backups_found == 1) {
fprintf(outfile, ",\"%g\",\"%.3f\"",
range_p->backups,
(float)(100 * range_p->backups) / range_size);
oh.bup);
}
fprintf(outfile, "\n");
range_p++;
range_size = get_range_size(range_p);
}
fprintf(outfile, "\n");
}
@ -855,23 +892,20 @@ static int output_csv(void)
for (i = 0; i < num_shared_networks; i++) {
shared_p++;
shnet_output_helper(&oh, shared_p);
fprintf(outfile,
"\"%s\",\"%g\",\"%g\",\"%.3f\",\"%g\",\"%g\",\"%.3f\"",
shared_p->name, shared_p->available,
shared_p->name,
shared_p->available,
shared_p->used,
shared_p->available == 0 ? NAN : (float)(100 * shared_p->used) /
shared_p->available, shared_p->touched,
shared_p->touched + shared_p->used,
shared_p->available == 0 ? NAN : (float)(100 *
(shared_p->touched +
shared_p->used)) /
shared_p->available);
oh.percent,
shared_p->touched,
oh.tc,
oh.tcp);
if (config.backups_found == 1) {
fprintf(outfile, ",\"%g\",\"%.3f\"",
shared_p->backups,
shared_p->available ==
0 ? NAN : (float)(100 * shared_p->backups) /
shared_p->available);
oh.bup);
}
fprintf(outfile, "\n");
@ -888,25 +922,20 @@ static int output_csv(void)
fprintf(outfile, "\n");
}
if (config.number_limit & A_BIT) {
shnet_output_helper(&oh, shared_networks);
fprintf(outfile,
"\"%s\",\"%g\",\"%g\",\"%.3f\",\"%g\",\"%g\",\"%.3f\"",
shared_networks->name, shared_networks->available,
shared_networks->name,
shared_networks->available,
shared_networks->used,
shared_networks->available ==
0 ? NAN : (float)(100 * shared_networks->used) /
shared_networks->available, shared_networks->touched,
shared_networks->touched + shared_networks->used,
shared_networks->available ==
0 ? NAN : (float)(100 *
(shared_networks->touched +
shared_networks->used)) / shared_networks->available);
oh.percent,
shared_networks->touched,
oh.tc,
oh.tcp);
if (config.backups_found == 1) {
fprintf(outfile, "%7g %8.3f",
shared_networks->backups,
shared_networks->available ==
0 ? NAN : (float)(100 * shared_networks->backups) /
shared_networks->available);
oh.bup);
}
fprintf(outfile, "\n");
}
@ -931,8 +960,8 @@ static int output_alarming(void)
struct range_t *range_p;
double range_size;
struct shared_network_t *shared_p;
struct output_helper_t oh;
unsigned int i;
float perc;
int rw = 0, rc = 0, ro = 0, ri = 0, sw = 0, sc = 0, so = 0, si = 0;
int ret_val, ret;
@ -951,39 +980,47 @@ static int output_alarming(void)
if (config.number_limit & R_BIT) {
for (i = 0; i < num_ranges; i++) {
if (config.snet_alarms && range_p->shared_net != shared_networks) {
continue;
}
if (config.minsize < range_size) {
perc = (float)(100 * range_p->count) / range_size;
if (config.critical < perc && (range_size - range_p->count) < config.crit_count)
rc++;
else if (config.warning < perc && (range_size - range_p->count) < config.warn_count)
rw++;
else
ro++;
} else {
range_output_helper(&oh, range_p);
switch (oh.status) {
case STATUS_SUPPRESSED:
break;
case STATUS_IGNORED:
ri++;
break;
case STATUS_CRIT:
rc++;
break;
case STATUS_WARN:
rw++;
break;
case STATUS_OK:
ro++;
break;
default:
abort();
}
range_p++;
range_size = get_range_size(range_p);
}
}
if (config.number_limit & S_BIT) {
for (i = 0; i < num_shared_networks; i++) {
shared_p++;
if (config.minsize < shared_p->available) {
perc =
shared_p->available ==
0 ? 100 : (float)(100 * shared_p->used) / shared_p->available;
if (config.critical < perc && shared_p->used < config.crit_count)
sc++;
else if (config.warning < perc && shared_p->used < config.warn_count)
sw++;
else
so++;
} else {
shnet_output_helper(&oh, shared_p);
switch (oh.status) {
case STATUS_IGNORED:
si++;
break;
case STATUS_CRIT:
sc++;
break;
case STATUS_WARN:
sw++;
break;
case STATUS_OK:
so++;
break;
default:
abort();
}
}
}

View file

@ -11,6 +11,6 @@ OK: Ranges - crit: 0 warn: 0 ok: 0 ignored: 5; | range_crit=0 range_warn=0 range
Shared nets - crit: 0 warn: 0 ok: 0 ignored: 2; | snet_crit=0 snet_warn=0 snet_ok=0 snet_ignored=2
0
== snet alarms ==
WARNING: dhcpd-pools: Ranges - crit: 0 warn: 0 ok: 0; | range_crit=0 range_warn=0 range_ok=0
WARNING: dhcpd-pools: Ranges - crit: 0 warn: 0 ok: 1; | range_crit=0 range_warn=0 range_ok=1
Shared nets - crit: 0 warn: 2 ok: 0; | snet_crit=0 snet_warn=2 snet_ok=0
1