From c029c7581a2068ec3f1627c8145f9ac2e5e21014 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sun, 12 Nov 2017 22:09:19 +0000 Subject: [PATCH] output: make warning and critical colors work in html output Users should combine this with --color=always to web pages to work correctly. Signed-off-by: Sami Kerola --- man/dhcpd-pools.1.in | 4 +-- src/dhcpd-pools.h | 57 +++++++++++++++++---------------- src/output.c | 75 +++++++++++++++++++++++++++++--------------- 3 files changed, 81 insertions(+), 55 deletions(-) diff --git a/man/dhcpd-pools.1.in b/man/dhcpd-pools.1.in index e5f014e..e0d50da 100644 --- a/man/dhcpd-pools.1.in +++ b/man/dhcpd-pools.1.in @@ -88,8 +88,8 @@ Text .RI ( t ). Full-html .RI ( H ) -page output. -The +page output. In html page critical and warning thresholds can be visualized +with \-\-color=always option. The .RI ( c ) stands for comma-separated values. Output format xml .RI ( x ) diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index 170f099..258649a 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -108,6 +108,35 @@ enum prefix_t { NUM_OF_PREFIX }; +/*! \enum output_formats + * \brief Enumeration of output formats. Keep the text and html first, they + * are used color array selector. + */ +enum output_formats { + OUT_FORM_TEXT, + OUT_FORM_HTML, + NUM_OF_OUT_FORMS +}; + +/*! \enum count_status_t + * \brief Enumeration of possible range and shared net statuses. + */ +enum count_status_t { + STATUS_OK, + STATUS_WARN, + STATUS_CRIT, + STATUS_IGNORED, + STATUS_SUPPRESSED, + COLOR_RESET +}; + +enum color_mode { + color_unknown, + color_off, + color_on, + color_auto /* default */ +}; + /*! \struct shared_network_t * \brief Counters for an individual shared network. */ @@ -133,17 +162,6 @@ struct range_t { double backups; }; -/*! \enum count_status_t - * \brief Enumeration of possible range and shared net statuses. - */ -enum count_status_t { - STATUS_OK, - STATUS_WARN, - STATUS_CRIT, - STATUS_IGNORED, - STATUS_SUPPRESSED -}; - /*! \struct output_helper_t * \brief Various per range and shared net temporary calculation results. */ @@ -204,21 +222,6 @@ enum limbits { # define STATE_WARNING 1 # define STATE_CRITICAL 2 -/*! \def COLOR_BOLD_RED - * \brief Shell warning color. - */ -# define COLOR_BOLD_RED "\033[1;31m" -# define COLOR_BOLD_YELLOW "\033[1;33m" -# define COLOR_BOLD_GREEN "\033[1;32m" -# define COLOR_BOLD_BLUE "\033[1;34m" -# define COLOR_RESET "\033[0m" -enum color_mode { - color_unknown, - color_off, - color_on, - color_auto /* default */ -}; - /*! \var comparer_t * \brief Function pointer holding sort algorithm. */ @@ -246,7 +249,7 @@ struct conf_t { enum dhcp_version ip_version; const char *dhcpdconf_file; const char *dhcpdlease_file; - const int output_format; + int output_format; struct output_sort *sorts; const char *output_file; const char *mustach_template; diff --git a/src/output.c b/src/output.c index a0d5946..69689ec 100644 --- a/src/output.c +++ b/src/output.c @@ -59,6 +59,17 @@ #include "dhcpd-pools.h" +static const char *color_tags[][NUM_OF_OUT_FORMS] = { + [STATUS_OK] = { "", "" }, + [STATUS_WARN] = { "\033[1;33m", "" }, + [STATUS_CRIT] = { "\033[1;31m", "" }, + [STATUS_IGNORED] = { "\033[1;32m", "" }, + [STATUS_SUPPRESSED] = { "\033[1;34m", "" }, + [COLOR_RESET] = { "\033[0m", "" } +}; + + + /*! \brief Calculate range percentages and such. */ int range_output_helper(struct conf_t *state, struct output_helper_t *oh, struct range_t *range_p) @@ -124,25 +135,13 @@ int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh, } -static int start_color(struct output_helper_t *oh, FILE *outfile) +static int start_color(struct conf_t *state, struct output_helper_t *oh, FILE *outfile) { - if (oh->status == STATUS_CRIT) { - fputs(COLOR_BOLD_RED, outfile); - return 1; + if (oh->status == STATUS_OK) { + return 0; } - if (oh->status == STATUS_WARN) { - fputs(COLOR_BOLD_YELLOW, outfile); - return 1; - } - if (oh->status == STATUS_IGNORED) { - fputs(COLOR_BOLD_GREEN, outfile); - return 1; - } - if (oh->status == STATUS_SUPPRESSED) { - fputs(COLOR_BOLD_BLUE, outfile); - return 1; - } - return 0; + fputs(color_tags[oh->status][state->output_format], outfile); + return 1; } static FILE *open_outfile(struct conf_t *state) @@ -212,7 +211,7 @@ static int output_txt(struct conf_t *state) continue; } if (state->color_mode == color_on) - color_set = start_color(&oh, outfile); + color_set = start_color(state, &oh, outfile); if (range_p->shared_net) { fprintf(outfile, "%-20s", range_p->shared_net->name); } else { @@ -237,7 +236,7 @@ static int output_txt(struct conf_t *state) fprintf(outfile, "%7g %8.3f", range_p->backups, oh.bup); } if (color_set) - fputs(COLOR_RESET, outfile); + fputs(color_tags[COLOR_RESET][state->output_format], outfile); fprintf(outfile, "\n"); range_p++; } @@ -261,7 +260,7 @@ static int output_txt(struct conf_t *state) if (shnet_output_helper(state, &oh, shared_p)) continue; if (state->color_mode == color_on) - color_set = start_color(&oh, outfile); + color_set = start_color(state, &oh, outfile); fprintf(outfile, "%-20s %5g %5g %10.3f %7g %6g %9.3f", shared_p->name, @@ -275,7 +274,7 @@ static int output_txt(struct conf_t *state) fprintf(outfile, "%7g %8.3f", shared_p->backups, oh.bup); } if (color_set) - fputs(COLOR_RESET, outfile); + fputs(color_tags[COLOR_RESET][state->output_format], outfile); fprintf(outfile, "\n"); } } @@ -297,7 +296,7 @@ static int output_txt(struct conf_t *state) shnet_output_helper(state, &oh, state->shared_net_root); if (state->color_mode == color_on) - color_set = start_color(&oh, outfile); + color_set = start_color(state, &oh, outfile); fprintf(outfile, "%-20s %5g %5g %10.3f %7g %6g %9.3f", state->shared_net_root->name, state->shared_net_root->available, @@ -311,7 +310,7 @@ static int output_txt(struct conf_t *state) fprintf(outfile, "%7g %8.3f", state->shared_net_root->backups, oh.bup); } if (color_set) - fputs(COLOR_RESET, outfile); + fputs(color_tags[COLOR_RESET][state->output_format], outfile); fprintf(outfile, "\n"); } close_outfile(outfile); @@ -661,6 +660,28 @@ static void output_double(FILE *restrict f, char const *restrict type, double d) fprintf(f, "<%s>%g\n", type, d, type); } +/*! \brief Line with a potentially colored digit in html output format. + * + * \param state Runtime configuration state. + * \param f Output file descriptor. + * \param type HMTL tag name. + * \param d Actual payload of the printout. + */ +static void output_double_color(struct conf_t *state, + struct output_helper_t *oh, FILE *restrict f, + char const *restrict type) +{ + int color_set = 0; + + fprintf(f, "<%s>", type); + if (state->color_mode == color_on) + color_set = start_color(state, oh, f); + fprintf(f, "%g", oh->percent); + if (color_set) + fputs(color_tags[COLOR_RESET][state->output_format], f); + fprintf(f, "\n", type); +} + /*! \brief Line with float in html output format. * * \param f Output file descriptor. @@ -778,7 +799,7 @@ static int output_html(struct conf_t *state) output_line(outfile, "td", shared_p->name); output_double(outfile, "td", shared_p->available); output_double(outfile, "td", shared_p->used); - output_float(outfile, "td", oh.percent); + output_double_color(state, &oh, outfile, "td"); output_double(outfile, "td", shared_p->touched); output_double(outfile, "td", oh.tc); output_float(outfile, "td", oh.tcp); @@ -801,7 +822,7 @@ static int output_html(struct conf_t *state) output_line(outfile, "th", "last ip"); output_line(outfile, "th", "max"); output_line(outfile, "th", "cur"); - output_line(outfile, "th", "percent"); + output_double_color(state, &oh, outfile, "td"); output_line(outfile, "th", "touch"); output_line(outfile, "th", "t+c"); output_line(outfile, "th", "t+c perc"); @@ -829,7 +850,7 @@ static int output_html(struct conf_t *state) output_line(outfile, "td", ntop_ipaddr(&range_p->last_ip)); output_double(outfile, "td", oh.range_size); output_double(outfile, "td", range_p->count); - output_float(outfile, "td", oh.percent); + output_double_color(state, &oh, outfile, "td"); output_double(outfile, "td", range_p->touched); output_double(outfile, "td", oh.tc); output_float(outfile, "td", oh.tcp); @@ -1119,6 +1140,7 @@ int output_analysis(struct conf_t *state, const char output_format) switch (output_format) { case 't': + state->output_format = OUT_FORM_TEXT; ret = output_txt(state); break; case 'a': @@ -1128,6 +1150,7 @@ int output_analysis(struct conf_t *state, const char output_format) error(EXIT_FAILURE, 0, "html table only output format is deprecated"); break; case 'H': + state->output_format = OUT_FORM_HTML; ret = output_html(state); break; case 'x':