]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
osd: extract collect_sys_info() out of OSD::_collect_metadata()
authorKefu Chai <kchai@redhat.com>
Thu, 26 Mar 2015 17:06:28 +0000 (01:06 +0800)
committerKefu Chai <kchai@redhat.com>
Thu, 7 May 2015 14:29:39 +0000 (07:29 -0700)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/util.cc
src/include/util.h
src/osd/OSD.cc

index 212384b51942bcfe283d0d82880364c58d596e7c..13132f5e8f9fd1ca9a364a03d62331d870ef53f4 100644 (file)
  */
 
 #include <errno.h>
+#include <sys/utsname.h>
+#include <boost/lexical_cast.hpp>
 
 #include "include/util.h"
+#include "common/debug.h"
 #include "common/errno.h"
 #include "common/strtol.h"
 
@@ -126,3 +129,109 @@ int get_fs_stats(ceph_data_stats_t &stats, const char *path)
   stats.avail_percent = (((float)stats.byte_avail/stats.byte_total)*100);
   return 0;
 }
+
+static bool lsb_release_set(char *buf, const char *prefix,
+                           map<string, string> *pm, const char *key)
+{
+  if (strncmp(buf, prefix, strlen(prefix))) {
+    return false;
+  }
+
+  if (buf[strlen(buf)-1] == '\n')
+    buf[strlen(buf)-1] = '\0';
+
+  char *value = buf + strlen(prefix) + 1;
+  (*pm)[key] = value;
+  return true;
+}
+
+static void lsb_release_parse(map<string, string> *m, CephContext *cct)
+{
+  FILE *fp = popen("lsb_release -idrc", "r");
+  if (!fp) {
+    int ret = -errno;
+    lderr(cct) << "lsb_release_parse - failed to call lsb_release binary with error: " << cpp_strerror(ret) << dendl;
+    return;
+  }
+
+  char buf[512];
+  while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
+    if (lsb_release_set(buf, "Distributor ID:", m, "distro"))
+      continue;
+    if (lsb_release_set(buf, "Description:", m, "distro_description"))
+      continue;
+    if (lsb_release_set(buf, "Release:", m, "distro_version"))
+      continue;
+    if (lsb_release_set(buf, "Codename:", m, "distro_codename"))
+      continue;
+
+    lderr(cct) << "unhandled output: " << buf << dendl;
+  }
+
+  if (pclose(fp)) {
+    int ret = -errno;
+    lderr(cct) << "lsb_release_parse - pclose failed: " << cpp_strerror(ret) << dendl;
+  }
+}
+
+void collect_sys_info(map<string, string> *m, CephContext *cct)
+{
+  // kernel info
+  struct utsname u;
+  int r = uname(&u);
+  if (r >= 0) {
+    (*m)["os"] = u.sysname;
+    (*m)["kernel_version"] = u.release;
+    (*m)["kernel_description"] = u.version;
+    (*m)["hostname"] = u.nodename;
+    (*m)["arch"] = u.machine;
+  }
+
+  // memory
+  FILE *f = fopen("/proc/meminfo", "r");
+  if (f) {
+    char buf[100];
+    while (!feof(f)) {
+      char *line = fgets(buf, sizeof(buf), f);
+      if (!line)
+       break;
+      char key[40];
+      long long value;
+      int r = sscanf(line, "%s %lld", key, &value);
+      if (r == 2) {
+       if (strcmp(key, "MemTotal:") == 0)
+         (*m)["mem_total_kb"] = boost::lexical_cast<string>(value);
+       else if (strcmp(key, "SwapTotal:") == 0)
+         (*m)["mem_swap_kb"] = boost::lexical_cast<string>(value);
+      }
+    }
+    fclose(f);
+  }
+
+  // processor
+  f = fopen("/proc/cpuinfo", "r");
+  if (f) {
+    char buf[100];
+    while (!feof(f)) {
+      char *line = fgets(buf, sizeof(buf), f);
+      if (!line)
+       break;
+      if (strncmp(line, "model name", 10) == 0) {
+       char *c = strchr(buf, ':');
+       c++;
+       while (*c == ' ')
+         ++c;
+       char *nl = c;
+       while (*nl != '\n')
+         ++nl;
+       *nl = '\0';
+       (*m)["cpu"] = c;
+       break;
+      }
+    }
+    fclose(f);
+  }
+
+  // distro info
+  lsb_release_parse(m, cct);
+}
index 87f64999c6df8f87e8f2724c74125e4e5ecbb077..c3a28bc443eec1dd525e56d2b73dc401bedad1e1 100644 (file)
@@ -75,4 +75,8 @@ struct ceph_data_stats
 typedef struct ceph_data_stats ceph_data_stats_t;
 
 int get_fs_stats(ceph_data_stats_t &stats, const char *path);
