summaryrefslogtreecommitdiffstats
path: root/src/common/util.cc
blob: dc676c2647e3fa89d206cf5dfa0fb0529839c8f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2012 Inktank Storage, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */

#ifndef _WIN32
#include <sys/utsname.h>
#endif

#include <fstream>
#include <boost/algorithm/string.hpp>

#include "include/compat.h"
#include "include/util.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/version.h"

#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif

#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/param.h>
#include <sys/mount.h>
#if defined(__APPLE__)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
#endif

#include <string>

#include <stdio.h>

using std::list;
using std::map;
using std::string;

using ceph::bufferlist;
using ceph::Formatter;

#ifndef _WIN32
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;
}
#else
int get_fs_stats(ceph_data_stats_t &stats, const char *path)
{
  ULARGE_INTEGER avail_bytes, total_bytes, total_free_bytes;

  if (!GetDiskFreeSpaceExA(path, &avail_bytes,
                           &total_bytes, &total_free_bytes)) {
    return -EINVAL;
  }

  stats.byte_total = total_bytes.QuadPart;
  stats.byte_used = total_bytes.QuadPart - total_free_bytes.QuadPart;
  // may not be equal to total_free_bytes due to quotas
  stats.byte_avail = avail_bytes.QuadPart;
  stats.avail_percent = ((float)stats.byte_avail / stats.byte_total) * 100;
  return 0;
}
#endif

static char* value_sanitize(char *value)
{
  while (isspace(*value) || *value == '"')
    value++;

  char* end = value + strlen(value) - 1;
  while (end > value && (isspace(*end) || *end == '"'))
    end--;

  *(end + 1) = '\0';

  return value;
}

static bool value_set(char *buf, const char *prefix,
		      map<string, string> *pm, const char *key)
{
  if (strncmp(buf, prefix, strlen(prefix))) {
    return false;
  }

  (*pm)[key] = value_sanitize(buf + strlen(prefix));
  return true;
}

static void file_values_parse(const map<string, string>& kvm, FILE *fp, map<string, string> *m, CephContext *cct) {
  char buf[512];
  while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
    for (auto& kv : kvm) {
      if (value_set(buf, kv.second.c_str(), m, kv.first.c_str()))
        continue;
    }
  }
}

static bool os_release_parse(map<string, string> *m, CephContext *cct)
{
#if defined(__linux__)
  static const map<string, string> kvm = {
    { "distro", "ID=" },
    { "distro_description", "PRETTY_NAME=" },
    { "distro_version", "VERSION_ID=" }
  };

  FILE *fp = fopen("/etc/os-release", "r");
  if (!fp) {
    int ret = -errno;
    lderr(cct) << "os_release_parse - failed to open /etc/os-release: " << cpp_strerror(ret) << dendl;
    return false;
  }

  file_values_parse(kvm, fp, m, cct);

  fclose(fp);
#elif defined(__FreeBSD__)
  struct utsname u;
  int r = uname(&u);
  if (!r) {
     m->insert(std::make_pair("distro", u.sysname));
     m->insert(std::make_pair("distro_description", u.version));
     m->insert(std::make_pair("distro_version", u.release));
  }
#endif

  return true;
}

static void distro_detect(map<string, string> *m, CephContext *cct)
{
  if (!os_release_parse(m, cct)) {
    lderr(cct) << "distro_detect - /etc/os-release is required" << dendl;
  }

  for (const char* rk: {"distro", "distro_description"}) {
    if (m->find(rk) == m->end())
      lderr(cct) << "distro_detect - can't detect " << rk << dendl;
  }
}

int get_cgroup_memory_limit(uint64_t *limit)
{
#if defined(__linux__)
  // /sys/fs/cgroup/memory/memory.limit_in_bytes

  // the magic value 9223372036854771712 or 0x7ffffffffffff000
  // appears to mean no limit.
  FILE *f = fopen(PROCPREFIX "/sys/fs/cgroup/memory/memory.limit_in_bytes", "r");
  if (!f) {
    return -errno;
  }
  char buf[100];
  int ret = 0;
  long long value;
  char *line = fgets(buf, sizeof(buf), f);
  if (!line) {
    ret = -EINVAL;
    goto out;
  }
  if (sscanf(line, "%lld", &value) != 1) {
    ret = -EINVAL;
  }
  if (value == 0x7ffffffffffff000) {
    *limit = 0;  // no limit
  } else {
    *limit = value;
  }
out:
  fclose(f);
  return ret;
#else
  return 0;
#endif
}

