7 #include <sys/statvfs.h>
9 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) || defined(__DragonFly__)
10 #include <sys/param.h>
11 #include <sys/mount.h>
13 #include <yajl/yajl_gen.h>
14 #include <yajl/yajl_version.h>
18 #define BINARY_BASE UINT64_C(1024)
19 #define DECIMAL_BASE UINT64_C(1000)
21 #define MAX_EXPONENT 4
23 static const char * const iec_symbols[MAX_EXPONENT+1] = {"", "Ki", "Mi", "Gi", "Ti"};
24 static const char * const si_symbols[MAX_EXPONENT+1] = {"", "k", "M", "G", "T"};
25 static const char * const custom_symbols[MAX_EXPONENT+1] = {"", "K", "M", "G", "T"};
28 * Formats bytes according to the given base and set of symbols.
31 static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char * const symbols[]) {
34 while (size >= base && exponent < MAX_EXPONENT) {
38 return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
42 * Prints the given amount of bytes in a human readable manner.
45 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
46 if (strcasecmp(prefix_type, "decimal") == 0) {
47 return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
48 } else if (strcasecmp(prefix_type, "custom") == 0) {
49 return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
51 return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
56 * Determines whether remaining bytes are below given threshold.
59 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
60 static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
62 static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
64 if (strcasecmp(threshold_type, "percentage_free") == 0) {
65 return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
66 } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
67 return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
68 } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
69 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
70 } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
71 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
72 } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type+1, "bytes_", strlen("bytes_")) == 0) {
73 uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
76 switch (threshold_type[0]) {
94 if (strcasecmp(threshold_type+1, "bytes_free") == 0) {
95 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
96 } else if (strcasecmp(threshold_type+1, "bytes_avail") == 0) {
97 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
105 * Does a statvfs and prints either free, used or total amounts of bytes in a
106 * human readable manner.
109 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type, const char *threshold_type, const double low_threshold) {
111 char *outwalk = buffer;
112 bool colorful_output = false;
116 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
119 if (statfs(path, &buf) == -1)
124 if (statvfs(path, &buf) == -1)
128 if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
129 START_COLOR("color_bad");
130 colorful_output = true;
133 for (walk = format; *walk != '\0'; walk++) {
135 *(outwalk++) = *walk;
139 if (BEGINS_WITH(walk+1, "free")) {
140 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
141 walk += strlen("free");
144 if (BEGINS_WITH(walk+1, "used")) {
145 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
146 walk += strlen("used");
149 if (BEGINS_WITH(walk+1, "total")) {
150 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
151 walk += strlen("total");
154 if (BEGINS_WITH(walk+1, "avail")) {
155 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
156 walk += strlen("avail");
159 if (BEGINS_WITH(walk+1, "percentage_free")) {
160 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks);
161 walk += strlen("percentage_free");
164 if (BEGINS_WITH(walk+1, "percentage_used_of_avail")) {
165 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks);
166 walk += strlen("percentage_used_of_avail");
169 if (BEGINS_WITH(walk+1, "percentage_used")) {
170 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks);
171 walk += strlen("percentage_used");
174 if (BEGINS_WITH(walk+1, "percentage_avail")) {
175 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks);
176 walk += strlen("percentage_avail");
184 OUTPUT_FULL_TEXT(buffer);