Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 1 | #include <nlohmann/json.hpp> |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 2 | #include <phosphor-logging/log.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> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 14 | #include <map> |
| 15 | #include <string> |
| 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace health |
| 20 | { |
| 21 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 22 | using namespace phosphor::logging; |
| 23 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 24 | using Json = nlohmann::json; |
| 25 | using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
| 26 | |
| 27 | using CriticalInterface = |
| 28 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical; |
| 29 | |
| 30 | using WarningInterface = |
| 31 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning; |
| 32 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 33 | using AssociationDefinitionInterface = |
| 34 | sdbusplus::xyz::openbmc_project::Association::server::Definitions; |
| 35 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 36 | using healthIfaces = |
| 37 | sdbusplus::server::object::object<ValueIface, CriticalInterface, |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 38 | WarningInterface, |
| 39 | AssociationDefinitionInterface>; |
| 40 | |
| 41 | using AssociationTuple = std::tuple<std::string, std::string, std::string>; |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 42 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 43 | struct HealthConfig |
| 44 | { |
| 45 | std::string name; |
| 46 | uint16_t freq; |
| 47 | uint16_t windowSize; |
| 48 | double criticalHigh; |
| 49 | double warningHigh; |
| 50 | bool criticalLog; |
| 51 | bool warningLog; |
| 52 | std::string criticalTgt; |
| 53 | std::string warningTgt; |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 54 | std::string path; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 57 | class HealthSensor : public healthIfaces |
| 58 | { |
| 59 | public: |
| 60 | HealthSensor() = delete; |
| 61 | HealthSensor(const HealthSensor&) = delete; |
| 62 | HealthSensor& operator=(const HealthSensor&) = delete; |
| 63 | HealthSensor(HealthSensor&&) = delete; |
| 64 | HealthSensor& operator=(HealthSensor&&) = delete; |
| 65 | virtual ~HealthSensor() = default; |
| 66 | |
| 67 | /** @brief Constructs HealthSensor |
| 68 | * |
| 69 | * @param[in] bus - Handle to system dbus |
| 70 | * @param[in] objPath - The Dbus path of health sensor |
| 71 | */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 72 | HealthSensor(sdbusplus::bus::bus& bus, const char* objPath, |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 73 | HealthConfig& sensorConfig, |
| 74 | const std::vector<std::string>& bmcIds) : |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 75 | healthIfaces(bus, objPath), |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 76 | bus(bus), sensorConfig(sensorConfig), |
| 77 | timerEvent(sdeventplus::Event::get_default()), |
| 78 | readTimer(timerEvent, std::bind(&HealthSensor::readHealthSensor, this)) |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 79 | { |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 80 | initHealthSensor(bmcIds); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 81 | } |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 82 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 83 | /** @brief list of sensor data values */ |
| 84 | std::deque<double> valQueue; |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 85 | /** @brief Initialize sensor, set default value and association */ |
| 86 | void initHealthSensor(const std::vector<std::string>& bmcIds); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 87 | /** @brief Set sensor value utilization to health sensor D-bus */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 88 | void setSensorValueToDbus(const double value); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 89 | /** @brief Set Sensor Threshold to D-bus at beginning */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 90 | void setSensorThreshold(double criticalHigh, double warningHigh); |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 91 | /** @brief Check Sensor threshold and update alarm and log */ |
| 92 | void checkSensorThreshold(const double value); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 93 | |
| 94 | private: |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 95 | /** @brief sdbusplus bus client connection. */ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 96 | sdbusplus::bus::bus& bus; |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 97 | /** @brief Sensor config from config file */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 98 | HealthConfig& sensorConfig; |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 99 | /** @brief the Event Loop structure */ |
| 100 | sdeventplus::Event timerEvent; |
| 101 | /** @brief Sensor Read Timer */ |
| 102 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> readTimer; |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 103 | /** @brief Read sensor at regular intrval */ |
| 104 | void readHealthSensor(); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | class HealthMon |
| 108 | { |
| 109 | public: |
| 110 | HealthMon() = delete; |
| 111 | HealthMon(const HealthMon&) = delete; |
| 112 | HealthMon& operator=(const HealthMon&) = delete; |
| 113 | HealthMon(HealthMon&&) = delete; |
| 114 | HealthMon& operator=(HealthMon&&) = delete; |
| 115 | virtual ~HealthMon() = default; |
| 116 | |
| 117 | /** @brief Constructs HealthMon |
| 118 | * |
| 119 | * @param[in] bus - Handle to system dbus |
| 120 | */ |
| 121 | HealthMon(sdbusplus::bus::bus& bus) : bus(bus) |
| 122 | { |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 123 | std::vector<std::string> bmcIds = {}; |
| 124 | |
| 125 | // Find all BMCs (DBus objects implementing the |
| 126 | // Inventory.Item.Bmc interface that may be created by |
| 127 | // configuring the Inventory Manager) |
| 128 | sdbusplus::message::message msg = bus.new_method_call( |
| 129 | "xyz.openbmc_project.ObjectMapper", |
| 130 | "/xyz/openbmc_project/object_mapper", |
| 131 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths"); |
| 132 | |
| 133 | // Search term |
| 134 | msg.append("/xyz/openbmc_project/inventory/system"); |
| 135 | |
| 136 | // Limit the depth to 2. Example of "depth": |
| 137 | // /xyz/openbmc_project/inventory/system/chassis has a depth of 1 |
| 138 | // since it has 1 '/' after "/xyz/openbmc_project/inventory/system". |
| 139 | msg.append(2); |
| 140 | |
| 141 | // Must have the Inventory.Item.Bmc interface |
| 142 | msg.append( |
| 143 | std::vector<std::string>{"xyz.openbmc_project.Inventory.Item.Bmc"}); |
| 144 | |
| 145 | sdbusplus::message::message reply = bus.call(msg, 0); |
| 146 | if (reply.get_signature() == std::string("as")) |
| 147 | { |
| 148 | reply.read(bmcIds); |
| 149 | log<level::INFO>("BMC inventory found"); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | log<level::WARNING>( |
| 154 | "Did not find BMC inventory, cannot create association"); |
| 155 | } |
| 156 | |
| 157 | // Read JSON file |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 158 | sensorConfigs = getHealthConfig(); |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 159 | |
| 160 | // Create health sensors |
| 161 | createHealthSensors(bmcIds); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 164 | /** @brief Parse Health config JSON file */ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 165 | Json parseConfigFile(std::string configFile); |
| 166 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 167 | /** @brief Read config for each health sensor component */ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 168 | void getConfigData(Json& data, HealthConfig& cfg); |
| 169 | |
| 170 | /** @brief Map of the object HealthSensor */ |
| 171 | std::unordered_map<std::string, std::shared_ptr<HealthSensor>> |
| 172 | healthSensors; |
| 173 | |
| 174 | /** @brief Create sensors for health monitoring */ |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 175 | void createHealthSensors(const std::vector<std::string>& bmcIds); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 176 | |
| 177 | private: |
| 178 | sdbusplus::bus::bus& bus; |
| 179 | std::vector<HealthConfig> sensorConfigs; |
| 180 | std::vector<HealthConfig> getHealthConfig(); |
| 181 | }; |
| 182 | |
| 183 | } // namespace health |
| 184 | } // namespace phosphor |