#ifdef _WIN32
int get_windows_version(POSVERSIONINFOEXW ver) {
  using  get_version_func_t = DWORD (WINAPI *)(OSVERSIONINFOEXW*);

  // We'll load the library directly to avoid depending on the NTDDK.
  HMODULE ntdll_lib = LoadLibraryW(L"Ntdll.dll");
  if (!ntdll_lib) {
    return -EINVAL;
  }

  // The standard "GetVersion" returned values depend on the application
  // manifest. We'll get the "real" version by using the Rtl* version.
  auto get_version_func = (
    get_version_func_t)GetProcAddress(ntdll_lib, "RtlGetVersion");
  int ret = 0;
  if (!get_version_func || get_version_func(ver)) {
    // RtlGetVersion returns non-zero values in case of errors.
    ret = -EINVAL;
  }

  FreeLibrary(ntdll_lib);
  return ret;
}
#endif

void collect_sys_info(map<string, string> *m, CephContext *cct)
{
  // version
  (*m)["ceph_version"] = pretty_version_to_str();
  (*m)["ceph_version_short"] = ceph_version_to_str();
  (*m)["ceph_release"] = ceph_release_to_str();

  #ifndef _WIN32
  // 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;
  }
  #else
  OSVERSIONINFOEXW ver = {0};
  ver.dwOSVersionInfoSize = sizeof(ver);
  get_windows_version(&ver);

  char version_str[64];
  snprintf(version_str, 64, "%lu.%lu (%lu)",
           ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber);

  char hostname[64];
  DWORD hostname_sz = sizeof(hostname);
  GetComputerNameA(hostname, &hostname_sz);

  SYSTEM_INFO sys_info;
  const char* arch_str;
  GetNativeSystemInfo(&sys_info);

  switch (sys_info.wProcessorArchitecture) {
    case PROCESSOR_ARCHITECTURE_AMD64:
      arch_str = "x86_64";
      break;
    case PROCESSOR_ARCHITECTURE_INTEL:
      arch_str = "x86";
      break;
    case PROCESSOR_ARCHITECTURE_ARM:
      arch_str = "arm";
      break;
    default:
      arch_str = "unknown";
      break;
  }

  (*m)["os"] = "Windows";
  (*m)["kernel_version"] = version_str;
  (*m)["kernel_description"] = version_str;
  (*m)["hostname"] = hostname;
  (*m)["arch"] = arch_str;
  #endif

  // but wait, am i in a container?
  bool in_container = false;

  if (const char *pod_name = getenv("POD_NAME")) {
    (*m)["pod_name"] = pod_name;
    in_container = true;
  }
  if (const char *container_name = getenv("CONTAINER_NAME")) {
    (*m)["container_name"] = container_name;
    in_container = true;
  }
  if (const char *container_image = getenv("CONTAINER_IMAGE")) {
    (*m)["container_image"] = container_image;
    in_container = true;
  }
  if (in_container) {
    if (const char *node_name = getenv("NODE_NAME")) {
      (*m)["container_hostname"] = (*m)["hostname"];
      (*m)["hostname"] = node_name;
    }
    if (const char *ns = getenv("POD_NAMESPACE")) {
      (*m)["pod_namespace"] = ns;
    }
  }

