i3status/src/print_load.c

58 lines
1.6 KiB
C
Raw Normal View History

// vim:ts=4:sw=4:expandtab
#include "i3status.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
2012-03-25 18:55:55 +00:00
#include <yajl/yajl_gen.h>
2012-04-08 12:05:47 +00:00
#include <yajl/yajl_version.h>
2013-07-07 15:54:50 +00:00
void print_load(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold) {
char *outwalk = buffer;
/* Get load */
2012-11-14 01:29:55 +00:00
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__)
double loadavg[3];
const char *walk;
bool colorful_output = false;
if (getloadavg(loadavg, 3) == -1)
goto error;
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (loadavg[0] >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
if (BEGINS_WITH(walk + 1, "1min")) {
outwalk += sprintf(outwalk, "%1.2f", loadavg[0]);
walk += strlen("1min");
}
if (BEGINS_WITH(walk + 1, "5min")) {
outwalk += sprintf(outwalk, "%1.2f", loadavg[1]);
walk += strlen("5min");
}
if (BEGINS_WITH(walk + 1, "15min")) {
outwalk += sprintf(outwalk, "%1.2f", loadavg[2]);
walk += strlen("15min");
}
if (colorful_output)
END_COLOR;
}
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
return;
2011-08-25 21:24:06 +00:00
error:
#endif
OUTPUT_FULL_TEXT("cant read load");
(void)fputs("i3status: Cannot read system load using getloadavg()\n", stderr);
}