1 // vim:ts=4:sw=4:expandtab
8 #include <sys/statvfs.h>
10 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__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_below_threshold, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold) {
114 const char *selected_format = format;
116 char *outwalk = buffer;
117 bool colorful_output = false;
118 bool mounted = false;
122 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
125 if (statfs(path, &buf) == -1)
129 #elif defined(__NetBSD__)
132 if (statvfs(path, &buf) == -1)
139 if (statvfs(path, &buf) == -1) {
140 /* If statvfs errors, e.g., due to the path not existing,
141 * we consider the device not mounted. */
144 char *sanitized = sstrdup(path);
145 if (strlen(sanitized) > 1 && sanitized[strlen(sanitized) - 1] == '/')
146 sanitized[strlen(sanitized) - 1] = '\0';
147 FILE *mntentfile = setmntent("/etc/mtab", "r");
150 while ((m = getmntent(mntentfile)) != NULL) {
151 if (strcmp(m->mnt_dir, sanitized) == 0) {
156 endmntent(mntentfile);
162 if (format_not_mounted == NULL)
163 format_not_mounted = "";
164 selected_format = format_not_mounted;
165 } else if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
166 START_COLOR("color_bad");
167 colorful_output = true;
168 if (format_below_threshold != NULL)
169 selected_format = format_below_threshold;
172 for (walk = selected_format; *walk != '\0'; walk++) {
174 *(outwalk++) = *walk;
178 if (BEGINS_WITH(walk + 1, "free")) {
179 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
180 walk += strlen("free");
183 if (BEGINS_WITH(walk + 1, "used")) {
184 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
185 walk += strlen("used");
188 if (BEGINS_WITH(walk + 1, "total")) {
189 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
190 walk += strlen("total");
193 if (BEGINS_WITH(walk + 1, "avail")) {
194 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
195 walk += strlen("avail");
198 if (BEGINS_WITH(walk + 1, "percentage_free")) {
199 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark);
200 walk += strlen("percentage_free");
203 if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) {
204 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark);
205 walk += strlen("percentage_used_of_avail");
208 if (BEGINS_WITH(walk + 1, "percentage_used")) {
209 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark);
210 walk += strlen("percentage_used");
213 if (BEGINS_WITH(walk + 1, "percentage_avail")) {
214 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks, pct_mark);
215 walk += strlen("percentage_avail");
223 OUTPUT_FULL_TEXT(buffer);