disk: Colorize output when below given threshold

New disk module options:
* threshold_type: ^(percentage|[kmgt]?bytes)_(free|avail)$
* low_threshold: <double>

fixes #912
This commit is contained in:
Mats 2014-03-08 00:24:42 +01:00 committed by Michael Stapelberg
parent 1de12e7b20
commit bc0bd8c9e0
4 changed files with 78 additions and 3 deletions

View File

@ -382,7 +382,10 @@ int main(int argc, char *argv[]) {
cfg_opt_t disk_opts[] = {
CFG_STR("format", "%free", CFGF_NONE),
CFG_STR("prefix_type", "binary", CFGF_NONE),
CFG_STR("threshold_type", "percentage_avail", CFGF_NONE),
CFG_FLOAT("low_threshold", 0, CFGF_NONE),
CFG_CUSTOM_ALIGN_OPT,
CFG_CUSTOM_COLOR_OPTS,
CFG_CUSTOM_MIN_WIDTH_OPT,
CFG_END()
};
@ -601,7 +604,7 @@ int main(int argc, char *argv[]) {
CASE_SEC_TITLE("disk") {
SEC_OPEN_MAP("disk_info");
print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "prefix_type"));
print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "prefix_type"), cfg_getstr(sec, "threshold_type"), cfg_getfloat(sec, "low_threshold"));
SEC_CLOSE_MAP;
}

View File

@ -174,7 +174,7 @@ char *auto_detect_format();
void set_timezone(const char *tz);
void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type);
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type, const char *threshold_type, const double low_threshold);
void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds);
void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *tz, time_t t);
void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);

View File

@ -223,6 +223,16 @@ SI prefixes (k, M, G, T) represent multiples of powers of 1000.
custom::
The custom prefixes (K, M, G, T) represent multiples of powers of 1024.
It is possible to define a low_threshold that causes the disk text to be
displayed using color_bad. The low_threshold type can be of threshold_type
"bytes_free", "bytes_avail", "percentage_free", or "percentage_avail", where
the former two can be prepended by a generic prefix (k, m, g, t) having
prefix_type. So, if you configure low_threshold to 2, threshold_type to
"gbytes_avail", and prefix_type to "binary", and the remaining available disk
space is below 2 GiB, it will be colored bad. If not specified, threshold_type
is assumed to be "percentage_avail" and low_threshold to be set to 0, which
implies no coloring at all.
*Example order*: +disk /mnt/usbstick+
*Example format*: +%free (%avail)/ %total+
@ -231,6 +241,10 @@ The custom prefixes (K, M, G, T) represent multiples of powers of 1024.
*Example prefix_type*: +custom+
*Example low_threshold*: +5+
*Example threshold_type*: +percentage_free+
=== Run-watch
Expands the given path to a pidfile and checks if the process ID found inside

View File

@ -52,14 +52,64 @@ static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_t
}
}
/*
* Determines whether remaining bytes are below given threshold.
*
*/
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
#else
static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
#endif
if (strcasecmp(threshold_type, "percentage_free") == 0) {
return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
} else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
} else if (strcasecmp(threshold_type, "bytes_free") == 0) {
return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
} else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
} else if (threshold_type[0] != '\0' && strncasecmp(threshold_type+1, "bytes_", strlen("bytes_")) == 0) {
uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
double factor = 1;
switch (threshold_type[0]) {
case 'T':
case 't':
factor *= base;
case 'G':
case 'g':
factor *= base;
case 'M':
case 'm':
factor *= base;
case 'K':
case 'k':
factor *= base;
break;
default:
return false;
}
if (strcasecmp(threshold_type+1, "bytes_free") == 0) {
return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
} else if (strcasecmp(threshold_type+1, "bytes_avail") == 0) {
return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
}
}
return false;
}
/*
* Does a statvfs and prints either free, used or total amounts of bytes in a
* human readable manner.
*
*/
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type) {
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type, const char *threshold_type, const double low_threshold) {
const char *walk;
char *outwalk = buffer;
bool colorful_output = false;
INSTANCE(path);
@ -75,6 +125,11 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
return;
#endif
if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
START_COLOR("color_bad");
colorful_output = true;
}
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
@ -122,6 +177,9 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
}
}
if (colorful_output)
END_COLOR;
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
}