load: fix colors (+param struct)

This commit is contained in:
Michael Stapelberg 2021-11-02 21:25:15 +01:00
parent 6b2f4cd20c
commit 9db19ffa35
3 changed files with 32 additions and 12 deletions

View File

@ -802,7 +802,15 @@ 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"), cfg_getstr(sec, "format_above_threshold"), cfg_getfloat(sec, "max_threshold")); load_ctx_t ctx = {
.json_gen = json_gen,
.buf = buffer,
.buflen = sizeof(buffer),
.format = cfg_getstr(sec, "format"),
.format_above_threshold = cfg_getstr(sec, "format_above_threshold"),
.max_threshold = cfg_getfloat(sec, "max_threshold"),
};
print_load(&ctx);
SEC_CLOSE_MAP; SEC_CLOSE_MAP;
} }

View File

@ -350,7 +350,17 @@ typedef struct {
void print_eth_info(eth_info_ctx_t *ctx); void print_eth_info(eth_info_ctx_t *ctx);
void print_load(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const float max_threshold); typedef struct {
yajl_gen json_gen;
char *buf;
const size_t buflen;
const char *format;
const char *format_above_threshold;
const float max_threshold;
} load_ctx_t;
void print_load(load_ctx_t *ctx);
void print_memory(yajl_gen json_gen, char *buffer, const char *format, const char *format_degraded, const char *threshold_degraded, const char *threshold_critical, const char *memory_used_method, const char *unit, const int decimals); void print_memory(yajl_gen json_gen, char *buffer, const char *format, const char *format_degraded, const char *threshold_degraded, const char *threshold_critical, const char *memory_used_method, const char *unit, const int decimals);
typedef struct { typedef struct {

View File

@ -11,23 +11,24 @@
#define STRING_SIZE 10 #define STRING_SIZE 10
void print_load(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const float max_threshold) { void print_load(load_ctx_t *ctx) {
char *outwalk = buffer; char *outwalk = ctx->buf;
/* Get load */ /* Get load */
#define json_gen ctx->json_gen
#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 *selected_format = format; const char *selected_format = ctx->format;
bool colorful_output = false; bool colorful_output = false;
if (getloadavg(loadavg, 3) == -1) if (getloadavg(loadavg, 3) == -1)
goto error; goto error;
if (loadavg[0] >= max_threshold) { if (loadavg[0] >= ctx->max_threshold) {
START_COLOR("color_bad"); START_COLOR("color_bad");
colorful_output = true; colorful_output = true;
if (format_above_threshold != NULL) if (ctx->format_above_threshold != NULL)
selected_format = format_above_threshold; selected_format = ctx->format_above_threshold;
} }
char string_loadavg_1[STRING_SIZE]; char string_loadavg_1[STRING_SIZE];
@ -44,14 +45,15 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format, const char
{.name = "%15min", .value = string_loadavg_15}}; {.name = "%15min", .value = string_loadavg_15}};
const size_t num = sizeof(placeholders) / sizeof(placeholder_t); const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
buffer = format_placeholders(selected_format, &placeholders[0], num); char *formatted = format_placeholders(selected_format, &placeholders[0], num);
OUTPUT_FORMATTED;
free(formatted);
if (colorful_output) if (colorful_output)
END_COLOR; END_COLOR;
*outwalk = '\0'; *outwalk = '\0';
OUTPUT_FULL_TEXT(buffer); OUTPUT_FULL_TEXT(ctx->buf);
free(buffer);
return; return;
error: error: