blob: f7cb598efa07ea2f10ff0273c296fd4b57d9a544 [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áš Peckaa193c032020-06-22 17:33:31 +02006#include "VELIA_VERSION.h"
Tomáš Pecka139e3612021-01-25 19:49:00 +01007#include "health/Factory.h"
Tomáš Pecka261c8862020-11-05 11:23:08 +01008#include "health/inputs/DbusSystemdInput.h"
9#include "health/manager/StateManager.h"
Tomáš Pecka261c8862020-11-05 11:23:08 +010010#include "health/outputs/callables.h"
Tomáš Pecka33563f52021-01-25 14:31:41 +010011#include "main.h"
Tomáš Peckaa193c032020-06-22 17:33:31 +020012#include "utils/exceptions.h"
Tomáš Peckaa4336812020-07-24 15:32:58 +020013#include "utils/journal.h"
Tomáš Pecka5123e002020-06-05 15:29:21 +020014#include "utils/log-init.h"
15
Tomáš Peckaa193c032020-06-22 17:33:31 +020016/** @short Extract log level from a CLI option */
17spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
18{
19 long x;
20 try {
21 x = option.asLong();
22 } catch (std::invalid_argument&) {
23 throw std::runtime_error(name + " log level: expecting integer");
24 }
25 static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
26 static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
27 if (x < 0 || x > 5)
28 throw std::runtime_error(name + " log level invalid or out-of-range");
29
30 return static_cast<spdlog::level::level_enum>(5 - x);
31}
32
33static const char usage[] =
Tomáš Pecka261c8862020-11-05 11:23:08 +010034 R"(Monitor system health status.
Tomáš Peckaa193c032020-06-22 17:33:31 +020035
36Usage:
Tomáš Peckacd19e622021-01-27 09:18:11 +010037 veliad-health
Tomáš Peckad03044f2020-11-12 14:33:10 +010038 [--appliance=<Model>]
Tomáš Peckaa193c032020-06-22 17:33:31 +020039 [--log-level=<Level>]
Tomáš Pecka7c6b1d32020-11-25 14:17:21 +010040 [--health-log-level=<Level>]
Tomáš Peckaf2391d62020-11-06 14:02:00 +010041 [--systemd-ignore-unit=<Unit>]...
Tomáš Peckacd19e622021-01-27 09:18:11 +010042 veliad-health (-h | --help)
43 veliad-health --version
Tomáš Peckaa193c032020-06-22 17:33:31 +020044
45Options:
46 -h --help Show this screen.
47 --version Show version.
48 --log-level=<N> Log level for everything [default: 3]
Tomáš Peckaa193c032020-06-22 17:33:31 +020049 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
50 4 -> debug, 5 -> trace)
Tomáš Pecka7c6b1d32020-11-25 14:17:21 +010051 --health-log-level=<N> Log level for the health monitoring [default: 3]
Tomáš Peckaf2391d62020-11-06 14:02:00 +010052 --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 +020053)";
54
Tomáš Pecka33563f52021-01-25 14:31:41 +010055DBUS_EVENTLOOP_INIT
Tomáš Peckae8239a72020-07-17 11:29:22 +020056
Tomáš Peckaa193c032020-06-22 17:33:31 +020057int main(int argc, char* argv[])
Tomáš Pecka5123e002020-06-05 15:29:21 +020058{
Tomáš Peckaa4336812020-07-24 15:32:58 +020059 std::shared_ptr<spdlog::sinks::sink> loggingSink;
60 if (velia::utils::isJournaldActive()) {
61 loggingSink = velia::utils::create_journald_sink();
62 } else {
63 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
64 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020065
Tomáš Peckacd19e622021-01-27 09:18:11 +010066 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-health " VELIA_VERSION, true);
Tomáš Peckaa193c032020-06-22 17:33:31 +020067
Tomáš Peckabd828862020-06-15 15:53:35 +020068 velia::utils::initLogs(loggingSink);
Tomáš Pecka5123e002020-06-05 15:29:21 +020069 spdlog::set_level(spdlog::level::info);
70
Tomáš Peckaa193c032020-06-22 17:33:31 +020071 try {
72 spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
Tomáš Peckad03044f2020-11-12 14:33:10 +010073
Tomáš Pecka33563f52021-01-25 14:31:41 +010074 DBUS_EVENTLOOP_START
Tomáš Peckaa193c032020-06-22 17:33:31 +020075
Tomáš Peckad03044f2020-11-12 14:33:10 +010076 // health
Tomáš Pecka261c8862020-11-05 11:23:08 +010077 auto manager = std::make_shared<velia::health::StateManager>();
Tomáš Peckaa193c032020-06-22 17:33:31 +020078
79 // output configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +020080 spdlog::get("main")->debug("Initializing LED drivers");
Tomáš Peckad03044f2020-11-12 14:33:10 +010081 if (const auto& appliance = args["--appliance"]) {
82 manager->m_outputSignal.connect(velia::health::createOutput(appliance.asString()));
83 }
Tomáš Peckaa193c032020-06-22 17:33:31 +020084
Tomáš Pecka31caebd2020-08-21 11:14:10 +020085 spdlog::get("main")->debug("All outputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020086
87 // input configuration
Tomáš Peckaf2391d62020-11-06 14:02:00 +010088 std::set<std::string> ignoredUnits(args["--systemd-ignore-unit"].asStringList().begin(), args["--systemd-ignore-unit"].asStringList().end());
Tomáš Pecka31caebd2020-08-21 11:14:10 +020089 spdlog::get("main")->debug("Starting DBus systemd watcher");
Tomáš Peckaf2391d62020-11-06 14:02:00 +010090 if (!ignoredUnits.empty()) {
91 spdlog::get("main")->debug("Systemd input will ignore changes of the following units: {}", args["--systemd-ignore-unit"]);
92 }
Tomáš Pecka261c8862020-11-05 11:23:08 +010093 auto inputSystemdDbus = std::make_shared<velia::health::DbusSystemdInput>(manager, ignoredUnits, *g_dbusConnection);
Tomáš Peckaa193c032020-06-22 17:33:31 +020094
Tomáš Pecka31caebd2020-08-21 11:14:10 +020095 spdlog::get("main")->debug("All inputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020096
Tomáš Pecka33563f52021-01-25 14:31:41 +010097 DBUS_EVENTLOOP_END;
Tomáš Peckaa193c032020-06-22 17:33:31 +020098
Tomáš Peckaa193c032020-06-22 17:33:31 +020099 return 0;
100 } catch (std::exception& e) {
101 velia::utils::fatalException(spdlog::get("main"), e, "main");
102 }
Tomáš Pecka5123e002020-06-05 15:29:21 +0200103}