2 ** Copyright (C) 2006- Sami Kerola <kerolasa@iki.fi>
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
28 #include "dhcpd-pools.h"
33 struct in_addr first, last;
34 struct range_t *range_p;
35 struct shared_network_t *shared_p;
39 if (config.output_file[0]) {
40 outfile = fopen(config.output_file, "w+");
41 if (outfile == NULL) {
42 eprintf("output_txt: %s:", config.output_file);
50 shared_p = shared_networks;
52 if (config.output_limit[0] & output_limit_bit_1) {
53 fprintf(outfile, "Ranges:\n");
56 "shared net name first ip last ip max cur percent touch t+c t+c perc");
57 if (num_backups > 0) {
58 fprintf(outfile, " bu bu perc");
60 fprintf(outfile, "\n");
62 if (config.output_limit[1] & output_limit_bit_1) {
63 for (i = 0; i < num_ranges; i++) {
64 first.s_addr = ntohl(range_p->first_ip + 1);
65 last.s_addr = ntohl(range_p->last_ip - 1);
67 if (range_p->shared_net) {
68 fprintf(outfile, "%-20s",
69 range_p->shared_net->name);
71 fprintf(outfile, "not_defined ");
73 fprintf(outfile, "%-16s", inet_ntoa(first));
75 " - %-16s %5lu %5lu %10.3f %5lu %5lu %9.3f",
77 range_p->last_ip - range_p->first_ip - 1,
79 (float) (100 * range_p->count) /
80 (range_p->last_ip - range_p->first_ip - 1),
82 range_p->touched + range_p->count,
86 (range_p->last_ip - range_p->first_ip -
88 if (num_backups > 0) {
89 fprintf(outfile, "%7lu %8.3f",
91 (float) (100 * range_p->backups) /
93 range_p->first_ip - 1));
95 fprintf(outfile, "\n");
99 if (config.output_limit[1] & output_limit_bit_1
100 && config.output_limit[0] & output_limit_bit_2) {
101 fprintf(outfile, "\n");
103 if (config.output_limit[0] & output_limit_bit_2) {
104 fprintf(outfile, "Shared networks:\n");
106 "name max cur percent touch t+c t+c perc");
107 if (num_backups > 0) {
108 fprintf(outfile, " bu bu perc");
110 fprintf(outfile, "\n");
112 if (config.output_limit[1] & output_limit_bit_2) {
113 for (i = 0; i < num_shared_networks; i++) {
116 "%-20s %5lu %5lu %10.3f %7lu %6lu %9.3f",
117 shared_p->name, shared_p->available,
119 (float) (100 * shared_p->used) /
120 shared_p->available, shared_p->touched,
121 shared_p->touched + shared_p->used,
125 shared_p->available);
126 if (num_backups > 0) {
127 fprintf(outfile, "%7lu %8.3f",
129 (float) (100 * shared_p->backups) /
130 shared_p->available);
133 fprintf(outfile, "\n");
136 if (config.output_limit[1] & output_limit_bit_2
137 && config.output_limit[0] & output_limit_bit_3) {
138 fprintf(outfile, "\n");
140 if (config.output_limit[0] & output_limit_bit_3) {
141 fprintf(outfile, "Sum of all ranges:\n");
143 "name max cur percent touch t+c t+c perc");
145 if (num_backups > 0) {
146 fprintf(outfile, " bu bu perc");
148 fprintf(outfile, "\n");
150 if (config.output_limit[1] & output_limit_bit_3) {
151 fprintf(outfile, "%-20s %5lu %5lu %10.3f %7lu %6lu %9.3f",
152 shared_networks->name,
153 shared_networks->available,
154 shared_networks->used,
155 (float) (100 * shared_networks->used) /
156 shared_networks->available,
157 shared_networks->touched,
158 shared_networks->touched + shared_networks->used,
160 (shared_networks->touched +
161 shared_networks->used)) /
162 shared_networks->available);
164 if (num_backups > 0) {
165 fprintf(outfile, "%7lu %8.3f",
166 shared_networks->backups,
167 (float) (100 * shared_networks->backups) /
168 shared_networks->available);
170 fprintf(outfile, "\n");
172 if (outfile == stdout) {
173 ret = fflush(stdout);
175 eprintf("output_txt: fflush:");
178 ret = fclose(outfile);
180 eprintf("output_txt: fclose:");
190 struct in_addr first, last;
191 struct range_t *range_p;
192 struct shared_network_t *shared_p;
196 if (config.output_file[0]) {
197 outfile = fopen(config.output_file, "w+");
198 if (outfile == NULL) {
199 eprintf("output_xml: %s:", config.output_file);
207 shared_p = shared_networks;
209 if (config.output_limit[1] & output_limit_bit_1) {
210 for (i = 0; i < num_ranges; i++) {
211 first.s_addr = ntohl(range_p->first_ip + 1);
212 last.s_addr = ntohl(range_p->last_ip - 1);
214 fprintf(outfile, "<subnet>\n");
216 if (range_p->shared_net) {
218 "\t<location>%s</location>\n",
219 range_p->shared_net->name);
222 "\t<location></location>\n");
225 fprintf(outfile, "\t<network></network>\n");
226 fprintf(outfile, "\t<netmask></netmask>\n");
227 fprintf(outfile, "\t<range>%s ", inet_ntoa(first));
228 fprintf(outfile, "- %s</range>\n",
230 fprintf(outfile, "\t<gateway></gateway>\n");
231 fprintf(outfile, "\t<defined>%lu</defined>\n",
232 range_p->last_ip - range_p->first_ip - 1);
233 fprintf(outfile, "\t<used>%lu</used>\n",
235 fprintf(outfile, "\t<free>%lu</free>\n",
236 range_p->last_ip - range_p->first_ip - 1 -
241 fprintf(outfile, "</subnet>\n");
245 if (config.output_limit[1] & output_limit_bit_2) {
246 for (i = 0; i < num_shared_networks; i++) {
249 fprintf(outfile, "<shared-network>\n");
250 fprintf(outfile, "\t<location>%s</location>\n",
252 fprintf(outfile, "\t<defined>%lu</defined>\n",
253 shared_p->available);
254 fprintf(outfile, "\t<used>%lu</used>\n",
256 fprintf(outfile, "\t<free>%lu</free>\n",
257 shared_p->available - shared_p->used);
258 fprintf(outfile, "</shared-network>\n");
262 if (config.output_limit[0] & output_limit_bit_3) {
263 fprintf(outfile, "<summary>\n");
264 fprintf(outfile, "\t<location>%s</location>\n",
265 shared_networks->name);
266 fprintf(outfile, "\t<defined>%lu</defined>\n",
267 shared_networks->available);
268 fprintf(outfile, "\t<used>%lu</used>\n",
269 shared_networks->used);
270 fprintf(outfile, "\t<free>%lu</free>\n",
271 shared_networks->available -
272 shared_networks->used);
273 fprintf(outfile, "</summary>\n");
276 if (outfile == stdout) {
277 ret = fflush(stdout);
279 eprintf("output_xml: fflush:");
282 ret = fclose(outfile);
284 eprintf("output_xml: fclose:");
291 void html_header(FILE * f)
294 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \n");
295 fprintf(f, " \"http://www.w3.org/TR/html4/strict.dtd\">\n");
296 fprintf(f, "<html>\n");
297 fprintf(f, "<head>\n");
298 fprintf(f, "<meta http-equiv=\"Content-Type\" ");
299 fprintf(f, "content=\"text/html; charset=iso-8859-1\">\n");
300 fprintf(f, " <title>ISC dhcpd stats</title>\n");
301 fprintf(f, " <style TYPE=\"text/css\">\n");
302 fprintf(f, " <!--\n");
303 fprintf(f, " table.dhcpd-pools {\n");
304 fprintf(f, " color: black;\n");
305 fprintf(f, " vertical-align: middle;\n");
306 fprintf(f, " text-align: center;\n");
308 fprintf(f, " table.dhcpd-pools th.section {\n");
309 fprintf(f, " color: black;\n");
310 fprintf(f, " font-size: large;\n");
311 fprintf(f, " vertical-align: middle;\n");
312 fprintf(f, " text-align: left;\n");
314 fprintf(f, " table.dhcpd-pools th.calign {\n");
315 fprintf(f, " color: black;\n");
316 fprintf(f, " vertical-align: middle;\n");
317 fprintf(f, " text-align: center;\n");
318 fprintf(f, " text-decoration: underline;\n");
320 fprintf(f, " table.dhcpd-pools th.ralign {\n");
321 fprintf(f, " color: black;\n");
322 fprintf(f, " vertical-align: middle;\n");
323 fprintf(f, " text-align: right;\n");
324 fprintf(f, " text-decoration: underline;\n");
326 fprintf(f, " table.dhcpd-pools td.calign {\n");
327 fprintf(f, " color: black;\n");
328 fprintf(f, " vertical-align: middle;\n");
329 fprintf(f, " text-align: center;\n");
331 fprintf(f, " table.dhcpd-pools td.ralign {\n");
332 fprintf(f, " color: black;\n");
333 fprintf(f, " vertical-align: middle;\n");
334 fprintf(f, " text-align: right;\n");
336 fprintf(f, " p.created {\n");
337 fprintf(f, " font-size: small;\n");
338 fprintf(f, " color: grey;\n");
340 fprintf(f, " p.updated {\n");
341 fprintf(f, " font-size: small;\n");
342 fprintf(f, " color: grey;\n");
343 fprintf(f, " font-style: italic;\n");
345 fprintf(f, " -->\n");
346 fprintf(f, " </style>\n");
347 fprintf(f, "</head>\n");
348 fprintf(f, "<body>\n");
351 void html_footer(FILE * f)
353 fprintf(f, "<p><br></p>\n");
354 fprintf(f, "<hr>\n");
355 fprintf(f, "<p class=created>\nData generated by ");
356 fprintf(f, "<a href=\"%s\">", PACKAGE_URL);
357 fprintf(f, "dhcpd-pools</a>.\n</p>\n");
359 fprintf(f, "<p class=updated>\n");
360 fprintf(f, "<script type=\"text/javascript\">\n");
361 fprintf(f, " document.write(\"Last Updated On \" + ");
362 fprintf(f, "document.lastModified + \".\")\n");
363 fprintf(f, "</script>\n<br>\n</p>\n");
364 fprintf(f, "</body>\n");
365 fprintf(f, "</html>\n");
368 void newrow(FILE * f)
370 fprintf(f, "<tr>\n");
373 void endrow(FILE * f)
375 fprintf(f, "</tr>\n\n");
378 void output_line(FILE * f, char *type, char *class, char *text)
380 fprintf(f, " <%s class=%s>%s</%s>\n", type, class, text, type);
383 void output_long(FILE * f, char *type, unsigned long unlong)
385 fprintf(f, " <%s class=ralign>%lu</%s>\n", type, unlong, type);
388 void output_float(FILE * f, char *type, float fl)
390 fprintf(f, " <%s class=ralign>%.3f</%s>\n", type, fl, type);
393 void table_start(FILE * f)
395 fprintf(f, "<table width=\"75%%\" ");
396 fprintf(f, "class=\"dhcpd-pools\" ");
397 fprintf(f, "summary=\"ISC dhcpd pool usage report\">\n");
400 void table_end(FILE * f)
402 fprintf(f, "</table>\n");
405 void newsection(FILE * f, char *title)
408 output_line(f, "td", "calign", " ");
412 output_line(f, "th", "section", title);
416 int output_html(void)
419 struct in_addr first, last;
420 struct range_t *range_p;
421 struct shared_network_t *shared_p;
425 if (config.output_file[0]) {
426 outfile = fopen(config.output_file, "w+");
427 if (outfile == NULL) {
428 eprintf("output_html: %s:", config.output_file);
436 shared_p = shared_networks;
439 html_header(outfile);
441 table_start(outfile);
442 if (config.output_limit[0] & output_limit_bit_1) {
443 newsection(outfile, "Ranges:");
445 output_line(outfile, "th", "calign", "shared net name");
446 output_line(outfile, "th", "calign", "first ip");
447 output_line(outfile, "th", "calign", "last ip");
448 output_line(outfile, "th", "ralign", "max");
449 output_line(outfile, "th", "ralign", "cur");
450 output_line(outfile, "th", "ralign", "percent");
451 output_line(outfile, "th", "ralign", "touch");
452 output_line(outfile, "th", "ralign", "t+c");
453 output_line(outfile, "th", "ralign", "t+c perc");
454 if (num_backups > 0) {
455 output_line(outfile, "th", "ralign", "bu");
456 output_line(outfile, "th", "ralign", "bu perc");
460 if (config.output_limit[1] & output_limit_bit_1) {
461 for (i = 0; i < num_ranges; i++) {
462 first.s_addr = ntohl(range_p->first_ip + 1);
463 last.s_addr = ntohl(range_p->last_ip - 1);
466 if (range_p->shared_net) {
467 output_line(outfile, "td", "calign",
468 range_p->shared_net->name);
470 output_line(outfile, "td", "calign",
473 output_line(outfile, "td", "calign",
475 output_line(outfile, "td", "calign",
477 output_long(outfile, "td",
478 range_p->last_ip - range_p->first_ip -
480 output_long(outfile, "td", range_p->count);
481 output_float(outfile, "td",
482 (float) (100 * range_p->count) /
484 range_p->first_ip - 1));
485 output_long(outfile, "td", range_p->touched);
486 output_long(outfile, "td",
487 range_p->touched + range_p->count);
488 output_float(outfile, "td",
493 range_p->first_ip - 1));
494 if (num_backups > 0) {
495 output_long(outfile, "td",
497 output_float(outfile, "td",
501 range_p->first_ip - 1));
508 table_start(outfile);
509 if (config.output_limit[0] & output_limit_bit_2) {
510 newsection(outfile, "Shared networks:");
513 output_line(outfile, "th", "calign", "name");
514 output_line(outfile, "th", "ralign", "max");
515 output_line(outfile, "th", "ralign", "cur");
516 output_line(outfile, "th", "ralign", "percent");
517 output_line(outfile, "th", "ralign", "touch");
518 output_line(outfile, "th", "ralign", "t+c");
519 output_line(outfile, "th", "ralign", "t+c perc");
520 if (num_backups > 0) {
521 output_line(outfile, "th", "ralign", "bu");
522 output_line(outfile, "th", "ralign", "bu perc");
526 if (config.output_limit[1] & output_limit_bit_2) {
527 for (i = 0; i < num_shared_networks; i++) {
530 output_line(outfile, "td", "calign",
532 output_long(outfile, "td", shared_p->available);
533 output_long(outfile, "td", shared_p->used);
534 output_float(outfile, "td",
535 (float) (100 * shared_p->used) /
536 shared_p->available);
537 output_long(outfile, "td", shared_p->touched);
538 output_long(outfile, "td",
539 shared_p->touched + shared_p->used);
540 output_float(outfile, "td",
544 shared_p->available);
545 if (num_backups > 0) {
546 output_long(outfile, "td",
548 output_float(outfile, "td",
551 shared_p->available);
557 if (config.output_limit[0] & output_limit_bit_3) {
558 newsection(outfile, "Sum of all ranges:");
561 output_line(outfile, "th", "calign", "name");
562 output_line(outfile, "th", "ralign", "max");
563 output_line(outfile, "th", "ralign", "cur");
564 output_line(outfile, "th", "ralign", "percent");
565 output_line(outfile, "th", "ralign", "touch");
566 output_line(outfile, "th", "ralign", "t+c");
567 output_line(outfile, "th", "ralign", "t+c perc");
568 if (num_backups > 0) {
569 output_line(outfile, "th", "ralign", "bu");
570 output_line(outfile, "th", "ralign", "bu perc");
575 if (config.output_limit[1] & output_limit_bit_3) {
577 output_line(outfile, "td", "calign",
578 shared_networks->name);
579 output_long(outfile, "td", shared_networks->available);
580 output_long(outfile, "td", shared_networks->used);
581 output_float(outfile, "td",
582 (float) (100 * shared_networks->used) /
583 shared_networks->available);
584 output_long(outfile, "td", shared_networks->touched);
585 output_long(outfile, "td",
586 shared_networks->touched +
587 shared_networks->used);
588 output_float(outfile, "td",
590 (shared_networks->touched +
591 shared_networks->used)) /
592 shared_networks->available);
593 if (num_backups > 0) {
594 output_long(outfile, "td",
595 shared_networks->backups);
596 output_float(outfile, "td",
598 shared_networks->backups) /
599 shared_networks->available);
605 html_footer(outfile);
607 if (outfile == stdout) {
608 ret = fflush(stdout);
610 eprintf("output_html: fflush:");
613 ret = fclose(outfile);
615 eprintf("output_html: fclose:");
624 struct in_addr first, last;
625 struct range_t *range_p;
626 struct shared_network_t *shared_p;
630 if (config.output_file[0]) {
631 outfile = fopen(config.output_file, "w+");
632 if (outfile == NULL) {
633 eprintf("output_csv: %s:", config.output_file);
641 shared_p = shared_networks;
643 if (config.output_limit[0] & output_limit_bit_1) {
644 fprintf(outfile, "\"Ranges:\"\n");
647 "\"shared net name\",\"first ip\",\"last ip\",\"max\",\"cur\",\"percent\",\"touch\",\"t+c\",\"t+c perc\"");
648 if (num_backups > 0) {
649 fprintf(outfile, ",\"bu\",\"bu perc\"");
651 fprintf(outfile, "\n");
654 if (config.output_limit[1] & output_limit_bit_1) {
655 for (i = 0; i < num_ranges; i++) {
656 first.s_addr = ntohl(range_p->first_ip + 1);
657 last.s_addr = ntohl(range_p->last_ip - 1);
659 if (range_p->shared_net) {
660 fprintf(outfile, "\"%s\",",
661 range_p->shared_net->name);
663 fprintf(outfile, "\"not_defined\",");
665 fprintf(outfile, "\"%s\",", inet_ntoa(first));
667 "\"%s\",\"%lu\",\"%lu\",\"%.3f\",\"%lu\",\"%lu\",\"%.3f\"",
669 range_p->last_ip - range_p->first_ip - 1,
671 (float) (100 * range_p->count) /
672 (range_p->last_ip - range_p->first_ip - 1),
674 range_p->touched + range_p->count,
678 (range_p->last_ip - range_p->first_ip -
680 if (num_backups > 0) {
681 fprintf(outfile, ",\"%lu\",\"%.3f\"",
683 (float) (100 * range_p->backups) /
685 range_p->first_ip - 1));
689 fprintf(outfile, "\n");
692 fprintf(outfile, "\n");
694 if (config.output_limit[0] & output_limit_bit_2) {
695 fprintf(outfile, "\"Shared networks:\"\n");
697 "\"name\",\"max\",\"cur\",\"percent\",\"touch\",\"t+c\",\"t+c perc\"");
698 if (num_backups > 0) {
699 fprintf(outfile, ",\"bu\",\"bu perc\"");
701 fprintf(outfile, "\n");
703 if (config.output_limit[1] & output_limit_bit_2) {
705 for (i = 0; i < num_shared_networks; i++) {
708 "\"%s\",\"%lu\",\"%lu\",\"%.3f\",\"%lu\",\"%lu\",\"%.3f\"",
709 shared_p->name, shared_p->available,
711 (float) (100 * shared_p->used) /
712 shared_p->available, shared_p->touched,
713 shared_p->touched + shared_p->used,
717 shared_p->available);
718 if (num_backups > 0) {
719 fprintf(outfile, ",\"%lu\",\"%.3f\"",
721 (float) (100 * shared_p->backups) /
722 shared_p->available);
725 fprintf(outfile, "\n");
729 fprintf(outfile, "\n");
731 if (config.output_limit[0] & output_limit_bit_3) {
732 fprintf(outfile, "\"Sum of all ranges:\"\n");
734 "\"name\",\"max\",\"cur\",\"percent\",\"touch\",\"t+c\",\"t+c perc\"");
735 if (num_backups > 0) {
736 fprintf(outfile, ",\"bu\",\"bu perc\"");
738 fprintf(outfile, "\n");
740 if (config.output_limit[1] & output_limit_bit_3) {
743 "\"%s\",\"%lu\",\"%lu\",\"%.3f\",\"%lu\",\"%lu\",\"%.3f\"",
744 shared_networks->name, shared_networks->available,
745 shared_networks->used,
746 (float) (100 * shared_networks->used) /
747 shared_networks->available,
748 shared_networks->touched,
749 shared_networks->touched + shared_networks->used,
751 (shared_networks->touched +
752 shared_networks->used)) /
753 shared_networks->available);
754 if (num_backups > 0) {
755 fprintf(outfile, "%7lu %8.3f",
756 shared_networks->backups,
757 (float) (100 * shared_networks->backups) /
758 shared_networks->available);
760 fprintf(outfile, "\n");
763 if (outfile == stdout) {
764 ret = fflush(stdout);
766 eprintf("output_cvs: fflush:");
770 ret = fclose(outfile);
772 eprintf("output_cvs: fclose:");