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