/* ** Copyright (C) 2006- Sami Kerola ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation; either version 2 of the License, or ** (at your option) any later version. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "dhcpd-pools.h" int output_txt(void) { unsigned int i; struct in_addr first, last; struct range_t *range_p; struct shared_network_t *shared_p; int ret; FILE *outfile; if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { err(EXIT_FAILURE, "output_txt: %s", config.output_file); } } else { outfile = stdout; } range_p = ranges; shared_p = shared_networks; if (config.output_limit[0] & output_limit_bit_1) { fprintf(outfile, "Ranges:\n"); fprintf (outfile, "shared net name first ip last ip max cur percent touch t+c t+c perc"); if (0 < num_backups) { fprintf(outfile, " bu bu perc"); } fprintf(outfile, "\n"); } if (config.output_limit[1] & output_limit_bit_1) { for (i = 0; i < num_ranges; i++) { first.s_addr = ntohl(range_p->first_ip + 1); last.s_addr = ntohl(range_p->last_ip - 1); if (range_p->shared_net) { fprintf(outfile, "%-20s", range_p->shared_net->name); } else { fprintf(outfile, "not_defined "); } fprintf(outfile, "%-16s", inet_ntoa(first)); fprintf(outfile, " - %-16s %5lu %5lu %10.3f %5lu %5lu %9.3f", inet_ntoa(last), range_p->last_ip - range_p->first_ip - 1, range_p->count, (float) (100 * range_p->count) / (range_p->last_ip - range_p->first_ip - 1), range_p->touched, range_p->touched + range_p->count, (float) (100 * (range_p->touched + range_p->count)) / (range_p->last_ip - range_p->first_ip - 1)); if (0 < num_backups) { fprintf(outfile, "%7lu %8.3f", range_p->backups, (float) (100 * range_p->backups) / (range_p->last_ip - range_p->first_ip - 1)); } fprintf(outfile, "\n"); range_p++; } } if (config.output_limit[1] & output_limit_bit_1 && config.output_limit[0] & output_limit_bit_2) { fprintf(outfile, "\n"); } if (config.output_limit[0] & output_limit_bit_2) { fprintf(outfile, "Shared networks:\n"); fprintf(outfile, "name max cur percent touch t+c t+c perc"); if (0 < num_backups) { fprintf(outfile, " bu bu perc"); } fprintf(outfile, "\n"); } if (config.output_limit[1] & output_limit_bit_2) { for (i = 0; i < num_shared_networks; i++) { shared_p++; fprintf(outfile, "%-20s %5lu %5lu %10.3f %7lu %6lu %9.3f", shared_p->name, shared_p->available, shared_p->used, (float) (100 * shared_p->used) / shared_p->available, shared_p->touched, shared_p->touched + shared_p->used, (float) (100 * (shared_p->touched + shared_p->used)) / shared_p->available); if (0 < num_backups) { fprintf(outfile, "%7lu %8.3f", shared_p->backups, (float) (100 * shared_p->backups) / shared_p->available); } fprintf(outfile, "\n"); } } if (config.output_limit[1] & output_limit_bit_2 && config.output_limit[0] & output_limit_bit_3) { fprintf(outfile, "\n"); } if (config.output_limit[0] & output_limit_bit_3) { fprintf(outfile, "Sum of all ranges:\n"); fprintf(outfile, "name max cur percent touch t+c t+c perc"); if (0 < num_backups) { fprintf(outfile, " bu bu perc"); } fprintf(outfile, "\n"); } if (config.output_limit[1] & output_limit_bit_3) { fprintf(outfile, "%-20s %5lu %5lu %10.3f %7lu %6lu %9.3f", shared_networks->name, shared_networks->available, shared_networks->used, (float) (100 * shared_networks->used) / shared_networks->available, shared_networks->touched, shared_networks->touched + shared_networks->used, (float) (100 * (shared_networks->touched + shared_networks->used)) / shared_networks->available); if (0 < num_backups) { fprintf(outfile, "%7lu %8.3f", shared_networks->backups, (float) (100 * shared_networks->backups) / shared_networks->available); } fprintf(outfile, "\n"); } if (outfile == stdout) { ret = fflush(stdout); if (ret) { warn("output_txt: fflush"); } } else { ret = fclose(outfile); if (ret) { warn("output_txt: fclose"); } } return 0; } int output_xml(void) { unsigned int i; struct in_addr first, last; struct range_t *range_p; struct shared_network_t *shared_p; int ret; FILE *outfile; if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { err(EXIT_FAILURE, "output_xml: %s", config.output_file); } } else { outfile = stdout; } range_p = ranges; shared_p = shared_networks; if (config.output_limit[1] & output_limit_bit_1) { for (i = 0; i < num_ranges; i++) { first.s_addr = ntohl(range_p->first_ip + 1); last.s_addr = ntohl(range_p->last_ip - 1); fprintf(outfile, "\n"); if (range_p->shared_net) { fprintf(outfile, "\t%s\n", range_p->shared_net->name); } else { fprintf(outfile, "\t\n"); } fprintf(outfile, "\t\n"); fprintf(outfile, "\t\n"); fprintf(outfile, "\t%s ", inet_ntoa(first)); fprintf(outfile, "- %s\n", inet_ntoa(last)); fprintf(outfile, "\t\n"); fprintf(outfile, "\t%lu\n", range_p->last_ip - range_p->first_ip - 1); fprintf(outfile, "\t%lu\n", range_p->count); fprintf(outfile, "\t%lu\n", range_p->last_ip - range_p->first_ip - 1 - range_p->count); range_p++; fprintf(outfile, "\n"); } } if (config.output_limit[1] & output_limit_bit_2) { for (i = 0; i < num_shared_networks; i++) { shared_p++; fprintf(outfile, "\n"); fprintf(outfile, "\t%s\n", shared_p->name); fprintf(outfile, "\t%lu\n", shared_p->available); fprintf(outfile, "\t%lu\n", shared_p->used); fprintf(outfile, "\t%lu\n", shared_p->available - shared_p->used); fprintf(outfile, "\n"); } } if (config.output_limit[0] & output_limit_bit_3) { fprintf(outfile, "\n"); fprintf(outfile, "\t%s\n", shared_networks->name); fprintf(outfile, "\t%lu\n", shared_networks->available); fprintf(outfile, "\t%lu\n", shared_networks->used); fprintf(outfile, "\t%lu\n", shared_networks->available - shared_networks->used); fprintf(outfile, "\n"); } if (outfile == stdout) { ret = fflush(stdout); if (ret) { warn("output_xml: fflush"); } } else { ret = fclose(outfile); if (ret) { warn("output_xml: fclose"); } } return 0; } void html_header(FILE * f) { fprintf(f, "\n"); fprintf(f, "\n"); fprintf(f, "\n"); fprintf(f, "\n"); fprintf(f, " ISC dhcpd stats\n"); fprintf(f, " \n"); fprintf(f, "\n"); fprintf(f, "\n"); } void html_footer(FILE * f) { fprintf(f, "


\n"); fprintf(f, "
\n"); fprintf(f, "

\nData generated by "); fprintf(f, "", PACKAGE_URL); fprintf(f, "dhcpd-pools.\n

\n"); fprintf(f, "

\n"); fprintf(f, "\n
\n

\n"); fprintf(f, "\n"); fprintf(f, "\n"); } void newrow(FILE * f) { fprintf(f, "\n"); } void endrow(FILE * f) { fprintf(f, "\n\n"); } void output_line(FILE * f, char *type, char *class, char *text) { fprintf(f, " <%s class=%s>%s\n", type, class, text, type); } void output_long(FILE * f, char *type, unsigned long unlong) { fprintf(f, " <%s class=ralign>%lu\n", type, unlong, type); } void output_float(FILE * f, char *type, float fl) { fprintf(f, " <%s class=ralign>%.3f\n", type, fl, type); } void table_start(FILE * f) { fprintf(f, "\n"); } void table_end(FILE * f) { fprintf(f, "
\n"); } void newsection(FILE * f, char *title) { newrow(f); output_line(f, "td", "calign", " "); endrow(f); newrow(f); output_line(f, "th", "section", title); endrow(f); } int output_html(void) { unsigned int i; struct in_addr first, last; struct range_t *range_p; struct shared_network_t *shared_p; int ret; FILE *outfile; if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { err(EXIT_FAILURE, "output_html: %s", config.output_file); } } else { outfile = stdout; } range_p = ranges; shared_p = shared_networks; if (fullhtml) { html_header(outfile); } table_start(outfile); if (config.output_limit[0] & output_limit_bit_1) { newsection(outfile, "Ranges:"); newrow(outfile); output_line(outfile, "th", "calign", "shared net name"); output_line(outfile, "th", "calign", "first ip"); output_line(outfile, "th", "calign", "last ip"); output_line(outfile, "th", "ralign", "max"); output_line(outfile, "th", "ralign", "cur"); output_line(outfile, "th", "ralign", "percent"); output_line(outfile, "th", "ralign", "touch"); output_line(outfile, "th", "ralign", "t+c"); output_line(outfile, "th", "ralign", "t+c perc"); if (0 < num_backups) { output_line(outfile, "th", "ralign", "bu"); output_line(outfile, "th", "ralign", "bu perc"); } endrow(outfile); } if (config.output_limit[1] & output_limit_bit_1) { for (i = 0; i < num_ranges; i++) { first.s_addr = ntohl(range_p->first_ip + 1); last.s_addr = ntohl(range_p->last_ip - 1); newrow(outfile); if (range_p->shared_net) { output_line(outfile, "td", "calign", range_p->shared_net->name); } else { output_line(outfile, "td", "calign", "not_defined"); } output_line(outfile, "td", "calign", inet_ntoa(first)); output_line(outfile, "td", "calign", inet_ntoa(last)); output_long(outfile, "td", range_p->last_ip - range_p->first_ip - 1); output_long(outfile, "td", range_p->count); output_float(outfile, "td", (float) (100 * range_p->count) / (range_p->last_ip - range_p->first_ip - 1)); output_long(outfile, "td", range_p->touched); output_long(outfile, "td", range_p->touched + range_p->count); output_float(outfile, "td", (float) (100 * (range_p->touched + range_p->count)) / (range_p->last_ip - range_p->first_ip - 1)); if (0 < num_backups) { output_long(outfile, "td", range_p->backups); output_float(outfile, "td", (float) (100 * range_p->backups) / (range_p->last_ip - range_p->first_ip - 1)); } endrow(outfile); range_p++; } } table_end(outfile); table_start(outfile); if (config.output_limit[0] & output_limit_bit_2) { newsection(outfile, "Shared networks:"); newrow(outfile); output_line(outfile, "th", "calign", "name"); output_line(outfile, "th", "ralign", "max"); output_line(outfile, "th", "ralign", "cur"); output_line(outfile, "th", "ralign", "percent"); output_line(outfile, "th", "ralign", "touch"); output_line(outfile, "th", "ralign", "t+c"); output_line(outfile, "th", "ralign", "t+c perc"); if (0 < num_backups) { output_line(outfile, "th", "ralign", "bu"); output_line(outfile, "th", "ralign", "bu perc"); } endrow(outfile); } if (config.output_limit[1] & output_limit_bit_2) { for (i = 0; i < num_shared_networks; i++) { shared_p++; newrow(outfile); output_line(outfile, "td", "calign", shared_p->name); output_long(outfile, "td", shared_p->available); output_long(outfile, "td", shared_p->used); output_float(outfile, "td", (float) (100 * shared_p->used) / shared_p->available); output_long(outfile, "td", shared_p->touched); output_long(outfile, "td", shared_p->touched + shared_p->used); output_float(outfile, "td", (float) (100 * (shared_p->touched + shared_p->used)) / shared_p->available); if (0 < num_backups) { output_long(outfile, "td", shared_p->backups); output_float(outfile, "td", (float) (100 * shared_p->backups) / shared_p->available); } endrow(outfile); } } if (config.output_limit[0] & output_limit_bit_3) { newsection(outfile, "Sum of all ranges:"); newrow(outfile); output_line(outfile, "th", "calign", "name"); output_line(outfile, "th", "ralign", "max"); output_line(outfile, "th", "ralign", "cur"); output_line(outfile, "th", "ralign", "percent"); output_line(outfile, "th", "ralign", "touch"); output_line(outfile, "th", "ralign", "t+c"); output_line(outfile, "th", "ralign", "t+c perc"); if (0 < num_backups) { output_line(outfile, "th", "ralign", "bu"); output_line(outfile, "th", "ralign", "bu perc"); } endrow(outfile); } if (config.output_limit[1] & output_limit_bit_3) { newrow(outfile); output_line(outfile, "td", "calign", shared_networks->name); output_long(outfile, "td", shared_networks->available); output_long(outfile, "td", shared_networks->used); output_float(outfile, "td", (float) (100 * shared_networks->used) / shared_networks->available); output_long(outfile, "td", shared_networks->touched); output_long(outfile, "td", shared_networks->touched + shared_networks->used); output_float(outfile, "td", (float) (100 * (shared_networks->touched + shared_networks->used)) / shared_networks->available); if (0 < num_backups) { output_long(outfile, "td", shared_networks->backups); output_float(outfile, "td", (float) (100 * shared_networks->backups) / shared_networks->available); } endrow(outfile); } table_end(outfile); if (fullhtml) { html_footer(outfile); } if (outfile == stdout) { ret = fflush(stdout); if (ret) { warn("output_html: fflush"); } } else { ret = fclose(outfile); if (ret) { warn("output_html: fclose"); } } return 0; } int output_csv(void) { unsigned int i; struct in_addr first, last; struct range_t *range_p; struct shared_network_t *shared_p; FILE *outfile; int ret; if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { err(EXIT_FAILURE, "output_csv: %s", config.output_file); } } else { outfile = stdout; } range_p = ranges; shared_p = shared_networks; if (config.output_limit[0] & output_limit_bit_1) { fprintf(outfile, "\"Ranges:\"\n"); fprintf (outfile, "\"shared net name\",\"first ip\",\"last ip\",\"max\",\"cur\",\"percent\",\"touch\",\"t+c\",\"t+c perc\""); if (0 < num_backups) { fprintf(outfile, ",\"bu\",\"bu perc\""); } fprintf(outfile, "\n"); } if (config.output_limit[1] & output_limit_bit_1) { for (i = 0; i < num_ranges; i++) { first.s_addr = ntohl(range_p->first_ip + 1); last.s_addr = ntohl(range_p->last_ip - 1); if (range_p->shared_net) { fprintf(outfile, "\"%s\",", range_p->shared_net->name); } else { fprintf(outfile, "\"not_defined\","); } fprintf(outfile, "\"%s\",", inet_ntoa(first)); fprintf(outfile, "\"%s\",\"%lu\",\"%lu\",\"%.3f\",\"%lu\",\"%lu\",\"%.3f\"", inet_ntoa(last), range_p->last_ip - range_p->first_ip - 1, range_p->count, (float) (100 * range_p->count) / (range_p->last_ip - range_p->first_ip - 1), range_p->touched, range_p->touched + range_p->count, (float) (100 * (range_p->touched + range_p->count)) / (range_p->last_ip - range_p->first_ip - 1)); if (0 < num_backups) { fprintf(outfile, ",\"%lu\",\"%.3f\"", range_p->backups, (float) (100 * range_p->backups) / (range_p->last_ip - range_p->first_ip - 1)); } fprintf(outfile, "\n"); range_p++; } fprintf(outfile, "\n"); } if (config.output_limit[0] & output_limit_bit_2) { fprintf(outfile, "\"Shared networks:\"\n"); fprintf(outfile, "\"name\",\"max\",\"cur\",\"percent\",\"touch\",\"t+c\",\"t+c perc\""); if (0 < num_backups) { fprintf(outfile, ",\"bu\",\"bu perc\""); } fprintf(outfile, "\n"); } if (config.output_limit[1] & output_limit_bit_2) { for (i = 0; i < num_shared_networks; i++) { shared_p++; fprintf(outfile, "\"%s\",\"%lu\",\"%lu\",\"%.3f\",\"%lu\",\"%lu\",\"%.3f\"", shared_p->name, shared_p->available, shared_p->used, (float) (100 * shared_p->used) / shared_p->available, shared_p->touched, shared_p->touched + shared_p->used, (float) (100 * (shared_p->touched + shared_p->used)) / shared_p->available); if (0 < num_backups) { fprintf(outfile, ",\"%lu\",\"%.3f\"", shared_p->backups, (float) (100 * shared_p->backups) / shared_p->available); } fprintf(outfile, "\n"); } fprintf(outfile, "\n"); } if (config.output_limit[0] & output_limit_bit_3) { fprintf(outfile, "\"Sum of all ranges:\"\n"); fprintf(outfile, "\"name\",\"max\",\"cur\",\"percent\",\"touch\",\"t+c\",\"t+c perc\""); if (0 < num_backups) { fprintf(outfile, ",\"bu\",\"bu perc\""); } fprintf(outfile, "\n"); } if (config.output_limit[1] & output_limit_bit_3) { fprintf(outfile, "\"%s\",\"%lu\",\"%lu\",\"%.3f\",\"%lu\",\"%lu\",\"%.3f\"", shared_networks->name, shared_networks->available, shared_networks->used, (float) (100 * shared_networks->used) / shared_networks->available, shared_networks->touched, shared_networks->touched + shared_networks->used, (float) (100 * (shared_networks->touched + shared_networks->used)) / shared_networks->available); if (0 < num_backups) { fprintf(outfile, "%7lu %8.3f", shared_networks->backups, (float) (100 * shared_networks->backups) / shared_networks->available); } fprintf(outfile, "\n"); } if (outfile == stdout) { ret = fflush(stdout); if (ret) { warn("output_cvs: fflush"); } } else { ret = fclose(outfile); if (ret) { warn("output_cvs: fclose"); } } return 0; }