blob: cc8f0e65dd207f4675c4bf994ce35399ab05b7a6 [file] [log] [blame]
Tomáš Peckaa193c032020-06-22 17:33:31 +02001#include <docopt.h>
2#include <future>
3#include <sdbus-c++/sdbus-c++.h>
Tomáš Pecka5123e002020-06-05 15:29:21 +02004#include <spdlog/sinks/ansicolor_sink.h>
5#include <spdlog/spdlog.h>
Tomáš Peckad03044f2020-11-12 14:33:10 +01006#include <sysrepo-cpp/Session.hpp>
7#include "Factory.h"
Tomáš Peckaa193c032020-06-22 17:33:31 +02008#include "VELIA_VERSION.h"
Tomáš Pecka261c8862020-11-05 11:23:08 +01009#include "health/inputs/DbusSystemdInput.h"
10#include "health/manager/StateManager.h"
Tomáš Pecka261c8862020-11-05 11:23:08 +010011#include "health/outputs/callables.h"
Tomáš Peckad03044f2020-11-12 14:33:10 +010012#include "ietf-hardware/sysrepo/Sysrepo.h"
Tomáš Pecka33563f52021-01-25 14:31:41 +010013#include "main.h"
Tomáš Peckaa193c032020-06-22 17:33:31 +020014#include "utils/exceptions.h"
Tomáš Peckaa4336812020-07-24 15:32:58 +020015#include "utils/journal.h"
Tomáš Pecka5123e002020-06-05 15:29:21 +020016#include "utils/log-init.h"
17
Tomáš Peckaa193c032020-06-22 17:33:31 +020018/** @short Extract log level from a CLI option */
19spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
20{
21 long x;
22 try {
23 x = option.asLong();
24 } catch (std::invalid_argument&) {
25 throw std::runtime_error(name + " log level: expecting integer");
26 }
27 static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
28 static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
29 if (x < 0 || x > 5)
30 throw std::runtime_error(name + " log level invalid or out-of-range");
31
32 return static_cast<spdlog::level::level_enum>(5 - x);
33}
34
35static const char usage[] =
Tomáš Pecka261c8862020-11-05 11:23:08 +010036 R"(Monitor system health status.
Tomáš Peckaa193c032020-06-22 17:33:31 +020037
38Usage:
39 veliad
Tomáš Peckad03044f2020-11-12 14:33:10 +010040 [--appliance=<Model>]
Tomáš Peckaa193c032020-06-22 17:33:31 +020041 [--log-level=<Level>]
Tomáš Pecka7c6b1d32020-11-25 14:17:21 +010042 [--health-log-level=<Level>]
Tomáš Pecka9f3035f2020-11-27 11:10:47 +010043 [--sysrepo-log-level=<Level>]
44 [--hardware-log-level=<Level>]
Tomáš Peckaf2391d62020-11-06 14:02:00 +010045 [--systemd-ignore-unit=<Unit>]...
Tomáš Peckaa193c032020-06-22 17:33:31 +020046 veliad (-h | --help)
47 veliad --version
48
49Options:
50 -h --help Show this screen.
51 --version Show version.
Tomáš Peckad03044f2020-11-12 14:33:10 +010052 --appliance=<Model> Initialize IETF Hardware and outputs for specific appliance.
Tomáš Peckaa193c032020-06-22 17:33:31 +020053 --log-level=<N> Log level for everything [default: 3]
Tomáš Peckaa193c032020-06-22 17:33:31 +020054 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
55 4 -> debug, 5 -> trace)
Tomáš Pecka7c6b1d32020-11-25 14:17:21 +010056 --health-log-level=<N> Log level for the health monitoring [default: 3]
Tomáš Pecka9f3035f2020-11-27 11:10:47 +010057 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3]
58 --hardware-log-level=<N> Log level for the hardware drivers [default: 3]
Tomáš Peckaf2391d62020-11-06 14:02:00 +010059 --systemd-ignore-unit=<Unit> Ignore state of systemd's unit in systemd state tracker. Can be specified multiple times.
Tomáš Peckaa193c032020-06-22 17:33:31 +020060)";
61
Tomáš Pecka33563f52021-01-25 14:31:41 +010062DBUS_EVENTLOOP_INIT
Tomáš Peckae8239a72020-07-17 11:29:22 +020063
Tomáš Peckaa193c032020-06-22 17:33:31 +020064int main(int argc, char* argv[])
Tomáš Pecka5123e002020-06-05 15:29:21 +020065{
Tomáš Peckaa4336812020-07-24 15:32:58 +020066 std::shared_ptr<spdlog::sinks::sink> loggingSink;
67 if (velia::utils::isJournaldActive()) {
68 loggingSink = velia::utils::create_journald_sink();
69 } else {
70 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
71 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020072
Tomáš Peckaa193c032020-06-22 17:33:31 +020073 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad " VELIA_VERSION, true);
74
Tomáš Peckabd828862020-06-15 15:53:35 +020075 velia::utils::initLogs(loggingSink);
Tomáš Pecka5123e002020-06-05 15:29:21 +020076 spdlog::set_level(spdlog::level::info);
77
Tomáš Peckaa193c032020-06-22 17:33:31 +020078 try {
79 spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
Tomáš Pecka9f3035f2020-11-27 11:10:47 +010080 spdlog::get("hardware")->set_level(parseLogLevel("Hardware loggers", args["--hardware-log-level"]));
81 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
82
Tomáš Peckad03044f2020-11-12 14:33:10 +010083 spdlog::get("main")->debug("Opening Sysrepo connection");
84 auto srConn = std::make_shared<sysrepo::Connection>();
85 auto srSess = std::make_shared<sysrepo::Session>(srConn);
86 auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSess);
87
88 // initialize ietf-hardware
89 spdlog::get("main")->debug("Initializing IETFHardware module");
90 std::shared_ptr<velia::ietf_hardware::IETFHardware> ietfHardware;
91 if (const auto& appliance = args["--appliance"]) {
92 ietfHardware = velia::ietf_hardware::create(appliance.asString());
93 } else {
94 ietfHardware = std::make_shared<velia::ietf_hardware::IETFHardware>();
95 }
96
97 spdlog::get("main")->debug("Initializing Sysrepo ietf-hardware callback");
98 auto sysrepoIETFHardware = velia::ietf_hardware::sysrepo::Sysrepo(srSubscription, ietfHardware);
99
Tomáš Pecka33563f52021-01-25 14:31:41 +0100100 DBUS_EVENTLOOP_START
Tomáš Peckaa193c032020-06-22 17:33:31 +0200101
Tomáš Peckad03044f2020-11-12 14:33:10 +0100102 // health
Tomáš Pecka261c8862020-11-05 11:23:08 +0100103 auto manager = std::make_shared<velia::health::StateManager>();
Tomáš Peckaa193c032020-06-22 17:33:31 +0200104
105 // output configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +0200106 spdlog::get("main")->debug("Initializing LED drivers");
Tomáš Peckad03044f2020-11-12 14:33:10 +0100107 if (const auto& appliance = args["--appliance"]) {
108 manager->m_outputSignal.connect(velia::health::createOutput(appliance.asString()));
109 }
Tomáš Peckaa193c032020-06-22 17:33:31 +0200110
Tomáš Pecka31caebd2020-08-21 11:14:10 +0200111 spdlog::get("main")->debug("All outputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +0200112
113 // input configuration
Tomáš Peckaf2391d62020-11-06 14:02:00 +0100114 std::set<std::string> ignoredUnits(args["--systemd-ignore-unit"].asStringList().begin(), args["--systemd-ignore-unit"].asStringList().end());
Tomáš Pecka31caebd2020-08-21 11:14:10 +0200115 spdlog::get("main")->debug("Starting DBus systemd watcher");
Tomáš Peckaf2391d62020-11-06 14:02:00 +0100116 if (!ignoredUnits.empty()) {
117 spdlog::get("main")->debug("Systemd input will ignore changes of the following units: {}", args["--systemd-ignore-unit"]);
118 }
Tomáš Pecka261c8862020-11-05 11:23:08 +0100119 auto inputSystemdDbus = std::make_shared<velia::health::DbusSystemdInput>(manager, ignoredUnits, *g_dbusConnection);
Tomáš Peckaa193c032020-06-22 17:33:31 +0200120
Tomáš Pecka31caebd2020-08-21 11:14:10 +0200121 spdlog::get("main")->debug("All inputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +0200122
Tomáš Pecka33563f52021-01-25 14:31:41 +0100123 DBUS_EVENTLOOP_END;
Tomáš Peckaa193c032020-06-22 17:33:31 +0200124
Tomáš Peckaa193c032020-06-22 17:33:31 +0200125 return 0;
126 } catch (std::exception& e) {
127 velia::utils::fatalException(spdlog::get("main"), e, "main");
128 }
Tomáš Pecka5123e002020-06-05 15:29:21 +0200129}