Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 1 | #include <nlohmann/json.hpp> |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 2 | #include <phosphor-logging/lg2.hpp> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 3 | #include <sdbusplus/bus.hpp> |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 4 | #include <sdbusplus/message.hpp> |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 5 | #include <sdeventplus/clock.hpp> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 6 | #include <sdeventplus/event.hpp> |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 7 | #include <sdeventplus/utility/timer.hpp> |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Association/Definitions/server.hpp> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp> |
| 10 | #include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp> |
| 11 | #include <xyz/openbmc_project/Sensor/Value/server.hpp> |
| 12 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 13 | #include <deque> |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 14 | #include <limits> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 15 | #include <map> |
| 16 | #include <string> |
| 17 | |
| 18 | namespace phosphor |
| 19 | { |
| 20 | namespace health |
| 21 | { |
| 22 | |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 23 | const char* InventoryPath = "/xyz/openbmc_project/inventory"; |
| 24 | |
| 25 | // Used for identifying the BMC inventory creation signal |
| 26 | const char* BMCActivationPath = "/xyz/openbmc_project/inventory/bmc/activation"; |
| 27 | |
| 28 | bool FindSystemInventoryInObjectMapper(sdbusplus::bus::bus& bus) |
| 29 | { |
| 30 | sdbusplus::message::message msg = |
| 31 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 32 | "/xyz/openbmc_project/object_mapper", |
| 33 | "xyz.openbmc_project.ObjectMapper", "GetObject"); |
| 34 | msg.append(InventoryPath); |
| 35 | msg.append(std::vector<std::string>{}); |
| 36 | |
| 37 | try |
| 38 | { |
| 39 | sdbusplus::message::message reply = bus.call(msg, 0); |
| 40 | return true; |
| 41 | } |
| 42 | catch (const std::exception& e) |
| 43 | {} |
| 44 | return false; |
| 45 | } |
| 46 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 47 | using Json = nlohmann::json; |
| 48 | using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
| 49 | |
| 50 | using CriticalInterface = |
| 51 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical; |
| 52 | |
| 53 | using WarningInterface = |
| 54 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning; |
| 55 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 56 | using AssociationDefinitionInterface = |
| 57 | sdbusplus::xyz::openbmc_project::Association::server::Definitions; |
| 58 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 59 | using healthIfaces = |
| 60 | sdbusplus::server::object::object<ValueIface, CriticalInterface, |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 61 | WarningInterface, |
| 62 | AssociationDefinitionInterface>; |
| 63 | |
| 64 | using AssociationTuple = std::tuple<std::string, std::string, std::string>; |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 65 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 66 | struct HealthConfig |
| 67 | { |
| 68 | std::string name; |
| 69 | uint16_t freq; |
| 70 | uint16_t windowSize; |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 71 | double criticalHigh = std::numeric_limits<double>::quiet_NaN(); |
| 72 | double warningHigh = std::numeric_limits<double>::quiet_NaN(); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 73 | bool criticalLog; |
| 74 | bool warningLog; |
| 75 | std::string criticalTgt; |
| 76 | std::string warningTgt; |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 77 | std::string path; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 80 | class HealthSensor : public healthIfaces |
| 81 | { |
| 82 | public: |
| 83 | HealthSensor() = delete; |
| 84 | HealthSensor(const HealthSensor&) = delete; |
| 85 | HealthSensor& operator=(const HealthSensor&) = delete; |
| 86 | HealthSensor(HealthSensor&&) = delete; |
| 87 | HealthSensor& operator=(HealthSensor&&) = delete; |
| 88 | virtual ~HealthSensor() = default; |
| 89 | |
| 90 | /** @brief Constructs HealthSensor |
| 91 | * |
| 92 | * @param[in] bus - Handle to system dbus |
| 93 | * @param[in] objPath - The Dbus path of health sensor |
| 94 | */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 95 | HealthSensor(sdbusplus::bus::bus& bus, const char* objPath, |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 96 | HealthConfig& sensorConfig, |
| 97 | const std::vector<std::string>& bmcIds) : |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 98 | healthIfaces(bus, objPath), |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 99 | bus(bus), sensorConfig(sensorConfig), |
| 100 | timerEvent(sdeventplus::Event::get_default()), |
| 101 | readTimer(timerEvent, std::bind(&HealthSensor::readHealthSensor, this)) |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 102 | { |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 103 | initHealthSensor(bmcIds); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 104 | } |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 105 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 106 | /** @brief list of sensor data values */ |
| 107 | std::deque<double> valQueue; |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 108 | /** @brief Initialize sensor, set default value and association */ |
| 109 | void initHealthSensor(const std::vector<std::string>& bmcIds); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 110 | /** @brief Set sensor value utilization to health sensor D-bus */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 111 | void setSensorValueToDbus(const double value); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 112 | /** @brief Set Sensor Threshold to D-bus at beginning */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 113 | void setSensorThreshold(double criticalHigh, double warningHigh); |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 114 | /** @brief Check Sensor threshold and update alarm and log */ |
| 115 | void checkSensorThreshold(const double value); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 116 | |
| 117 | private: |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 118 | /** @brief sdbusplus bus client connection. */ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 119 | sdbusplus::bus::bus& bus; |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 120 | /** @brief Sensor config from config file */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 121 | HealthConfig& sensorConfig; |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 122 | /** @brief the Event Loop structure */ |
| 123 | sdeventplus::Event timerEvent; |
| 124 | /** @brief Sensor Read Timer */ |
| 125 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> readTimer; |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 126 | /** @brief Read sensor at regular intrval */ |
| 127 | void readHealthSensor(); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | class HealthMon |
| 131 | { |
| 132 | public: |
| 133 | HealthMon() = delete; |
| 134 | HealthMon(const HealthMon&) = delete; |
| 135 | HealthMon& operator=(const HealthMon&) = delete; |
| 136 | HealthMon(HealthMon&&) = delete; |
| 137 | HealthMon& operator=(HealthMon&&) = delete; |
| 138 | virtual ~HealthMon() = default; |
| 139 | |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 140 | /** @brief Recreates sensor objects and their association if possible |
| 141 | */ |
| 142 | void recreateSensors(); |
| 143 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 144 | /** @brief Constructs HealthMon |
| 145 | * |
| 146 | * @param[in] bus - Handle to system dbus |
| 147 | */ |
| 148 | HealthMon(sdbusplus::bus::bus& bus) : bus(bus) |
| 149 | { |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 150 | // Read JSON file |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 151 | sensorConfigs = getHealthConfig(); |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 152 | recreateSensors(); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 155 | /** @brief Parse Health config JSON file */ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 156 | Json parseConfigFile(std::string configFile); |
| 157 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 158 | /** @brief Read config for each health sensor component */ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 159 | void getConfigData(Json& data, HealthConfig& cfg); |
| 160 | |
| 161 | /** @brief Map of the object HealthSensor */ |
| 162 | std::unordered_map<std::string, std::shared_ptr<HealthSensor>> |
| 163 | healthSensors; |
| 164 | |
| 165 | /** @brief Create sensors for health monitoring */ |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 166 | void createHealthSensors(const std::vector<std::string>& bmcIds); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 167 | |
| 168 | private: |
| 169 | sdbusplus::bus::bus& bus; |
| 170 | std::vector<HealthConfig> sensorConfigs; |
| 171 | std::vector<HealthConfig> getHealthConfig(); |
| 172 | }; |
| 173 | |
| 174 | } // namespace health |
| 175 | } // namespace phosphor |