1 // vim:ts=4:sw=4:expandtab
8 #include <sys/statvfs.h>
10 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) || defined(__DragonFly__)
11 #include <sys/param.h>
12 #include <sys/mount.h>
16 #include <yajl/yajl_gen.h>
17 #include <yajl/yajl_version.h>
21 #define BINARY_BASE UINT64_C(1024)
22 #define DECIMAL_BASE UINT64_C(1000)
24 #define MAX_EXPONENT 4
26 static const char *const iec_symbols[MAX_EXPONENT + 1] = {"", "Ki", "Mi", "Gi", "Ti"};
27 static const char *const si_symbols[MAX_EXPONENT + 1] = {"", "k", "M", "G", "T"};
28 static const char *const custom_symbols[MAX_EXPONENT + 1] = {"", "K", "M", "G", "T"};
31 * Formats bytes according to the given base and set of symbols.
34 static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char *const symbols[]) {
37 while (size >= base && exponent < MAX_EXPONENT) {
41 return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
45 * Prints the given amount of bytes in a human readable manner.
48 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
49 if (strcasecmp(prefix_type, "decimal") == 0) {
50 return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
51 } else if (strcasecmp(prefix_type, "custom") == 0) {
52 return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
54 return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
59 * Determines whether remaining bytes are below given threshold.
62 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
63 static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
65 static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
67 if (strcasecmp(threshold_type, "percentage_free") == 0) {
68 return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
69 } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
70 return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
71 } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
72 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
73 } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
74 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
75 } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type + 1, "bytes_", strlen("bytes_")) == 0) {
76 uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
79 switch (threshold_type[0]) {
97 if (strcasecmp(threshold_type + 1, "bytes_free") == 0) {
98 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
99 } else if (strcasecmp(threshold_type + 1, "bytes_avail") == 0) {
100 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
108 * Does a statvfs and prints either free, used or total amounts of bytes in a
109 * human readable manner.
112 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold) {
114 char *outwalk = buffer;
115 bool colorful_output = false;
119 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
122 if (statfs(path, &buf) == -1)
127 if (statvfs(path, &buf) == -1)
130 if (format_not_mounted != NULL) {
131 FILE *mntentfile = setmntent("/etc/mtab", "r");
135 while ((m = getmntent(mntentfile)) != NULL) {
136 if (strcmp(m->mnt_dir, path) == 0) {
141 endmntent(mntentfile);
144 format = format_not_mounted;
149 if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
150 START_COLOR("color_bad");
151 colorful_output = true;
154 for (walk = format; *walk != '\0'; walk++) {
156 *(outwalk++) = *walk;
160 if (BEGINS_WITH(walk + 1, "free")) {
161 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
162 walk += strlen("free");
165 if (BEGINS_WITH(walk + 1, "used")) {
166 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
167 walk += strlen("used");
170 if (BEGINS_WITH(walk + 1, "total")) {
171 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
172 walk += strlen("total");
175 if (BEGINS_WITH(walk + 1, "avail")) {
176 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
177 walk += strlen("avail");
180 if (BEGINS_WITH(walk + 1, "percentage_free")) {
181 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks);
182 walk += strlen("percentage_free");
185 if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) {
186 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks);
187 walk += strlen("percentage_used_of_avail");
190 if (BEGINS_WITH(walk + 1, "percentage_used")) {
191 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks);
192 walk += strlen("percentage_used");
195 if (BEGINS_WITH(walk + 1, "percentage_avail")) {
196 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks);
197 walk += strlen("percentage_avail");
205 OUTPUT_FULL_TEXT(buffer);