Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 1 | #include <docopt.h> |
| 2 | #include <future> |
Tomáš Pecka | e4c82ee | 2022-07-13 19:42:18 +0200 | [diff] [blame] | 3 | #include <health/outputs/AlarmsOutputs.h> |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 4 | #include <sdbus-c++/sdbus-c++.h> |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 5 | #include <spdlog/sinks/ansicolor_sink.h> |
| 6 | #include <spdlog/spdlog.h> |
Tomáš Pecka | e4c82ee | 2022-07-13 19:42:18 +0200 | [diff] [blame] | 7 | #include <sysrepo-cpp/Connection.hpp> |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 8 | #include "VELIA_VERSION.h" |
Tomáš Pecka | 139e361 | 2021-01-25 19:49:00 +0100 | [diff] [blame] | 9 | #include "health/Factory.h" |
Tomáš Pecka | 1d2b95c | 2023-05-11 16:01:46 +0200 | [diff] [blame] | 10 | #include "health/SystemdUnits.h" |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 11 | #include "health/outputs/callables.h" |
Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 12 | #include "main.h" |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 13 | #include "utils/exceptions.h" |
Tomáš Pecka | a433681 | 2020-07-24 15:32:58 +0200 | [diff] [blame] | 14 | #include "utils/journal.h" |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 15 | #include "utils/log-init.h" |
Tomáš Pecka | e4c82ee | 2022-07-13 19:42:18 +0200 | [diff] [blame] | 16 | #include "utils/log.h" |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 17 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 18 | static const char usage[] = |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 19 | R"(Monitor system health status. |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 20 | |
| 21 | Usage: |
Tomáš Pecka | cd19e62 | 2021-01-27 09:18:11 +0100 | [diff] [blame] | 22 | veliad-health |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 23 | [--appliance=<Model>] |
Tomáš Pecka | 7c6b1d3 | 2020-11-25 14:17:21 +0100 | [diff] [blame] | 24 | [--health-log-level=<Level>] |
Jan Kundrát | ad8169c | 2023-05-29 10:49:13 +0200 | [diff] [blame^] | 25 | [--main-log-level=<Level>] |
| 26 | [--sysrepo-log-level=<Level>] |
Tomáš Pecka | cd19e62 | 2021-01-27 09:18:11 +0100 | [diff] [blame] | 27 | veliad-health (-h | --help) |
| 28 | veliad-health --version |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 29 | |
| 30 | Options: |
| 31 | -h --help Show this screen. |
| 32 | --version Show version. |
Václav Kubernát | 3da1467 | 2021-02-09 09:39:24 +0100 | [diff] [blame] | 33 | --health-log-level=<N> Log level for the health monitoring [default: 3] |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 34 | (0 -> critical, 1 -> error, 2 -> warning, 3 -> info, |
| 35 | 4 -> debug, 5 -> trace) |
Jan Kundrát | ad8169c | 2023-05-29 10:49:13 +0200 | [diff] [blame^] | 36 | --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áš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 38 | )"; |
| 39 | |
Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 40 | DBUS_EVENTLOOP_INIT |
Tomáš Pecka | e8239a7 | 2020-07-17 11:29:22 +0200 | [diff] [blame] | 41 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 42 | int main(int argc, char* argv[]) |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 43 | { |
Tomáš Pecka | a433681 | 2020-07-24 15:32:58 +0200 | [diff] [blame] | 44 | std::shared_ptr<spdlog::sinks::sink> loggingSink; |
| 45 | if (velia::utils::isJournaldActive()) { |
| 46 | loggingSink = velia::utils::create_journald_sink(); |
| 47 | } else { |
| 48 | loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>(); |
| 49 | } |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 50 | |
Tomáš Pecka | cd19e62 | 2021-01-27 09:18:11 +0100 | [diff] [blame] | 51 | auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-health " VELIA_VERSION, true); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 52 | |
Tomáš Pecka | bd82886 | 2020-06-15 15:53:35 +0200 | [diff] [blame] | 53 | velia::utils::initLogs(loggingSink); |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 54 | spdlog::set_level(spdlog::level::info); |
| 55 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 56 | try { |
Tomáš Pecka | b172486 | 2022-01-09 21:29:47 +0100 | [diff] [blame] | 57 | spdlog::get("health")->set_level(parseLogLevel("Health checker logger", args["--health-log-level"])); |
Jan Kundrát | ad8169c | 2023-05-29 10:49:13 +0200 | [diff] [blame^] | 58 | spdlog::get("main")->set_level(parseLogLevel("other messages", args["--main-log-level"])); |
| 59 | spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"])); |
Tomáš Pecka | b172486 | 2022-01-09 21:29:47 +0100 | [diff] [blame] | 60 | |
Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 61 | DBUS_EVENTLOOP_START |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 62 | |
Tomáš Pecka | e4c82ee | 2022-07-13 19:42:18 +0200 | [diff] [blame] | 63 | auto srSessionAlarms = sysrepo::Connection{}.sessionStart(); |
| 64 | srSessionAlarms.switchDatastore(sysrepo::Datastore::Operational); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 65 | |
| 66 | // output configuration |
Tomáš Pecka | e4c82ee | 2022-07-13 19:42:18 +0200 | [diff] [blame] | 67 | std::vector<std::function<void(velia::health::State)>> outputHandlers; |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 68 | if (const auto& appliance = args["--appliance"]) { |
Jan Kundrát | cbc7a0b | 2021-02-18 09:47:29 +0100 | [diff] [blame] | 69 | spdlog::get("health")->debug("Initializing LED drivers"); |
Tomáš Pecka | e4c82ee | 2022-07-13 19:42:18 +0200 | [diff] [blame] | 70 | outputHandlers.emplace_back(velia::health::createOutput(appliance.asString())); |
Tomáš Pecka | d03044f | 2020-11-12 14:33:10 +0100 | [diff] [blame] | 71 | } |
Tomáš Pecka | e4c82ee | 2022-07-13 19:42:18 +0200 | [diff] [blame] | 72 | velia::health::AlarmsOutputs alarms(srSessionAlarms, outputHandlers); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 73 | |
Jan Kundrát | cbc7a0b | 2021-02-18 09:47:29 +0100 | [diff] [blame] | 74 | spdlog::get("health")->debug("All outputs initialized."); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 75 | |
| 76 | // input configuration |
Tomáš Pecka | e4c82ee | 2022-07-13 19:42:18 +0200 | [diff] [blame] | 77 | spdlog::get("health")->debug("Starting DBus systemd units watcher"); |
| 78 | auto srSessionSystemdUnits = sysrepo::Connection{}.sessionStart(); |
| 79 | auto inputSystemdDbus = std::make_shared<velia::health::SystemdUnits>(srSessionSystemdUnits, *g_dbusConnection); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 80 | |
Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 81 | DBUS_EVENTLOOP_END; |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 82 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 83 | return 0; |
| 84 | } catch (std::exception& e) { |
| 85 | velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| 86 | } |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 87 | } |