memory: fix colors (+param struct)

This commit is contained in:
Michael Stapelberg 2021-11-02 21:30:22 +01:00
parent 9db19ffa35
commit e57f14ffa1
3 changed files with 49 additions and 22 deletions

View File

@ -816,7 +816,19 @@ int main(int argc, char *argv[]) {
CASE_SEC("memory") {
SEC_OPEN_MAP("memory");
print_memory(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_degraded"), cfg_getstr(sec, "threshold_degraded"), cfg_getstr(sec, "threshold_critical"), cfg_getstr(sec, "memory_used_method"), cfg_getstr(sec, "unit"), cfg_getint(sec, "decimals"));
memory_ctx_t ctx = {
.json_gen = json_gen,
.buf = buffer,
.buflen = sizeof(buffer),
.format = cfg_getstr(sec, "format"),
.format_degraded = cfg_getstr(sec, "format_degraded"),
.threshold_degraded = cfg_getstr(sec, "threshold_degraded"),
.threshold_critical = cfg_getstr(sec, "threshold_critical"),
.memory_used_method = cfg_getstr(sec, "memory_used_method"),
.unit = cfg_getstr(sec, "unit"),
.decimals = cfg_getint(sec, "decimals"),
};
print_memory(&ctx);
SEC_CLOSE_MAP;
}

View File

@ -361,7 +361,20 @@ typedef struct {
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);
typedef struct {
yajl_gen json_gen;
char *buf;
const size_t buflen;
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;
} memory_ctx_t;
void print_memory(memory_ctx_t *ctx);
typedef struct {
yajl_gen json_gen;

View File

@ -86,11 +86,12 @@ static unsigned long memory_absolute(const char *mem_amount, const unsigned long
}
#endif
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) {
char *outwalk = buffer;
void print_memory(memory_ctx_t *ctx) {
char *outwalk = ctx->buf;
#define json_gen ctx->json_gen
#if defined(__linux__)
const char *selected_format = format;
const char *selected_format = ctx->format;
const char *output_color = NULL;
int unread_fields = 6;
@ -140,23 +141,23 @@ void print_memory(yajl_gen json_gen, char *buffer, const char *format, const cha
ram_shared *= 1024UL;
unsigned long ram_used;
if (BEGINS_WITH(memory_used_method, "memavailable")) {
if (BEGINS_WITH(ctx->memory_used_method, "memavailable")) {
ram_used = ram_total - ram_available;
} else if (BEGINS_WITH(memory_used_method, "classical")) {
} else if (BEGINS_WITH(ctx->memory_used_method, "classical")) {
ram_used = ram_total - ram_free - ram_buffers - ram_cached;
} else {
die("Unexpected value: memory_used_method = %s", memory_used_method);
die("Unexpected value: memory_used_method = %s", ctx->memory_used_method);
}
if (threshold_degraded) {
const unsigned long threshold = memory_absolute(threshold_degraded, ram_total);
if (ctx->threshold_degraded) {
const unsigned long threshold = memory_absolute(ctx->threshold_degraded, ram_total);
if (ram_available < threshold) {
output_color = "color_degraded";
}
}
if (threshold_critical) {
const unsigned long threshold = memory_absolute(threshold_critical, ram_total);
if (ctx->threshold_critical) {
const unsigned long threshold = memory_absolute(ctx->threshold_critical, ram_total);
if (ram_available < threshold) {
output_color = "color_bad";
}
@ -165,8 +166,8 @@ void print_memory(yajl_gen json_gen, char *buffer, const char *format, const cha
if (output_color) {
START_COLOR(output_color);
if (format_degraded)
selected_format = format_degraded;
if (ctx->format_degraded)
selected_format = ctx->format_degraded;
}
char string_ram_total[STRING_SIZE];
@ -179,11 +180,11 @@ void print_memory(yajl_gen json_gen, char *buffer, const char *format, const cha
char string_percentage_used[STRING_SIZE];
char string_percentage_shared[STRING_SIZE];
print_bytes_human(string_ram_total, ram_total, unit, decimals);
print_bytes_human(string_ram_used, ram_used, unit, decimals);
print_bytes_human(string_ram_free, ram_free, unit, decimals);
print_bytes_human(string_ram_available, ram_available, unit, decimals);
print_bytes_human(string_ram_shared, ram_shared, unit, decimals);
print_bytes_human(string_ram_total, ram_total, ctx->unit, ctx->decimals);
print_bytes_human(string_ram_used, ram_used, ctx->unit, ctx->decimals);
print_bytes_human(string_ram_free, ram_free, ctx->unit, ctx->decimals);
print_bytes_human(string_ram_available, ram_available, ctx->unit, ctx->decimals);
print_bytes_human(string_ram_shared, ram_shared, ctx->unit, ctx->decimals);
print_percentage(string_percentage_free, 100.0 * ram_free / ram_total);
print_percentage(string_percentage_available, 100.0 * ram_available / ram_total);
print_percentage(string_percentage_used, 100.0 * ram_used / ram_total);
@ -201,13 +202,14 @@ void print_memory(yajl_gen json_gen, char *buffer, const char *format, const cha
{.name = "%percentage_shared", .value = string_percentage_shared}};
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 (output_color)
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
free(buffer);
OUTPUT_FULL_TEXT(ctx->buf);
return;
error: