blob: e3571194e1af4627ebffd767641606cf8a3bce9e [file] [log] [blame]
Tomáš Peckaaae9fe32021-01-25 19:57:22 +01001#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át6018f082021-02-11 01:32:18 +010011#include "utils/log.h"
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010012#include "utils/log-init.h"
13#include "utils/waitUntilSignalled.h"
14
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010015static const char usage[] =
16 R"(Hardware monitoring via Sysrepo.
17
18Usage:
19 veliad-hardware
20 [--appliance=<Model>]
21 [--log-level=<Level>]
22 [--sysrepo-log-level=<Level>]
23 [--hardware-log-level=<Level>]
24 veliad-hardware (-h | --help)
25 veliad-hardware --version
26
27Options:
28 -h --help Show this screen.
29 --version Show version.
30 --appliance=<Model> Initialize IETF Hardware and outputs for specific appliance.
31 --log-level=<N> Log level for everything [default: 3]
32 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
33 4 -> debug, 5 -> trace)
34 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3]
35 --hardware-log-level=<N> Log level for the hardware drivers [default: 3]
36)";
37
38int main(int argc, char* argv[])
39{
40 std::shared_ptr<spdlog::sinks::sink> loggingSink;
41 if (velia::utils::isJournaldActive()) {
42 loggingSink = velia::utils::create_journald_sink();
43 } else {
44 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
45 }
46
47 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-system " VELIA_VERSION, true);
48
49 velia::utils::initLogs(loggingSink);
50 spdlog::set_level(spdlog::level::info);
51
52 try {
53 spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
54 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
55 spdlog::get("hardware")->set_level(parseLogLevel("Hardware loggers", args["--hardware-log-level"]));
56
57 spdlog::get("main")->debug("Opening Sysrepo connection");
58 auto srConn = std::make_shared<sysrepo::Connection>();
59 auto srSess = std::make_shared<sysrepo::Session>(srConn);
60 auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSess);
61
62 // initialize ietf-hardware
63 spdlog::get("main")->debug("Initializing IETFHardware module");
64 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
71 spdlog::get("main")->debug("Initializing Sysrepo ietf-hardware callback");
72 auto sysrepoIETFHardware = velia::ietf_hardware::sysrepo::Sysrepo(srSubscription, ietfHardware);
73
74 waitUntilSignaled();
75
76 return 0;
77 } catch (std::exception& e) {
78 velia::utils::fatalException(spdlog::get("main"), e, "main");
79 }
80}