blob: c3e447513a88fed56aef7aa86a994c35c4c93a58 [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áš 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"
16
Tomáš Peckaa193c032020-06-22 17:33:31 +020017/** @short Extract log level from a CLI option */
18spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
19{
20 long x;
21 try {
22 x = option.asLong();
23 } catch (std::invalid_argument&) {
24 throw std::runtime_error(name + " log level: expecting integer");
25 }
26 static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
27 static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
28 if (x < 0 || x > 5)
29 throw std::runtime_error(name + " log level invalid or out-of-range");
30
31 return static_cast<spdlog::level::level_enum>(5 - x);
32}
33
34static const char usage[] =
Tomáš Pecka261c8862020-11-05 11:23:08 +010035 R"(Monitor system health status.
Tomáš Peckaa193c032020-06-22 17:33:31 +020036
37Usage:
Tomáš Peckacd19e622021-01-27 09:18:11 +010038 veliad-health
Tomáš Peckad03044f2020-11-12 14:33:10 +010039 [--appliance=<Model>]
Tomáš Peckaa193c032020-06-22 17:33:31 +020040 [--log-level=<Level>]
Tomáš Pecka7c6b1d32020-11-25 14:17:21 +010041 [--health-log-level=<Level>]
Tomáš Pecka9f3035f2020-11-27 11:10:47 +010042 [--sysrepo-log-level=<Level>]
Tomáš Peckaf2391d62020-11-06 14:02:00 +010043 [--systemd-ignore-unit=<Unit>]...
Tomáš Peckacd19e622021-01-27 09:18:11 +010044 veliad-health (-h | --help)
45 veliad-health --version
Tomáš Peckaa193c032020-06-22 17:33:31 +020046
47Options:
48 -h --help Show this screen.
49 --version Show version.
50 --log-level=<N> Log level for everything [default: 3]
Tomáš Peckaa193c032020-06-22 17:33:31 +020051 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
52 4 -> debug, 5 -> trace)
Tomáš Pecka7c6b1d32020-11-25 14:17:21 +010053 --health-log-level=<N> Log level for the health monitoring [default: 3]
Tomáš Pecka9f3035f2020-11-27 11:10:47 +010054 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3]
Tomáš Peckaf2391d62020-11-06 14:02:00 +010055 --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 +020056)";
57
Tomáš Pecka33563f52021-01-25 14:31:41 +010058DBUS_EVENTLOOP_INIT
Tomáš Peckae8239a72020-07-17 11:29:22 +020059
Tomáš Peckaa193c032020-06-22 17:33:31 +020060int main(int argc, char* argv[])
Tomáš Pecka5123e002020-06-05 15:29:21 +020061{
Tomáš Peckaa4336812020-07-24 15:32:58 +020062 std::shared_ptr<spdlog::sinks::sink> loggingSink;
63 if (velia::utils::isJournaldActive()) {
64 loggingSink = velia::utils::create_journald_sink();
65 } else {
66 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
67 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020068
Tomáš Peckacd19e622021-01-27 09:18:11 +010069 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-health " VELIA_VERSION, true);
Tomáš Peckaa193c032020-06-22 17:33:31 +020070
Tomáš Peckabd828862020-06-15 15:53:35 +020071 velia::utils::initLogs(loggingSink);
Tomáš Pecka5123e002020-06-05 15:29:21 +020072 spdlog::set_level(spdlog::level::info);
73
Tomáš Peckaa193c032020-06-22 17:33:31 +020074 try {
75 spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
Tomáš Pecka9f3035f2020-11-27 11:10:47 +010076 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
77
Tomáš Peckad03044f2020-11-12 14:33:10 +010078 spdlog::get("main")->debug("Opening Sysrepo connection");
79 auto srConn = std::make_shared<sysrepo::Connection>();
80 auto srSess = std::make_shared<sysrepo::Session>(srConn);
81 auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSess);
82
Tomáš Pecka33563f52021-01-25 14:31:41 +010083 DBUS_EVENTLOOP_START
Tomáš Peckaa193c032020-06-22 17:33:31 +020084
Tomáš Peckad03044f2020-11-12 14:33:10 +010085 // health
Tomáš Pecka261c8862020-11-05 11:23:08 +010086 auto manager = std::make_shared<velia::health::StateManager>();
Tomáš Peckaa193c032020-06-22 17:33:31 +020087
88 // output configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +020089 spdlog::get("main")->debug("Initializing LED drivers");
Tomáš Peckad03044f2020-11-12 14:33:10 +010090 if (const auto& appliance = args["--appliance"]) {
91 manager->m_outputSignal.connect(velia::health::createOutput(appliance.asString()));
92 }
Tomáš Peckaa193c032020-06-22 17:33:31 +020093
Tomáš Pecka31caebd2020-08-21 11:14:10 +020094 spdlog::get("main")->debug("All outputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020095
96 // input configuration
Tomáš Peckaf2391d62020-11-06 14:02:00 +010097 std::set<std::string> ignoredUnits(args["--systemd-ignore-unit"].asStringList().begin(), args["--systemd-ignore-unit"].asStringList().end());
Tomáš Pecka31caebd2020-08-21 11:14:10 +020098 spdlog::get("main")->debug("Starting DBus systemd watcher");
Tomáš Peckaf2391d62020-11-06 14:02:00 +010099 if (!ignoredUnits.empty()) {
100 spdlog::get("main")->debug("Systemd input will ignore changes of the following units: {}", args["--systemd-ignore-unit"]);
101 }
Tomáš Pecka261c8862020-11-05 11:23:08 +0100102 auto inputSystemdDbus = std::make_shared<velia::health::DbusSystemdInput>(manager, ignoredUnits, *g_dbusConnection);
Tomáš Peckaa193c032020-06-22 17:33:31 +0200103
Tomáš Pecka31caebd2020-08-21 11:14:10 +0200104 spdlog::get("main")->debug("All inputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +0200105
Tomáš Pecka33563f52021-01-25 14:31:41 +0100106 DBUS_EVENTLOOP_END;
Tomáš Peckaa193c032020-06-22 17:33:31 +0200107
Tomáš Peckaa193c032020-06-22 17:33:31 +0200108 return 0;
109 } catch (std::exception& e) {
110 velia::utils::fatalException(spdlog::get("main"), e, "main");
111 }
Tomáš Pecka5123e002020-06-05 15:29:21 +0200112}