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 (format_not_mounted == NULL) {
134 format_not_mounted = "";
137 if (statvfs(path, &buf) == -1) {
138 /* If statvfs errors, e.g., due to the path not existing,
139 * we use the format for a not mounted device. */
140 format = format_not_mounted;
142 FILE *mntentfile = setmntent("/etc/mtab", "r");
146 while ((m = getmntent(mntentfile)) != NULL) {
147 if (strcmp(m->mnt_dir, path) == 0) {
152 endmntent(mntentfile);
155 format = format_not_mounted;
160 if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
161 START_COLOR("color_bad");
162 colorful_output = true;
165 for (walk = format; *walk != '\0'; walk++) {
167 *(outwalk++) = *walk;
171 if (BEGINS_WITH(walk + 1, "free")) {
172 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
173 walk += strlen("free");
176 if (BEGINS_WITH(walk + 1, "used")) {
177 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
178 walk += strlen("used");
181 if (BEGINS_WITH(walk + 1, "total")) {
182 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
183 walk += strlen("total");
186 if (BEGINS_WITH(walk + 1, "avail")) {
187 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
188 walk += strlen("avail");
191 if (BEGINS_WITH(walk + 1, "percentage_free")) {
192 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark);
193 walk += strlen("percentage_free");
196 if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) {
197 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark);
198 walk += strlen("percentage_used_of_avail");
201 if (BEGINS_WITH(walk + 1, "percentage_used")) {
202 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark);
203 walk += strlen("percentage_used");
206 if (BEGINS_WITH(walk + 1, "percentage_avail")) {
207 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks, pct_mark);
208 walk += strlen("percentage_avail");
216 OUTPUT_FULL_TEXT(buffer);