Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 1 | #include <docopt.h> |
| 2 | #include <future> |
| 3 | #include <sdbus-c++/sdbus-c++.h> |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 4 | #include <spdlog/sinks/ansicolor_sink.h> |
| 5 | #include <spdlog/spdlog.h> |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 6 | #include <sysrepo-cpp/Session.hpp> |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 7 | #include "VELIA_VERSION.h" |
Tomáš Pecka | 139e361 | 2021-01-25 19:49:00 +0100 | [diff] [blame] | 8 | #include "health/Factory.h" |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 9 | #include "health/inputs/DbusSystemdInput.h" |
| 10 | #include "health/manager/StateManager.h" |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 11 | #include "health/outputs/callables.h" |
Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 12 | #include "main.h" |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 13 | #include "utils/exceptions.h" |
Tomáš Pecka | a433681 | 2020-07-24 15:32:58 +0200 | [diff] [blame] | 14 | #include "utils/journal.h" |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 15 | #include "utils/log-init.h" |
| 16 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 17 | /** @short Extract log level from a CLI option */ |
| 18 | spdlog::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 | |
| 34 | static const char usage[] = |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 35 | R"(Monitor system health status. |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 36 | |
| 37 | Usage: |
Tomáš Pecka | cd19e62 | 2021-01-27 09:18:11 +0100 | [diff] [blame^] | 38 | veliad-health |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 39 | [--appliance=<Model>] |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 40 | [--log-level=<Level>] |
Tomáš Pecka | 7c6b1d3 | 2020-11-25 14:17:21 +0100 | [diff] [blame] | 41 | [--health-log-level=<Level>] |
Tomáš Pecka | 9f3035f | 2020-11-27 11:10:47 +0100 | [diff] [blame] | 42 | [--sysrepo-log-level=<Level>] |
Tomáš Pecka | f2391d6 | 2020-11-06 14:02:00 +0100 | [diff] [blame] | 43 | [--systemd-ignore-unit=<Unit>]... |
Tomáš Pecka | cd19e62 | 2021-01-27 09:18:11 +0100 | [diff] [blame^] | 44 | veliad-health (-h | --help) |
| 45 | veliad-health --version |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 46 | |
| 47 | Options: |
| 48 | -h --help Show this screen. |
| 49 | --version Show version. |
| 50 | --log-level=<N> Log level for everything [default: 3] |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 51 | (0 -> critical, 1 -> error, 2 -> warning, 3 -> info, |
| 52 | 4 -> debug, 5 -> trace) |
Tomáš Pecka | 7c6b1d3 | 2020-11-25 14:17:21 +0100 | [diff] [blame] | 53 | --health-log-level=<N> Log level for the health monitoring [default: 3] |
Tomáš Pecka | 9f3035f | 2020-11-27 11:10:47 +0100 | [diff] [blame] | 54 | --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3] |
Tomáš Pecka | f2391d6 | 2020-11-06 14:02:00 +0100 | [diff] [blame] | 55 | --systemd-ignore-unit=<Unit> Ignore state of systemd's unit in systemd state tracker. Can be specified multiple times. |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 56 | )"; |
| 57 | |
Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 58 | DBUS_EVENTLOOP_INIT |
Tomáš Pecka | e8239a7 | 2020-07-17 11:29:22 +0200 | [diff] [blame] | 59 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 60 | int main(int argc, char* argv[]) |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 61 | { |
Tomáš Pecka | a433681 | 2020-07-24 15:32:58 +0200 | [diff] [blame] | 62 | 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áš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 68 | |
Tomáš Pecka | cd19e62 | 2021-01-27 09:18:11 +0100 | [diff] [blame^] | 69 | auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-health " VELIA_VERSION, true); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 70 | |
Tomáš Pecka | bd82886 | 2020-06-15 15:53:35 +0200 | [diff] [blame] | 71 | velia::utils::initLogs(loggingSink); |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 72 | spdlog::set_level(spdlog::level::info); |
| 73 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 74 | try { |
| 75 | spdlog::set_level(parseLogLevel("Generic", args["--log-level"])); |
Tomáš Pecka | 9f3035f | 2020-11-27 11:10:47 +0100 | [diff] [blame] | 76 | spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"])); |
| 77 | |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 78 | 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áš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 83 | DBUS_EVENTLOOP_START |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 84 | |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 85 | // health |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 86 | auto manager = std::make_shared<velia::health::StateManager>(); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 87 | |
| 88 | // output configuration |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 89 | spdlog::get("main")->debug("Initializing LED drivers"); |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 90 | if (const auto& appliance = args["--appliance"]) { |
| 91 | manager->m_outputSignal.connect(velia::health::createOutput(appliance.asString())); |
| 92 | } |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 93 | |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 94 | spdlog::get("main")->debug("All outputs initialized."); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 95 | |
| 96 | // input configuration |
Tomáš Pecka | f2391d6 | 2020-11-06 14:02:00 +0100 | [diff] [blame] | 97 | std::set<std::string> ignoredUnits(args["--systemd-ignore-unit"].asStringList().begin(), args["--systemd-ignore-unit"].asStringList().end()); |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 98 | spdlog::get("main")->debug("Starting DBus systemd watcher"); |
Tomáš Pecka | f2391d6 | 2020-11-06 14:02:00 +0100 | [diff] [blame] | 99 | if (!ignoredUnits.empty()) { |
| 100 | spdlog::get("main")->debug("Systemd input will ignore changes of the following units: {}", args["--systemd-ignore-unit"]); |
| 101 | } |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 102 | auto inputSystemdDbus = std::make_shared<velia::health::DbusSystemdInput>(manager, ignoredUnits, *g_dbusConnection); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 103 | |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 104 | spdlog::get("main")->debug("All inputs initialized."); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 105 | |
Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 106 | DBUS_EVENTLOOP_END; |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 107 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 108 | return 0; |
| 109 | } catch (std::exception& e) { |
| 110 | velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| 111 | } |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 112 | } |