blob: 37bec57e035e4fc0b8412215ed3d4dc0936b01bd [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>]
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010021 [--sysrepo-log-level=<Level>]
22 [--hardware-log-level=<Level>]
23 veliad-hardware (-h | --help)
24 veliad-hardware --version
25
26Options:
27 -h --help Show this screen.
28 --version Show version.
29 --appliance=<Model> Initialize IETF Hardware and outputs for specific appliance.
Václav Kubernát5f57dec2021-02-11 02:02:46 +010030 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 2]
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010031 --hardware-log-level=<N> Log level for the hardware drivers [default: 3]
Václav Kubernát3da14672021-02-09 09:39:24 +010032 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
33 4 -> debug, 5 -> trace)
Tomáš Peckaaae9fe32021-01-25 19:57:22 +010034)";
35
36int 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áš Peckaaae9fe32021-01-25 19:57:22 +010051 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áš Peckaaae9fe32021-01-25 19:57:22 +010054 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áš Peckaaae9fe32021-01-25 19:57:22 +010059 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áš Peckaaae9fe32021-01-25 19:57:22 +010066 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}