blob: 1d3cd5f77b29344a927e9cde132d44b09ddf1d4a [file] [log] [blame]
Tomáš Peckaa193c032020-06-22 17:33:31 +02001#include <docopt.h>
2#include <future>
Tomáš Peckae4c82ee2022-07-13 19:42:18 +02003#include <health/outputs/AlarmsOutputs.h>
Tomáš Peckaa193c032020-06-22 17:33:31 +02004#include <sdbus-c++/sdbus-c++.h>
Tomáš Pecka5123e002020-06-05 15:29:21 +02005#include <spdlog/sinks/ansicolor_sink.h>
6#include <spdlog/spdlog.h>
Tomáš Peckae4c82ee2022-07-13 19:42:18 +02007#include <sysrepo-cpp/Connection.hpp>
Tomáš Peckaa193c032020-06-22 17:33:31 +02008#include "VELIA_VERSION.h"
Tomáš Pecka139e3612021-01-25 19:49:00 +01009#include "health/Factory.h"
Tomáš Pecka1d2b95c2023-05-11 16:01:46 +020010#include "health/SystemdUnits.h"
Tomáš Pecka261c8862020-11-05 11:23:08 +010011#include "health/outputs/callables.h"
Tomáš Pecka33563f52021-01-25 14:31:41 +010012#include "main.h"
Tomáš Peckaa193c032020-06-22 17:33:31 +020013#include "utils/exceptions.h"
Tomáš Peckaa4336812020-07-24 15:32:58 +020014#include "utils/journal.h"
Tomáš Pecka5123e002020-06-05 15:29:21 +020015#include "utils/log-init.h"
Tomáš Peckae4c82ee2022-07-13 19:42:18 +020016#include "utils/log.h"
Tomáš Pecka5123e002020-06-05 15:29:21 +020017
Tomáš Peckaa193c032020-06-22 17:33:31 +020018static const char usage[] =
Tomáš Pecka261c8862020-11-05 11:23:08 +010019 R"(Monitor system health status.
Tomáš Peckaa193c032020-06-22 17:33:31 +020020
21Usage:
Tomáš Peckacd19e622021-01-27 09:18:11 +010022 veliad-health
Tomáš Peckad03044f2020-11-12 14:33:10 +010023 [--appliance=<Model>]
Tomáš Pecka7c6b1d32020-11-25 14:17:21 +010024 [--health-log-level=<Level>]
Jan Kundrátad8169c2023-05-29 10:49:13 +020025 [--main-log-level=<Level>]
26 [--sysrepo-log-level=<Level>]
Tomáš Peckacd19e622021-01-27 09:18:11 +010027 veliad-health (-h | --help)
28 veliad-health --version
Tomáš Peckaa193c032020-06-22 17:33:31 +020029
30Options:
31 -h --help Show this screen.
32 --version Show version.
Václav Kubernát3da14672021-02-09 09:39:24 +010033 --health-log-level=<N> Log level for the health monitoring [default: 3]
Tomáš Peckaa193c032020-06-22 17:33:31 +020034 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
35 4 -> debug, 5 -> trace)
Jan Kundrátad8169c2023-05-29 10:49:13 +020036 --main-log-level=<N> Log level for other messages [default: 2]
37 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 2]
Tomáš Peckaa193c032020-06-22 17:33:31 +020038)";
39
Tomáš Pecka33563f52021-01-25 14:31:41 +010040DBUS_EVENTLOOP_INIT
Tomáš Peckae8239a72020-07-17 11:29:22 +020041
Tomáš Peckaa193c032020-06-22 17:33:31 +020042int main(int argc, char* argv[])
Tomáš Pecka5123e002020-06-05 15:29:21 +020043{
Tomáš Peckaa4336812020-07-24 15:32:58 +020044 std::shared_ptr<spdlog::sinks::sink> loggingSink;
45 if (velia::utils::isJournaldActive()) {
46 loggingSink = velia::utils::create_journald_sink();
47 } else {
48 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
49 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020050
Tomáš Peckacd19e622021-01-27 09:18:11 +010051 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-health " VELIA_VERSION, true);
Tomáš Peckaa193c032020-06-22 17:33:31 +020052
Tomáš Peckabd828862020-06-15 15:53:35 +020053 velia::utils::initLogs(loggingSink);
Tomáš Pecka5123e002020-06-05 15:29:21 +020054 spdlog::set_level(spdlog::level::info);
55
Tomáš Peckaa193c032020-06-22 17:33:31 +020056 try {
Tomáš Peckab1724862022-01-09 21:29:47 +010057 spdlog::get("health")->set_level(parseLogLevel("Health checker logger", args["--health-log-level"]));
Jan Kundrátad8169c2023-05-29 10:49:13 +020058 spdlog::get("main")->set_level(parseLogLevel("other messages", args["--main-log-level"]));
59 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
Tomáš Peckab1724862022-01-09 21:29:47 +010060
Tomáš Pecka33563f52021-01-25 14:31:41 +010061 DBUS_EVENTLOOP_START
Tomáš Peckaa193c032020-06-22 17:33:31 +020062
Tomáš Peckae4c82ee2022-07-13 19:42:18 +020063 auto srSessionAlarms = sysrepo::Connection{}.sessionStart();
64 srSessionAlarms.switchDatastore(sysrepo::Datastore::Operational);
Tomáš Peckaa193c032020-06-22 17:33:31 +020065
66 // output configuration
Tomáš Peckae4c82ee2022-07-13 19:42:18 +020067 std::vector<std::function<void(velia::health::State)>> outputHandlers;
Tomáš Peckad03044f2020-11-12 14:33:10 +010068 if (const auto& appliance = args["--appliance"]) {
Jan Kundrátcbc7a0b2021-02-18 09:47:29 +010069 spdlog::get("health")->debug("Initializing LED drivers");
Tomáš Peckae4c82ee2022-07-13 19:42:18 +020070 outputHandlers.emplace_back(velia::health::createOutput(appliance.asString()));
Tomáš Peckad03044f2020-11-12 14:33:10 +010071 }
Tomáš Peckae4c82ee2022-07-13 19:42:18 +020072 velia::health::AlarmsOutputs alarms(srSessionAlarms, outputHandlers);
Tomáš Peckaa193c032020-06-22 17:33:31 +020073
Jan Kundrátcbc7a0b2021-02-18 09:47:29 +010074 spdlog::get("health")->debug("All outputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020075
76 // input configuration
Tomáš Peckae4c82ee2022-07-13 19:42:18 +020077 spdlog::get("health")->debug("Starting DBus systemd units watcher");
78 auto srSessionSystemdUnits = sysrepo::Connection{}.sessionStart();
79 auto inputSystemdDbus = std::make_shared<velia::health::SystemdUnits>(srSessionSystemdUnits, *g_dbusConnection);
Tomáš Peckaa193c032020-06-22 17:33:31 +020080
Tomáš Pecka33563f52021-01-25 14:31:41 +010081 DBUS_EVENTLOOP_END;
Tomáš Peckaa193c032020-06-22 17:33:31 +020082
Tomáš Peckaa193c032020-06-22 17:33:31 +020083 return 0;
84 } catch (std::exception& e) {
85 velia::utils::fatalException(spdlog::get("main"), e, "main");
86 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020087}