blob: cf2f66b85920d1c953b9542bc436733b33ea1dc5 [file] [log] [blame]
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -08001#pragma once
2
3#include "health_metric_config.hpp"
4#include "health_utils.hpp"
5
6#include <xyz/openbmc_project/Association/Definitions/server.hpp>
7#include <xyz/openbmc_project/Inventory/Item/Bmc/server.hpp>
8#include <xyz/openbmc_project/Metric/Value/server.hpp>
9
10#include <deque>
11#include <tuple>
12
13namespace phosphor::health::metric
14{
15
16using phosphor::health::utils::paths_t;
17using phosphor::health::utils::startUnit;
18using AssociationIntf =
19 sdbusplus::xyz::openbmc_project::Association::server::Definitions;
20using ValueIntf = sdbusplus::xyz::openbmc_project::Metric::server::Value;
21using PathIntf =
22 sdbusplus::common::xyz::openbmc_project::metric::Value::namespace_path;
23static constexpr auto BmcPath =
24 sdbusplus::common::xyz::openbmc_project::metric::Value::bmc;
25using BmcIntf = sdbusplus::xyz::openbmc_project::Inventory::Item::server::Bmc;
26using MetricIntf =
27 sdbusplus::server::object_t<ValueIntf, ThresholdIntf, AssociationIntf>;
28
29struct MValue
30{
Jagpal Singh Gill6a3884a2024-02-24 18:08:23 -080031 /** @brief Current value of metric */
32 double current;
33 /** @brief Total value of metric */
34 double total;
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080035};
36
37class HealthMetric : public MetricIntf
38{
39 public:
Patrick Williams658efd52024-03-04 12:53:52 -060040 using MType = phosphor::health::metric::Type;
41
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080042 HealthMetric() = delete;
43 HealthMetric(const HealthMetric&) = delete;
44 HealthMetric(HealthMetric&&) = delete;
45 virtual ~HealthMetric() = default;
46
Jagpal Singh Gill97582802024-02-27 13:59:11 -080047 HealthMetric(sdbusplus::bus_t& bus, MType type,
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080048 const config::HealthMetric& config, const paths_t& bmcPaths) :
Jagpal Singh Gill97582802024-02-27 13:59:11 -080049 MetricIntf(bus, getPath(type, config.name, config.subType).c_str(),
50 action::defer_emit),
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080051 bus(bus), type(type), config(config)
52 {
53 create(bmcPaths);
54 this->emit_object_added();
55 }
56
57 /** @brief Update the health metric with the given value */
58 void update(MValue value);
59
60 private:
61 /** @brief Create a new health metric object */
62 void create(const paths_t& bmcPaths);
63 /** @brief Init properties for the health metric object */
64 void initProperties();
Jagpal Singh Gillb94b1222024-03-02 17:53:30 -080065 /** @brief Check if specified value should be notified based on hysteresis
66 */
67 auto shouldNotify(MValue value) -> bool;
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080068 /** @brief Check specified threshold for the given value */
Patrick Williams658efd52024-03-04 12:53:52 -060069 void checkThreshold(Type type, Bound bound, MValue value);
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080070 /** @brief Check all thresholds for the given value */
Jagpal Singh Gill6a3884a2024-02-24 18:08:23 -080071 void checkThresholds(MValue value);
Jagpal Singh Gill97582802024-02-27 13:59:11 -080072 /** @brief Get the object path for the given type, name and subtype */
Patrick Williams658efd52024-03-04 12:53:52 -060073 auto getPath(MType type, std::string name, SubType subType) -> std::string;
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080074 /** @brief D-Bus bus connection */
Patrick Williamscfd889f2024-02-10 02:39:17 -060075 sdbusplus::bus_t& bus;
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080076 /** @brief Metric type */
Jagpal Singh Gill97582802024-02-27 13:59:11 -080077 MType type;
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080078 /** @brief Metric configuration */
79 const config::HealthMetric config;
80 /** @brief Window for metric history */
81 std::deque<double> history;
Jagpal Singh Gillb94b1222024-03-02 17:53:30 -080082 /** @brief Last notified value for the metric change */
83 double lastNotifiedValue = 0;
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080084};
85
86} // namespace phosphor::health::metric