-void print_disk_info(const char *path, const char *format) {
- const char *walk;
- struct statvfs buf;
-
- if (statvfs(path, &buf) == -1)
- return;
-
- for (walk = format; *walk != '\0'; walk++) {
- if (*walk != '%') {
- putchar(*walk);
- continue;
- }
-
- if (BEGINS_WITH(walk+1, "free")) {
- print_bytes_human((uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree);
- walk += strlen("free");
- }
-
- if (BEGINS_WITH(walk+1, "used")) {
- print_bytes_human((uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree));
- walk += strlen("used");
- }
-
- if (BEGINS_WITH(walk+1, "total")) {
- print_bytes_human((uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks);
- walk += strlen("total");
- }
+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) {
+ const char *walk;
+ char *outwalk = buffer;
+ bool colorful_output = false;
+
+ INSTANCE(path);
+
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
+ struct statfs buf;
+
+ if (statfs(path, &buf) == -1)
+ return;
+#else
+ struct statvfs buf;
+
+ if (statvfs(path, &buf) == -1) {
+ /* If statvfs errors, e.g., due to the path not existing,
+ * we use the format for a not mounted device. */
+ format = format_not_mounted;
+ } else if (format_not_mounted != NULL) {
+ FILE *mntentfile = setmntent("/etc/mtab", "r");
+ struct mntent *m;
+ bool found = false;
+
+ while ((m = getmntent(mntentfile)) != NULL) {
+ if (strcmp(m->mnt_dir, path) == 0) {
+ found = true;
+ break;
+ }
+ }
+ endmntent(mntentfile);
+
+ if (!found) {
+ format = format_not_mounted;
+ }
+ }
+#endif
+
+ if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
+ START_COLOR("color_bad");
+ colorful_output = true;
+ }
+
+ for (walk = format; *walk != '\0'; walk++) {
+ if (*walk != '%') {
+ *(outwalk++) = *walk;
+ continue;
+ }
+
+ if (BEGINS_WITH(walk + 1, "free")) {
+ outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
+ walk += strlen("free");
+ }
+
+ if (BEGINS_WITH(walk + 1, "used")) {
+ outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
+ walk += strlen("used");
+ }
+
+ if (BEGINS_WITH(walk + 1, "total")) {
+ outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
+ walk += strlen("total");
+ }
+
+ if (BEGINS_WITH(walk + 1, "avail")) {
+ outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
+ walk += strlen("avail");
+ }
+
+ if (BEGINS_WITH(walk + 1, "percentage_free")) {
+ outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks);
+ walk += strlen("percentage_free");