38{
40 const ConfigParser config(input.cmdOptionExists(
"--config") ? input.getCmdOption(
"--config") :
"config.json");
41 const MainLogger logger(config.get(
"LOKI_ADDRESS"), config.get(
"SENTRY_ADDRESS"));
42
43
44 if (curl_global_init(CURL_GLOBAL_DEFAULT) < 0)
45 {
46 spdlog::critical("Can't init curl");
47 return EXIT_FAILURE;
48 }
49
50
51
52
53
54
55
56
57
58
59 if (input.cmdOptionExists("-v"))
60 {
61 spdlog::set_level(spdlog::level::info);
62 }
63 if (input.cmdOptionExists("-vv"))
64 {
65 spdlog::set_level(spdlog::level::debug);
66 }
67 if (input.cmdOptionExists("-vvv"))
68 {
69 spdlog::set_level(spdlog::level::trace);
70 }
71
72
73
74
75
76
77
78
79
80
81 spdlog::debug("======== Detected input arguments ========");
82 for (const auto &entry : input.getCmdOptions())
83 {
84 spdlog::debug("{} = {}", entry.first, entry.second);
85 }
86
87
88 spdlog::debug("======== Detected configuration ========");
89 for (const auto &entry : config.getConfigMap())
90 {
91 spdlog::debug("{} = {}", entry.first, entry.second);
92 }
93
94
95 if (std::signal(SIGINT, interruptFunc) == SIG_ERR)
96 {
97 spdlog::critical(
"Can't set signal handler (SIGINT): {}",
getErrnoString(errno));
98 return EXIT_FAILURE;
99 }
100
101
102 if (std::signal(SIGTERM, interruptFunc) == SIG_ERR)
103 {
104 spdlog::critical(
"Can't set signal handler (SIGTERM): {}",
getErrnoString(errno));
105 return EXIT_FAILURE;
106 }
107
108
109 if (std::signal(SIGALRM, alarmFunc) == SIG_ERR)
110 {
111 spdlog::critical(
"Can't set signal handler (SIGALRM): {}",
getErrnoString(errno));
112 return EXIT_FAILURE;
113 }
115
116
117 std::unique_ptr<Tracer> crashpadController(nullptr);
118 vCheckFlag.emplace_back(
"Crashpad Handler", std::make_shared<std::atomic_flag>(
false));
119 crashpadController = std::make_unique<Tracer>(
120 vCheckFlag[
vCheckFlag.size() - 1].second, config.get(
"CRASHPAD_REMOTE"), config.get(
"CRASHPAD_PROXY"),
121 config.get("CRASHPAD_EXECUTABLE_PATH"), config.get("CRASHPAD_REPORT_DIR"));
122
123
124 std::unique_ptr<PrometheusServer> mainPrometheusServer(nullptr);
125 const std::string prometheusAddr = input.getCmdOption("--enable-prometheus");
126 if (!prometheusAddr.empty())
127 {
128 try
129 {
130 mainPrometheusServer = std::make_unique<PrometheusServer>(prometheusAddr);
131 spdlog::info("Prometheus server start at {}", prometheusAddr);
132 }
133 catch (const std::exception &e)
134 {
135 spdlog::error("Can't start Prometheus Server: {}", e.what());
136 return EXIT_FAILURE;
137 }
138 }
139
140
141 std::unique_ptr<ProcessMetrics> selfMonitor(nullptr);
142 vCheckFlag.emplace_back(
"Self Monitor", std::make_shared<std::atomic_flag>(
false));
143 if (mainPrometheusServer)
144 {
146 mainPrometheusServer->createNewRegistry());
147 }
148
149
150 std::unique_ptr<ZeroMQServer> zmqController(nullptr);
151 vCheckFlag.emplace_back(
"ZeroMQ Server", std::make_shared<std::atomic_flag>(
false));
152 const std::string zeromqServerAddr = input.getCmdOption("--enable-zeromq");
153 if (!zeromqServerAddr.empty())
154 {
155 try
156 {
157 zmqController = std::make_unique<ZeroMQServer>(
159 mainPrometheusServer ? mainPrometheusServer->createNewRegistry() : nullptr);
161 zmqController->initialise();
162 }
163 catch (const std::exception &e)
164 {
165 spdlog::error("Can't start ZeroMQ Server: {}", e.what());
166 return EXIT_FAILURE;
167 }
168 }
169
170
171 std::shared_ptr<TelnetServer> telnetController(nullptr);
172 vCheckFlag.emplace_back(
"Telnet Server", std::make_shared<std::atomic_flag>(
false));
173 const unsigned long telnetPort =
174 input.cmdOptionExists("--enable-telnet") ? std::stoul(input.getCmdOption("--enable-telnet")) : 0;
175 if (telnetPort > 0 && telnetPort < 65536)
176 {
177 try
178 {
179 telnetController = std::make_shared<TelnetServer>();
184 mainPrometheusServer ? mainPrometheusServer->createNewRegistry() : nullptr);
185 }
186 catch (const std::exception &e)
187 {
188 spdlog::error("Can't start Telnet Server: {}", e.what());
189 return EXIT_FAILURE;
190 }
191 }
192 else if (telnetPort != 0)
193 {
194 spdlog::error("Invalid Telnet port: {}", telnetPort);
195 return EXIT_FAILURE;
196 }
197
198
199
200
201
202
203
204
205
206 while (interruptFlag == 0)
207 {
208 std::this_thread::sleep_for(std::chrono::milliseconds(500));
209 }
210
211
212
213
214
215
216
217
218
219 curl_global_cleanup();
220
221 return EXIT_SUCCESS;
222}
std::vector< std::pair< std::string, std::shared_ptr< std::atomic_flag > > > vCheckFlag
Global variable to check if the servers are running.
std::string getErrnoString(int errVal)
void TelnetConnectedCallback(const SP_TelnetSession &session)
std::string TelnetTabCallback(const SP_TelnetSession &session, std::string_view line)
bool TelnetMessageCallback(const SP_TelnetSession &session, const std::string &line)
bool ZeroMQServerMessageCallback(const std::vector< zmq::message_t > &recvMsgs, std::vector< zmq::message_t > &replyMsgs)
constexpr uintmax_t alarmInterval