Add colorized output for load avg

This commit is contained in:
Raphael Michel 2012-12-31 18:13:36 +01:00 committed by Michael Stapelberg
parent 67c2c1a7ea
commit 6279964c6b
4 changed files with 17 additions and 4 deletions

View File

@ -257,6 +257,8 @@ int main(int argc, char *argv[]) {
cfg_opt_t load_opts[] = { cfg_opt_t load_opts[] = {
CFG_STR("format", "%1min %5min %15min", CFGF_NONE), CFG_STR("format", "%1min %5min %15min", CFGF_NONE),
CFG_INT("max_threshold", 5, CFGF_NONE),
CFG_CUSTOM_COLOR_OPTS,
CFG_END() CFG_END()
}; };
@ -460,7 +462,7 @@ int main(int argc, char *argv[]) {
CASE_SEC("load") { CASE_SEC("load") {
SEC_OPEN_MAP("load"); SEC_OPEN_MAP("load");
print_load(json_gen, buffer, cfg_getstr(sec, "format")); print_load(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getint(sec, "max_threshold"));
SEC_CLOSE_MAP; SEC_CLOSE_MAP;
} }

View File

@ -151,7 +151,7 @@ void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const c
void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int); void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format); void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down); void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
void print_load(yajl_gen json_gen, char *buffer, const char *format); void print_load(yajl_gen json_gen, char *buffer, const char *format, const int max_threshold);
void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *device, const char *mixer, int mixer_idx); void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *device, const char *mixer, int mixer_idx);
bool process_runs(const char *path); bool process_runs(const char *path);

View File

@ -259,12 +259,16 @@ Gets the percentual CPU usage from +/proc/stat+ (Linux) or +sysctl(3)+ (FreeBSD/
=== Load === Load
Gets the system load (number of processes waiting for CPU time in the last Gets the system load (number of processes waiting for CPU time in the last
1, 5 and 15 minutes). 1, 5 and 15 minutes). It is possible to define a max_threshold that will
color the load value red in case the load average of the last minute is
getting higher than the configured threshold. Defaults to 5.
*Example order*: +load+ *Example order*: +load+
*Example format*: +%1min %5min %15min+ *Example format*: +%1min %5min %15min+
*Example max_threshold*: 5
=== Time === Time
Outputs the current time in the local timezone. Outputs the current time in the local timezone.

View File

@ -6,13 +6,14 @@
#include <yajl/yajl_gen.h> #include <yajl/yajl_gen.h>
#include <yajl/yajl_version.h> #include <yajl/yajl_version.h>
void print_load(yajl_gen json_gen, char *buffer, const char *format) { void print_load(yajl_gen json_gen, char *buffer, const char *format, const int max_threshold) {
char *outwalk = buffer; char *outwalk = buffer;
/* Get load */ /* Get load */
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__) #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__)
double loadavg[3]; double loadavg[3];
const char *walk; const char *walk;
bool colorful_output = false;
if (getloadavg(loadavg, 3) == -1) if (getloadavg(loadavg, 3) == -1)
goto error; goto error;
@ -22,6 +23,10 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format) {
*(outwalk++) = *walk; *(outwalk++) = *walk;
continue; continue;
} }
if (loadavg[0] >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
if (BEGINS_WITH(walk+1, "1min")) { if (BEGINS_WITH(walk+1, "1min")) {
outwalk += sprintf(outwalk, "%1.2f", loadavg[0]); outwalk += sprintf(outwalk, "%1.2f", loadavg[0]);
@ -37,6 +42,8 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format) {
outwalk += sprintf(outwalk, "%1.2f", loadavg[2]); outwalk += sprintf(outwalk, "%1.2f", loadavg[2]);
walk += strlen("15min"); walk += strlen("15min");
} }
if (colorful_output)
END_COLOR;
} }
*outwalk = '\0'; *outwalk = '\0';