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
19namespace
20{
21 // Interruption flag
22 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
23 volatile sig_atomic_t interruptFlag = 0;
24
25 // Default SIGALRM function
26 void alarmFunc(int /*unused*/)
27 {
28 alarm(alarmInterval);
29
30 // Clear all flags
31 std::for_each(vCheckFlag.begin(), vCheckFlag.end(), [](auto &entry) { entry.second->clear(); });
32 }
33
34 void interruptFunc(int /*unused*/) { interruptFlag = 1; }
35} // namespace
36
37int main(int argc, char **argv)
38{
39 const InputParser input(argc, argv);
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 // Initialize curl as soon as possible
44 if (curl_global_init(CURL_GLOBAL_DEFAULT) < 0)
45 {
46 spdlog::critical("Can't init curl");
47 return EXIT_FAILURE;
48 }
49
50 // Adjust log level
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 // Log input arguments
65 spdlog::debug("======== Detected input arguments ========");
66 for (const auto &entry : input.getCmdOptions())
67 {
68 spdlog::debug("{} = {}", entry.first, entry.second);
69 }
70
71 // Log detected configuration to debug
72 spdlog::debug("======== Detected configuration ========");
73 for (const auto &entry : config.getConfigMap())
74 {
75 spdlog::debug("{} = {}", entry.first, entry.second);
76 }
77
78 // Register interrupt signal handler
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 // Register termination signal handler
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 // Register alarm signal handler
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 }
98 alarm(alarmInterval);
99
100 // Initialize Crashpad handler
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 // Initialize Prometheus server
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 // Initialize self monitoring
125 std::unique_ptr<ProcessMetrics> selfMonitor(nullptr);
126 vCheckFlag.emplace_back("Self Monitor", std::make_shared<std::atomic_flag>(false));
127 if (mainPrometheusServer)
128 {
129 selfMonitor = std::make_unique<ProcessMetrics>(vCheckFlag[vCheckFlag.size() - 1].second,
130 mainPrometheusServer->createNewRegistry());
131 }
132
133 // Initialize ZeroMQ server
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>(
142 zeromqServerAddr, vCheckFlag[vCheckFlag.size() - 1].second,
143 mainPrometheusServer ? mainPrometheusServer->createNewRegistry() : nullptr);
144 zmqController->messageCallback(ZeroMQServerMessageCallback);
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 // Initialize Telnet server
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>();
164 telnetController->connectedCallback(TelnetConnectedCallback);
165 telnetController->newLineCallback(TelnetMessageCallback);
166 telnetController->tabCallback(TelnetTabCallback);
167 telnetController->initialise(telnetPort, vCheckFlag[vCheckFlag.size() - 1].second, "> ",
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 /* ############################# MAKE MODIFICATIONS HERE ############################# */
184 /* ################################################################################### */
185
186 /* ################################################################################### */
187 /* ################################ END MODIFICATIONS ################################ */
188 /* ################################################################################### */
189
190 while (interruptFlag == 0)
191 {
192 std::this_thread::sleep_for(std::chrono::milliseconds(500));
193 }
194
195 /* ################################################################################### */
196 /* ############################# MAKE MODIFICATIONS HERE ############################# */
197 /* ################################################################################### */
198
199 /* ################################################################################### */
200 /* ################################ END MODIFICATIONS ################################ */
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)
std::string get(const std::string &key) const
const std::unordered_map< std::string, std::string > & getConfigMap() const
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
constexpr uintmax_t alarmInterval
Definition main.cpp:17
int main(int argc, char **argv)
Definition main.cpp:37