blob: ee2214884556287c36b76acb02caf12d0a103829 [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"
Václav Kubernát6018f082021-02-11 01:32:18 +010014#include "utils/log.h"
Tomáš Pecka5123e002020-06-05 15:29:21 +020015#include "utils/log-init.h"
16
Tomáš Peckaa193c032020-06-22 17:33:31 +020017static const char usage[] =
Tomáš Pecka261c8862020-11-05 11:23:08 +010018 R"(Monitor system health status.
Tomáš Peckaa193c032020-06-22 17:33:31 +020019
20Usage:
Tomáš Peckacd19e622021-01-27 09:18:11 +010021 veliad-health
Tomáš Peckad03044f2020-11-12 14:33:10 +010022 [--appliance=<Model>]
Tomáš Pecka7c6b1d32020-11-25 14:17:21 +010023 [--health-log-level=<Level>]
Tomáš Peckaf2391d62020-11-06 14:02:00 +010024 [--systemd-ignore-unit=<Unit>]...
Tomáš Peckacd19e622021-01-27 09:18:11 +010025 veliad-health (-h | --help)
26 veliad-health --version
Tomáš Peckaa193c032020-06-22 17:33:31 +020027
28Options:
29 -h --help Show this screen.
30 --version Show version.
Václav Kubernát3da14672021-02-09 09:39:24 +010031 --health-log-level=<N> Log level for the health monitoring [default: 3]
Tomáš Peckaa193c032020-06-22 17:33:31 +020032 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
33 4 -> debug, 5 -> trace)
Tomáš Peckaf2391d62020-11-06 14:02:00 +010034 --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 +020035)";
36
Tomáš Pecka33563f52021-01-25 14:31:41 +010037DBUS_EVENTLOOP_INIT
Tomáš Peckae8239a72020-07-17 11:29:22 +020038
Tomáš Peckaa193c032020-06-22 17:33:31 +020039int main(int argc, char* argv[])
Tomáš Pecka5123e002020-06-05 15:29:21 +020040{
Tomáš Peckaa4336812020-07-24 15:32:58 +020041 std::shared_ptr<spdlog::sinks::sink> loggingSink;
42 if (velia::utils::isJournaldActive()) {
43 loggingSink = velia::utils::create_journald_sink();
44 } else {
45 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
46 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020047
Tomáš Peckacd19e622021-01-27 09:18:11 +010048 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-health " VELIA_VERSION, true);
Tomáš Peckaa193c032020-06-22 17:33:31 +020049
Tomáš Peckabd828862020-06-15 15:53:35 +020050 velia::utils::initLogs(loggingSink);
Tomáš Pecka5123e002020-06-05 15:29:21 +020051 spdlog::set_level(spdlog::level::info);
52
Tomáš Peckaa193c032020-06-22 17:33:31 +020053 try {
Tomáš Peckab1724862022-01-09 21:29:47 +010054 spdlog::get("health")->set_level(parseLogLevel("Health checker logger", args["--health-log-level"]));
55
Tomáš Pecka33563f52021-01-25 14:31:41 +010056 DBUS_EVENTLOOP_START
Tomáš Peckaa193c032020-06-22 17:33:31 +020057
Tomáš Peckad03044f2020-11-12 14:33:10 +010058 // health
Tomáš Pecka261c8862020-11-05 11:23:08 +010059 auto manager = std::make_shared<velia::health::StateManager>();
Tomáš Peckaa193c032020-06-22 17:33:31 +020060
61 // output configuration
Tomáš Peckad03044f2020-11-12 14:33:10 +010062 if (const auto& appliance = args["--appliance"]) {
Jan Kundrátcbc7a0b2021-02-18 09:47:29 +010063 spdlog::get("health")->debug("Initializing LED drivers");
Tomáš Peckad03044f2020-11-12 14:33:10 +010064 manager->m_outputSignal.connect(velia::health::createOutput(appliance.asString()));
65 }
Tomáš Peckaa193c032020-06-22 17:33:31 +020066
Jan Kundrátcbc7a0b2021-02-18 09:47:29 +010067 spdlog::get("health")->debug("All outputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020068
69 // input configuration
Tomáš Peckaf2391d62020-11-06 14:02:00 +010070 std::set<std::string> ignoredUnits(args["--systemd-ignore-unit"].asStringList().begin(), args["--systemd-ignore-unit"].asStringList().end());
Jan Kundrátcbc7a0b2021-02-18 09:47:29 +010071 spdlog::get("health")->debug("Starting DBus systemd watcher");
Tomáš Peckaf2391d62020-11-06 14:02:00 +010072 if (!ignoredUnits.empty()) {
Jan Kundrátcbc7a0b2021-02-18 09:47:29 +010073 spdlog::get("health")->debug("Systemd input will ignore changes of the following units: {}", args["--systemd-ignore-unit"]);
Tomáš Peckaf2391d62020-11-06 14:02:00 +010074 }
Tomáš Pecka261c8862020-11-05 11:23:08 +010075 auto inputSystemdDbus = std::make_shared<velia::health::DbusSystemdInput>(manager, ignoredUnits, *g_dbusConnection);
Tomáš Peckaa193c032020-06-22 17:33:31 +020076
Tomáš Pecka33563f52021-01-25 14:31:41 +010077 DBUS_EVENTLOOP_END;
Tomáš Peckaa193c032020-06-22 17:33:31 +020078
Tomáš Peckaa193c032020-06-22 17:33:31 +020079 return 0;
80 } catch (std::exception& e) {
81 velia::utils::fatalException(spdlog::get("main"), e, "main");
82 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020083}