]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: util: add get_fs_stats() function
authorJoao Eduardo Luis <joao@redhat.com>
Thu, 18 Sep 2014 15:32:20 +0000 (16:32 +0100)
committerJoao Eduardo Luis <joao@redhat.com>
Mon, 22 Sep 2014 16:36:28 +0000 (17:36 +0100)
simplifies the task of obtaining available/used disk space, as well as
used available percentage.

Signed-off-by: Joao Eduardo Luis <joao@redhat.com>
src/common/util.cc
src/include/util.h

index ab417befef691ea8ab8f0d6670d45d5b4b1dedf1..212384b51942bcfe283d0d82880364c58d596e7c 100644 (file)
 #include "common/errno.h"
 #include "common/strtol.h"
 
+#ifdef HAVE_SYS_VFS_H
+#include <sys/vfs.h>
+#endif
+
 // test if an entire buf is zero in 8-byte chunks
 bool buf_is_zero(const char *buf, size_t len)
 {
@@ -104,3 +108,21 @@ int64_t unit_to_bytesize(string val, ostream *pss)
   }
   return (r * (1LL << modifier));
 }
+
+int get_fs_stats(ceph_data_stats_t &stats, const char *path)
+{
+  if (!path)
+    return -EINVAL;
+
+  struct statfs stbuf;
+  int err = ::statfs(path, &stbuf);
+  if (err < 0) {
+    return -errno;
+  }
+
+  stats.byte_total = stbuf.f_blocks * stbuf.f_bsize;
+  stats.byte_used = (stbuf.f_blocks - stbuf.f_bfree) * stbuf.f_bsize;
+  stats.byte_avail = stbuf.f_bavail * stbuf.f_bsize;
+  stats.avail_percent = (((float)stats.byte_avail/stats.byte_total)*100);
+  return 0;
+}
index a08bb459e4a84618986c08cb68188359a3eb719d..4e4476ad644c9da6b410589ccddace7307284f45 100644 (file)
@@ -4,6 +4,7 @@
  * Ceph - scalable distributed file system
  *
  * Copyright (C) 2012 Inktank Storage, Inc.
+ * Copyright (C) 2014 Red Hat <contact@redhat.com>
  *
  * This is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 
 // is buf~len completely zero (in 8-byte chunks)
 
+#include "common/Formatter.h"
 #include "include/types.h"
 
 bool buf_is_zero(const char *buf, size_t len);
 
 int64_t unit_to_bytesize(string val, ostream *pss);
+
+struct ceph_data_stats
+{
+  uint64_t byte_total;
+  uint64_t byte_used;
+  uint64_t byte_avail;
+  int avail_percent;
+
+  void dump(Formatter *f) const {
+    assert(f != NULL);
+    f->dump_int("total", byte_total);
+    f->dump_int("used", byte_used);
+    f->dump_int("avail", byte_avail);
+    f->dump_int("avail_percent", avail_percent);
+  }
+};
+typedef struct ceph_data_stats ceph_data_stats_t;
+
+int get_fs_stats(ceph_data_stats_t &stats, const char *path);
 #endif /* CEPH_UTIL_H */