Tomáš Pecka | aae9fe3 | 2021-01-25 19:57:22 +0100 | [diff] [blame] | 1 | #include <docopt.h> |
| 2 | #include <spdlog/sinks/ansicolor_sink.h> |
| 3 | #include <spdlog/spdlog.h> |
| 4 | #include <sysrepo-cpp/Session.hpp> |
| 5 | #include "VELIA_VERSION.h" |
| 6 | #include "ietf-hardware/Factory.h" |
| 7 | #include "ietf-hardware/IETFHardware.h" |
| 8 | #include "ietf-hardware/sysrepo/Sysrepo.h" |
| 9 | #include "utils/exceptions.h" |
| 10 | #include "utils/journal.h" |
Václav Kubernát | 6018f08 | 2021-02-11 01:32:18 +0100 | [diff] [blame] | 11 | #include "utils/log.h" |
Tomáš Pecka | aae9fe3 | 2021-01-25 19:57:22 +0100 | [diff] [blame] | 12 | #include "utils/log-init.h" |
| 13 | #include "utils/waitUntilSignalled.h" |
| 14 | |
Tomáš Pecka | aae9fe3 | 2021-01-25 19:57:22 +0100 | [diff] [blame] | 15 | static const char usage[] = |
| 16 | R"(Hardware monitoring via Sysrepo. |
| 17 | |
| 18 | Usage: |
| 19 | veliad-hardware |
| 20 | [--appliance=<Model>] |
Tomáš Pecka | aae9fe3 | 2021-01-25 19:57:22 +0100 | [diff] [blame] | 21 | [--sysrepo-log-level=<Level>] |
| 22 | [--hardware-log-level=<Level>] |
| 23 | veliad-hardware (-h | --help) |
| 24 | veliad-hardware --version |
| 25 | |
| 26 | Options: |
| 27 | -h --help Show this screen. |
| 28 | --version Show version. |
| 29 | --appliance=<Model> Initialize IETF Hardware and outputs for specific appliance. |
Václav Kubernát | 5f57dec | 2021-02-11 02:02:46 +0100 | [diff] [blame] | 30 | --sysrepo-log-level=<N> Log level for the sysrepo library [default: 2] |
Tomáš Pecka | aae9fe3 | 2021-01-25 19:57:22 +0100 | [diff] [blame] | 31 | --hardware-log-level=<N> Log level for the hardware drivers [default: 3] |
Václav Kubernát | 3da1467 | 2021-02-09 09:39:24 +0100 | [diff] [blame] | 32 | (0 -> critical, 1 -> error, 2 -> warning, 3 -> info, |
| 33 | 4 -> debug, 5 -> trace) |
Tomáš Pecka | aae9fe3 | 2021-01-25 19:57:22 +0100 | [diff] [blame] | 34 | )"; |
| 35 | |
| 36 | int main(int argc, char* argv[]) |
| 37 | { |
| 38 | std::shared_ptr<spdlog::sinks::sink> loggingSink; |
| 39 | if (velia::utils::isJournaldActive()) { |
| 40 | loggingSink = velia::utils::create_journald_sink(); |
| 41 | } else { |
| 42 | loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>(); |
| 43 | } |
| 44 | |
| 45 | auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-system " VELIA_VERSION, true); |
| 46 | |
| 47 | velia::utils::initLogs(loggingSink); |
| 48 | spdlog::set_level(spdlog::level::info); |
| 49 | |
| 50 | try { |
Tomáš Pecka | aae9fe3 | 2021-01-25 19:57:22 +0100 | [diff] [blame] | 51 | spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"])); |
| 52 | spdlog::get("hardware")->set_level(parseLogLevel("Hardware loggers", args["--hardware-log-level"])); |
| 53 | |
Tomáš Pecka | aae9fe3 | 2021-01-25 19:57:22 +0100 | [diff] [blame] | 54 | auto srConn = std::make_shared<sysrepo::Connection>(); |
| 55 | auto srSess = std::make_shared<sysrepo::Session>(srConn); |
| 56 | auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSess); |
| 57 | |
| 58 | // initialize ietf-hardware |
Tomáš Pecka | aae9fe3 | 2021-01-25 19:57:22 +0100 | [diff] [blame] | 59 | std::shared_ptr<velia::ietf_hardware::IETFHardware> ietfHardware; |
| 60 | if (const auto& appliance = args["--appliance"]) { |
| 61 | ietfHardware = velia::ietf_hardware::create(appliance.asString()); |
| 62 | } else { |
| 63 | ietfHardware = std::make_shared<velia::ietf_hardware::IETFHardware>(); |
| 64 | } |
| 65 | |
Tomáš Pecka | aae9fe3 | 2021-01-25 19:57:22 +0100 | [diff] [blame] | 66 | auto sysrepoIETFHardware = velia::ietf_hardware::sysrepo::Sysrepo(srSubscription, ietfHardware); |
| 67 | |
| 68 | waitUntilSignaled(); |
| 69 | |
| 70 | return 0; |
| 71 | } catch (std::exception& e) { |
| 72 | velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| 73 | } |
| 74 | } |