mirror of
git://git.code.sf.net/p/dhcpd-pools/code
synced 2025-12-18 08:47:00 +00:00
all files: replace global variables with runtime config state structure
Earlier variables magically appeared to scope of functions that took void as argument. One could figure out perhaps they were globals, but programs that do that are unnessarily hard to follow. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
adda925c1e
commit
1875a13733
12 changed files with 551 additions and 626 deletions
398
src/output.c
398
src/output.c
|
|
@ -60,34 +60,34 @@
|
|||
#include "dhcpd-pools.h"
|
||||
|
||||
/*! \brief Calculate range percentages and such. */
|
||||
void range_output_helper(struct output_helper_t *oh, struct range_t *range_p)
|
||||
void 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);
|
||||
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) {
|
||||
if (state->backups_found == 1) {
|
||||
oh->bup = (double)(100 * range_p->backups) / oh->range_size;
|
||||
}
|
||||
/* set status */
|
||||
oh->status = STATUS_OK;
|
||||
if (config.critical < oh->percent
|
||||
&& (oh->range_size - range_p->count) < config.crit_count)
|
||||
if (state->critical < oh->percent
|
||||
&& (oh->range_size - range_p->count) < state->crit_count)
|
||||
oh->status = STATUS_CRIT;
|
||||
else if (config.warning < oh->percent
|
||||
&& (oh->range_size - range_p->count) < config.warn_count)
|
||||
else if (state->warning < oh->percent
|
||||
&& (oh->range_size - range_p->count) < state->warn_count)
|
||||
oh->status = STATUS_WARN;
|
||||
if (oh->status != STATUS_OK) {
|
||||
if (oh->range_size <= config.minsize)
|
||||
if (oh->range_size <= state->minsize)
|
||||
oh->status = STATUS_IGNORED;
|
||||
else if (config.snet_alarms && range_p->shared_net != shared_networks)
|
||||
else if (state->snet_alarms && range_p->shared_net != state->shared_networks)
|
||||
oh->status = STATUS_SUPPRESSED;
|
||||
}
|
||||
}
|
||||
|
||||
/*! \brief Calculate shared network percentages and such. */
|
||||
void shnet_output_helper(struct output_helper_t *oh, struct shared_network_t *shared_p)
|
||||
void 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;
|
||||
|
|
@ -99,7 +99,7 @@ void shnet_output_helper(struct output_helper_t *oh, struct shared_network_t *sh
|
|||
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) {
|
||||
if (state->backups_found == 1) {
|
||||
oh->bup = (double)(100 * shared_p->backups) / shared_p->available;
|
||||
}
|
||||
}
|
||||
|
|
@ -108,15 +108,15 @@ void shnet_output_helper(struct output_helper_t *oh, struct shared_network_t *sh
|
|||
oh->status = STATUS_SUPPRESSED;
|
||||
return;
|
||||
}
|
||||
if (shared_p->available <= config.minsize) {
|
||||
if (shared_p->available <= state->minsize) {
|
||||
oh->status = STATUS_IGNORED;
|
||||
return;
|
||||
}
|
||||
if (config.critical < oh->percent && shared_p->used < config.crit_count) {
|
||||
if (state->critical < oh->percent && shared_p->used < state->crit_count) {
|
||||
oh->status = STATUS_CRIT;
|
||||
return;
|
||||
}
|
||||
if (config.warning < oh->percent && shared_p->used < config.warn_count) {
|
||||
if (state->warning < oh->percent && shared_p->used < state->warn_count) {
|
||||
oh->status = STATUS_WARN;
|
||||
return;
|
||||
}
|
||||
|
|
@ -144,14 +144,14 @@ static int start_color(struct output_helper_t *oh, FILE *outfile)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static FILE *open_outfile(void)
|
||||
static FILE *open_outfile(struct conf_t *state)
|
||||
{
|
||||
FILE *outfile;
|
||||
|
||||
if (config.output_file[0]) {
|
||||
outfile = fopen(config.output_file, "w+");
|
||||
if (state->output_file) {
|
||||
outfile = fopen(state->output_file, "w+");
|
||||
if (outfile == NULL) {
|
||||
error(EXIT_FAILURE, errno, "open_outfile: %s", config.output_file);
|
||||
error(EXIT_FAILURE, errno, "open_outfile: %s", state->output_file);
|
||||
}
|
||||
} else {
|
||||
outfile = stdout;
|
||||
|
|
@ -171,24 +171,24 @@ static void close_outfile(FILE *outfile)
|
|||
}
|
||||
|
||||
/*! \brief Text output format, which is the default. */
|
||||
static int output_txt(void)
|
||||
static int output_txt(struct conf_t *state)
|
||||
{
|
||||
unsigned int i;
|
||||
struct range_t *range_p;
|
||||
struct shared_network_t *shared_p;
|
||||
struct output_helper_t oh;
|
||||
FILE *outfile;
|
||||
int max_ipaddr_length = config.ip_version == IPv6 ? 39 : 16;
|
||||
int max_ipaddr_length = state->ip_version == IPv6 ? 39 : 16;
|
||||
|
||||
if (config.color_mode == color_auto && isatty(STDIN_FILENO)) {
|
||||
config.color_mode = color_on;
|
||||
if (state->color_mode == color_auto && isatty(STDIN_FILENO)) {
|
||||
state->color_mode = color_on;
|
||||
}
|
||||
|
||||
outfile = open_outfile();
|
||||
range_p = ranges;
|
||||
shared_p = shared_networks;
|
||||
outfile = open_outfile(state);
|
||||
range_p = state->ranges;
|
||||
shared_p = state->shared_networks;
|
||||
|
||||
if (config.header_limit & R_BIT) {
|
||||
if (state->header_limit & R_BIT) {
|
||||
fprintf(outfile, "Ranges:\n");
|
||||
fprintf
|
||||
(outfile,
|
||||
|
|
@ -198,20 +198,20 @@ static int output_txt(void)
|
|||
"first ip",
|
||||
max_ipaddr_length,
|
||||
"last ip", "max", "cur", "percent", "touch", "t+c", "t+c perc");
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, " bu bu perc");
|
||||
}
|
||||
fprintf(outfile, "\n");
|
||||
}
|
||||
if (config.number_limit & R_BIT) {
|
||||
for (i = 0; i < num_ranges; i++) {
|
||||
if (state->number_limit & R_BIT) {
|
||||
for (i = 0; i < state->num_ranges; i++) {
|
||||
int color_set = 0;
|
||||
range_output_helper(&oh, range_p);
|
||||
if (config.skip_ok && oh.status == STATUS_OK) {
|
||||
range_output_helper(state, &oh, range_p);
|
||||
if (state->skip_ok && oh.status == STATUS_OK) {
|
||||
range_p++;
|
||||
continue;
|
||||
}
|
||||
if (config.color_mode == color_on)
|
||||
if (state->color_mode == color_on)
|
||||
color_set = start_color(&oh, outfile);
|
||||
if (range_p->shared_net) {
|
||||
fprintf(outfile, "%-20s", range_p->shared_net->name);
|
||||
|
|
@ -233,7 +233,7 @@ static int output_txt(void)
|
|||
range_p->touched,
|
||||
oh.tc,
|
||||
oh.tcp);
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, "%7g %8.3f",
|
||||
range_p->backups, oh.bup);
|
||||
}
|
||||
|
|
@ -243,26 +243,26 @@ static int output_txt(void)
|
|||
range_p++;
|
||||
}
|
||||
}
|
||||
if (config.number_limit & R_BIT && config.header_limit & S_BIT) {
|
||||
if (state->number_limit & R_BIT && state->header_limit & S_BIT) {
|
||||
fprintf(outfile, "\n");
|
||||
}
|
||||
if (config.header_limit & S_BIT) {
|
||||
if (state->header_limit & S_BIT) {
|
||||
fprintf(outfile, "Shared networks:\n");
|
||||
fprintf(outfile,
|
||||
"name max cur percent touch t+c t+c perc");
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, " bu bu perc");
|
||||
}
|
||||
fprintf(outfile, "\n");
|
||||
}
|
||||
if (config.number_limit & S_BIT) {
|
||||
for (i = 0; i < num_shared_networks; i++) {
|
||||
if (state->number_limit & S_BIT) {
|
||||
for (i = 0; i < state->num_shared_networks; i++) {
|
||||
int color_set = 0;
|
||||
shared_p++;
|
||||
shnet_output_helper(&oh, shared_p);
|
||||
if (config.skip_ok && oh.status == STATUS_OK)
|
||||
shnet_output_helper(state, &oh, shared_p);
|
||||
if (state->skip_ok && oh.status == STATUS_OK)
|
||||
continue;
|
||||
if (config.color_mode == color_on)
|
||||
if (state->color_mode == color_on)
|
||||
color_set = start_color(&oh, outfile);
|
||||
fprintf(outfile,
|
||||
"%-20s %5g %5g %10.3f %7g %6g %9.3f",
|
||||
|
|
@ -273,7 +273,7 @@ static int output_txt(void)
|
|||
shared_p->touched,
|
||||
oh.tc,
|
||||
oh.tcp);
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, "%7g %8.3f",
|
||||
shared_p->backups,
|
||||
oh.bup);
|
||||
|
|
@ -283,36 +283,36 @@ static int output_txt(void)
|
|||
fprintf(outfile, "\n");
|
||||
}
|
||||
}
|
||||
if (config.number_limit & S_BIT && config.header_limit & A_BIT) {
|
||||
if (state->number_limit & S_BIT && state->header_limit & A_BIT) {
|
||||
fprintf(outfile, "\n");
|
||||
}
|
||||
if (config.header_limit & A_BIT) {
|
||||
if (state->header_limit & A_BIT) {
|
||||
fprintf(outfile, "Sum of all ranges:\n");
|
||||
fprintf(outfile,
|
||||
"name max cur percent touch t+c t+c perc");
|
||||
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, " bu bu perc");
|
||||
}
|
||||
fprintf(outfile, "\n");
|
||||
}
|
||||
if (config.number_limit & A_BIT) {
|
||||
if (state->number_limit & A_BIT) {
|
||||
int color_set = 0;
|
||||
shnet_output_helper(&oh, shared_networks);
|
||||
if (config.color_mode == color_on)
|
||||
shnet_output_helper(state, &oh, state->shared_networks);
|
||||
if (state->color_mode == color_on)
|
||||
color_set = start_color(&oh, outfile);
|
||||
fprintf(outfile, "%-20s %5g %5g %10.3f %7g %6g %9.3f",
|
||||
shared_networks->name,
|
||||
shared_networks->available,
|
||||
shared_networks->used,
|
||||
state->shared_networks->name,
|
||||
state->shared_networks->available,
|
||||
state->shared_networks->used,
|
||||
oh.percent,
|
||||
shared_networks->touched,
|
||||
state->shared_networks->touched,
|
||||
oh.tc,
|
||||
oh.tcp);
|
||||
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, "%7g %8.3f",
|
||||
shared_networks->backups,
|
||||
state->shared_networks->backups,
|
||||
oh.bup);
|
||||
}
|
||||
if (color_set)
|
||||
|
|
@ -324,7 +324,7 @@ static int output_txt(void)
|
|||
}
|
||||
|
||||
/*! \brief The xml output formats. */
|
||||
static int output_xml(const int print_mac_addreses)
|
||||
static int output_xml(struct conf_t *state, const int print_mac_addreses)
|
||||
{
|
||||
unsigned int i;
|
||||
struct range_t *range_p;
|
||||
|
|
@ -332,16 +332,16 @@ static int output_xml(const int print_mac_addreses)
|
|||
struct output_helper_t oh;
|
||||
FILE *outfile;
|
||||
|
||||
outfile = open_outfile();
|
||||
range_p = ranges;
|
||||
shared_p = shared_networks;
|
||||
outfile = open_outfile(state);
|
||||
range_p = state->ranges;
|
||||
shared_p = state->shared_networks;
|
||||
|
||||
fprintf(outfile, "<dhcpstatus>\n");
|
||||
|
||||
if (print_mac_addreses == 1) {
|
||||
struct leases_t *l;
|
||||
|
||||
for (l = leases; l != NULL; l = l->hh.next) {
|
||||
for (l = state->leases; l != NULL; l = l->hh.next) {
|
||||
if (l->type == ACTIVE) {
|
||||
fputs("<active_lease>\n\t<ip>", outfile);
|
||||
fputs(ntop_ipaddr(&l->ip), outfile);
|
||||
|
|
@ -354,10 +354,10 @@ static int output_xml(const int print_mac_addreses)
|
|||
}
|
||||
}
|
||||
|
||||
if (config.number_limit & R_BIT) {
|
||||
for (i = 0; i < num_ranges; i++) {
|
||||
range_output_helper(&oh, range_p);
|
||||
if (config.skip_ok && oh.status == STATUS_OK) {
|
||||
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) {
|
||||
range_p++;
|
||||
continue;
|
||||
}
|
||||
|
|
@ -379,11 +379,11 @@ static int output_xml(const int print_mac_addreses)
|
|||
}
|
||||
}
|
||||
|
||||
if (config.number_limit & S_BIT) {
|
||||
for (i = 0; i < num_shared_networks; i++) {
|
||||
if (state->number_limit & S_BIT) {
|
||||
for (i = 0; i < state->num_shared_networks; i++) {
|
||||
shared_p++;
|
||||
shnet_output_helper(&oh, shared_p);
|
||||
if (config.skip_ok && oh.status == STATUS_OK)
|
||||
shnet_output_helper(state, &oh, shared_p);
|
||||
if (state->skip_ok && oh.status == STATUS_OK)
|
||||
continue;
|
||||
fprintf(outfile, "<shared-network>\n");
|
||||
fprintf(outfile, "\t<location>%s</location>\n", shared_p->name);
|
||||
|
|
@ -396,14 +396,14 @@ static int output_xml(const int print_mac_addreses)
|
|||
}
|
||||
}
|
||||
|
||||
if (config.header_limit & A_BIT) {
|
||||
if (state->header_limit & A_BIT) {
|
||||
fprintf(outfile, "<summary>\n");
|
||||
fprintf(outfile, "\t<location>%s</location>\n", shared_networks->name);
|
||||
fprintf(outfile, "\t<defined>%g</defined>\n", shared_networks->available);
|
||||
fprintf(outfile, "\t<used>%g</used>\n", shared_networks->used);
|
||||
fprintf(outfile, "\t<touched>%g</touched>\n", shared_networks->touched);
|
||||
fprintf(outfile, "\t<location>%s</location>\n", state->shared_networks->name);
|
||||
fprintf(outfile, "\t<defined>%g</defined>\n", state->shared_networks->available);
|
||||
fprintf(outfile, "\t<used>%g</used>\n", state->shared_networks->used);
|
||||
fprintf(outfile, "\t<touched>%g</touched>\n", state->shared_networks->touched);
|
||||
fprintf(outfile, "\t<free>%g</free>\n",
|
||||
shared_networks->available - shared_networks->used);
|
||||
state->shared_networks->available - state->shared_networks->used);
|
||||
fprintf(outfile, "</summary>\n");
|
||||
}
|
||||
|
||||
|
|
@ -413,7 +413,7 @@ static int output_xml(const int print_mac_addreses)
|
|||
}
|
||||
|
||||
/*! \brief The json output formats. */
|
||||
static int output_json(const int print_mac_addreses)
|
||||
static int output_json(struct conf_t *state, const int print_mac_addreses)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
struct range_t *range_p;
|
||||
|
|
@ -422,9 +422,9 @@ static int output_json(const int print_mac_addreses)
|
|||
FILE *outfile;
|
||||
unsigned int sep;
|
||||
|
||||
outfile = open_outfile();
|
||||
range_p = ranges;
|
||||
shared_p = shared_networks;
|
||||
outfile = open_outfile(state);
|
||||
range_p = state->ranges;
|
||||
shared_p = state->shared_networks;
|
||||
sep = 0;
|
||||
|
||||
fprintf(outfile, "{\n");
|
||||
|
|
@ -433,7 +433,7 @@ static int output_json(const int print_mac_addreses)
|
|||
struct leases_t *l;
|
||||
|
||||
fprintf(outfile, " \"active_leases\": [");
|
||||
for (l = leases; l != NULL; l = l->hh.next) {
|
||||
for (l = state->leases; l != NULL; l = l->hh.next) {
|
||||
if (l->type == ACTIVE) {
|
||||
if (i == 0) {
|
||||
i = 1;
|
||||
|
|
@ -453,14 +453,14 @@ static int output_json(const int print_mac_addreses)
|
|||
sep++;
|
||||
}
|
||||
|
||||
if (config.number_limit & R_BIT) {
|
||||
if (state->number_limit & R_BIT) {
|
||||
if (sep) {
|
||||
fprintf(outfile, ",\n");
|
||||
}
|
||||
fprintf(outfile, " \"subnets\": [\n");
|
||||
for (i = 0; i < num_ranges; i++) {
|
||||
range_output_helper(&oh, range_p);
|
||||
if (config.skip_ok && oh.status == STATUS_OK) {
|
||||
for (i = 0; i < state->num_ranges; i++) {
|
||||
range_output_helper(state, &oh, range_p);
|
||||
if (state->skip_ok && oh.status == STATUS_OK) {
|
||||
range_p++;
|
||||
continue;
|
||||
}
|
||||
|
|
@ -484,14 +484,14 @@ static int output_json(const int print_mac_addreses)
|
|||
fprintf(outfile, "\"percent\":%g, ", oh.percent);
|
||||
fprintf(outfile, "\"touch_count\":%g, ", oh.tc);
|
||||
fprintf(outfile, "\"touch_percent\":%g, ", oh.tcp);
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, "\"backup_count\":%g, ", range_p->backups);
|
||||
fprintf(outfile, "\"backup_percent\":%g, ", oh.bup);
|
||||
}
|
||||
fprintf(outfile, "\"status\":%d ", oh.status);
|
||||
|
||||
range_p++;
|
||||
if (i + 1 < num_ranges)
|
||||
if (i + 1 < state->num_ranges)
|
||||
fprintf(outfile, "},\n");
|
||||
else
|
||||
fprintf(outfile, "}\n");
|
||||
|
|
@ -500,15 +500,15 @@ static int output_json(const int print_mac_addreses)
|
|||
sep++;
|
||||
}
|
||||
|
||||
if (config.number_limit & S_BIT) {
|
||||
if (state->number_limit & S_BIT) {
|
||||
if (sep) {
|
||||
fprintf(outfile, ",\n");
|
||||
}
|
||||
fprintf(outfile, " \"shared-networks\": [\n");
|
||||
for (i = 0; i < num_shared_networks; i++) {
|
||||
for (i = 0; i < state->num_shared_networks; i++) {
|
||||
shared_p++;
|
||||
shnet_output_helper(&oh, shared_p);
|
||||
if (config.skip_ok && oh.status == STATUS_OK)
|
||||
shnet_output_helper(state, &oh, shared_p);
|
||||
if (state->skip_ok && oh.status == STATUS_OK)
|
||||
continue;
|
||||
fprintf(outfile, " ");
|
||||
fprintf(outfile, "{ ");
|
||||
|
|
@ -526,7 +526,7 @@ static int output_json(const int print_mac_addreses)
|
|||
fprintf(outfile, "\"touch_percent\":\"%g\", ", oh.tcp);
|
||||
else
|
||||
fprintf(outfile, "\"touch_percent\":%g, ", oh.tcp);
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, "\"backup_count\":%g, ", shared_p->backups);
|
||||
if (shared_p->available == 0)
|
||||
fprintf(outfile, "\"backup_percent\":\"%g\", ", oh.bup);
|
||||
|
|
@ -534,7 +534,7 @@ static int output_json(const int print_mac_addreses)
|
|||
fprintf(outfile, "\"backup_percent\":%g, ", oh.bup);
|
||||
}
|
||||
fprintf(outfile, "\"status\":%d ", oh.status);
|
||||
if (i + 1 < num_shared_networks)
|
||||
if (i + 1 < state->num_shared_networks)
|
||||
fprintf(outfile, "},\n");
|
||||
else
|
||||
fprintf(outfile, "}\n");
|
||||
|
|
@ -543,23 +543,23 @@ static int output_json(const int print_mac_addreses)
|
|||
sep++;
|
||||
}
|
||||
|
||||
if (config.header_limit & A_BIT) {
|
||||
shnet_output_helper(&oh, shared_networks);
|
||||
if (state->header_limit & A_BIT) {
|
||||
shnet_output_helper(state, &oh, state->shared_networks);
|
||||
if (sep) {
|
||||
fprintf(outfile, ",\n");
|
||||
}
|
||||
fprintf(outfile, " \"summary\": {\n");
|
||||
fprintf(outfile, " \"location\":\"%s\",\n", shared_networks->name);
|
||||
fprintf(outfile, " \"defined\":%g,\n", shared_networks->available);
|
||||
fprintf(outfile, " \"used\":%g,\n", shared_networks->used);
|
||||
fprintf(outfile, " \"touched\":%g,\n", shared_networks->touched);
|
||||
fprintf(outfile, " \"location\":\"%s\",\n", state->shared_networks->name);
|
||||
fprintf(outfile, " \"defined\":%g,\n", state->shared_networks->available);
|
||||
fprintf(outfile, " \"used\":%g,\n", state->shared_networks->used);
|
||||
fprintf(outfile, " \"touched\":%g,\n", state->shared_networks->touched);
|
||||
fprintf(outfile, " \"free\":%g,\n",
|
||||
shared_networks->available - shared_networks->used);
|
||||
state->shared_networks->available - state->shared_networks->used);
|
||||
fprintf(outfile, " \"percent\":%g,\n", oh.percent);
|
||||
fprintf(outfile, " \"touch_count\":%g,\n", oh.tc);
|
||||
fprintf(outfile, " \"touch_percent\":%g,\n", oh.tcp);
|
||||
if (config.backups_found == 1) {
|
||||
fprintf(outfile, " \"backup_count\":%g,\n", shared_p->backups);
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, " \"backup_count\":%g,\n", state->shared_networks->backups);
|
||||
fprintf(outfile, " \"backup_percent\":%g,\n", oh.bup);
|
||||
}
|
||||
fprintf(outfile, " \"status\":%d\n", oh.status);
|
||||
|
|
@ -574,14 +574,14 @@ static int output_json(const int print_mac_addreses)
|
|||
*
|
||||
* \param f Output file descriptor.
|
||||
*/
|
||||
static void html_header(FILE *restrict f)
|
||||
static void html_header(struct conf_t *state, FILE *restrict f)
|
||||
{
|
||||
char outstr[200];
|
||||
struct tm *tmp, result;
|
||||
|
||||
struct stat statbuf;
|
||||
|
||||
stat(config.dhcpdlease_file, &statbuf);
|
||||
stat(state->dhcpdlease_file, &statbuf);
|
||||
|
||||
tmp = localtime_r(&statbuf.st_mtime, &result);
|
||||
if (tmp == NULL) {
|
||||
|
|
@ -608,7 +608,7 @@ static void html_header(FILE *restrict f)
|
|||
fprintf(f, "<body>\n");
|
||||
fprintf(f, "<div class=\"container\">\n");
|
||||
fprintf(f, "<h2>ISC DHCPD status</h2>\n");
|
||||
fprintf(f, "<small>File %s was last modified at %s</small><hr />\n", config.dhcpdlease_file, outstr);
|
||||
fprintf(f, "<small>File %s was last modified at %s</small><hr />\n", state->dhcpdlease_file, outstr);
|
||||
}
|
||||
|
||||
/*! \brief Footer for full html output format.
|
||||
|
|
@ -713,7 +713,7 @@ static void newsection(FILE *restrict f, char const *restrict title)
|
|||
}
|
||||
|
||||
/*! \brief Output html format. */
|
||||
static int output_html(void)
|
||||
static int output_html(struct conf_t *state)
|
||||
{
|
||||
unsigned int i;
|
||||
struct range_t *range_p;
|
||||
|
|
@ -721,13 +721,13 @@ static int output_html(void)
|
|||
struct output_helper_t oh;
|
||||
FILE *outfile;
|
||||
|
||||
outfile = open_outfile();
|
||||
range_p = ranges;
|
||||
shared_p = shared_networks;
|
||||
html_header(outfile);
|
||||
outfile = open_outfile(state);
|
||||
range_p = state->ranges;
|
||||
shared_p = state->shared_networks;
|
||||
html_header(state, outfile);
|
||||
newsection(outfile, "Sum of all");
|
||||
table_start(outfile, "a", "all");
|
||||
if (config.header_limit & A_BIT) {
|
||||
if (state->header_limit & A_BIT) {
|
||||
start_tag(outfile, "thead");
|
||||
start_tag(outfile, "tr");
|
||||
output_line(outfile, "th", "name");
|
||||
|
|
@ -737,26 +737,26 @@ static int output_html(void)
|
|||
output_line(outfile, "th", "touch");
|
||||
output_line(outfile, "th", "t+c");
|
||||
output_line(outfile, "th", "t+c perc");
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
output_line(outfile, "th", "bu");
|
||||
output_line(outfile, "th", "bu perc");
|
||||
}
|
||||
end_tag(outfile, "tr");
|
||||
end_tag(outfile, "thead");
|
||||
}
|
||||
if (config.number_limit & A_BIT) {
|
||||
if (state->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);
|
||||
shnet_output_helper(state, &oh, state->shared_networks);
|
||||
output_line(outfile, "td", state->shared_networks->name);
|
||||
output_double(outfile, "td", state->shared_networks->available);
|
||||
output_double(outfile, "td", state->shared_networks->used);
|
||||
output_float(outfile, "td", oh.percent);
|
||||
output_double(outfile, "td", shared_networks->touched);
|
||||
output_double(outfile, "td", state->shared_networks->touched);
|
||||
output_double(outfile, "td", oh.tc);
|
||||
output_float(outfile, "td", oh.tcp);
|
||||
if (config.backups_found == 1) {
|
||||
output_double(outfile, "td", shared_networks->backups);
|
||||
if (state->backups_found == 1) {
|
||||
output_double(outfile, "td", state->shared_networks->backups);
|
||||
output_float(outfile, "td", oh.tcp);
|
||||
}
|
||||
end_tag(outfile, "tr");
|
||||
|
|
@ -765,7 +765,7 @@ static int output_html(void)
|
|||
table_end(outfile);
|
||||
newsection(outfile, "Shared networks");
|
||||
table_start(outfile, "s", "snet");
|
||||
if (config.header_limit & S_BIT) {
|
||||
if (state->header_limit & S_BIT) {
|
||||
start_tag(outfile, "thead");
|
||||
start_tag(outfile, "tr");
|
||||
output_line(outfile, "th", "name");
|
||||
|
|
@ -775,19 +775,19 @@ static int output_html(void)
|
|||
output_line(outfile, "th", "touch");
|
||||
output_line(outfile, "th", "t+c");
|
||||
output_line(outfile, "th", "t+c perc");
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
output_line(outfile, "th", "bu");
|
||||
output_line(outfile, "th", "bu perc");
|
||||
}
|
||||
end_tag(outfile, "tr");
|
||||
end_tag(outfile, "thead");
|
||||
}
|
||||
if (config.number_limit & S_BIT) {
|
||||
if (state->number_limit & S_BIT) {
|
||||
start_tag(outfile, "tbody");
|
||||
for (i = 0; i < num_shared_networks; i++) {
|
||||
for (i = 0; i < state->num_shared_networks; i++) {
|
||||
shared_p++;
|
||||
shnet_output_helper(&oh, shared_networks);
|
||||
if (config.skip_ok && oh.status == STATUS_OK)
|
||||
shnet_output_helper(state, &oh, state->shared_networks);
|
||||
if (state->skip_ok && oh.status == STATUS_OK)
|
||||
continue;
|
||||
start_tag(outfile, "tr");
|
||||
output_line(outfile, "td", shared_p->name);
|
||||
|
|
@ -797,7 +797,7 @@ static int output_html(void)
|
|||
output_double(outfile, "td", shared_p->touched);
|
||||
output_double(outfile, "td", oh.tc);
|
||||
output_float(outfile, "td", oh.tcp);
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
output_double(outfile, "td", shared_p->backups);
|
||||
output_float(outfile, "td", oh.bup);
|
||||
}
|
||||
|
|
@ -808,7 +808,7 @@ static int output_html(void)
|
|||
table_end(outfile);
|
||||
newsection(outfile, "Ranges");
|
||||
table_start(outfile, "r", "ranges");
|
||||
if (config.header_limit & R_BIT) {
|
||||
if (state->header_limit & R_BIT) {
|
||||
start_tag(outfile, "thead");
|
||||
start_tag(outfile, "tr");
|
||||
output_line(outfile, "th", "shared net name");
|
||||
|
|
@ -820,18 +820,18 @@ static int output_html(void)
|
|||
output_line(outfile, "th", "touch");
|
||||
output_line(outfile, "th", "t+c");
|
||||
output_line(outfile, "th", "t+c perc");
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
output_line(outfile, "th", "bu");
|
||||
output_line(outfile, "th", "bu perc");
|
||||
}
|
||||
end_tag(outfile, "tr");
|
||||
end_tag(outfile, "thead");
|
||||
}
|
||||
if (config.number_limit & R_BIT) {
|
||||
if (state->number_limit & R_BIT) {
|
||||
start_tag(outfile, "tbody");
|
||||
for (i = 0; i < num_ranges; i++) {
|
||||
range_output_helper(&oh, range_p);
|
||||
if (config.skip_ok && oh.status == STATUS_OK) {
|
||||
for (i = 0; i < state->num_ranges; i++) {
|
||||
range_output_helper(state, &oh, range_p);
|
||||
if (state->skip_ok && oh.status == STATUS_OK) {
|
||||
range_p++;
|
||||
continue;
|
||||
}
|
||||
|
|
@ -849,7 +849,7 @@ static int output_html(void)
|
|||
output_double(outfile, "td", range_p->touched);
|
||||
output_double(outfile, "td", oh.tc);
|
||||
output_float(outfile, "td", oh.tcp);
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
output_double(outfile, "td", range_p->backups);
|
||||
output_float(outfile, "td", oh.bup);
|
||||
}
|
||||
|
|
@ -865,7 +865,7 @@ static int output_html(void)
|
|||
}
|
||||
|
||||
/*! \brief Output cvs format. */
|
||||
static int output_csv(void)
|
||||
static int output_csv(struct conf_t *state)
|
||||
{
|
||||
unsigned int i;
|
||||
struct range_t *range_p;
|
||||
|
|
@ -873,23 +873,23 @@ static int output_csv(void)
|
|||
struct output_helper_t oh;
|
||||
FILE *outfile;
|
||||
|
||||
outfile = open_outfile();
|
||||
range_p = ranges;
|
||||
shared_p = shared_networks;
|
||||
if (config.header_limit & R_BIT) {
|
||||
outfile = open_outfile(state);
|
||||
range_p = state->ranges;
|
||||
shared_p = state->shared_networks;
|
||||
if (state->header_limit & R_BIT) {
|
||||
fprintf(outfile, "\"Ranges:\"\n");
|
||||
fprintf
|
||||
(outfile,
|
||||
"\"shared net name\",\"first ip\",\"last ip\",\"max\",\"cur\",\"percent\",\"touch\",\"t+c\",\"t+c perc\"");
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, ",\"bu\",\"bu perc\"");
|
||||
}
|
||||
fprintf(outfile, "\n");
|
||||
}
|
||||
if (config.number_limit & R_BIT) {
|
||||
for (i = 0; i < num_ranges; i++) {
|
||||
range_output_helper(&oh, range_p);
|
||||
if (config.skip_ok && oh.status == STATUS_OK) {
|
||||
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) {
|
||||
range_p++;
|
||||
continue;
|
||||
}
|
||||
|
|
@ -908,7 +908,7 @@ static int output_csv(void)
|
|||
range_p->touched,
|
||||
oh.tc,
|
||||
oh.tcp);
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, ",\"%g\",\"%.3f\"",
|
||||
range_p->backups,
|
||||
oh.bup);
|
||||
|
|
@ -919,21 +919,21 @@ static int output_csv(void)
|
|||
}
|
||||
fprintf(outfile, "\n");
|
||||
}
|
||||
if (config.header_limit & S_BIT) {
|
||||
if (state->header_limit & S_BIT) {
|
||||
fprintf(outfile, "\"Shared networks:\"\n");
|
||||
fprintf(outfile,
|
||||
"\"name\",\"max\",\"cur\",\"percent\",\"touch\",\"t+c\",\"t+c perc\"");
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, ",\"bu\",\"bu perc\"");
|
||||
}
|
||||
fprintf(outfile, "\n");
|
||||
}
|
||||
if (config.number_limit & S_BIT) {
|
||||
if (state->number_limit & S_BIT) {
|
||||
|
||||
for (i = 0; i < num_shared_networks; i++) {
|
||||
for (i = 0; i < state->num_shared_networks; i++) {
|
||||
shared_p++;
|
||||
shnet_output_helper(&oh, shared_p);
|
||||
if (config.skip_ok && oh.status == STATUS_OK)
|
||||
shnet_output_helper(state, &oh, shared_p);
|
||||
if (state->skip_ok && oh.status == STATUS_OK)
|
||||
continue;
|
||||
fprintf(outfile,
|
||||
"\"%s\",\"%g\",\"%g\",\"%.3f\",\"%g\",\"%g\",\"%.3f\"",
|
||||
|
|
@ -944,7 +944,7 @@ static int output_csv(void)
|
|||
shared_p->touched,
|
||||
oh.tc,
|
||||
oh.tcp);
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, ",\"%g\",\"%.3f\"",
|
||||
shared_p->backups,
|
||||
oh.bup);
|
||||
|
|
@ -954,29 +954,29 @@ static int output_csv(void)
|
|||
}
|
||||
fprintf(outfile, "\n");
|
||||
}
|
||||
if (config.header_limit & A_BIT) {
|
||||
if (state->header_limit & A_BIT) {
|
||||
fprintf(outfile, "\"Sum of all ranges:\"\n");
|
||||
fprintf(outfile,
|
||||
"\"name\",\"max\",\"cur\",\"percent\",\"touch\",\"t+c\",\"t+c perc\"");
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, ",\"bu\",\"bu perc\"");
|
||||
}
|
||||
fprintf(outfile, "\n");
|
||||
}
|
||||
if (config.number_limit & A_BIT) {
|
||||
shnet_output_helper(&oh, shared_networks);
|
||||
if (state->number_limit & A_BIT) {
|
||||
shnet_output_helper(state, &oh, state->shared_networks);
|
||||
fprintf(outfile,
|
||||
"\"%s\",\"%g\",\"%g\",\"%.3f\",\"%g\",\"%g\",\"%.3f\"",
|
||||
shared_networks->name,
|
||||
shared_networks->available,
|
||||
shared_networks->used,
|
||||
state->shared_networks->name,
|
||||
state->shared_networks->available,
|
||||
state->shared_networks->used,
|
||||
oh.percent,
|
||||
shared_networks->touched,
|
||||
state->shared_networks->touched,
|
||||
oh.tc,
|
||||
oh.tcp);
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, "%7g %8.3f",
|
||||
shared_networks->backups,
|
||||
state->shared_networks->backups,
|
||||
oh.bup);
|
||||
}
|
||||
fprintf(outfile, "\n");
|
||||
|
|
@ -986,7 +986,7 @@ static int output_csv(void)
|
|||
}
|
||||
|
||||
/*! \brief Output alarm text, and return program exit value. */
|
||||
static int output_alarming(void)
|
||||
static int output_alarming(struct conf_t *state)
|
||||
{
|
||||
FILE *outfile;
|
||||
struct range_t *range_p;
|
||||
|
|
@ -997,14 +997,14 @@ static int output_alarming(void)
|
|||
int rw = 0, rc = 0, ro = 0, ri = 0, sw = 0, sc = 0, so = 0, si = 0;
|
||||
int ret_val;
|
||||
|
||||
outfile = open_outfile();
|
||||
range_p = ranges;
|
||||
outfile = open_outfile(state);
|
||||
range_p = state->ranges;
|
||||
range_size = get_range_size(range_p);
|
||||
shared_p = shared_networks;
|
||||
shared_p = state->shared_networks;
|
||||
|
||||
if (config.number_limit & R_BIT) {
|
||||
for (i = 0; i < num_ranges; i++) {
|
||||
range_output_helper(&oh, range_p);
|
||||
if (state->number_limit & R_BIT) {
|
||||
for (i = 0; i < state->num_ranges; i++) {
|
||||
range_output_helper(state, &oh, range_p);
|
||||
switch (oh.status) {
|
||||
case STATUS_SUPPRESSED:
|
||||
break;
|
||||
|
|
@ -1026,10 +1026,10 @@ static int output_alarming(void)
|
|||
range_p++;
|
||||
}
|
||||
}
|
||||
if (config.number_limit & S_BIT) {
|
||||
for (i = 0; i < num_shared_networks; i++) {
|
||||
if (state->number_limit & S_BIT) {
|
||||
for (i = 0; i < state->num_shared_networks; i++) {
|
||||
shared_p++;
|
||||
shnet_output_helper(&oh, shared_p);
|
||||
shnet_output_helper(state, &oh, shared_p);
|
||||
switch (oh.status) {
|
||||
case STATUS_IGNORED:
|
||||
si++;
|
||||
|
|
@ -1056,14 +1056,14 @@ static int output_alarming(void)
|
|||
else
|
||||
ret_val = STATE_OK;
|
||||
|
||||
if ((0 < rc && config.number_limit & R_BIT)
|
||||
|| (0 < sc && config.number_limit & S_BIT)) {
|
||||
if ((0 < rc && state->number_limit & R_BIT)
|
||||
|| (0 < sc && state->number_limit & S_BIT)) {
|
||||
fprintf(outfile, "CRITICAL: %s:", program_name);
|
||||
} else if ((0 < rw && config.number_limit & R_BIT)
|
||||
|| (0 < sw && config.number_limit & S_BIT)) {
|
||||
} else if ((0 < rw && state->number_limit & R_BIT)
|
||||
|| (0 < sw && state->number_limit & S_BIT)) {
|
||||
fprintf(outfile, "WARNING: %s:", program_name);
|
||||
} else {
|
||||
if (config.number_limit & A_BIT)
|
||||
if (state->number_limit & A_BIT)
|
||||
fprintf(outfile, "OK:");
|
||||
else {
|
||||
if (close_stream(outfile)) {
|
||||
|
|
@ -1072,7 +1072,7 @@ static int output_alarming(void)
|
|||
return ret_val;
|
||||
}
|
||||
}
|
||||
if (config.header_limit & R_BIT) {
|
||||
if (state->header_limit & R_BIT) {
|
||||
fprintf(outfile, " Ranges - crit: %d warn: %d ok: %d", rc, rw, ro);
|
||||
if (ri != 0) {
|
||||
fprintf(outfile, " ignored: %d", ri);
|
||||
|
|
@ -1081,22 +1081,22 @@ static int output_alarming(void)
|
|||
if (ri != 0) {
|
||||
fprintf(outfile, " range_ignored=%d", ri);
|
||||
}
|
||||
if (config.perfdata == 1 && config.number_limit & R_BIT) {
|
||||
for (i = 0; i < num_ranges; i++) {
|
||||
if (state->perfdata == 1 && state->number_limit & R_BIT) {
|
||||
for (i = 0; i < state->num_ranges; i++) {
|
||||
range_p--;
|
||||
range_size = get_range_size(range_p);
|
||||
if (config.minsize < range_size) {
|
||||
if (state->minsize < range_size) {
|
||||
fprintf(outfile, " %s_r=",
|
||||
ntop_ipaddr(&range_p->first_ip));
|
||||
fprintf(outfile, "%g;%g;%g;0;%g",
|
||||
range_p->count,
|
||||
(range_size * config.warning / 100),
|
||||
(range_size * config.critical / 100),
|
||||
(range_size * state->warning / 100),
|
||||
(range_size * state->critical / 100),
|
||||
range_size);
|
||||
fprintf(outfile, " %s_rt=%g",
|
||||
ntop_ipaddr(&range_p->first_ip),
|
||||
range_p->touched);
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, " %s_rbu=%g",
|
||||
ntop_ipaddr(&range_p->first_ip),
|
||||
range_p->backups);
|
||||
|
|
@ -1108,7 +1108,7 @@ static int output_alarming(void)
|
|||
} else {
|
||||
fprintf(outfile, " ");
|
||||
}
|
||||
if (config.header_limit & S_BIT) {
|
||||
if (state->header_limit & S_BIT) {
|
||||
fprintf(outfile, "Shared nets - crit: %d warn: %d ok: %d", sc, sw, so);
|
||||
if (si != 0) {
|
||||
fprintf(outfile, " ignored: %d", si);
|
||||
|
|
@ -1117,19 +1117,19 @@ static int output_alarming(void)
|
|||
if (si != 0) {
|
||||
fprintf(outfile, " snet_ignored=%d", si);
|
||||
}
|
||||
if (config.perfdata == 1 && config.header_limit & R_BIT) {
|
||||
for (i = 0; i < num_shared_networks; i++) {
|
||||
if (config.minsize < shared_p->available) {
|
||||
if (state->perfdata == 1 && state->header_limit & R_BIT) {
|
||||
for (i = 0; i < state->num_shared_networks; i++) {
|
||||
if (state->minsize < shared_p->available) {
|
||||
fprintf(outfile, " '%s_s'=%g;%g;%g;0;%g",
|
||||
shared_p->name,
|
||||
shared_p->used,
|
||||
(shared_p->available * config.warning / 100),
|
||||
(shared_p->available * config.critical / 100),
|
||||
(shared_p->available * state->warning / 100),
|
||||
(shared_p->available * state->critical / 100),
|
||||
shared_p->available);
|
||||
fprintf(outfile, " '%s_st'=%g",
|
||||
shared_p->name,
|
||||
shared_p->touched);
|
||||
if (config.backups_found == 1) {
|
||||
if (state->backups_found == 1) {
|
||||
fprintf(outfile, " '%s_sbu'=%g",
|
||||
shared_p->name,
|
||||
shared_p->backups);
|
||||
|
|
@ -1146,44 +1146,44 @@ static int output_alarming(void)
|
|||
}
|
||||
|
||||
/*! \brief Return output_format_names enum based on single char input. */
|
||||
int output_analysis(const char c)
|
||||
int output_analysis(struct conf_t *state, const char output_format)
|
||||
{
|
||||
int ret = 1;
|
||||
switch (c) {
|
||||
switch (output_format) {
|
||||
case 't':
|
||||
ret = output_txt();
|
||||
ret = output_txt(state);
|
||||
break;
|
||||
case 'a':
|
||||
ret = output_alarming();
|
||||
ret = output_alarming(state);
|
||||
break;
|
||||
case 'h':
|
||||
error(EXIT_FAILURE, 0, "html table only output format is deprecated");
|
||||
break;
|
||||
case 'H':
|
||||
ret = output_html();
|
||||
ret = output_html(state);
|
||||
break;
|
||||
case 'x':
|
||||
ret = output_xml(0);
|
||||
ret = output_xml(state, 0);
|
||||
break;
|
||||
case 'X':
|
||||
ret = output_xml(1);
|
||||
ret = output_xml(state, 1);
|
||||
break;
|
||||
case 'j':
|
||||
ret = output_json(0);
|
||||
ret = output_json(state, 0);
|
||||
break;
|
||||
case 'J':
|
||||
ret = output_json(1);
|
||||
ret = output_json(state, 1);
|
||||
break;
|
||||
case 'c':
|
||||
ret = output_csv();
|
||||
ret = output_csv(state);
|
||||
break;
|
||||
#ifdef BUILD_MUSTACH
|
||||
case 'm':
|
||||
ret = mustach_dhcpd_pools();
|
||||
ret = mustach_dhcpd_pools(state);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
error(EXIT_FAILURE, 0, "unknown output format: '%c'", c);
|
||||
error(EXIT_FAILURE, 0, "unknown output format: '%c'", output_format);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue