7 #include <sys/statvfs.h>
9 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
10 #include <sys/param.h>
11 #include <sys/mount.h>
13 #include <yajl/yajl_gen.h>
17 #define TERABYTE (1024ULL * 1024 * 1024 * 1024)
18 #define GIGABYTE (1024ULL * 1024 * 1024)
19 #define MEGABYTE (1024ULL * 1024)
20 #define KILOBYTE (1024ULL)
23 * Prints the given amount of bytes in a human readable manner.
26 static int print_bytes_human(char *outwalk, uint64_t bytes) {
28 return sprintf(outwalk, "%.02f TB", (double)bytes / TERABYTE);
29 else if (bytes > GIGABYTE)
30 return sprintf(outwalk, "%.01f GB", (double)bytes / GIGABYTE);
31 else if (bytes > MEGABYTE)
32 return sprintf(outwalk, "%.01f MB", (double)bytes / MEGABYTE);
33 else if (bytes > KILOBYTE)
34 return sprintf(outwalk, "%.01f KB", (double)bytes / KILOBYTE);
36 return sprintf(outwalk, "%.01f B", (double)bytes);
41 * Does a statvfs and prints either free, used or total amounts of bytes in a
42 * human readable manner.
45 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format) {
47 char *outwalk = buffer;
51 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
54 if (statfs(path, &buf) == -1)
59 if (statvfs(path, &buf) == -1)
63 for (walk = format; *walk != '\0'; walk++) {
69 if (BEGINS_WITH(walk+1, "free")) {
70 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree);
71 walk += strlen("free");
74 if (BEGINS_WITH(walk+1, "used")) {
75 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree));
76 walk += strlen("used");
79 if (BEGINS_WITH(walk+1, "total")) {
80 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks);
81 walk += strlen("total");
84 if (BEGINS_WITH(walk+1, "avail")) {
85 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail);
86 walk += strlen("avail");
91 OUTPUT_FULL_TEXT(buffer);