disk: fix colors (+param struct)

This commit is contained in:
Michael Stapelberg 2021-11-02 21:16:25 +01:00
parent ffedf14066
commit 9a6f96b309
3 changed files with 48 additions and 21 deletions

View File

@ -784,7 +784,19 @@ int main(int argc, char *argv[]) {
CASE_SEC_TITLE("disk") { CASE_SEC_TITLE("disk") {
SEC_OPEN_MAP("disk_info"); SEC_OPEN_MAP("disk_info");
print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_below_threshold"), cfg_getstr(sec, "format_not_mounted"), cfg_getstr(sec, "prefix_type"), cfg_getstr(sec, "threshold_type"), cfg_getfloat(sec, "low_threshold")); disk_info_ctx_t ctx = {
.json_gen = json_gen,
.buf = buffer,
.buflen = sizeof(buffer),
.path = title,
.format = cfg_getstr(sec, "format"),
.format_below_threshold = cfg_getstr(sec, "format_below_threshold"),
.format_not_mounted = cfg_getstr(sec, "format_not_mounted"),
.prefix_type = cfg_getstr(sec, "prefix_type"),
.threshold_type = cfg_getstr(sec, "threshold_type"),
.low_threshold = cfg_getfloat(sec, "low_threshold"),
};
print_disk_info(&ctx);
SEC_CLOSE_MAP; SEC_CLOSE_MAP;
} }

View File

@ -244,7 +244,20 @@ typedef struct {
void print_ipv6_info(ipv6_info_ctx_t *ctx); void print_ipv6_info(ipv6_info_ctx_t *ctx);
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_below_threshold, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold); typedef struct {
yajl_gen json_gen;
char *buf;
const size_t buflen;
const char *path;
const char *format;
const char *format_below_threshold;
const char *format_not_mounted;
const char *prefix_type;
const char *threshold_type;
const double low_threshold;
} disk_info_ctx_t;
void print_disk_info(disk_info_ctx_t *ctx);
typedef struct { typedef struct {
yajl_gen json_gen; yajl_gen json_gen;

View File

@ -116,13 +116,14 @@ static bool below_threshold(struct statvfs buf, const char *prefix_type, const c
* human readable manner. * human readable manner.
* *
*/ */
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_below_threshold, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold) { void print_disk_info(disk_info_ctx_t *ctx) {
const char *selected_format = format; const char *selected_format = ctx->format;
char *outwalk = buffer; char *outwalk = ctx->buf;
bool colorful_output = false; bool colorful_output = false;
bool mounted = false; bool mounted = false;
#define json_gen ctx->json_gen
INSTANCE(path); INSTANCE(ctx->path);
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
struct statfs buf; struct statfs buf;
@ -141,12 +142,12 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
#else #else
struct statvfs buf; struct statvfs buf;
if (statvfs(path, &buf) == -1) { if (statvfs(ctx->path, &buf) == -1) {
/* If statvfs errors, e.g., due to the path not existing, /* If statvfs errors, e.g., due to the path not existing,
* we consider the device not mounted. */ * we consider the device not mounted. */
mounted = false; mounted = false;
} else { } else {
char *sanitized = sstrdup(path); char *sanitized = sstrdup(ctx->path);
if (strlen(sanitized) > 1 && sanitized[strlen(sanitized) - 1] == '/') if (strlen(sanitized) > 1 && sanitized[strlen(sanitized) - 1] == '/')
sanitized[strlen(sanitized) - 1] = '\0'; sanitized[strlen(sanitized) - 1] = '\0';
FILE *mntentfile = setmntent("/etc/mtab", "r"); FILE *mntentfile = setmntent("/etc/mtab", "r");
@ -171,14 +172,14 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
#endif #endif
if (!mounted) { if (!mounted) {
if (format_not_mounted == NULL) if (ctx->format_not_mounted == NULL)
format_not_mounted = ""; ctx->format_not_mounted = "";
selected_format = format_not_mounted; selected_format = ctx->format_not_mounted;
} else if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) { } else if (ctx->low_threshold > 0 && below_threshold(buf, ctx->prefix_type, ctx->threshold_type, ctx->low_threshold)) {
START_COLOR("color_bad"); START_COLOR("color_bad");
colorful_output = true; colorful_output = true;
if (format_below_threshold != NULL) if (ctx->format_below_threshold != NULL)
selected_format = format_below_threshold; selected_format = ctx->format_below_threshold;
} }
char string_free[STRING_SIZE]; char string_free[STRING_SIZE];
@ -190,10 +191,10 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
char string_percentage_used[STRING_SIZE]; char string_percentage_used[STRING_SIZE];
char string_percentage_avail[STRING_SIZE]; char string_percentage_avail[STRING_SIZE];
print_bytes_human(string_free, (uint64_t)buf.f_frsize * (uint64_t)buf.f_bfree, prefix_type); print_bytes_human(string_free, (uint64_t)buf.f_frsize * (uint64_t)buf.f_bfree, ctx->prefix_type);
print_bytes_human(string_used, (uint64_t)buf.f_frsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type); print_bytes_human(string_used, (uint64_t)buf.f_frsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), ctx->prefix_type);
print_bytes_human(string_total, (uint64_t)buf.f_frsize * (uint64_t)buf.f_blocks, prefix_type); print_bytes_human(string_total, (uint64_t)buf.f_frsize * (uint64_t)buf.f_blocks, ctx->prefix_type);
print_bytes_human(string_avail, (uint64_t)buf.f_frsize * (uint64_t)buf.f_bavail, prefix_type); print_bytes_human(string_avail, (uint64_t)buf.f_frsize * (uint64_t)buf.f_bavail, ctx->prefix_type);
snprintf(string_percentage_free, STRING_SIZE, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark); snprintf(string_percentage_free, STRING_SIZE, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark);
snprintf(string_percentage_used_of_avail, STRING_SIZE, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark); snprintf(string_percentage_used_of_avail, STRING_SIZE, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark);
snprintf(string_percentage_used, STRING_SIZE, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark); snprintf(string_percentage_used, STRING_SIZE, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark);
@ -210,12 +211,13 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
{.name = "%percentage_avail", .value = string_percentage_avail}}; {.name = "%percentage_avail", .value = string_percentage_avail}};
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);
} }