#ifdef __APPLE__
  // memory
  {
    uint64_t size;
    size_t len = sizeof(size);
    r = sysctlbyname("hw.memsize", &size, &len, NULL, 0);
    if (r == 0) {
      (*m)["mem_total_kb"] = std::to_string(size);
    }
  }
  {
    xsw_usage vmusage;
    size_t len = sizeof(vmusage);
    r = sysctlbyname("vm.swapusage", &vmusage, &len, NULL, 0);
    if (r == 0) {
      (*m)["mem_swap_kb"] = std::to_string(vmusage.xsu_total);
    }
  }
  // processor
  {
    char buf[100];
    size_t len = sizeof(buf);
    r = sysctlbyname("machdep.cpu.brand_string", buf, &len, NULL, 0);
    if (r == 0) {
      buf[len - 1] = '\0';
      (*m)["cpu"] = buf;
    }
  }
#elif !defined(_WIN32)
  // memory
  if (std::ifstream f{PROCPREFIX "/proc/meminfo"}; !f.fail()) {
    for (std::string line; std::getline(f, line); ) {
      std::vector<string> parts;
      boost::split(parts, line, boost::is_any_of(":\t "), boost::token_compress_on);
      if (parts.size() != 3) {
	continue;
      }
      if (parts[0] == "MemTotal") {
	(*m)["mem_total_kb"] = parts[1];
      } else if (parts[0] == "SwapTotal") {
	(*m)["mem_swap_kb"] = parts[1];
      }
    }
  }
  uint64_t cgroup_limit;
  if (get_cgroup_memory_limit(&cgroup_limit) == 0 &&
      cgroup_limit > 0) {
    (*m)["mem_cgroup_limit"] = std::to_string(cgroup_limit);
  }

  // processor
  if (std::ifstream f{PROCPREFIX "/proc/cpuinfo"}; !f.fail()) {
    for (std::string line; std::getline(f, line); ) {
      std::vector<string> parts;
      boost::split(parts, line, boost::is_any_of(":"));
      if (parts.size() != 2) {
	continue;
      }
      boost::trim(parts[0]);
      boost::trim(parts[1]);
      if (parts[0] == "model name") {
	(*m)["cpu"] = parts[1];
	break;
      }
    }
  }
#endif
  // distro info
  distro_detect(m, cct);
}

void dump_services(Formatter* f, const map<string, list<int> >& services, const char* type)
{
  ceph_assert(f);

  f->open_object_section(type);
  for (auto host = services.begin();
       host != services.end(); ++host) {
    f->open_array_section(host->first.c_str());
    const list<int>& hosted = host->second;
    for (auto s = hosted.cbegin();
	 s != hosted.cend(); ++s) {
      f->dump_int(type, *s);
    }
    f->close_section();
  }
  f->close_section();
}

void dump_services(Formatter* f, const map<string, list<string> >& services, const char* type)
{
  ceph_assert(f);

  f->open_object_section(type);
  for (const auto& host : services) {
    f->open_array_section(host.first.c_str());
    const auto& hosted = host.second;
    for (const auto& s : hosted) {
      f->dump_string(type, s);
    }
    f->close_section();
  }
  f->close_section();
}

// If non-printable characters found then convert bufferlist to
// base64 encoded string indicating whether it did.
string cleanbin(bufferlist &bl, bool &base64, bool show)
{
  bufferlist::iterator it;
  for (it = bl.begin(); it != bl.end(); ++it) {
    if (iscntrl(*it))
      break;
  }
  if (it == bl.end()) {
    base64 = false;
    string result(bl.c_str(), bl.length());
    return result;
  }

  bufferlist b64;
  bl.encode_base64(b64);
  string encoded(b64.c_str(), b64.length());
  if (show)
    encoded = "Base64:" + encoded;
  base64 = true;
  return encoded;
}

// If non-printable characters found then convert to "Base64:" followed by
// base64 encoding
string cleanbin(string &str)
{
  bool base64;
  bufferlist bl;
  bl.append(str);
  string result = cleanbin(bl, base64, true);
  return result;
}

std::string bytes2str(uint64_t count) {
  static char s[][2] = {"\0", "k", "M", "G", "T", "P", "E", "\0"};
  int i = 0;
  while (count >= 1024 && *s[i+1]) {
    count >>= 10;
    i++;
  }
  char str[128];
  snprintf(str, sizeof str, "%" PRIu64 "%sB", count, s[i]);
  return std::string(str);
}