Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 1 | /* |
| 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áš Pecka | 0f8f15b | 2023-05-17 20:00:48 +0200 | [diff] [blame] | 10 | #include <mutex> |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 11 | #include <sdbus-c++/sdbus-c++.h> |
| 12 | #include <set> |
| 13 | #include <sysrepo-cpp/Session.hpp> |
| 14 | #include "utils/log-fwd.h" |
| 15 | |
| 16 | namespace velia::health { |
| 17 | |
| 18 | /** @brief Watches for systemd units state via DBus and reports their state changes via ietf-alarms */ |
| 19 | class SystemdUnits { |
| 20 | public: |
| 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 | |
| 25 | private: |
Tomáš Pecka | 26ac0fa | 2024-01-24 16:34:16 +0100 | [diff] [blame^] | 26 | struct UnitState { |
| 27 | std::string activeState; |
| 28 | std::string subState; |
| 29 | |
| 30 | bool operator==(const UnitState&) const = default; |
| 31 | }; |
| 32 | |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 33 | velia::Log m_log; |
| 34 | |
| 35 | sysrepo::Session m_srSession; |
| 36 | |
| 37 | std::string m_busName; |
| 38 | std::string m_unitIface; |
| 39 | std::unique_ptr<sdbus::IProxy> m_proxyManager; |
| 40 | |
Tomáš Pecka | 0f8f15b | 2023-05-17 20:00:48 +0200 | [diff] [blame] | 41 | std::mutex m_mtx; |
| 42 | |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 43 | /** List of registered unit watchers */ |
| 44 | std::map<sdbus::ObjectPath, std::unique_ptr<sdbus::IProxy>> m_proxyUnits; |
| 45 | |
| 46 | /** Current unit state. */ |
Tomáš Pecka | 26ac0fa | 2024-01-24 16:34:16 +0100 | [diff] [blame^] | 47 | std::map<std::string, UnitState> m_unitState; |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 48 | |
Tomáš Pecka | 0e87484 | 2023-05-18 09:28:49 +0200 | [diff] [blame] | 49 | void registerSystemdUnit(sdbus::IConnection& connection, const std::string& unitName, const sdbus::ObjectPath& unitObjectPath); |
Tomáš Pecka | 26ac0fa | 2024-01-24 16:34:16 +0100 | [diff] [blame^] | 50 | void onUnitStateChange(const std::string& name, const UnitState& unitState); |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | } |