summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Stapelberg <stapelberg@users.noreply.github.com>2016-07-30 19:23:20 +0200
committerGitHub <noreply@github.com>2016-07-30 19:23:20 +0200
commit8e852fa94554f4caa162256e5f6ce2413027100b (patch)
tree373a3fbf687ff7fb8d5d0ee405b46dd8736d499f
parentdd0f6bf4b76e6431b3d51abceef42fd60bfb16b5 (diff)
parentdbc0f9bd8c3561eb7b9c2d7c13518cb825695d23 (diff)
Merge pull request #139 from Gjum/wb-colored-cpu-usage
Add CPU usage color thresholds
-rw-r--r--i3status.c5
-rw-r--r--include/i3status.h2
-rw-r--r--man/i3status.man15
-rw-r--r--src/print_cpu_usage.c14
4 files changed, 32 insertions, 4 deletions
diff --git a/i3status.c b/i3status.c
index e5e85bf..892d4aa 100644
--- a/i3status.c
+++ b/i3status.c
@@ -412,7 +412,10 @@ int main(int argc, char *argv[]) {
cfg_opt_t usage_opts[] = {
CFG_STR("format", "%usage", CFGF_NONE),
+ CFG_FLOAT("max_threshold", 95, CFGF_NONE),
+ CFG_FLOAT("degraded_threshold", 90, CFGF_NONE),
CFG_CUSTOM_ALIGN_OPT,
+ CFG_CUSTOM_COLOR_OPTS,
CFG_CUSTOM_MIN_WIDTH_OPT,
CFG_CUSTOM_SEPARATOR_OPT,
CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
@@ -725,7 +728,7 @@ int main(int argc, char *argv[]) {
CASE_SEC("cpu_usage") {
SEC_OPEN_MAP("cpu_usage");
- print_cpu_usage(json_gen, buffer, cfg_getstr(sec, "format"));
+ print_cpu_usage(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getfloat(sec, "max_threshold"), cfg_getfloat(sec, "degraded_threshold"));
SEC_CLOSE_MAP;
}
}
diff --git a/include/i3status.h b/include/i3status.h
index 7e756e2..b4cf83f 100644
--- a/include/i3status.h
+++ b/include/i3status.h
@@ -216,7 +216,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down);
void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down);
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, const float max_threshold, const float degraded_threshold);
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, const float max_threshold);
void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *fmt_muted, const char *device, const char *mixer, int mixer_idx);
diff --git a/man/i3status.man b/man/i3status.man
index 26ea831..32bc6e6 100644
--- a/man/i3status.man
+++ b/man/i3status.man
@@ -381,12 +381,25 @@ specified thermal zone is getting too hot. Defaults to 75 degrees C.
=== CPU Usage
-Gets the percentual CPU usage from +/proc/stat+ (Linux) or +sysctl(3)+ (FreeBSD/OpenBSD).
+Gets the percentual CPU usage from +/proc/stat+ (Linux) or +sysctl(3)+
+(FreeBSD/OpenBSD).
+
+It is possible to define a max_threshold that will color the load
+value red in case the CPU average over the last interval is getting
+higher than the configured threshold. Defaults to 95.
+
+It is possible to define a degraded_threshold that will color the load
+value yellow in case the CPU average over the last interval is getting
+higher than the configured threshold. Defaults to 90.
*Example order*: +cpu_usage+
*Example format*: +%usage+
+*Example max_threshold*: +75+
+
+*Example degraded_threshold*: +25+
+
=== Load
Gets the system load (number of processes waiting for CPU time in the last
diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c
index adf2d04..1753cf5 100644
--- a/src/print_cpu_usage.c
+++ b/src/print_cpu_usage.c
@@ -41,11 +41,12 @@ static int prev_idle = 0;
* percentage.
*
*/
-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, const float max_threshold, const float degraded_threshold) {
const char *walk;
char *outwalk = buffer;
int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total;
int diff_idle, diff_total, diff_usage;
+ bool colorful_output = false;
#if defined(LINUX)
static char statpath[512];
@@ -101,10 +102,21 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
continue;
}
+ if (diff_usage >= max_threshold) {
+ START_COLOR("color_bad");
+ colorful_output = true;
+ } else if (diff_usage >= degraded_threshold) {
+ START_COLOR("color_degraded");
+ colorful_output = true;
+ }
+
if (BEGINS_WITH(walk + 1, "usage")) {
outwalk += sprintf(outwalk, "%02d%s", diff_usage, pct_mark);
walk += strlen("usage");
}
+
+ if (colorful_output)
+ END_COLOR;
}
OUTPUT_FULL_TEXT(buffer);