diff --git a/src/dhcpd-pools.c b/src/dhcpd-pools.c index e4f0866..9fdc5d5 100644 --- a/src/dhcpd-pools.c +++ b/src/dhcpd-pools.c @@ -42,7 +42,6 @@ #include #include #include -#include #include #include @@ -77,6 +76,15 @@ int (*output_analysis) (void); void (*add_lease) (union ipaddr_t *ip, enum ltype type); struct leases_t *(*find_lease) (union ipaddr_t *ip); +static int return_limit(const char c) +{ + if ('0' <= c && c < '8') + return c - '0'; + clean_up(); + error(EXIT_FAILURE, 0, "return_limit: output mask '%s' is illegal", optarg); + return 0; +} + /*! \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 @@ -142,9 +150,9 @@ int main(int argc, char **argv) strncpy(config.dhcpdconf_file, DHCPDCONF_FILE, MAXLEN - 1); strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE, MAXLEN - 1); tmp = OUTPUT_LIMIT; - config.output_limit[0] = (*tmp - '0'); + config.header_limit = (*tmp - '0'); tmp++; - config.output_limit[1] = (*tmp - '0'); + config.number_limit = (*tmp - '0'); /* Make sure some output format is selected by default */ strncpy(config.output_format, OUTPUT_FORMAT, (size_t)1); /* Default sort order is by IPs small to big */ @@ -200,15 +208,8 @@ int main(int argc, char **argv) break; case 'L': /* Specification what will be printed */ - for (i = 0; i < 2; i++) { - if (optarg[i] >= '0' && optarg[i] < '8') - config.output_limit[i] = optarg[i] - '0'; - else { - clean_up(); - error(EXIT_FAILURE, 0, - "main: output mask `%s' is illegal", optarg); - } - } + config.header_limit = return_limit(optarg[0]); + config.number_limit = return_limit(optarg[1]); break; case OPT_SNET_ALARMS: config.snet_alarms = true; diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index d413f89..f2bb8de 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -44,7 +44,6 @@ # include # include -# include # include # include # include @@ -198,16 +197,18 @@ struct configuration_t { char *dhcpdlease_file; char output_format[2]; struct output_sort *sorts; - bool reverse_order; char *output_file; - int output_limit[2]; - bool backups_found; - bool snet_alarms; double warning; double critical; double warn_count; double crit_count; double minsize; + unsigned int + reverse_order:1, + backups_found:1, + snet_alarms:1, + header_limit:3, + number_limit:3; }; /* Global variables */ /* \var prefix_length Length of each prefix. */ diff --git a/src/other.c b/src/other.c index eacc5c6..0226995 100644 --- a/src/other.c +++ b/src/other.c @@ -43,7 +43,6 @@ #include #include #include -#include #include #include #include diff --git a/src/output.c b/src/output.c index 041c1e1..3afccf9 100644 --- a/src/output.c +++ b/src/output.c @@ -45,7 +45,6 @@ #include #include #include -#include #include #include #include @@ -82,7 +81,7 @@ int output_txt(void) range_size = get_range_size(range_p); shared_p = shared_networks; - if (config.output_limit[0] & R_BIT) { + if (config.header_limit & R_BIT) { fprintf(outfile, "Ranges:\n"); fprintf (outfile, @@ -92,12 +91,12 @@ int output_txt(void) "first ip", max_ipaddr_length, "last ip", "max", "cur", "percent", "touch", "t+c", "t+c perc"); - if (config.backups_found == true) { + if (config.backups_found == 1) { fprintf(outfile, " bu bu perc"); } fprintf(outfile, "\n"); } - if (config.output_limit[1] & R_BIT) { + if (config.number_limit & R_BIT) { for (i = 0; i < num_ranges; i++) { if (range_p->shared_net) { fprintf(outfile, "%-20s", range_p->shared_net->name); @@ -119,7 +118,7 @@ int output_txt(void) range_p->touched, range_p->touched + range_p->count, (float)(100 * (range_p->touched + range_p->count)) / range_size); - if (config.backups_found == true) { + if (config.backups_found == 1) { fprintf(outfile, "%7g %8.3f", range_p->backups, (float)(100 * range_p->backups) / range_size); @@ -129,19 +128,19 @@ int output_txt(void) range_size = get_range_size(range_p); } } - if (config.output_limit[1] & R_BIT && config.output_limit[0] & S_BIT) { + if (config.number_limit & R_BIT && config.header_limit & S_BIT) { fprintf(outfile, "\n"); } - if (config.output_limit[0] & S_BIT) { + if (config.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 == true) { + if (config.backups_found == 1) { fprintf(outfile, " bu bu perc"); } fprintf(outfile, "\n"); } - if (config.output_limit[1] & S_BIT) { + if (config.number_limit & S_BIT) { for (i = 0; i < num_shared_networks; i++) { shared_p++; fprintf(outfile, @@ -154,7 +153,7 @@ int output_txt(void) shared_p->available == 0 ? -NAN : ((float)(100 * (shared_p->touched + shared_p->used)) / shared_p->available)); - if (config.backups_found == true) { + if (config.backups_found == 1) { fprintf(outfile, "%7g %8.3f", shared_p->backups, (float)(100 * shared_p->backups) / shared_p->available); @@ -163,20 +162,20 @@ int output_txt(void) fprintf(outfile, "\n"); } } - if (config.output_limit[1] & S_BIT && config.output_limit[0] & A_BIT) { + if (config.number_limit & S_BIT && config.header_limit & A_BIT) { fprintf(outfile, "\n"); } - if (config.output_limit[0] & A_BIT) { + if (config.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 == true) { + if (config.backups_found == 1) { fprintf(outfile, " bu bu perc"); } fprintf(outfile, "\n"); } - if (config.output_limit[1] & A_BIT) { + if (config.number_limit & A_BIT) { fprintf(outfile, "%-20s %5g %5g %10.3f %7g %6g %9.3f", shared_networks->name, shared_networks->available, @@ -190,7 +189,7 @@ int output_txt(void) (shared_networks->touched + shared_networks->used)) / shared_networks->available); - if (config.backups_found == true) { + if (config.backups_found == 1) { fprintf(outfile, "%7g %8.3f", shared_networks->available == 0 ? -NAN : shared_networks->backups, (float)(100 * shared_networks->backups) / @@ -254,7 +253,7 @@ int output_xml(void) } } - if (config.output_limit[1] & R_BIT) { + if (config.number_limit & R_BIT) { for (i = 0; i < num_ranges; i++) { fprintf(outfile, "\n"); if (range_p->shared_net) { @@ -275,7 +274,7 @@ int output_xml(void) } } - if (config.output_limit[1] & S_BIT) { + if (config.number_limit & S_BIT) { for (i = 0; i < num_shared_networks; i++) { shared_p++; fprintf(outfile, "\n"); @@ -289,7 +288,7 @@ int output_xml(void) } } - if (config.output_limit[0] & A_BIT) { + if (config.header_limit & A_BIT) { fprintf(outfile, "\n"); fprintf(outfile, "\t%s\n", shared_networks->name); fprintf(outfile, "\t%g\n", shared_networks->available); @@ -367,7 +366,7 @@ int output_json(void) sep++; } - if (config.output_limit[1] & R_BIT) { + if (config.number_limit & R_BIT) { if (sep) { fprintf(outfile, ",\n"); } @@ -399,7 +398,7 @@ int output_json(void) sep++; } - if (config.output_limit[1] & S_BIT) { + if (config.number_limit & S_BIT) { if (sep) { fprintf(outfile, ",\n"); } @@ -422,7 +421,7 @@ int output_json(void) sep++; } - if (config.output_limit[0] & A_BIT) { + if (config.header_limit & A_BIT) { if (sep) { fprintf(outfile, ",\n"); } @@ -618,7 +617,7 @@ int output_html(void) html_header(outfile); newsection(outfile, "Sum of all"); table_start(outfile, "a", "all"); - if (config.output_limit[0] & A_BIT) { + if (config.header_limit & A_BIT) { start_tag(outfile, "thead"); start_tag(outfile, "tr"); output_line(outfile, "th", "name"); @@ -628,14 +627,14 @@ 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 == true) { + if (config.backups_found == 1) { output_line(outfile, "th", "bu"); output_line(outfile, "th", "bu perc"); } end_tag(outfile, "tr"); end_tag(outfile, "thead"); } - if (config.output_limit[1] & A_BIT) { + if (config.number_limit & A_BIT) { start_tag(outfile, "tbody"); start_tag(outfile, "tr"); output_line(outfile, "td", shared_networks->name); @@ -653,7 +652,7 @@ int output_html(void) + shared_networks->used)) / shared_networks->available); - if (config.backups_found == true) { + if (config.backups_found == 1) { output_double(outfile, "td", shared_networks->backups); output_float(outfile, "td", shared_networks->available == 0 ? -NAN : (float)(100 * @@ -666,7 +665,7 @@ int output_html(void) table_end(outfile); newsection(outfile, "Shared networks"); table_start(outfile, "s", "snet"); - if (config.output_limit[0] & S_BIT) { + if (config.header_limit & S_BIT) { start_tag(outfile, "thead"); start_tag(outfile, "tr"); output_line(outfile, "th", "name"); @@ -676,14 +675,14 @@ 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 == true) { + if (config.backups_found == 1) { output_line(outfile, "th", "bu"); output_line(outfile, "th", "bu perc"); } end_tag(outfile, "tr"); end_tag(outfile, "thead"); } - if (config.output_limit[1] & S_BIT) { + if (config.number_limit & S_BIT) { start_tag(outfile, "tbody"); for (i = 0; i < num_shared_networks; i++) { shared_p++; @@ -702,7 +701,7 @@ int output_html(void) (shared_p->touched + shared_p->used)) / shared_p->available); - if (config.backups_found == true) { + if (config.backups_found == 1) { output_double(outfile, "td", shared_p->backups); output_float(outfile, "td", shared_p->available == 0 ? -NAN : (float)(100 * @@ -716,7 +715,7 @@ int output_html(void) table_end(outfile); newsection(outfile, "Ranges"); table_start(outfile, "r", "ranges"); - if (config.output_limit[0] & R_BIT) { + if (config.header_limit & R_BIT) { start_tag(outfile, "thead"); start_tag(outfile, "tr"); output_line(outfile, "th", "shared net name"); @@ -728,14 +727,14 @@ 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 == true) { + if (config.backups_found == 1) { output_line(outfile, "th", "bu"); output_line(outfile, "th", "bu perc"); } end_tag(outfile, "tr"); end_tag(outfile, "thead"); } - if (config.output_limit[1] & R_BIT) { + if (config.number_limit & R_BIT) { start_tag(outfile, "tbody"); for (i = 0; i < num_ranges; i++) { start_tag(outfile, "tr"); @@ -754,7 +753,7 @@ int output_html(void) output_float(outfile, "td", (float)(100 * (range_p->touched + range_p->count)) / range_size); - if (config.backups_found == true) { + if (config.backups_found == 1) { output_double(outfile, "td", range_p->backups); output_float(outfile, "td", (float)(100 * range_p->backups) / range_size); @@ -803,17 +802,17 @@ int output_csv(void) range_p = ranges; range_size = get_range_size(range_p); shared_p = shared_networks; - if (config.output_limit[0] & R_BIT) { + if (config.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 == true) { + if (config.backups_found == 1) { fprintf(outfile, ",\"bu\",\"bu perc\""); } fprintf(outfile, "\n"); } - if (config.output_limit[1] & R_BIT) { + if (config.number_limit & R_BIT) { for (i = 0; i < num_ranges; i++) { if (range_p->shared_net) { fprintf(outfile, "\"%s\",", range_p->shared_net->name); @@ -829,7 +828,7 @@ int output_csv(void) range_p->touched, range_p->touched + range_p->count, (float)(100 * (range_p->touched + range_p->count)) / range_size); - if (config.backups_found == true) { + if (config.backups_found == 1) { fprintf(outfile, ",\"%g\",\"%.3f\"", range_p->backups, (float)(100 * range_p->backups) / range_size); @@ -841,16 +840,16 @@ int output_csv(void) } fprintf(outfile, "\n"); } - if (config.output_limit[0] & S_BIT) { + if (config.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 == true) { + if (config.backups_found == 1) { fprintf(outfile, ",\"bu\",\"bu perc\""); } fprintf(outfile, "\n"); } - if (config.output_limit[1] & S_BIT) { + if (config.number_limit & S_BIT) { for (i = 0; i < num_shared_networks; i++) { shared_p++; @@ -865,7 +864,7 @@ int output_csv(void) (shared_p->touched + shared_p->used)) / shared_p->available); - if (config.backups_found == true) { + if (config.backups_found == 1) { fprintf(outfile, ",\"%g\",\"%.3f\"", shared_p->backups, shared_p->available == @@ -877,16 +876,16 @@ int output_csv(void) } fprintf(outfile, "\n"); } - if (config.output_limit[0] & A_BIT) { + if (config.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 == true) { + if (config.backups_found == 1) { fprintf(outfile, ",\"bu\",\"bu perc\""); } fprintf(outfile, "\n"); } - if (config.output_limit[1] & A_BIT) { + if (config.number_limit & A_BIT) { fprintf(outfile, "\"%s\",\"%g\",\"%g\",\"%.3f\",\"%g\",\"%g\",\"%.3f\"", @@ -900,7 +899,7 @@ int output_csv(void) 0 ? -NAN : (float)(100 * (shared_networks->touched + shared_networks->used)) / shared_networks->available); - if (config.backups_found == true) { + if (config.backups_found == 1) { fprintf(outfile, "%7g %8.3f", shared_networks->backups, shared_networks->available == @@ -948,7 +947,7 @@ int output_alarming(void) outfile = stdout; } - if (config.output_limit[1] & R_BIT) { + if (config.number_limit & R_BIT) { for (i = 0; i < num_ranges; i++) { if (config.snet_alarms && range_p->shared_net != shared_networks) { continue; @@ -968,7 +967,7 @@ int output_alarming(void) range_size = get_range_size(range_p); } } - if (config.output_limit[1] & S_BIT) { + if (config.number_limit & S_BIT) { for (i = 0; i < num_shared_networks; i++) { shared_p++; if (config.minsize < shared_p->available) { @@ -994,19 +993,19 @@ int output_alarming(void) else ret_val = STATE_OK; - if ((0 < rc && config.output_limit[1] & R_BIT) - || (0 < sc && config.output_limit[1] & S_BIT)) { + if ((0 < rc && config.number_limit & R_BIT) + || (0 < sc && config.number_limit & S_BIT)) { fprintf(outfile, "CRITICAL: %s:", program_name); - } else if ((0 < rw && config.output_limit[1] & R_BIT) - || (0 < sw && config.output_limit[1] & S_BIT)) { + } else if ((0 < rw && config.number_limit & R_BIT) + || (0 < sw && config.number_limit & S_BIT)) { fprintf(outfile, "WARNING: %s:", program_name); } else { - if (config.output_limit[1] & A_BIT) + if (config.number_limit & A_BIT) fprintf(outfile, "OK:"); else return ret_val; } - if (config.output_limit[0] & R_BIT) { + if (config.header_limit & R_BIT) { fprintf(outfile, " Ranges - crit: %d warn: %d ok: %d", rc, rw, ro); if (ri != 0) { fprintf(outfile, " ignored: %d", ri); @@ -1019,7 +1018,7 @@ int output_alarming(void) } else { fprintf(outfile, " "); } - if (config.output_limit[0] & S_BIT) { + if (config.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);