| #include <docopt.h> |
| #include <future> |
| #include <health/outputs/AlarmsOutputs.h> |
| #include <sdbus-c++/sdbus-c++.h> |
| #include <spdlog/sinks/ansicolor_sink.h> |
| #include <spdlog/spdlog.h> |
| #include <sysrepo-cpp/Connection.hpp> |
| #include "VELIA_VERSION.h" |
| #include "health/Factory.h" |
| #include "health/alarms/SystemdUnits.h" |
| #include "health/outputs/callables.h" |
| #include "main.h" |
| #include "utils/exceptions.h" |
| #include "utils/journal.h" |
| #include "utils/log-init.h" |
| #include "utils/log.h" |
| |
| static const char usage[] = |
| R"(Monitor system health status. |
| |
| Usage: |
| veliad-health |
| [--appliance=<Model>] |
| [--health-log-level=<Level>] |
| veliad-health (-h | --help) |
| veliad-health --version |
| |
| Options: |
| -h --help Show this screen. |
| --version Show version. |
| --health-log-level=<N> Log level for the health monitoring [default: 3] |
| (0 -> critical, 1 -> error, 2 -> warning, 3 -> info, |
| 4 -> debug, 5 -> trace) |
| )"; |
| |
| 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::get("health")->set_level(parseLogLevel("Health checker logger", args["--health-log-level"])); |
| |
| DBUS_EVENTLOOP_START |
| |
| auto srSessionAlarms = sysrepo::Connection{}.sessionStart(); |
| srSessionAlarms.switchDatastore(sysrepo::Datastore::Operational); |
| |
| // output configuration |
| std::vector<std::function<void(velia::health::State)>> outputHandlers; |
| if (const auto& appliance = args["--appliance"]) { |
| spdlog::get("health")->debug("Initializing LED drivers"); |
| outputHandlers.emplace_back(velia::health::createOutput(appliance.asString())); |
| } |
| velia::health::AlarmsOutputs alarms(srSessionAlarms, outputHandlers); |
| |
| spdlog::get("health")->debug("All outputs initialized."); |
| |
| // input configuration |
| spdlog::get("health")->debug("Starting DBus systemd units watcher"); |
| auto srSessionSystemdUnits = sysrepo::Connection{}.sessionStart(); |
| auto inputSystemdDbus = std::make_shared<velia::health::SystemdUnits>(srSessionSystemdUnits, *g_dbusConnection); |
| |
| DBUS_EVENTLOOP_END; |
| |
| return 0; |
| } catch (std::exception& e) { |
| velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| } |
| } |