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