obmon  0.0.0
 All Classes Functions Variables Enumerations Groups Pages
obmon-broker.cpp
1 #include <czmq.h>
2 #include <getopt.h>
3 #include <iostream>
4 #include <map>
5 #include <spdlog/fmt/ostr.h>
6 #include <spdlog/spdlog.h>
7 #include <utility>
8 
9 using namespace std;
10 
11 shared_ptr<spdlog::logger> logger = nullptr;
12 
13 void help() {
14  logger->info("obmon-broker -i tcp://*:5000 -o tcp://*:5001");
15 }
16 
17 int main(int argc, char **argv) {
18 
19  // creating logger
20  auto console = spdlog::stdout_logger_mt("console");
21  logger = spdlog::get("console");
22 
23  char *short_options = strdup("i:o:h");
24  struct option long_options[] = {{"in", 1, NULL, 'i'},
25  {"out", 1, NULL, 'o'},
26  {"help", 0, NULL, 'h'},
27  {NULL, 0, NULL, 0}};
28 
29  string in = "@tcp://*:5000";
30  string out = "@tcp://*:5001";
31  char nextOption = getopt_long(argc, argv, short_options, long_options, NULL);
32  while (nextOption != -1) {
33  switch (nextOption) {
34  case 'i':
35  in = "@";
36  in.append(optarg);
37  break;
38  case 'o':
39  out = "@";
40  out.append(optarg);
41  break;
42  case 'h':
43  help();
44  exit(0);
45  break;
46  default:
47  help();
48  exit(1);
49  }
50  nextOption = getopt_long(argc, argv, short_options, long_options, NULL);
51  }
52 
53  // Setting log level
54  int debugLevel = 0;
55  if (getenv("OBMON_LOG")) {
56  int rc = sscanf(getenv("OBMON_LOG"), "%d", &debugLevel);
57  if (!rc) {
58  logger->error("salsa: OBMON_LOG is not a number: {}",
59  getenv("OBMON_LOG"));
60  return 2;
61  }
62 
63  if (debugLevel < 0)
64  debugLevel = 0;
65 
66  if (debugLevel > 6)
67  debugLevel = 6;
68 
69  logger->info(
70  "Setting log level to '{}' ...",
71  spdlog::level::to_str((spdlog::level::level_enum)(6 - debugLevel)));
72  spdlog::set_level((spdlog::level::level_enum)(6 - debugLevel));
73  }
74 
75  map<string, string> state;
76  logger->info("Connected to in={} out={}", in, out);
77 
78  zsock_t *sub = zsock_new_sub(in.data(), "");
79  assert(sub);
80 
81  zsock_t *pub = zsock_new_pub(out.data());
82  assert(pub);
83  while (!zsys_interrupted) {
84  zmsg_t *msg_in = zmsg_recv(sub);
85  if (!msg_in)
86  break;
87 
88  char *sub_str = zmsg_popstr(msg_in);
89  char *cluster = zmsg_popstr(msg_in);
90  char *host = zmsg_popstr(msg_in);
91  char *json_str = zmsg_popstr(msg_in);
92  zmsg_destroy(&msg_in);
93 
94  state[host] = json_str;
95 
96  string json = "[";
97  for (auto &kv : state) {
98  // cout << kv.first << " has value " << kv.second << endl;
99  json += "{ \"name\" : \"";
100  json += kv.first;
101  json += "\", \"data\" : { ";
102  json += kv.second;
103  json += "} }";
104  json += ",";
105  }
106 
107  if (json.back() == ',')
108  json.pop_back();
109 
110  json += "]";
111 
112  logger->trace("{}", json);
113 
114  zmsg_t *msg_out = zmsg_new();
115  zmsg_addstr(msg_out, sub_str);
116  zmsg_addstr(msg_out, cluster);
117  zmsg_addstr(msg_out, host);
118  zmsg_addstr(msg_out, json.data());
119 
120  zmsg_send(&msg_out, pub);
121 
122  free(sub_str);
123  free(json_str);
124  free(host);
125  free(cluster);
126 
127  zmsg_destroy(&msg_out);
128  }
129  zsock_destroy(&sub);
130  zsock_destroy(&pub);
131 
132  // cleaning spdlog
133  spdlog::drop_all();
134 
135  return 0;
136 }