output: add color support to text output

When --warning or --critical thresholds are defined with text output lines
that exceed threshold will be either yellow (warning) or red (critical).

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2017-11-05 17:07:16 +00:00
parent 48962004b8
commit 344ed2900d
No known key found for this signature in database
GPG key ID: A9553245FDE9B739
8 changed files with 128 additions and 28 deletions

View file

@ -50,6 +50,7 @@
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "close-stream.h"
#include "error.h"
@ -70,25 +71,19 @@ static void range_output_helper(struct output_helper_t *oh, struct range_t *rang
oh->bup = (double)(100 * range_p->backups) / oh->range_size;
}
/* set status */
if (config.snet_alarms && range_p->shared_net != shared_networks) {
oh->status = STATUS_SUPPRESSED;
return;
}
if (oh->range_size <= config.minsize) {
oh->status = STATUS_IGNORED;
return;
}
if (config.critical < oh->percent
&& (oh->range_size - range_p->count) < config.crit_count) {
oh->status = STATUS_CRIT;
return;
}
if (config.warning < oh->percent
&& (oh->range_size - range_p->count) < config.warn_count) {
oh->status = STATUS_WARN;
return;
}
oh->status = STATUS_OK;
if (config.critical < oh->percent
&& (oh->range_size - range_p->count) < config.crit_count)
oh->status = STATUS_CRIT;
else if (config.warning < oh->percent
&& (oh->range_size - range_p->count) < config.warn_count)
oh->status = STATUS_WARN;
if (oh->status != STATUS_OK) {
if (oh->range_size <= config.minsize)
oh->status = STATUS_IGNORED;
else if (config.snet_alarms && range_p->shared_net != shared_networks)
oh->status = STATUS_SUPPRESSED;
}
}
/*! \brief Calculate shared network percentages and such. */
@ -109,7 +104,11 @@ static void shnet_output_helper(struct output_helper_t *oh, struct shared_networ
}
}
/* set status */
if (oh->percent == NAN || shared_p->available <= config.minsize) {
if (oh->percent == NAN) {
oh->status = STATUS_SUPPRESSED;
return;
}
if (shared_p->available <= config.minsize) {
oh->status = STATUS_IGNORED;
return;
}
@ -124,6 +123,26 @@ static void shnet_output_helper(struct output_helper_t *oh, struct shared_networ
oh->status = STATUS_OK;
}
static int start_color(struct output_helper_t *oh, FILE *outfile)
{
if (oh->status == STATUS_CRIT) {
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;
}
/*! \brief Text output format, which is the default. */
static int output_txt(void)
{
@ -135,6 +154,10 @@ static int output_txt(void)
FILE *outfile;
int max_ipaddr_length = config.ip_version == IPv6 ? 39 : 16;
if (config.color_mode == color_auto && isatty(STDIN_FILENO)) {
config.color_mode = color_on;
}
if (config.output_file[0]) {
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
@ -164,7 +187,10 @@ static int output_txt(void)
}
if (config.number_limit & R_BIT) {
for (i = 0; i < num_ranges; i++) {
int color_set = 0;
range_output_helper(&oh, range_p);
if (config.color_mode == color_on)
color_set = start_color(&oh, outfile);
if (range_p->shared_net) {
fprintf(outfile, "%-20s", range_p->shared_net->name);
} else {
@ -189,6 +215,8 @@ static int output_txt(void)
fprintf(outfile, "%7g %8.3f",
range_p->backups, oh.bup);
}
if (color_set)
fputs(COLOR_RESET, outfile);
fprintf(outfile, "\n");
range_p++;
}
@ -207,8 +235,11 @@ static int output_txt(void)
}
if (config.number_limit & S_BIT) {
for (i = 0; i < num_shared_networks; i++) {
int color_set = 0;
shared_p++;
shnet_output_helper(&oh, shared_p);
if (config.color_mode == color_on)
color_set = start_color(&oh, outfile);
fprintf(outfile,
"%-20s %5g %5g %10.3f %7g %6g %9.3f",
shared_p->name,
@ -223,7 +254,8 @@ static int output_txt(void)
shared_p->backups,
oh.bup);
}
if (color_set)
fputs(COLOR_RESET, outfile);
fprintf(outfile, "\n");
}
}
@ -241,7 +273,10 @@ static int output_txt(void)
fprintf(outfile, "\n");
}
if (config.number_limit & A_BIT) {
int color_set = 0;
shnet_output_helper(&oh, shared_networks);
if (config.color_mode == color_on)
color_set = start_color(&oh, outfile);
fprintf(outfile, "%-20s %5g %5g %10.3f %7g %6g %9.3f",
shared_networks->name,
shared_networks->available,
@ -256,6 +291,8 @@ static int output_txt(void)
shared_networks->backups,
oh.bup);
}
if (color_set)
fputs(COLOR_RESET, outfile);
fprintf(outfile, "\n");
}
if (outfile == stdout) {