7 #include <sys/statvfs.h>
9 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) || defined(__DragonFly__)
10 #include <sys/param.h>
11 #include <sys/mount.h>
13 #include <yajl/yajl_gen.h>
14 #include <yajl/yajl_version.h>
18 #define BINARY_BASE UINT64_C(1024)
19 #define DECIMAL_BASE UINT64_C(1000)
21 #define MAX_EXPONENT 4
23 static const char * const iec_symbols[MAX_EXPONENT+1] = {"", "Ki", "Mi", "Gi", "Ti"};
24 static const char * const si_symbols[MAX_EXPONENT+1] = {"", "k", "M", "G", "T"};
25 static const char * const custom_symbols[MAX_EXPONENT+1] = {"", "K", "M", "G", "T"};
28 * Formats bytes according to the given base and set of symbols.
31 static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char * const symbols[]) {
34 while (size >= base && exponent < MAX_EXPONENT) {
38 return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
42 * Prints the given amount of bytes in a human readable manner.
45 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
46 if (strncmp(prefix_type, "decimal", strlen(prefix_type)) == 0) {
47 return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
48 } else if (strncmp(prefix_type, "custom", strlen(prefix_type)) == 0) {
49 return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
51 return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
56 * Does a statvfs and prints either free, used or total amounts of bytes in a
57 * human readable manner.
60 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type) {
62 char *outwalk = buffer;
66 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
69 if (statfs(path, &buf) == -1)
74 if (statvfs(path, &buf) == -1)
78 for (walk = format; *walk != '\0'; walk++) {
84 if (BEGINS_WITH(walk+1, "free")) {
85 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
86 walk += strlen("free");
89 if (BEGINS_WITH(walk+1, "used")) {
90 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
91 walk += strlen("used");
94 if (BEGINS_WITH(walk+1, "total")) {
95 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
96 walk += strlen("total");
99 if (BEGINS_WITH(walk+1, "avail")) {
100 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
101 walk += strlen("avail");
104 if (BEGINS_WITH(walk+1, "percentage_free")) {
105 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks);
106 walk += strlen("percentage_free");
109 if (BEGINS_WITH(walk+1, "percentage_used_of_avail")) {
110 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks);
111 walk += strlen("percentage_used_of_avail");
114 if (BEGINS_WITH(walk+1, "percentage_used")) {
115 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks);
116 walk += strlen("percentage_used");
119 if (BEGINS_WITH(walk+1, "percentage_avail")) {
120 outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks);
121 walk += strlen("percentage_avail");
126 OUTPUT_FULL_TEXT(buffer);