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> |
| 7 | #include "Factory.h" |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 8 | #include "VELIA_VERSION.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 | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 12 | #include "ietf-hardware/sysrepo/Sysrepo.h" |
Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 13 | #include "main.h" |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 14 | #include "utils/exceptions.h" |
Tomáš Pecka | a433681 | 2020-07-24 15:32:58 +0200 | [diff] [blame] | 15 | #include "utils/journal.h" |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 16 | #include "utils/log-init.h" |
| 17 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 18 | /** @short Extract log level from a CLI option */ |
| 19 | spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option) |
| 20 | { |
| 21 | long x; |
| 22 | try { |
| 23 | x = option.asLong(); |
| 24 | } catch (std::invalid_argument&) { |
| 25 | throw std::runtime_error(name + " log level: expecting integer"); |
| 26 | } |
| 27 | static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed"); |
| 28 | static_assert(spdlog::level::off == 6, "spdlog::level levels have changed"); |
| 29 | if (x < 0 || x > 5) |
| 30 | throw std::runtime_error(name + " log level invalid or out-of-range"); |
| 31 | |
| 32 | return static_cast<spdlog::level::level_enum>(5 - x); |
| 33 | } |
| 34 | |
| 35 | static const char usage[] = |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 36 | R"(Monitor system health status. |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 37 | |
| 38 | Usage: |
| 39 | veliad |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 40 | [--appliance=<Model>] |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 41 | [--log-level=<Level>] |
Tomáš Pecka | 7c6b1d3 | 2020-11-25 14:17:21 +0100 | [diff] [blame] | 42 | [--health-log-level=<Level>] |
Tomáš Pecka | 9f3035f | 2020-11-27 11:10:47 +0100 | [diff] [blame] | 43 | [--sysrepo-log-level=<Level>] |
| 44 | [--hardware-log-level=<Level>] |
Tomáš Pecka | f2391d6 | 2020-11-06 14:02:00 +0100 | [diff] [blame] | 45 | [--systemd-ignore-unit=<Unit>]... |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 46 | veliad (-h | --help) |
| 47 | veliad --version |
| 48 | |
| 49 | Options: |
| 50 | -h --help Show this screen. |
| 51 | --version Show version. |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 52 | --appliance=<Model> Initialize IETF Hardware and outputs for specific appliance. |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 53 | --log-level=<N> Log level for everything [default: 3] |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 54 | (0 -> critical, 1 -> error, 2 -> warning, 3 -> info, |
| 55 | 4 -> debug, 5 -> trace) |
Tomáš Pecka | 7c6b1d3 | 2020-11-25 14:17:21 +0100 | [diff] [blame] | 56 | --health-log-level=<N> Log level for the health monitoring [default: 3] |
Tomáš Pecka | 9f3035f | 2020-11-27 11:10:47 +0100 | [diff] [blame] | 57 | --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3] |
| 58 | --hardware-log-level=<N> Log level for the hardware drivers [default: 3] |
Tomáš Pecka | f2391d6 | 2020-11-06 14:02:00 +0100 | [diff] [blame] | 59 | --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] | 60 | )"; |
| 61 | |
Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 62 | DBUS_EVENTLOOP_INIT |
Tomáš Pecka | e8239a7 | 2020-07-17 11:29:22 +0200 | [diff] [blame] | 63 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 64 | int main(int argc, char* argv[]) |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 65 | { |
Tomáš Pecka | a433681 | 2020-07-24 15:32:58 +0200 | [diff] [blame] | 66 | std::shared_ptr<spdlog::sinks::sink> loggingSink; |
| 67 | if (velia::utils::isJournaldActive()) { |
| 68 | loggingSink = velia::utils::create_journald_sink(); |
| 69 | } else { |
| 70 | loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>(); |
| 71 | } |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 72 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 73 | auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad " VELIA_VERSION, true); |
| 74 | |
Tomáš Pecka | bd82886 | 2020-06-15 15:53:35 +0200 | [diff] [blame] | 75 | velia::utils::initLogs(loggingSink); |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 76 | spdlog::set_level(spdlog::level::info); |
| 77 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 78 | try { |
| 79 | spdlog::set_level(parseLogLevel("Generic", args["--log-level"])); |
Tomáš Pecka | 9f3035f | 2020-11-27 11:10:47 +0100 | [diff] [blame] | 80 | spdlog::get("hardware")->set_level(parseLogLevel("Hardware loggers", args["--hardware-log-level"])); |
| 81 | spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"])); |
| 82 | |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 83 | spdlog::get("main")->debug("Opening Sysrepo connection"); |
| 84 | auto srConn = std::make_shared<sysrepo::Connection>(); |
| 85 | auto srSess = std::make_shared<sysrepo::Session>(srConn); |
| 86 | auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSess); |
| 87 | |
| 88 | // initialize ietf-hardware |
| 89 | spdlog::get("main")->debug("Initializing IETFHardware module"); |
| 90 | std::shared_ptr<velia::ietf_hardware::IETFHardware> ietfHardware; |
| 91 | if (const auto& appliance = args["--appliance"]) { |
| 92 | ietfHardware = velia::ietf_hardware::create(appliance.asString()); |
| 93 | } else { |
| 94 | ietfHardware = std::make_shared<velia::ietf_hardware::IETFHardware>(); |
| 95 | } |
| 96 | |
| 97 | spdlog::get("main")->debug("Initializing Sysrepo ietf-hardware callback"); |
| 98 | auto sysrepoIETFHardware = velia::ietf_hardware::sysrepo::Sysrepo(srSubscription, ietfHardware); |
| 99 | |
Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 100 | DBUS_EVENTLOOP_START |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 101 | |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 102 | // health |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 103 | auto manager = std::make_shared<velia::health::StateManager>(); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 104 | |
| 105 | // output configuration |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 106 | spdlog::get("main")->debug("Initializing LED drivers"); |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 107 | if (const auto& appliance = args["--appliance"]) { |
| 108 | manager->m_outputSignal.connect(velia::health::createOutput(appliance.asString())); |
| 109 | } |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 110 | |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 111 | spdlog::get("main")->debug("All outputs initialized."); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 112 | |
| 113 | // input configuration |
Tomáš Pecka | f2391d6 | 2020-11-06 14:02:00 +0100 | [diff] [blame] | 114 | 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] | 115 | spdlog::get("main")->debug("Starting DBus systemd watcher"); |
Tomáš Pecka | f2391d6 | 2020-11-06 14:02:00 +0100 | [diff] [blame] | 116 | if (!ignoredUnits.empty()) { |
| 117 | spdlog::get("main")->debug("Systemd input will ignore changes of the following units: {}", args["--systemd-ignore-unit"]); |
| 118 | } |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 119 | auto inputSystemdDbus = std::make_shared<velia::health::DbusSystemdInput>(manager, ignoredUnits, *g_dbusConnection); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 120 | |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 121 | spdlog::get("main")->debug("All inputs initialized."); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 122 | |
Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 123 | DBUS_EVENTLOOP_END; |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 124 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 125 | return 0; |
| 126 | } catch (std::exception& e) { |
| 127 | velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| 128 | } |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 129 | } |