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 if (input.cmdOptionExists("-v"))
52 {
53 spdlog::set_level(spdlog::level::info);
54 }
55 if (input.cmdOptionExists("-vv"))
56 {
57 spdlog::set_level(spdlog::level::debug);
58 }
59 if (input.cmdOptionExists("-vvv"))
60 {
61 spdlog::set_level(spdlog::level::trace);
62 }
63
64
65 spdlog::debug("======== Detected input arguments ========");
66 for (const auto &entry : input.getCmdOptions())
67 {
68 spdlog::debug("{} = {}", entry.first, entry.second);
69 }
70
71
72 spdlog::debug("======== Detected configuration ========");
73 for (const auto &entry : config.getConfigMap())
74 {
75 spdlog::debug("{} = {}", entry.first, entry.second);
76 }
77
78
79 if (std::signal(SIGINT, interruptFunc) == SIG_ERR)
80 {
81 spdlog::critical(
"Can't set signal handler (SIGINT): {}",
getErrnoString(errno));
82 return EXIT_FAILURE;
83 }
84
85
86 if (std::signal(SIGTERM, interruptFunc) == SIG_ERR)
87 {
88 spdlog::critical(
"Can't set signal handler (SIGTERM): {}",
getErrnoString(errno));
89 return EXIT_FAILURE;
90 }
91
92
93 if (std::signal(SIGALRM, alarmFunc) == SIG_ERR)
94 {
95 spdlog::critical(
"Can't set signal handler (SIGALRM): {}",
getErrnoString(errno));
96 return EXIT_FAILURE;
97 }
99
100
101 std::unique_ptr<Tracer> crashpadController(nullptr);
102 vCheckFlag.emplace_back(
"Crashpad Handler", std::make_shared<std::atomic_flag>(
false));
103 crashpadController = std::make_unique<Tracer>(
104 vCheckFlag[
vCheckFlag.size() - 1].second, config.get(
"CRASHPAD_REMOTE"), config.get(
"CRASHPAD_PROXY"),
105 config.get("CRASHPAD_EXECUTABLE_PATH"), config.get("CRASHPAD_REPORT_DIR"));
106
107
108 std::unique_ptr<PrometheusServer> mainPrometheusServer(nullptr);
109 const std::string prometheusAddr = input.getCmdOption("--enable-prometheus");
110 if (!prometheusAddr.empty())
111 {
112 try
113 {
114 mainPrometheusServer = std::make_unique<PrometheusServer>(prometheusAddr);
115 spdlog::info("Prometheus server start at {}", prometheusAddr);
116 }
117 catch (const std::exception &e)
118 {
119 spdlog::error("Can't start Prometheus Server: {}", e.what());
120 return EXIT_FAILURE;
121 }
122 }
123
124
125 std::unique_ptr<ProcessMetrics> selfMonitor(nullptr);
126 vCheckFlag.emplace_back(
"Self Monitor", std::make_shared<std::atomic_flag>(
false));
127 if (mainPrometheusServer)
128 {
130 mainPrometheusServer->createNewRegistry());
131 }
132
133
134 std::unique_ptr<ZeroMQServer> zmqController(nullptr);
135 vCheckFlag.emplace_back(
"ZeroMQ Server", std::make_shared<std::atomic_flag>(
false));
136 const std::string zeromqServerAddr = input.getCmdOption("--enable-zeromq");
137 if (!zeromqServerAddr.empty())
138 {
139 try
140 {
141 zmqController = std::make_unique<ZeroMQServer>(
143 mainPrometheusServer ? mainPrometheusServer->createNewRegistry() : nullptr);
145 zmqController->initialise();
146 }
147 catch (const std::exception &e)
148 {
149 spdlog::error("Can't start ZeroMQ Server: {}", e.what());
150 return EXIT_FAILURE;
151 }
152 }
153
154
155 std::unique_ptr<TelnetServer> telnetController(nullptr);
156 vCheckFlag.emplace_back(
"Telnet Server", std::make_shared<std::atomic_flag>(
false));
157 const unsigned long telnetPort =
158 input.cmdOptionExists("--enable-telnet") ? std::stoul(input.getCmdOption("--enable-telnet")) : 0;
159 if (telnetPort > 0 && telnetPort < 65536)
160 {
161 try
162 {
163 telnetController = std::make_unique<TelnetServer>();
168 mainPrometheusServer ? mainPrometheusServer->createNewRegistry() : nullptr);
169 }
170 catch (const std::exception &e)
171 {
172 spdlog::error("Can't start Telnet Server: {}", e.what());
173 return EXIT_FAILURE;
174 }
175 }
176 else if (telnetPort != 0)
177 {
178 spdlog::error("Invalid Telnet port: {}", telnetPort);
179 return EXIT_FAILURE;
180 }
181
182
183
184
185
186
187
188
189
190 while (interruptFlag == 0)
191 {
192 std::this_thread::sleep_for(std::chrono::milliseconds(500));
193 }
194
195
196
197
198
199
200
201
202
203 curl_global_cleanup();
204
205 return EXIT_SUCCESS;
206}
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