+
+/// collect info from @p uname(2), @p /proc/meminfo and @p /proc/cpuinfo
+void collect_sys_info(map<string, string> *m, CephContext *cct);
+
 #endif /* CEPH_UTIL_H */
index 88b08e556055b97dd00bdc6a2e676a55750be13e..73b62a8d6e8b4a23da6f1e39682650f18e2e4a91 100644 (file)
@@ -17,7 +17,6 @@
 #include <iostream>
 #include <errno.h>
 #include <sys/stat.h>
-#include <sys/utsname.h>
 #include <signal.h>
 #include <ctype.h>
 #include <boost/scoped_ptr.hpp>
 
 #include "common/cmdparse.h"
 #include "include/str_list.h"
+#include "include/util.h"
 
 #include "include/assert.h"
 #include "common/config.h"
@@ -4461,53 +4461,6 @@ void OSD::_send_boot()
   monc->send_mon_message(mboot);
 }
 
-bool OSD::_lsb_release_set (char *buf, const char *str, map<string,string> *pm, const char *key)
-{
-  if (strncmp (buf, str, strlen (str)) == 0) {
-    char *value;
-
-    if (buf[strlen(buf)-1] == '\n')
-      buf[strlen(buf)-1] = '\0';
-
-    value = buf + strlen (str) + 1;
-    (*pm)[key] = value;
-
-    return true;
-  }
-  return false;
-}
-
-void OSD::_lsb_release_parse (map<string,string> *pm)
-{
-  FILE *fp = NULL;
-  char buf[512];
-
-  fp = popen("lsb_release -idrc", "r");
-  if (!fp) {
-    int ret = -errno;
-    derr << "lsb_release_parse - failed to call lsb_release binary with error: " << cpp_strerror(ret) << dendl;
-    return;
-  }
-
-  while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
-    if (_lsb_release_set(buf, "Distributor ID:", pm, "distro")) 
-      continue;
-    if (_lsb_release_set(buf, "Description:", pm, "distro_description"))
-      continue;
-    if (_lsb_release_set(buf, "Release:", pm, "distro_version"))
-      continue;
-    if (_lsb_release_set(buf, "Codename:", pm, "distro_codename"))
-      continue;
-    
-    derr << "unhandled output: " << buf << dendl;
-  }
-
-  if (pclose(fp)) {
-    int ret = -errno;
-    derr << "lsb_release_parse - pclose failed: " << cpp_strerror(ret) << dendl;
-  }
-}
-
 void OSD::_collect_metadata(map<string,string> *pm)
 {
   (*pm)["ceph_version"] = pretty_version_to_str();
@@ -4524,64 +4477,7 @@ void OSD::_collect_metadata(map<string,string> *pm)
   (*pm)["osd_objectstore"] = g_conf->osd_objectstore;
   store->collect_metadata(pm);
 
-  // kernel info
-  struct utsname u;
-  int r = uname(&u);
-  if (r >= 0) {
-    (*pm)["os"] = u.sysname;
-    (*pm)["kernel_version"] = u.release;
-    (*pm)["kernel_description"] = u.version;
-    (*pm)["hostname"] = u.nodename;
-    (*pm)["arch"] = u.machine;
-  }
-
-  // memory
-  FILE *f = fopen("/proc/meminfo", "r");
-  if (f) {
-    char buf[100];
-    while (!feof(f)) {
-      char *line = fgets(buf, sizeof(buf), f);
-      if (!line)
-       break;
-      char key[40];
-      long long value;
-      int r = sscanf(line, "%s %lld", key, &value);
-      if (r == 2) {
-       if (strcmp(key, "MemTotal:") == 0)
-         (*pm)["mem_total_kb"] = stringify(value);
-       else if (strcmp(key, "SwapTotal:") == 0)
-         (*pm)["mem_swap_kb"] = stringify(value);
-      }
-    }
-    fclose(f);
-  }
-
-  // processor
-  f = fopen("/proc/cpuinfo", "r");
-  if (f) {
-    char buf[100];
-    while (!feof(f)) {
-      char *line = fgets(buf, sizeof(buf), f);
-      if (!line)
-       break;
-      if (strncmp(line, "model name", 10) == 0) {
-       char *c = strchr(buf, ':');
-       c++;
-       while (*c == ' ')
-         ++c;
-       char *nl = c;
-       while (*nl != '\n')
-         ++nl;
-       *nl = '\0';
-       (*pm)["cpu"] = c;
-       break;
-      }
-    }
-    fclose(f);
-  }
-
-  // distro info
-  _lsb_release_parse(pm); 
+  collect_sys_info(pm, g_ceph_context);
 
   dout(10) << __func__ << " " << *pm << dendl;
 }