From: Kefu Chai Date: Thu, 26 Mar 2015 17:06:28 +0000 (+0800) Subject: osd: extract collect_sys_info() out of OSD::_collect_metadata() X-Git-Tag: v9.0.2~199^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=0e3bee688efb44b68cf96c1c5715758258774cf1;p=ceph.git osd: extract collect_sys_info() out of OSD::_collect_metadata() Signed-off-by: Kefu Chai --- diff --git a/src/common/util.cc b/src/common/util.cc index 212384b51942..13132f5e8f9f 100644 --- a/src/common/util.cc +++ b/src/common/util.cc @@ -13,8 +13,11 @@ */ #include +#include +#include #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 *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 *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 *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(value); + else if (strcmp(key, "SwapTotal:") == 0) + (*m)["mem_swap_kb"] = boost::lexical_cast(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); +} diff --git a/src/include/util.h b/src/include/util.h index 87f64999c6df..c3a28bc443ee 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -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 *m, CephContext *cct); + #endif /* CEPH_UTIL_H */ diff --git a/src/osd/OSD.cc b/src/osd/OSD.cc index 88b08e556055..73b62a8d6e8b 100644 --- a/src/osd/OSD.cc +++ b/src/osd/OSD.cc @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include @@ -130,6 +129,7 @@ #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 *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 *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 *pm) { (*pm)["ceph_version"] = pretty_version_to_str(); @@ -4524,64 +4477,7 @@ void OSD::_collect_metadata(map *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; }