Repo-Init
 
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include "logging/Logger.hpp"
8#include "utils/Tracer.hpp"
10
11#include <curl/curl.h>
12#include <spdlog/spdlog.h>
13
14#include <csignal>
15
16// SIGALRM interval in seconds
17constexpr uintmax_t alarmInterval = 30;
18// Interruption flag
19// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
20volatile sig_atomic_t interruptFlag = 0;
21
22namespace
23{
24 // Default SIGALRM function
25 void alarmFunc(int /*unused*/)
26 {
27 alarm(alarmInterval);
28
29 // Clear all flags
30 std::for_each(vCheckFlag.begin(), vCheckFlag.end(), [](auto &entry) { entry.second->clear(); });
31 }
32
33 void interruptFunc(int /*unused*/) { interruptFlag = 1; }
34} // namespace
35
36int main(int argc, char **argv)
37{
38 const InputParser input(argc, argv);
39 const ConfigParser config(input.cmdOptionExists("--config") ? input.getCmdOption("--config") : "config.json");
40 const MainLogger logger(config.get("LOKI_ADDRESS"), config.get("SENTRY_ADDRESS"));
41
42 // Initialize curl as soon as possible
43 if (curl_global_init(CURL_GLOBAL_DEFAULT) < 0)
44 {
45 spdlog::critical("Can't init curl");
46 return EXIT_FAILURE;
47 }
48
49 // Adjust log level
50 if (input.cmdOptionExists("-v"))
51 {
52 spdlog::set_level(spdlog::level::info);
53 }
54 if (input.cmdOptionExists("-vv"))
55 {
56 spdlog::set_level(spdlog::level::debug);
57 }
58 if (input.cmdOptionExists("-vvv"))
59 {
60 spdlog::set_level(spdlog::level::trace);
61 }
62
63 // Log input arguments
64 spdlog::debug("======== Detected input arguments ========");
65 for (const auto &entry : input.getCmdOptions())
66 {
67 spdlog::debug("{} = {}", entry.first, entry.second);
68 }
69
70 // Log detected configuration to debug
71 spdlog::debug("======== Detected configuration ========");
72 for (const auto &entry : config.getConfigMap())
73 {
74 spdlog::debug("{} = {}", entry.first, entry.second);
75 }
76
77 // Register interrupt signal handler
78 if (std::signal(SIGINT, interruptFunc) == SIG_ERR)
79 {
80 spdlog::critical("Can't set signal handler (SIGINT): {}", getErrnoString(errno));
81 return EXIT_FAILURE;
82 }
83
84 // Register termination signal handler
85 if (std::signal(SIGTERM, interruptFunc) == SIG_ERR)
86 {
87 spdlog::critical("Can't set signal handler (SIGTERM): {}", getErrnoString(errno));
88 return EXIT_FAILURE;
89 }
90
91 // Register alarm signal handler
92 if (std::signal(SIGALRM, alarmFunc) == SIG_ERR)
93 {
94 spdlog::critical("Can't set signal handler (SIGALRM): {}", getErrnoString(errno));
95 return EXIT_FAILURE;
96 }
97 alarm(alarmInterval);
98
99 // Initialize Crashpad handler
100 std::unique_ptr<Tracer> crashpadController(nullptr);
101 vCheckFlag.emplace_back("Crashpad Handler", std::make_shared<std::atomic_flag>(false));
102 crashpadController = std::make_unique<Tracer>(
103 vCheckFlag[vCheckFlag.size() - 1].second, config.get("CRASHPAD_REMOTE"), config.get("CRASHPAD_PROXY"),
104 config.get("CRASHPAD_EXECUTABLE_DIR"), config.get("CRASHPAD_REPORT_DIR"));
105
106 // Initialize Prometheus server
107 std::unique_ptr<PrometheusServer> mainPrometheusServer(nullptr);
108 const std::string prometheusAddr = input.getCmdOption("--enable-prometheus");
109 if (!prometheusAddr.empty())
110 {
111 try
112 {
113 mainPrometheusServer = std::make_unique<PrometheusServer>(prometheusAddr);
114 spdlog::info("Prometheus server start at {}", prometheusAddr);
115 }
116 catch (const std::exception &e)
117 {
118 spdlog::error("Can't start Prometheus Server: {}", e.what());
119 return EXIT_FAILURE;
120 }
121 }
122
123 // Initialize self monitoring
124 std::unique_ptr<ProcessMetrics> selfMonitor(nullptr);
125 vCheckFlag.emplace_back("Self Monitor", std::make_shared<std::atomic_flag>(false));
126 if (mainPrometheusServer)
127 {
128 selfMonitor = std::make_unique<ProcessMetrics>(vCheckFlag[vCheckFlag.size() - 1].second,
129 mainPrometheusServer->createNewRegistry());
130 }
131
132 // Initialize ZeroMQ server
133 std::unique_ptr<ZeroMQServer> zmqController(nullptr);
134 vCheckFlag.emplace_back("ZeroMQ Server", std::make_shared<std::atomic_flag>(false));
135 const std::string zeromqServerAddr = input.getCmdOption("--enable-zeromq");
136 if (!zeromqServerAddr.empty())
137 {
138 try
139 {
140 zmqController = std::make_unique<ZeroMQServer>(
141 zeromqServerAddr, vCheckFlag[vCheckFlag.size() - 1].second,
142 mainPrometheusServer ? mainPrometheusServer->createNewRegistry() : nullptr);
143 zmqController->messageCallback(ZeroMQServerMessageCallback);
144 zmqController->initialise();
145 }
146 catch (const std::exception &e)
147 {
148 spdlog::error("Can't start ZeroMQ Server: {}", e.what());
149 return EXIT_FAILURE;
150 }
151 }
152
153 // Initialize Telnet server
154 std::unique_ptr<TelnetServer> telnetController(nullptr);
155 vCheckFlag.emplace_back("Telnet Server", std::make_shared<std::atomic_flag>(false));
156 const unsigned long telnetPort =
157 input.cmdOptionExists("--enable-telnet") ? std::stoul(input.getCmdOption("--enable-telnet")) : 0;
158 if (telnetPort > 0 && telnetPort < 65536)
159 {
160 try
161 {
162 telnetController = std::make_unique<TelnetServer>();
163 telnetController->connectedCallback(TelnetConnectedCallback);
164 telnetController->newLineCallback(TelnetMessageCallback);
165 telnetController->tabCallback(TelnetTabCallback);
166 telnetController->initialise(telnetPort, vCheckFlag[vCheckFlag.size() - 1].second, "> ",
167 mainPrometheusServer ? mainPrometheusServer->createNewRegistry() : nullptr);
168 }
169 catch (const std::exception &e)
170 {
171 spdlog::error("Can't start Telnet Server: {}", e.what());
172 return EXIT_FAILURE;
173 }
174 }
175 else if (telnetPort != 0)
176 {
177 spdlog::error("Invalid Telnet port: {}", telnetPort);
178 return EXIT_FAILURE;
179 }
180
181 /* ################################################################################### */
182 /* ############################# MAKE MODIFICATIONS HERE ############################# */
183 /* ################################################################################### */
184
185 /* ################################################################################### */
186 /* ################################ END MODIFICATIONS ################################ */
187 /* ################################################################################### */
188
189 while (interruptFlag == 0)
190 {
191 std::this_thread::sleep_for(std::chrono::milliseconds(500));
192 }
193
194 /* ################################################################################### */
195 /* ############################# MAKE MODIFICATIONS HERE ############################# */
196 /* ################################################################################### */
197
198 /* ################################################################################### */
199 /* ################################ END MODIFICATIONS ################################ */
200 /* ################################################################################### */
201
202 curl_global_cleanup();
203
204 return EXIT_SUCCESS;
205}
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)
const std::string & getCmdOption(const std::string &option) const
std::vector< std::pair< std::string, std::string > > getCmdOptions() const
bool cmdOptionExists(const std::string &option) const
volatile sig_atomic_t interruptFlag
Definition main.cpp:20
constexpr uintmax_t alarmInterval
Definition main.cpp:17
int main(int argc, char **argv)
Definition main.cpp:36