output: move shared net andn range status check to output_helper

Having same logic in many places is error prone if and when the logic needs
maintenance.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2017-11-12 17:43:42 +00:00
parent 15f08bbf02
commit a64630aa49
No known key found for this signature in database
GPG key ID: A9553245FDE9B739
3 changed files with 35 additions and 48 deletions

View file

@ -336,10 +336,10 @@ extern double get_range_size_v4(const struct range_t *r);
extern double get_range_size_v6(const struct range_t *r);
/* output.c */
extern void range_output_helper(struct conf_t *state, struct output_helper_t *oh,
struct range_t *range_p);
extern void shnet_output_helper(struct conf_t *state, struct output_helper_t *oh,
struct shared_network_t *shared_p);
extern int range_output_helper(struct conf_t *state, struct output_helper_t *oh,
struct range_t *range_p);
extern int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh,
struct shared_network_t *shared_p);
extern int output_analysis(struct conf_t *state, const char output_format);
/* sort.c */

View file

@ -211,8 +211,7 @@ static int must_next_range(void *closure)
e->current--;
if (e->current <= 0)
return 0;
range_output_helper(e->state, &e->oh, e->range_p);
} while (e->state->skip_ok && e->oh.status == STATUS_OK);
} while (range_output_helper(e->state, &e->oh, e->range_p));
return 1;
}
@ -226,8 +225,7 @@ static int must_next_shnet(void *closure)
e->shnet_p = e->shnet_p->next;
if (e->shnet_p == NULL)
break;
shnet_output_helper(e->state, &e->oh, e->shnet_p);
if (e->state->skip_ok && e->oh.status == STATUS_OK)
if (shnet_output_helper(e->state, &e->oh, e->shnet_p))
continue;
else
return 1;

View file

