static int absolute_value_from_relative_part_factor( int range_begin, int range_end, double relative_part_factor) { int range_size = get_range_size( range_begin, range_end); float round_factor = 0.5; if( relative_part_factor < 0) round_factor = -0.5; return range_begin + (int)((relative_part_factor * range_size) + round_factor); }
/*! \brief The json output formats. * FIXME: This function should return void. */ int output_json(void) { unsigned int i = 0; struct range_t *range_p; double range_size; struct shared_network_t *shared_p; int ret; FILE *outfile; unsigned int sep; if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { err(EXIT_FAILURE, "output_json: %s", config.output_file); } } else { outfile = stdout; } range_p = ranges; range_size = get_range_size(range_p); shared_p = shared_networks; sep = 0; fprintf(outfile, "{\n"); if (config.output_format[0] == 'X' || config.output_format[0] == 'J') { struct leases_t *l; fprintf(outfile, " \"active_leases\": ["); for (l = leases; l != NULL; l = l->hh.next) { if (l->type == ACTIVE) { if (i == 0) { i = 1; } else { fputc(',', outfile); } fputs("\n { \"ip\":\"", outfile); fputs(ntop_ipaddr(&l->ip), outfile); fputs("\", \"macaddress\":\"", outfile); if (l->ethernet != NULL) { fputs(l->ethernet, outfile); } fputs("\" }", outfile); } } fprintf(outfile, "\n ]"); /* end of active_leases */ sep++; } if (config.output_limit[1] & BIT1) { if (sep) { fprintf(outfile, ",\n"); } fprintf(outfile, " \"subnets\": [\n"); for (i = 0; i < num_ranges; i++) { fprintf(outfile, " "); fprintf(outfile, "{ "); if (range_p->shared_net) { fprintf(outfile, "\"location\":\"%s\", ", range_p->shared_net->name); } else { fprintf(outfile, "\"location\":\"\", "); } fprintf(outfile, "\"range\":\"%s", ntop_ipaddr(&range_p->first_ip)); fprintf(outfile, " - %s\", ", ntop_ipaddr(&range_p->last_ip)); fprintf(outfile, "\"defined\":%g, ", range_size); fprintf(outfile, "\"used\":%g, ", range_p->count); fprintf(outfile, "\"free\":%g ", range_size - range_p->count); range_p++; range_size = get_range_size(range_p); if (i + 1 < num_ranges) fprintf(outfile, "},\n"); else fprintf(outfile, "}\n"); } fprintf(outfile, " ]"); /* end of subnets */ sep++; } if (config.output_limit[1] & BIT2) { if (sep) { fprintf(outfile, ",\n"); } fprintf(outfile, " \"shared-networks\": [\n"); for (i = 0; i < num_shared_networks; i++) { fprintf(outfile, " "); shared_p++; fprintf(outfile, "{ "); fprintf(outfile, "\"location\":\"%s\", ", shared_p->name); fprintf(outfile, "\"defined\":%g, ", shared_p->available); fprintf(outfile, "\"used\":%g, ", shared_p->used); fprintf(outfile, "\"free\":%g ", shared_p->available - shared_p->used); if (i + 1 < num_shared_networks) fprintf(outfile, "},\n"); else fprintf(outfile, "}\n"); } fprintf(outfile, " ]"); /* end of shared-networks */ sep++; } if (config.output_limit[0] & BIT3) { 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, " \"free\":%g\n", shared_networks->available - shared_networks->used); fprintf(outfile, " }"); /* end of summary */ sep++; } fprintf(outfile, "\n}\n"); if (outfile == stdout) { ret = fflush(stdout); if (ret) { warn("output_json: fflush"); } } else { ret = close_stream(outfile); if (ret) { warn("output_json: fclose"); } } return 0; }
/*! \brief Text output format, which is the default. * FIXME: This function should return void. */ int output_txt(void) { unsigned int i; struct range_t *range_p; double range_size; struct shared_network_t *shared_p; int ret; FILE *outfile; int max_ipaddr_length = config.dhcp_version == VERSION_6 ? 39 : 16; 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; range_size = get_range_size(range_p); shared_p = shared_networks; if (config.output_limit[0] & BIT1) { fprintf(outfile, "Ranges:\n"); fprintf (outfile, "%-20s%-*s %-*s %5s %5s %10s %5s %5s %9s", "shared net name", max_ipaddr_length, "first ip", max_ipaddr_length, "last ip", "max", "cur", "percent", "touch", "t+c", "t+c perc"); if (config.backups_found == true) { fprintf(outfile, " bu bu perc"); } fprintf(outfile, "\n"); } if (config.output_limit[1] & BIT1) { for (i = 0; i < num_ranges; i++) { if (range_p->shared_net) { fprintf(outfile, "%-20s", range_p->shared_net->name); } else { fprintf(outfile, "not_defined "); } /* Outputting of first_ip and last_ip need to be * separate since ntop_ipaddr always returns the * same buffer */ fprintf(outfile, "%-*s", max_ipaddr_length, ntop_ipaddr(&range_p->first_ip)); fprintf(outfile, " - %-*s %5g %5g %10.3f %5g %5g %9.3f", max_ipaddr_length, ntop_ipaddr(&range_p->last_ip), range_size, range_p->count, (float)(100 * range_p->count) / range_size, range_p->touched, range_p->touched + range_p->count, (float)(100 * (range_p->touched + range_p->count)) / range_size); if (config.backups_found == true) { fprintf(outfile, "%7g %8.3f", range_p->backups, (float)(100 * range_p->backups) / range_size); } fprintf(outfile, "\n"); range_p++; range_size = get_range_size(range_p); } } if (config.output_limit[1] & BIT1 && config.output_limit[0] & BIT2) { fprintf(outfile, "\n"); } if (config.output_limit[0] & BIT2) { fprintf(outfile, "Shared networks:\n"); fprintf(outfile, "name max cur percent touch t+c t+c perc"); if (config.backups_found == true) { fprintf(outfile, " bu bu perc"); } fprintf(outfile, "\n"); } if (config.output_limit[1] & BIT2) { for (i = 0; i < num_shared_networks; i++) { shared_p++; fprintf(outfile, "%-20s %5g %5g %10.3f %7g %6g %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 (config.backups_found == true) { fprintf(outfile, "%7g %8.3f", shared_p->backups, (float)(100 * shared_p->backups) / shared_p->available); } fprintf(outfile, "\n"); } } if (config.output_limit[1] & BIT2 && config.output_limit[0] & BIT3) { fprintf(outfile, "\n"); } if (config.output_limit[0] & BIT3) { fprintf(outfile, "Sum of all ranges:\n"); fprintf(outfile, "name max cur percent touch t+c t+c perc"); if (config.backups_found == true) { fprintf(outfile, " bu bu perc"); } fprintf(outfile, "\n"); } if (config.output_limit[1] & BIT3) { fprintf(outfile, "%-20s %5g %5g %10.3f %7g %6g %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 (config.backups_found == true) { fprintf(outfile, "%7g %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 = close_stream(outfile); if (ret) { warn("output_txt: fclose"); } } return 0; }
/*! \brief The xml output formats. * FIXME: This function should return void. */ int output_xml(void) { unsigned int i; struct range_t *range_p; double range_size; 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; range_size = get_range_size(range_p); shared_p = shared_networks; fprintf(outfile, "<dhcpstatus>\n"); if (config.output_format[0] == 'X' || config.output_format[0] == 'J') { struct leases_t *l; for (l = leases; l != NULL; l = l->hh.next) { if (l->type == ACTIVE) { fputs("<active_lease>\n\t<ip>", outfile); fputs(ntop_ipaddr(&l->ip), outfile); fputs("</ip>\n\t<macaddress>", outfile); if (l->ethernet != NULL) { fputs(l->ethernet, outfile); } fputs("</macaddress>\n</active_lease>\n", outfile); } } } if (config.output_limit[1] & BIT1) { for (i = 0; i < num_ranges; i++) { fprintf(outfile, "<subnet>\n"); if (range_p->shared_net) { fprintf(outfile, "\t<location>%s</location>\n", range_p->shared_net->name); } else { fprintf(outfile, "\t<location></location>\n"); } fprintf(outfile, "\t<network></network>\n"); fprintf(outfile, "\t<netmask></netmask>\n"); fprintf(outfile, "\t<range>%s ", ntop_ipaddr(&range_p->first_ip)); fprintf(outfile, "- %s</range>\n", ntop_ipaddr(&range_p->last_ip)); fprintf(outfile, "\t<gateway></gateway>\n"); fprintf(outfile, "\t<defined>%g</defined>\n", range_size); fprintf(outfile, "\t<used>%g</used>\n", range_p->count); fprintf(outfile, "\t<free>%g</free>\n", range_size - range_p->count); range_p++; range_size = get_range_size(range_p); fprintf(outfile, "</subnet>\n"); } } if (config.output_limit[1] & BIT2) { for (i = 0; i < num_shared_networks; i++) { shared_p++; fprintf(outfile, "<shared-network>\n"); fprintf(outfile, "\t<location>%s</location>\n", shared_p->name); fprintf(outfile, "\t<defined>%g</defined>\n", shared_p->available); fprintf(outfile, "\t<used>%g</used>\n", shared_p->used); fprintf(outfile, "\t<free>%g</free>\n", shared_p->available - shared_p->used); fprintf(outfile, "</shared-network>\n"); } } if (config.output_limit[0] & BIT3) { 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<free>%g</free>\n", shared_networks->available - shared_networks->used); fprintf(outfile, "</summary>\n"); } fprintf(outfile, "</dhcpstatus>\n"); if (outfile == stdout) { ret = fflush(stdout); if (ret) { warn("output_xml: fflush"); } } else { ret = close_stream(outfile); if (ret) { warn("output_xml: fclose"); } } return 0; }