9 #include <sys/statvfs.h>
10 #include <sys/types.h>
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) || defined(__DragonFly__)
12 #include <sys/param.h>
13 #include <sys/mount.h>
15 #include <yajl/yajl_gen.h>
16 #include <yajl/yajl_version.h>
20 #define BINARY_BASE UINT64_C(1024)
21 #define DECIMAL_BASE UINT64_C(1000)
23 #define MAX_EXPONENT 4
25 static const char * const iec_symbols[MAX_EXPONENT+1] = {"", "Ki", "Mi", "Gi", "Ti"};
26 static const char * const si_symbols[MAX_EXPONENT+1] = {"", "k", "M", "G", "T"};
27 static const char * const custom_symbols[MAX_EXPONENT+1] = {"", "K", "M", "G", "T"};
30 * Formats bytes according to the given base and set of symbols.
33 static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char * const symbols[]) {
36 while (size >= base && exponent < MAX_EXPONENT) {
40 return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
44 * Prints the given amount of bytes in a human readable manner.
47 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
48 if (strcasecmp(prefix_type, "decimal") == 0) {
49 return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
50 } else if (strcasecmp(prefix_type, "custom") == 0) {
51 return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
53 return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
58 * Determines whether remaining bytes are below given threshold.
61 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
62 static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
64 static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
66 if (strcasecmp(threshold_type, "percentage_free") == 0) {
67 return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
68 } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
69 return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
70 } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
71 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
72 } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
73 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
74 } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type+1, "bytes_", strlen("bytes_")) == 0) {
75 uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
78 switch (threshold_type[0]) {
96 if (strcasecmp(threshold_type+1, "bytes_free") == 0) {
97 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
98 } else if (strcasecmp(threshold_type+1, "bytes_avail") == 0) {
99 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
107 * Does a statvfs and prints either free, used or total amounts of bytes in a
108 * human readable manner.
111 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) {
113 char *outwalk = buffer;
114 bool colorful_output = false;
118 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
121 if (statfs(path, &buf) == -1)
126 if (statvfs(path, &buf) == -1)
129 if (format_not_mounted != NULL) {
130 FILE *mntentfile = setmntent("/etc/mtab", "r");
134 while (NULL != (m = getmntent(mntentfile))) {
135 if (strcmp(m->mnt_dir, path) == 0) {
140 endmntent(mntentfile);
143 format = format_not_mounted;
148 if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
149 START_COLOR("color_bad");
150 colorful_output = true;
153 for (walk = format; *walk != '\0'; walk++) {
155 *(outwalk++) = *walk;
159 if (BEGINS_WITH(walk+1, "free")) {
160 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
161 walk += strlen("free");
164 if (BEGINS_WITH(walk+1, "used")) {
165 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
166 walk += strlen("used");
169 if (BEGINS_WITH(walk+1, "total")) {
170 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
171 walk += strlen("total");
174 if (BEGINS_WITH(walk+1, "avail")) {
175 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
176 walk += strlen("avail");
179 if (BEGINS_WITH(walk+1, "percentage_free")) {
180 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks);
181 walk += strlen("percentage_free");
184 if (BEGINS_WITH(walk+1, "percentage_used_of_avail")) {
185 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks);
186 walk += strlen("percentage_used_of_avail");
189 if (BEGINS_WITH(walk+1, "percentage_used")) {
190 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks);
191 walk += strlen("percentage_used");
194 if (BEGINS_WITH(walk+1, "percentage_avail")) {
195 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks);
196 walk += strlen("percentage_avail");
204 OUTPUT_FULL_TEXT(buffer);