@ -60,7 +60,8 @@
#include "dhcpd-pools.h"
/*! \brief Calculate range percentages and such. */
void range_output_helper(struct conf_t *state, struct output_helper_t *oh, struct range_t *range_p)
int range_output_helper(struct conf_t *state, struct output_helper_t *oh,
struct range_t *range_p)
{
/* counts and calculations */
oh->range_size = get_range_size(range_p);
@ -83,11 +84,14 @@ void range_output_helper(struct conf_t *state, struct output_helper_t *oh, struc
else if (state->snet_alarms && range_p->shared_net != state->shared_net_root)
oh->status = STATUS_SUPPRESSED;
}
if (state->skip_ok && oh->status == STATUS_OK)
return 1;
return 0;
}
/*! \brief Calculate shared network percentages and such. */
void shnet_output_helper(struct conf_t *state, struct output_helper_t *oh,
struct shared_network_t *shared_p)
int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh,
struct shared_network_t *shared_p)
{
/* counts and calculations */
oh->tc = shared_p->touched + shared_p->used;
@ -104,23 +108,20 @@ void shnet_output_helper(struct conf_t *state, struct output_helper_t *oh,
}
}
/* set status */
if (oh->percent == NAN) {
if (oh->percent == NAN)
oh->status = STATUS_SUPPRESSED;
return;
}
if (shared_p->available <= state->minsize) {
else if (shared_p->available <= state->minsize)
oh->status = STATUS_IGNORED;
return;
}
if (state->critical < oh->percent && shared_p->used < state->crit_count) {
else if (state->critical < oh->percent && shared_p->used < state->crit_count)
oh->status = STATUS_CRIT;
return;
}
if (state->warning < oh->percent && shared_p->used < state->warn_count) {
else if (state->warning < oh->percent && shared_p->used < state->warn_count)
oh->status = STATUS_WARN;
return;
}
oh->status = STATUS_OK;
else
oh->status = STATUS_OK;
if (state->skip_ok && oh->status == STATUS_OK)
return 1;
return 0;
}
static int start_color(struct output_helper_t *oh, FILE *outfile)
@ -206,8 +207,7 @@ static int output_txt(struct conf_t *state)
for (i = 0; i < state->num_ranges; i++) {
int color_set = 0;
range_output_helper(state, &oh, range_p);
if (state->skip_ok && oh.status == STATUS_OK) {
if (range_output_helper(state, &oh, range_p)) {
range_p++;
continue;
}
@ -258,8 +258,7 @@ static int output_txt(struct conf_t *state)
for (shared_p = state->shared_net_root->next; shared_p; shared_p = shared_p->next) {
int color_set = 0;
shnet_output_helper(state, &oh, shared_p);
if (state->skip_ok && oh.status == STATUS_OK)
if (shnet_output_helper(state, &oh, shared_p))
continue;
if (state->color_mode == color_on)
color_set = start_color(&oh, outfile);
@ -351,8 +350,7 @@ static int output_xml(struct conf_t *state, const int print_mac_addreses)
if (state->number_limit & R_BIT) {
for (i = 0; i < state->num_ranges; i++) {
range_output_helper(state, &oh, range_p);
if (state->skip_ok && oh.status == STATUS_OK) {
if (range_output_helper(state, &oh, range_p)) {
range_p++;
continue;
}
@ -376,8 +374,7 @@ static int output_xml(struct conf_t *state, const int print_mac_addreses)
if (state->number_limit & S_BIT) {
for (shared_p = state->shared_net_root->next; shared_p; shared_p = shared_p->next) {
shnet_output_helper(state, &oh, shared_p);
if (state->skip_ok && oh.status == STATUS_OK)
if (shnet_output_helper(state, &oh, shared_p))
continue;
fprintf(outfile, "<shared-network>\n");
fprintf(outfile, "\t<location>%s</location>\n", shared_p->name);
@ -452,8 +449,7 @@ static int output_json(struct conf_t *state, const int print_mac_addreses)
}
fprintf(outfile, " \"subnets\": [\n");
for (i = 0; i < state->num_ranges; i++) {
range_output_helper(state, &oh, range_p);
if (state->skip_ok && oh.status == STATUS_OK) {
if (range_output_helper(state, &oh, range_p)) {
range_p++;
continue;
}
@ -499,8 +495,7 @@ static int output_json(struct conf_t *state, const int print_mac_addreses)
}
fprintf(outfile, " \"shared-networks\": [\n");
for (shared_p = state->shared_net_root->next; shared_p; shared_p = shared_p->next) {
shnet_output_helper(state, &oh, shared_p);
if (state->skip_ok && oh.status == STATUS_OK)
if (shnet_output_helper(state, &oh, shared_p))
continue;
fprintf(outfile, " ");
fprintf(outfile, "{ ");
@ -777,8 +772,7 @@ static int output_html(struct conf_t *state)
if (state->number_limit & S_BIT) {
start_tag(outfile, "tbody");
for (shared_p = state->shared_net_root->next; shared_p; shared_p = shared_p->next) {
shnet_output_helper(state, &oh, shared_p);
if (state->skip_ok && oh.status == STATUS_OK)
if (shnet_output_helper(state, &oh, shared_p))
continue;
start_tag(outfile, "tr");
output_line(outfile, "td", shared_p->name);
@ -821,8 +815,7 @@ static int output_html(struct conf_t *state)
if (state->number_limit & R_BIT) {
start_tag(outfile, "tbody");
for (i = 0; i < state->num_ranges; i++) {
range_output_helper(state, &oh, range_p);
if (state->skip_ok && oh.status == STATUS_OK) {
if (range_output_helper(state, &oh, range_p)) {
range_p++;
continue;
}
@ -878,8 +871,7 @@ static int output_csv(struct conf_t *state)
}
if (state->number_limit & R_BIT) {
for (i = 0; i < state->num_ranges; i++) {
range_output_helper(state, &oh, range_p);
if (state->skip_ok && oh.status == STATUS_OK) {
if (range_output_helper(state, &oh, range_p)) {
range_p++;
continue;
}
@ -919,8 +911,7 @@ static int output_csv(struct conf_t *state)
if (state->number_limit & S_BIT) {
for (shared_p = state->shared_net_root->next; shared_p; shared_p = shared_p->next) {
shnet_output_helper(state, &oh, shared_p);
if (state->skip_ok && oh.status == STATUS_OK)
if (shnet_output_helper(state, &oh, shared_p))
continue;
fprintf(outfile,
"\"%s\",\"%g\",\"%g\",\"%.3f\",\"%g\",\"%g\",\"%.3f\"",
@ -1063,8 +1054,7 @@ static int output_alarming(struct conf_t *state)
if (state->perfdata == 1 && state->number_limit & R_BIT) {
for (i = 0; i < state->num_ranges; i++) {
range_p--;
range_output_helper(state, &oh, range_p);
if (state->skip_ok && oh.status == STATUS_OK)
if (range_output_helper(state, &oh, range_p))
continue;
if (state->minsize < oh.range_size) {
fprintf(outfile, " %s_r=", ntop_ipaddr(&range_p->first_ip));
@ -1097,8 +1087,7 @@ static int output_alarming(struct conf_t *state)
}
if (state->perfdata == 1 && state->header_limit & R_BIT) {
for (shared_p = state->shared_net_root->next; shared_p; shared_p = shared_p->next) {
shnet_output_helper(state, &oh, shared_p);
if (state->skip_ok && oh.status == STATUS_OK)
if (shnet_output_helper(state, &oh, shared_p))
continue;
if (state->minsize < shared_p->available) {
fprintf(outfile, " '%s_s'=%g;%g;%g;0;%g",