blob: cfae56ef33d6322e7d4400c45d44bbdeba6f8b3c [file] [log] [blame]
Tomáš Pecka43ef7ba2023-04-13 15:56:48 +02001#include <chrono>
Tomáš Peckaaae9fe32021-01-25 19:57:22 +01002#include <docopt.h>
3#include <spdlog/sinks/ansicolor_sink.h>
4#include <spdlog/spdlog.h>
Václav Kubernát7efd6d52021-11-09 01:31:11 +01005#include <sysrepo-cpp/Connection.hpp>
Tomáš Peckaaae9fe32021-01-25 19:57:22 +01006#include "VELIA_VERSION.h"
7#include "ietf-hardware/Factory.h"
8#include "ietf-hardware/IETFHardware.h"
9#include "ietf-hardware/sysrepo/Sysrepo.h"
10#include "utils/exceptions.h"
11#include "utils/journal.h"
12#include "utils/log-init.h"
Tomáš Pecka43ef7ba2023-04-13 15:56:48 +020013#include "utils/log.h"
Tomáš Pecka41784702021-05-26 13:57:32 +020014#include "utils/sysrepo.h"
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010015#include "utils/waitUntilSignalled.h"
16
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010017static const char usage[] =
18 R"(Hardware monitoring via Sysrepo.
19
20Usage:
21 veliad-hardware
22 [--appliance=<Model>]
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010023 [--hardware-log-level=<Level>]
Jan Kundrátad8169c2023-05-29 10:49:13 +020024 [--main-log-level=<Level>]
25 [--sysrepo-log-level=<Level>]
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010026 veliad-hardware (-h | --help)
27 veliad-hardware --version
28
29Options:
30 -h --help Show this screen.
31 --version Show version.
32 --appliance=<Model> Initialize IETF Hardware and outputs for specific appliance.
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010033 --hardware-log-level=<N> Log level for the hardware drivers [default: 3]
Václav Kubernát3da14672021-02-09 09:39:24 +010034 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
35 4 -> debug, 5 -> trace)
Jan Kundrátad8169c2023-05-29 10:49:13 +020036 --main-log-level=<N> Log level for other messages [default: 2]
37 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 2]
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010038)";
39
40int main(int argc, char* argv[])
41{
42 std::shared_ptr<spdlog::sinks::sink> loggingSink;
43 if (velia::utils::isJournaldActive()) {
44 loggingSink = velia::utils::create_journald_sink();
45 } else {
46 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
47 }
48
49 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-system " VELIA_VERSION, true);
50
51 velia::utils::initLogs(loggingSink);
Tomáš Pecka41784702021-05-26 13:57:32 +020052 velia::utils::initLogsSysrepo();
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010053 spdlog::set_level(spdlog::level::info);
54
55 try {
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010056 spdlog::get("hardware")->set_level(parseLogLevel("Hardware loggers", args["--hardware-log-level"]));
Jan Kundrátad8169c2023-05-29 10:49:13 +020057 spdlog::get("main")->set_level(parseLogLevel("other messages", args["--main-log-level"]));
58 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010059
Václav Kubernát7efd6d52021-11-09 01:31:11 +010060 auto srConn = sysrepo::Connection{};
61 auto srSess = srConn.sessionStart();
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010062
63 // initialize ietf-hardware
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010064 std::shared_ptr<velia::ietf_hardware::IETFHardware> ietfHardware;
65 if (const auto& appliance = args["--appliance"]) {
66 ietfHardware = velia::ietf_hardware::create(appliance.asString());
67 } else {
68 ietfHardware = std::make_shared<velia::ietf_hardware::IETFHardware>();
69 }
70
Tomáš Pecka43ef7ba2023-04-13 15:56:48 +020071 auto sysrepoIETFHardware = velia::ietf_hardware::sysrepo::Sysrepo(srSess, ietfHardware, std::chrono::milliseconds{1500});
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010072
73 waitUntilSignaled();
74
75 return 0;
76 } catch (std::exception& e) {
77 velia::utils::fatalException(spdlog::get("main"), e, "main");
78 }
79}