obmon  1.3.2
obmon-sensor.cpp
1 #include <ObSensorGpu.h>
2 #include <ObSensorSystem.h>
3 #include <czmq.h>
4 #include <getopt.h>
5 #include <glibtop/cpu.h>
6 #include <spdlog/fmt/ostr.h>
7 #include <spdlog/sinks/stdout_color_sinks.h>
8 #include <spdlog/spdlog.h>
9 #include <utility>
10 #include <vector>
11 using namespace std;
12 
13 static shared_ptr<spdlog::logger> logger = nullptr;
14 
15 void help();
16 void help() { logger->info("obmon-sensor -o tcp://localhost:5000 -t 1000"); }
17 
18 int main(int argc, char **argv) {
19 
20  // creating logger
21  auto console = spdlog::stdout_color_mt("console");
22  logger = spdlog::get("console");
23 
24  char *short_options = strdup("o:t:c:h");
25  struct option long_options[] = {{"out", 1, nullptr, 'o'},
26  {"timeout", 1, nullptr, 't'},
27  {"cluster", 1, nullptr, 'c'},
28  {"help", 0, nullptr, 'h'},
29  {nullptr, 0, nullptr, 0}};
30 
31  string out = ">tcp://localhost:5000";
32  string cluster_name = "wn";
33  int timeout = 1000;
34  string hostname = zsys_hostname();
35  string json;
36 
37  int nextOption =
38  getopt_long(argc, argv, short_options, long_options, nullptr);
39  while (nextOption != -1) {
40  switch (nextOption) {
41  case 'o':
42  out = ">";
43  out.append(optarg);
44  break;
45  case 't':
46  timeout = atoi(optarg);
47  break;
48  case 'c':
49  cluster_name = optarg;
50  break;
51  case 'h':
52  help();
53  exit(0);
54  // break; <-- Unreachable code - emits warning
55  default:
56  help();
57  exit(1);
58  }
59  nextOption = getopt_long(argc, argv, short_options, long_options, nullptr);
60  }
61 
62  // Setting log level
63  int debugLevel = 0;
64  if (getenv("OBMON_LOG")) {
65  int rc = sscanf(getenv("OBMON_LOG"), "%d", &debugLevel);
66  if (!rc) {
67  logger->error("salsa: OBMON_LOG is not a number: {}",
68  getenv("OBMON_LOG"));
69  return 2;
70  }
71 
72  if (debugLevel < 0)
73  debugLevel = 0;
74 
75  if (debugLevel > 6)
76  debugLevel = 6;
77 
78  logger->info("Setting log level to '{}' ...",
79  spdlog::level::to_string_view(
80  static_cast<spdlog::level::level_enum>(6 - debugLevel)));
81  spdlog::set_level(static_cast<spdlog::level::level_enum>(6 - debugLevel));
82  }
83 
84  zsock_t *pub = zsock_new_pub(out.data());
85  assert(pub);
86  assert(zsock_resolve(pub) != pub);
87  assert(streq(zsock_type_str(pub), "PUB"));
88  logger->info("Connected to {}", out);
89 
90  ObSensor s(hostname);
91  s.sensors().push_back(new ObSensorSystem("sys"));
92  s.sensors().push_back(new ObSensorGpu("gpu"));
93  s.init();
94  s.update(static_cast<unsigned int>(timeout));
95  zclock_sleep(timeout);
96  while (!zsys_interrupted) {
97  s.update(static_cast<unsigned int>(timeout));
98 
99  json = s.json();
100  logger->debug("Sending : {}", json.data());
101  zmsg_t *msg = zmsg_new();
102  zmsg_addstr(msg, "");
103  zmsg_addstrf(msg, "%s", cluster_name.data());
104  zmsg_addstrf(msg, "%s_%d", hostname.data(), getpid());
105  zmsg_addstrf(msg, "%s", json.data());
106  zmsg_send(&msg, pub);
107  zclock_sleep(timeout);
108  zmsg_destroy(&msg);
109  }
110 
111  zsock_destroy(&pub);
112 
113  // cleaning spdlog
114  spdlog::drop_all();
115 
116  return 0;
117 }
System Obmon sensor class
GPU Obmon sensor class
Definition: ObSensorGpu.h:23
Base Obmon sensor class
Definition: ObSensor.h:19