veliad: rename to veliad-health
As we are splitting the original veliad into smaller parts, the original
veliad is now only tracking health of the services. In the future, when
we connect the veliad-* processes with some IPC, it will also track the
health of the whole box itself (hardware warnings, optical components
warnings, etc.) and it will manipulate with the LEDs accordingly.
So, in order to better express the functionality of this daemon, the
original veliad is now called veliad-health.
Change-Id: I269b970360e804f05fb770e71b15de13c1301b9a
diff --git a/src/main-health.cpp b/src/main-health.cpp
new file mode 100644
index 0000000..c3e4475
--- /dev/null
+++ b/src/main-health.cpp
@@ -0,0 +1,112 @@
+#include <docopt.h>
+#include <future>
+#include <sdbus-c++/sdbus-c++.h>
+#include <spdlog/sinks/ansicolor_sink.h>
+#include <spdlog/spdlog.h>
+#include <sysrepo-cpp/Session.hpp>
+#include "VELIA_VERSION.h"
+#include "health/Factory.h"
+#include "health/inputs/DbusSystemdInput.h"
+#include "health/manager/StateManager.h"
+#include "health/outputs/callables.h"
+#include "main.h"
+#include "utils/exceptions.h"
+#include "utils/journal.h"
+#include "utils/log-init.h"
+
+/** @short Extract log level from a CLI option */
+spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
+{
+ long x;
+ try {
+ x = option.asLong();
+ } catch (std::invalid_argument&) {
+ throw std::runtime_error(name + " log level: expecting integer");
+ }
+ static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
+ static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
+ if (x < 0 || x > 5)
+ throw std::runtime_error(name + " log level invalid or out-of-range");
+
+ return static_cast<spdlog::level::level_enum>(5 - x);
+}
+
+static const char usage[] =
+ R"(Monitor system health status.
+
+Usage:
+ veliad-health
+ [--appliance=<Model>]
+ [--log-level=<Level>]
+ [--health-log-level=<Level>]
+ [--sysrepo-log-level=<Level>]
+ [--systemd-ignore-unit=<Unit>]...
+ veliad-health (-h | --help)
+ veliad-health --version
+
+Options:
+ -h --help Show this screen.
+ --version Show version.
+ --log-level=<N> Log level for everything [default: 3]
+ (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
+ 4 -> debug, 5 -> trace)
+ --health-log-level=<N> Log level for the health monitoring [default: 3]
+ --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3]
+ --systemd-ignore-unit=<Unit> Ignore state of systemd's unit in systemd state tracker. Can be specified multiple times.
+)";
+
+DBUS_EVENTLOOP_INIT
+
+int main(int argc, char* argv[])
+{
+ std::shared_ptr<spdlog::sinks::sink> loggingSink;
+ if (velia::utils::isJournaldActive()) {
+ loggingSink = velia::utils::create_journald_sink();
+ } else {
+ loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
+ }
+
+ auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-health " VELIA_VERSION, true);
+
+ velia::utils::initLogs(loggingSink);
+ spdlog::set_level(spdlog::level::info);
+
+ try {
+ spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
+ spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
+
+ spdlog::get("main")->debug("Opening Sysrepo connection");
+ auto srConn = std::make_shared<sysrepo::Connection>();
+ auto srSess = std::make_shared<sysrepo::Session>(srConn);
+ auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSess);
+
+ DBUS_EVENTLOOP_START
+
+ // health
+ auto manager = std::make_shared<velia::health::StateManager>();
+
+ // output configuration
+ spdlog::get("main")->debug("Initializing LED drivers");
+ if (const auto& appliance = args["--appliance"]) {
+ manager->m_outputSignal.connect(velia::health::createOutput(appliance.asString()));
+ }
+
+ spdlog::get("main")->debug("All outputs initialized.");
+
+ // input configuration
+ std::set<std::string> ignoredUnits(args["--systemd-ignore-unit"].asStringList().begin(), args["--systemd-ignore-unit"].asStringList().end());
+ spdlog::get("main")->debug("Starting DBus systemd watcher");
+ if (!ignoredUnits.empty()) {
+ spdlog::get("main")->debug("Systemd input will ignore changes of the following units: {}", args["--systemd-ignore-unit"]);
+ }
+ auto inputSystemdDbus = std::make_shared<velia::health::DbusSystemdInput>(manager, ignoredUnits, *g_dbusConnection);
+
+ spdlog::get("main")->debug("All inputs initialized.");
+
+ DBUS_EVENTLOOP_END;
+
+ return 0;
+ } catch (std::exception& e) {
+ velia::utils::fatalException(spdlog::get("main"), e, "main");
+ }
+}