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 <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2017-11-12 22:09:19 +00:00
parent a64630aa49
commit c029c7581a
No known key found for this signature in database
GPG key ID: A9553245FDE9B739
3 changed files with 81 additions and 55 deletions

View file

@ -88,8 +88,8 @@ Text
.RI ( t ). .RI ( t ).
Full-html Full-html
.RI ( H ) .RI ( H )
page output. page output. In html page critical and warning thresholds can be visualized
The with \-\-color=always option. The
.RI ( c ) .RI ( c )
stands for comma-separated values. Output format xml stands for comma-separated values. Output format xml
.RI ( x ) .RI ( x )

View file

@ -108,6 +108,35 @@ enum prefix_t {
NUM_OF_PREFIX 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 /*! \struct shared_network_t
* \brief Counters for an individual shared network. * \brief Counters for an individual shared network.
*/ */
@ -133,17 +162,6 @@ struct range_t {
double backups; 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 /*! \struct output_helper_t
* \brief Various per range and shared net temporary calculation results. * \brief Various per range and shared net temporary calculation results.
*/ */
@ -204,21 +222,6 @@ enum limbits {
# define STATE_WARNING 1 # define STATE_WARNING 1
# define STATE_CRITICAL 2 # 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 /*! \var comparer_t
* \brief Function pointer holding sort algorithm. * \brief Function pointer holding sort algorithm.
*/ */
@ -246,7 +249,7 @@ struct conf_t {
enum dhcp_version ip_version; enum dhcp_version ip_version;
const char *dhcpdconf_file; const char *dhcpdconf_file;
const char *dhcpdlease_file; const char *dhcpdlease_file;
const int output_format; int output_format;
struct output_sort *sorts; struct output_sort *sorts;
const char *output_file; const char *output_file;
const char *mustach_template; const char *mustach_template;

View file

@ -59,6 +59,17 @@
#include "dhcpd-pools.h" #include "dhcpd-pools.h"
static const char *color_tags[][NUM_OF_OUT_FORMS] = {
[STATUS_OK] = { "", "" },
[STATUS_WARN] = { "\033[1;33m", "<font color=\"yellow\">" },
[STATUS_CRIT] = { "\033[1;31m", "<font color=\"red\">" },
[STATUS_IGNORED] = { "\033[1;32m", "<font color=\"green\">" },
[STATUS_SUPPRESSED] = { "\033[1;34m", "<font color=\"blue\">" },
[COLOR_RESET] = { "\033[0m", "</font>" }
};
/*! \brief Calculate range percentages and such. */ /*! \brief Calculate range percentages and such. */
int range_output_helper(struct conf_t *state, struct output_helper_t *oh, int range_output_helper(struct conf_t *state, struct output_helper_t *oh,
struct range_t *range_p) struct range_t *range_p)
@ -124,26 +135,14 @@ 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) { if (oh->status == STATUS_OK) {
fputs(COLOR_BOLD_RED, outfile);
return 1;
}
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; return 0;
} }
fputs(color_tags[oh->status][state->output_format], outfile);
return 1;
}
static FILE *open_outfile(struct conf_t *state) static FILE *open_outfile(struct conf_t *state)
{ {
@ -212,7 +211,7 @@ static int output_txt(struct conf_t *state)
continue; continue;
} }
if (state->color_mode == color_on) if (state->color_mode == color_on)
color_set = start_color(&oh, outfile); color_set = start_color(state, &oh, outfile);
if (range_p->shared_net) { if (range_p->shared_net) {
fprintf(outfile, "%-20s", range_p->shared_net->name); fprintf(outfile, "%-20s", range_p->shared_net->name);
} else { } else {
@ -237,7 +236,7 @@ static int output_txt(struct conf_t *state)
fprintf(outfile, "%7g %8.3f", range_p->backups, oh.bup); fprintf(outfile, "%7g %8.3f", range_p->backups, oh.bup);
} }
if (color_set) if (color_set)
fputs(COLOR_RESET, outfile); fputs(color_tags[COLOR_RESET][state->output_format], outfile);
fprintf(outfile, "\n"); fprintf(outfile, "\n");
range_p++; range_p++;
} }
@ -261,7 +260,7 @@ static int output_txt(struct conf_t *state)
if (shnet_output_helper(state, &oh, shared_p)) if (shnet_output_helper(state, &oh, shared_p))
continue; continue;
if (state->color_mode == color_on) if (state->color_mode == color_on)
color_set = start_color(&oh, outfile); color_set = start_color(state, &oh, outfile);
fprintf(outfile, fprintf(outfile,
"%-20s %5g %5g %10.3f %7g %6g %9.3f", "%-20s %5g %5g %10.3f %7g %6g %9.3f",
shared_p->name, 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); fprintf(outfile, "%7g %8.3f", shared_p->backups, oh.bup);
} }
if (color_set) if (color_set)
fputs(COLOR_RESET, outfile); fputs(color_tags[COLOR_RESET][state->output_format], outfile);
fprintf(outfile, "\n"); fprintf(outfile, "\n");
} }
} }
@ -297,7 +296,7 @@ static int output_txt(struct conf_t *state)
shnet_output_helper(state, &oh, state->shared_net_root); shnet_output_helper(state, &oh, state->shared_net_root);
if (state->color_mode == color_on) 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", fprintf(outfile, "%-20s %5g %5g %10.3f %7g %6g %9.3f",
state->shared_net_root->name, state->shared_net_root->name,
state->shared_net_root->available, 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); fprintf(outfile, "%7g %8.3f", state->shared_net_root->backups, oh.bup);
} }
if (color_set) if (color_set)
fputs(COLOR_RESET, outfile); fputs(color_tags[COLOR_RESET][state->output_format], outfile);
fprintf(outfile, "\n"); fprintf(outfile, "\n");
} }
close_outfile(outfile); close_outfile(outfile);
@ -661,6 +660,28 @@ static void output_double(FILE *restrict f, char const *restrict type, double d)
fprintf(f, "<%s>%g</%s>\n", type, d, type); fprintf(f, "<%s>%g</%s>\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, "</%s>\n", type);
}
/*! \brief Line with float in html output format. /*! \brief Line with float in html output format.
* *
* \param f Output file descriptor. * \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_line(outfile, "td", shared_p->name);
output_double(outfile, "td", shared_p->available); output_double(outfile, "td", shared_p->available);
output_double(outfile, "td", shared_p->used); 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", shared_p->touched);
output_double(outfile, "td", oh.tc); output_double(outfile, "td", oh.tc);
output_float(outfile, "td", oh.tcp); 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", "last ip");
output_line(outfile, "th", "max"); output_line(outfile, "th", "max");
output_line(outfile, "th", "cur"); 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", "touch");
output_line(outfile, "th", "t+c"); output_line(outfile, "th", "t+c");
output_line(outfile, "th", "t+c perc"); 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_line(outfile, "td", ntop_ipaddr(&range_p->last_ip));
output_double(outfile, "td", oh.range_size); output_double(outfile, "td", oh.range_size);
output_double(outfile, "td", range_p->count); 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", range_p->touched);
output_double(outfile, "td", oh.tc); output_double(outfile, "td", oh.tc);
output_float(outfile, "td", oh.tcp); output_float(outfile, "td", oh.tcp);
@ -1119,6 +1140,7 @@ int output_analysis(struct conf_t *state, const char output_format)
switch (output_format) { switch (output_format) {
case 't': case 't':
state->output_format = OUT_FORM_TEXT;
ret = output_txt(state); ret = output_txt(state);
break; break;
case 'a': 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"); error(EXIT_FAILURE, 0, "html table only output format is deprecated");
break; break;
case 'H': case 'H':
state->output_format = OUT_FORM_HTML;
ret = output_html(state); ret = output_html(state);
break; break;
case 'x': case 'x':