blob: 7a50aeb6b58b00c5290dca08addf4cb97c9d376f [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áš Pecka6a2334b2022-07-12 13:57:54 +020033 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áš Pecka0f8f15b2023-05-17 20:00:48 +020041 std::mutex m_mtx;
42
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020043 /** List of registered unit watchers */
44 std::map<sdbus::ObjectPath, std::unique_ptr<sdbus::IProxy>> m_proxyUnits;
45
46 /** Current unit state. */
Tomáš Pecka26ac0fa2024-01-24 16:34:16 +010047 std::map<std::string, UnitState> m_unitState;
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020048
Tomáš Pecka0e874842023-05-18 09:28:49 +020049 void registerSystemdUnit(sdbus::IConnection& connection, const std::string& unitName, const sdbus::ObjectPath& unitObjectPath);
Tomáš Pecka26ac0fa2024-01-24 16:34:16 +010050 void onUnitStateChange(const std::string& name, const UnitState& unitState);
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020051};
52
53}