blob: bba3b52f49bf169f383e425cb3f15cf2c652b37b [file] [log] [blame]
Vijay Khemkae2795302020-07-15 17:28:45 -07001#include <nlohmann/json.hpp>
Patrick Williams957e03c2021-09-02 16:38:42 -05002#include <phosphor-logging/lg2.hpp>
Vijay Khemkae2795302020-07-15 17:28:45 -07003#include <sdbusplus/bus.hpp>
Sui Chen670cc132021-04-13 09:27:22 -07004#include <sdbusplus/message.hpp>
Vijay Khemkab38fd582020-07-23 13:21:23 -07005#include <sdeventplus/clock.hpp>
Vijay Khemkae2795302020-07-15 17:28:45 -07006#include <sdeventplus/event.hpp>
Vijay Khemkab38fd582020-07-23 13:21:23 -07007#include <sdeventplus/utility/timer.hpp>
Sui Chen670cc132021-04-13 09:27:22 -07008#include <xyz/openbmc_project/Association/Definitions/server.hpp>
Sui Chen517524a2021-12-19 20:52:46 -08009#include <xyz/openbmc_project/Inventory/Item/Bmc/server.hpp>
Vijay Khemkae2795302020-07-15 17:28:45 -070010#include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp>
11#include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp>
12#include <xyz/openbmc_project/Sensor/Value/server.hpp>
13
Sui Chen517524a2021-12-19 20:52:46 -080014constexpr char BMC_INVENTORY_ITEM[] = "xyz.openbmc_project.Inventory.Item.Bmc";
15constexpr char BMC_CONFIGURATION[] = "xyz.openbmc_project.Configuration.Bmc";
16
Vijay Khemka15537762020-07-22 11:44:56 -070017#include <deque>
Konstantin Aladysheva6cd7042021-12-21 15:36:01 +030018#include <limits>
Vijay Khemkae2795302020-07-15 17:28:45 -070019#include <map>
20#include <string>
21
22namespace phosphor
23{
24namespace health
25{
26
Sui Chen036f1612021-07-22 01:31:49 -070027const char* InventoryPath = "/xyz/openbmc_project/inventory";
28
29// Used for identifying the BMC inventory creation signal
30const char* BMCActivationPath = "/xyz/openbmc_project/inventory/bmc/activation";
31
Patrick Williamsbbfe7182022-07-22 19:26:56 -050032bool FindSystemInventoryInObjectMapper(sdbusplus::bus_t& bus)
Sui Chen036f1612021-07-22 01:31:49 -070033{
Patrick Williamsbbfe7182022-07-22 19:26:56 -050034 sdbusplus::message_t msg =
Sui Chen036f1612021-07-22 01:31:49 -070035 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
36 "/xyz/openbmc_project/object_mapper",
37 "xyz.openbmc_project.ObjectMapper", "GetObject");
38 msg.append(InventoryPath);
39 msg.append(std::vector<std::string>{});
40
41 try
42 {
Patrick Williamsbbfe7182022-07-22 19:26:56 -050043 sdbusplus::message_t reply = bus.call(msg, 0);
Sui Chen036f1612021-07-22 01:31:49 -070044 return true;
45 }
46 catch (const std::exception& e)
47 {}
48 return false;
49}
50
Vijay Khemkae2795302020-07-15 17:28:45 -070051using Json = nlohmann::json;
52using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
53
54using CriticalInterface =
55 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical;
56
57using WarningInterface =
58 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning;
59
Sui Chen670cc132021-04-13 09:27:22 -070060using AssociationDefinitionInterface =
61 sdbusplus::xyz::openbmc_project::Association::server::Definitions;
62
Vijay Khemkae2795302020-07-15 17:28:45 -070063using healthIfaces =
Patrick Williamsbbfe7182022-07-22 19:26:56 -050064 sdbusplus::server::object_t<ValueIface, CriticalInterface, WarningInterface,
65 AssociationDefinitionInterface>;
Sui Chen670cc132021-04-13 09:27:22 -070066
Patrick Williams9ca00452022-11-26 09:41:58 -060067using BmcInterface = sdbusplus::server::object_t<
Sui Chen517524a2021-12-19 20:52:46 -080068 sdbusplus::xyz::openbmc_project::Inventory::Item::server::Bmc>;
69
Sui Chen670cc132021-04-13 09:27:22 -070070using AssociationTuple = std::tuple<std::string, std::string, std::string>;
Vijay Khemkae2795302020-07-15 17:28:45 -070071
Vijay Khemka15537762020-07-22 11:44:56 -070072struct HealthConfig
73{
74 std::string name;
75 uint16_t freq;
76 uint16_t windowSize;
Konstantin Aladysheva6cd7042021-12-21 15:36:01 +030077 double criticalHigh = std::numeric_limits<double>::quiet_NaN();
78 double warningHigh = std::numeric_limits<double>::quiet_NaN();
Vijay Khemka15537762020-07-22 11:44:56 -070079 bool criticalLog;
80 bool warningLog;
81 std::string criticalTgt;
82 std::string warningTgt;
Bruceleequantatwaf9acbd2020-10-12 15:21:42 +080083 std::string path;
Vijay Khemka15537762020-07-22 11:44:56 -070084};
85
Vijay Khemkae2795302020-07-15 17:28:45 -070086class HealthSensor : public healthIfaces
87{
88 public:
89 HealthSensor() = delete;
90 HealthSensor(const HealthSensor&) = delete;
91 HealthSensor& operator=(const HealthSensor&) = delete;
92 HealthSensor(HealthSensor&&) = delete;
93 HealthSensor& operator=(HealthSensor&&) = delete;
94 virtual ~HealthSensor() = default;
95
96 /** @brief Constructs HealthSensor
97 *
98 * @param[in] bus - Handle to system dbus
99 * @param[in] objPath - The Dbus path of health sensor
100 */
Patrick Williamsbbfe7182022-07-22 19:26:56 -0500101 HealthSensor(sdbusplus::bus_t& bus, const char* objPath,
Sui Chen670cc132021-04-13 09:27:22 -0700102 HealthConfig& sensorConfig,
103 const std::vector<std::string>& bmcIds) :
Vijay Khemka15537762020-07-22 11:44:56 -0700104 healthIfaces(bus, objPath),
Vijay Khemkab38fd582020-07-23 13:21:23 -0700105 bus(bus), sensorConfig(sensorConfig),
106 timerEvent(sdeventplus::Event::get_default()),
107 readTimer(timerEvent, std::bind(&HealthSensor::readHealthSensor, this))
Vijay Khemka15537762020-07-22 11:44:56 -0700108 {
Sui Chen670cc132021-04-13 09:27:22 -0700109 initHealthSensor(bmcIds);
Vijay Khemka15537762020-07-22 11:44:56 -0700110 }
Vijay Khemkae2795302020-07-15 17:28:45 -0700111
Vijay Khemka15537762020-07-22 11:44:56 -0700112 /** @brief list of sensor data values */
113 std::deque<double> valQueue;
Sui Chen670cc132021-04-13 09:27:22 -0700114 /** @brief Initialize sensor, set default value and association */
115 void initHealthSensor(const std::vector<std::string>& bmcIds);
Vijay Khemkae2795302020-07-15 17:28:45 -0700116 /** @brief Set sensor value utilization to health sensor D-bus */
Vijay Khemka15537762020-07-22 11:44:56 -0700117 void setSensorValueToDbus(const double value);
Vijay Khemkae2795302020-07-15 17:28:45 -0700118 /** @brief Set Sensor Threshold to D-bus at beginning */
Vijay Khemka15537762020-07-22 11:44:56 -0700119 void setSensorThreshold(double criticalHigh, double warningHigh);
Vijay Khemkab7a7b8a2020-07-29 12:22:01 -0700120 /** @brief Check Sensor threshold and update alarm and log */
121 void checkSensorThreshold(const double value);
Vijay Khemkae2795302020-07-15 17:28:45 -0700122
123 private:
Vijay Khemkab38fd582020-07-23 13:21:23 -0700124 /** @brief sdbusplus bus client connection. */
Patrick Williamsbbfe7182022-07-22 19:26:56 -0500125 sdbusplus::bus_t& bus;
Vijay Khemkab38fd582020-07-23 13:21:23 -0700126 /** @brief Sensor config from config file */
Vijay Khemka15537762020-07-22 11:44:56 -0700127 HealthConfig& sensorConfig;
Vijay Khemkab38fd582020-07-23 13:21:23 -0700128 /** @brief the Event Loop structure */
129 sdeventplus::Event timerEvent;
130 /** @brief Sensor Read Timer */
131 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> readTimer;
Vijay Khemkab38fd582020-07-23 13:21:23 -0700132 /** @brief Read sensor at regular intrval */
133 void readHealthSensor();
Potin Lai156ecf32022-07-11 17:09:10 +0800134 /** @brief Start configured threshold systemd unit */
135 void startUnit(const std::string& sysdUnit);
Vijay Khemkae2795302020-07-15 17:28:45 -0700136};
137
Sui Chen517524a2021-12-19 20:52:46 -0800138class BmcInventory : public BmcInterface
139{
140 public:
141 BmcInventory() = delete;
Patrick Williams9ca00452022-11-26 09:41:58 -0600142 BmcInventory(sdbusplus::bus_t& bus, const char* objPath) :
Sui Chen517524a2021-12-19 20:52:46 -0800143 BmcInterface(bus, objPath)
144 {}
145};
146
Vijay Khemkae2795302020-07-15 17:28:45 -0700147class HealthMon
148{
149 public:
150 HealthMon() = delete;
151 HealthMon(const HealthMon&) = delete;
152 HealthMon& operator=(const HealthMon&) = delete;
153 HealthMon(HealthMon&&) = delete;
154 HealthMon& operator=(HealthMon&&) = delete;
155 virtual ~HealthMon() = default;
156
Sui Chen036f1612021-07-22 01:31:49 -0700157 /** @brief Recreates sensor objects and their association if possible
158 */
159 void recreateSensors();
160
Vijay Khemkae2795302020-07-15 17:28:45 -0700161 /** @brief Constructs HealthMon
162 *
163 * @param[in] bus - Handle to system dbus
164 */
Patrick Williamsbbfe7182022-07-22 19:26:56 -0500165 HealthMon(sdbusplus::bus_t& bus) : bus(bus)
Vijay Khemkae2795302020-07-15 17:28:45 -0700166 {
Sui Chen670cc132021-04-13 09:27:22 -0700167 // Read JSON file
Vijay Khemkae2795302020-07-15 17:28:45 -0700168 sensorConfigs = getHealthConfig();
Sui Chen036f1612021-07-22 01:31:49 -0700169 recreateSensors();
Vijay Khemkae2795302020-07-15 17:28:45 -0700170 }
171
Sui Chen670cc132021-04-13 09:27:22 -0700172 /** @brief Parse Health config JSON file */
Vijay Khemkae2795302020-07-15 17:28:45 -0700173 Json parseConfigFile(std::string configFile);
174
Sui Chen670cc132021-04-13 09:27:22 -0700175 /** @brief Read config for each health sensor component */
Vijay Khemkae2795302020-07-15 17:28:45 -0700176 void getConfigData(Json& data, HealthConfig& cfg);
177
178 /** @brief Map of the object HealthSensor */
179 std::unordered_map<std::string, std::shared_ptr<HealthSensor>>
180 healthSensors;
181
182 /** @brief Create sensors for health monitoring */
Sui Chen670cc132021-04-13 09:27:22 -0700183 void createHealthSensors(const std::vector<std::string>& bmcIds);
Vijay Khemkae2795302020-07-15 17:28:45 -0700184
Sui Chen517524a2021-12-19 20:52:46 -0800185 /** @brief Create the BMC Inventory object */
186 void createBmcInventoryIfNotCreated();
187
188 std::shared_ptr<BmcInventory> bmcInventory;
189
190 bool bmcInventoryCreated();
191
Vijay Khemkae2795302020-07-15 17:28:45 -0700192 private:
Patrick Williamsbbfe7182022-07-22 19:26:56 -0500193 sdbusplus::bus_t& bus;
Vijay Khemkae2795302020-07-15 17:28:45 -0700194 std::vector<HealthConfig> sensorConfigs;
195 std::vector<HealthConfig> getHealthConfig();
196};
197
198} // namespace health
199} // namespace phosphor