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