blob: 80669351751bd0928201abe541da20d2cf121132 [file] [log] [blame]
Tomáš Pecka6a2334b2022-07-12 13:57:54 +02001/*
2 * Copyright (C) 2020 - 2022 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
5 *
6 */
7#pragma once
8
9#include <memory>
Tomáš Pecka0f8f15b2023-05-17 20:00:48 +020010#include <mutex>
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020011#include <sdbus-c++/sdbus-c++.h>
12#include <set>
13#include <sysrepo-cpp/Session.hpp>
14#include "utils/log-fwd.h"
15
16namespace velia::health {
17
18/** @brief Watches for systemd units state via DBus and reports their state changes via ietf-alarms */
19class SystemdUnits {
20public:
21 SystemdUnits(sysrepo::Session session, sdbus::IConnection& connection);
22 SystemdUnits(sysrepo::Session session, sdbus::IConnection& connection, const std::string& busname, const std::string& managerObjectPath, const std::string& managerIface, const std::string& unitIface);
23 ~SystemdUnits();
24
25private:
Tomáš Pecka26ac0fa2024-01-24 16:34:16 +010026 struct UnitState {
27 std::string activeState;
28 std::string subState;
29
30 bool operator==(const UnitState&) const = default;
31 };
32
Tomáš Peckaca85a6d2024-01-24 16:55:44 +010033 enum class RegisterAlarmInventory {
34 Yes,
35 No,
36 };
37
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020038 velia::Log m_log;
39
40 sysrepo::Session m_srSession;
41
42 std::string m_busName;
43 std::string m_unitIface;
44 std::unique_ptr<sdbus::IProxy> m_proxyManager;
45
Tomáš Pecka0f8f15b2023-05-17 20:00:48 +020046 std::mutex m_mtx;
47
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020048 /** List of registered unit watchers */
49 std::map<sdbus::ObjectPath, std::unique_ptr<sdbus::IProxy>> m_proxyUnits;
50
51 /** Current unit state. */
Tomáš Pecka26ac0fa2024-01-24 16:34:16 +010052 std::map<std::string, UnitState> m_unitState;
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020053
Tomáš Peckaca85a6d2024-01-24 16:55:44 +010054 void registerSystemdUnit(sdbus::IConnection& connection, const std::string& unitName, const sdbus::ObjectPath& unitObjectPath, const std::optional<UnitState>& unitState, const RegisterAlarmInventory registerAlarmInventory);
Tomáš Pecka26ac0fa2024-01-24 16:34:16 +010055 void onUnitStateChange(const std::string& name, const UnitState& unitState);
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020056};
57
58}