1 // vim:ts=4:sw=4:expandtab
8 #include <sys/statvfs.h>
10 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
11 #include <sys/param.h>
12 #include <sys/mount.h>
13 #elif defined(__NetBSD__)
17 #include <yajl/yajl_gen.h>
18 #include <yajl/yajl_version.h>
22 #define BINARY_BASE UINT64_C(1024)
23 #define DECIMAL_BASE UINT64_C(1000)
25 #define MAX_EXPONENT 4
27 static const char *const iec_symbols[MAX_EXPONENT + 1] = {"", "Ki", "Mi", "Gi", "Ti"};
28 static const char *const si_symbols[MAX_EXPONENT + 1] = {"", "k", "M", "G", "T"};
29 static const char *const custom_symbols[MAX_EXPONENT + 1] = {"", "K", "M", "G", "T"};
32 * Formats bytes according to the given base and set of symbols.
35 static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char *const symbols[]) {
38 while (size >= base && exponent < MAX_EXPONENT) {
42 return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
46 * Prints the given amount of bytes in a human readable manner.
49 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
50 if (strcasecmp(prefix_type, "decimal") == 0) {
51 return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
52 } else if (strcasecmp(prefix_type, "custom") == 0) {
53 return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
55 return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
60 * Determines whether remaining bytes are below given threshold.
63 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
64 static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
66 static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
68 if (strcasecmp(threshold_type, "percentage_free") == 0) {
69 return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
70 } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
71 return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
72 } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
73 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
74 } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
75 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
76 } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type + 1, "bytes_", strlen("bytes_")) == 0) {
77 uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
80 switch (threshold_type[0]) {
98 if (strcasecmp(threshold_type + 1, "bytes_free") == 0) {
99 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
100 } else if (strcasecmp(threshold_type + 1, "bytes_avail") == 0) {
101 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
109 * Does a statvfs and prints either free, used or total amounts of bytes in a
110 * human readable manner.
113 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) {
115 char *outwalk = buffer;
116 bool colorful_output = false;
120 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
123 if (statfs(path, &buf) == -1)
125 #elif defined(__NetBSD__)
128 if (statvfs(path, &buf) == -1)
133 if (statvfs(path, &buf) == -1) {
134 /* If statvfs errors, e.g., due to the path not existing,
135 * we use the format for a not mounted device. */
136 if (format_not_mounted != NULL) {
137 format = format_not_mounted;
139 } else if (format_not_mounted != NULL) {
140 FILE *mntentfile = setmntent("/etc/mtab", "r");
144 while ((m = getmntent(mntentfile)) != NULL) {
145 if (strcmp(m->mnt_dir, path) == 0) {
150 endmntent(mntentfile);
153 format = format_not_mounted;
158 if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
159 START_COLOR("color_bad");
160 colorful_output = true;
163 for (walk = format; *walk != '\0'; walk++) {
165 *(outwalk++) = *walk;
169 if (BEGINS_WITH(walk + 1, "free")) {
170 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
171 walk += strlen("free");
174 if (BEGINS_WITH(walk + 1, "used")) {
175 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
176 walk += strlen("used");
179 if (BEGINS_WITH(walk + 1, "total")) {
180 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
181 walk += strlen("total");
184 if (BEGINS_WITH(walk + 1, "avail")) {
185 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
186 walk += strlen("avail");
189 if (BEGINS_WITH(walk + 1, "percentage_free")) {
190 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark);
191 walk += strlen("percentage_free");
194 if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) {
195 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark);
196 walk += strlen("percentage_used_of_avail");
199 if (BEGINS_WITH(walk + 1, "percentage_used")) {
200 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark);
201 walk += strlen("percentage_used");
204 if (BEGINS_WITH(walk + 1, "percentage_avail")) {
205 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks, pct_mark);
206 walk += strlen("percentage_avail");
214 OUTPUT_FULL_TEXT(buffer);