blob: 33316e51e8d900185f01b2ccf3dddefa78b70e7a [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>
Tomáš Peckaa193c032020-06-22 17:33:31 +02007#include "VELIA_VERSION.h"
Tomáš Pecka139e3612021-01-25 19:49:00 +01008#include "health/Factory.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áš Pecka139e3612021-01-25 19:49:00 +010012#include "ietf-hardware/Factory.h"
Tomáš Peckad03044f2020-11-12 14:33:10 +010013#include "ietf-hardware/sysrepo/Sysrepo.h"
Tomáš Pecka33563f52021-01-25 14:31:41 +010014#include "main.h"
Tomáš Peckaa193c032020-06-22 17:33:31 +020015#include "utils/exceptions.h"
Tomáš Peckaa4336812020-07-24 15:32:58 +020016#include "utils/journal.h"
Tomáš Pecka5123e002020-06-05 15:29:21 +020017#include "utils/log-init.h"
18
Tomáš Peckaa193c032020-06-22 17:33:31 +020019/** @short Extract log level from a CLI option */
20spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
21{
22 long x;
23 try {
24 x = option.asLong();
25 } catch (std::invalid_argument&) {
26 throw std::runtime_error(name + " log level: expecting integer");
27 }
28 static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
29 static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
30 if (x < 0 || x > 5)
31 throw std::runtime_error(name + " log level invalid or out-of-range");
32
33 return static_cast<spdlog::level::level_enum>(5 - x);
34}
35
36static const char usage[] =
Tomáš Pecka261c8862020-11-05 11:23:08 +010037 R"(Monitor system health status.
Tomáš Peckaa193c032020-06-22 17:33:31 +020038
39Usage:
40 veliad
Tomáš Peckad03044f2020-11-12 14:33:10 +010041 [--appliance=<Model>]
Tomáš Peckaa193c032020-06-22 17:33:31 +020042 [--log-level=<Level>]
Tomáš Pecka7c6b1d32020-11-25 14:17:21 +010043 [--health-log-level=<Level>]
Tomáš Pecka9f3035f2020-11-27 11:10:47 +010044 [--sysrepo-log-level=<Level>]
45 [--hardware-log-level=<Level>]
Tomáš Peckaf2391d62020-11-06 14:02:00 +010046 [--systemd-ignore-unit=<Unit>]...
Tomáš Peckaa193c032020-06-22 17:33:31 +020047 veliad (-h | --help)
48 veliad --version
49
50Options:
51 -h --help Show this screen.
52 --version Show version.
Tomáš Peckad03044f2020-11-12 14:33:10 +010053 --appliance=<Model> Initialize IETF Hardware and outputs for specific appliance.
Tomáš Peckaa193c032020-06-22 17:33:31 +020054 --log-level=<N> Log level for everything [default: 3]
Tomáš Peckaa193c032020-06-22 17:33:31 +020055 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
56 4 -> debug, 5 -> trace)
Tomáš Pecka7c6b1d32020-11-25 14:17:21 +010057 --health-log-level=<N> Log level for the health monitoring [default: 3]
Tomáš Pecka9f3035f2020-11-27 11:10:47 +010058 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3]
59 --hardware-log-level=<N> Log level for the hardware drivers [default: 3]
Tomáš Peckaf2391d62020-11-06 14:02:00 +010060 --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 +020061)";
62
Tomáš Pecka33563f52021-01-25 14:31:41 +010063DBUS_EVENTLOOP_INIT
Tomáš Peckae8239a72020-07-17 11:29:22 +020064
Tomáš Peckaa193c032020-06-22 17:33:31 +020065int main(int argc, char* argv[])
Tomáš Pecka5123e002020-06-05 15:29:21 +020066{
Tomáš Peckaa4336812020-07-24 15:32:58 +020067 std::shared_ptr<spdlog::sinks::sink> loggingSink;
68 if (velia::utils::isJournaldActive()) {
69 loggingSink = velia::utils::create_journald_sink();
70 } else {
71 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
72 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020073
Tomáš Peckaa193c032020-06-22 17:33:31 +020074 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad " VELIA_VERSION, true);
75
Tomáš Peckabd828862020-06-15 15:53:35 +020076 velia::utils::initLogs(loggingSink);
Tomáš Pecka5123e002020-06-05 15:29:21 +020077 spdlog::set_level(spdlog::level::info);
78
Tomáš Peckaa193c032020-06-22 17:33:31 +020079 try {
80 spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
Tomáš Pecka9f3035f2020-11-27 11:10:47 +010081 spdlog::get("hardware")->set_level(parseLogLevel("Hardware loggers", args["--hardware-log-level"]));
82 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
83
Tomáš Peckad03044f2020-11-12 14:33:10 +010084 spdlog::get("main")->debug("Opening Sysrepo connection");
85 auto srConn = std::make_shared<sysrepo::Connection>();
86 auto srSess = std::make_shared<sysrepo::Session>(srConn);
87 auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSess);
88
89 // initialize ietf-hardware
90 spdlog::get("main")->debug("Initializing IETFHardware module");
91 std::shared_ptr<velia::ietf_hardware::IETFHardware> ietfHardware;
92 if (const auto& appliance = args["--appliance"]) {
93 ietfHardware = velia::ietf_hardware::create(appliance.asString());
94 } else {
95 ietfHardware = std::make_shared<velia::ietf_hardware::IETFHardware>();
96 }
97
98 spdlog::get("main")->debug("Initializing Sysrepo ietf-hardware callback");
99 auto sysrepoIETFHardware = velia::ietf_hardware::sysrepo::Sysrepo(srSubscription, ietfHardware);
100
Tomáš Pecka33563f52021-01-25 14:31:41 +0100101 DBUS_EVENTLOOP_START
Tomáš Peckaa193c032020-06-22 17:33:31 +0200102
Tomáš Peckad03044f2020-11-12 14:33:10 +0100103 // health
Tomáš Pecka261c8862020-11-05 11:23:08 +0100104 auto manager = std::make_shared<velia::health::StateManager>();
Tomáš Peckaa193c032020-06-22 17:33:31 +0200105
106 // output configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +0200107 spdlog::get("main")->debug("Initializing LED drivers");
Tomáš Peckad03044f2020-11-12 14:33:10 +0100108 if (const auto& appliance = args["--appliance"]) {
109 manager->m_outputSignal.connect(velia::health::createOutput(appliance.asString()));
110 }
Tomáš Peckaa193c032020-06-22 17:33:31 +0200111
Tomáš Pecka31caebd2020-08-21 11:14:10 +0200112 spdlog::get("main")->debug("All outputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +0200113
114 // input configuration
Tomáš Peckaf2391d62020-11-06 14:02:00 +0100115 std::set<std::string> ignoredUnits(args["--systemd-ignore-unit"].asStringList().begin(), args["--systemd-ignore-unit"].asStringList().end());
Tomáš Pecka31caebd2020-08-21 11:14:10 +0200116 spdlog::get("main")->debug("Starting DBus systemd watcher");
Tomáš Peckaf2391d62020-11-06 14:02:00 +0100117 if (!ignoredUnits.empty()) {
118 spdlog::get("main")->debug("Systemd input will ignore changes of the following units: {}", args["--systemd-ignore-unit"]);
119 }
Tomáš Pecka261c8862020-11-05 11:23:08 +0100120 auto inputSystemdDbus = std::make_shared<velia::health::DbusSystemdInput>(manager, ignoredUnits, *g_dbusConnection);
Tomáš Peckaa193c032020-06-22 17:33:31 +0200121
Tomáš Pecka31caebd2020-08-21 11:14:10 +0200122 spdlog::get("main")->debug("All inputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +0200123
Tomáš Pecka33563f52021-01-25 14:31:41 +0100124 DBUS_EVENTLOOP_END;
Tomáš Peckaa193c032020-06-22 17:33:31 +0200125
Tomáš Peckaa193c032020-06-22 17:33:31 +0200126 return 0;
127 } catch (std::exception& e) {
128 velia::utils::fatalException(spdlog::get("main"), e, "main");
129 }
Tomáš Pecka5123e002020-06-05 15:29:21 +0200130}