From baa59772c55c45a237d471b1227296d09b33705d Mon Sep 17 00:00:00 2001 From: wangjue Date: Thu, 19 Dec 2024 19:11:25 +0800 Subject: [PATCH] Retrieve SEL sensor info from JSON file instead of calling from IPMI service Signed-off-by: wangjue --- fault-monitor/fru-fault-monitor.cpp | 95 +++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 12 deletions(-) diff --git a/fault-monitor/fru-fault-monitor.cpp b/fault-monitor/fru-fault-monitor.cpp index 1bc41aa..a64f375 100644 --- a/fault-monitor/fru-fault-monitor.cpp +++ b/fault-monitor/fru-fault-monitor.cpp @@ -5,11 +5,16 @@ #include #include #include +#include +#include #include #include #include #include #include +#include +#include +#include extern "C" { @@ -230,6 +235,14 @@ using AdditionalList = std::vector; using sensorCodes = std::vector; using ipmiSensorsInfo = std::unordered_map; std::optional ipmiSensors; +struct SensorInfo +{ + std::string sensorName; + uint8_t readingType; + uint8_t sensorType; +}; + +using SensorMessages = std::unordered_map; const std::unordered_map genericEventTypes{ {0x03, @@ -376,6 +389,72 @@ static const std::unordered_map> EVENT_TYPE_07H_TRANSITION_TO_NC_FROM_OK}}, }; +static constexpr auto sensorJsonPath = + "/usr/share/ipmi-providers/sensor-data-record.json"; + +SensorMessages loadSensorFromJson() +{ + try + { + const std::filesystem::path path(sensorJsonPath); + SensorMessages sensorMessages; + const nlohmann::json empty{}; + nlohmann::json js; + if (!std::filesystem::exists(path) || std::filesystem::is_empty(path)) + { + std::cout << "Incorrect File Path or empty file, FILE_PATH = " + << path << std::endl; + return sensorMessages; + } + + try + { + std::ifstream jsonFile(path); + js = nlohmann::json::parse(jsonFile); + } + catch (const std::exception& e) + { + std::cout << "Failed to parse config file, ERROR = " << e.what() + << ", FILE_PATH = " << path << std::endl; + throw std::runtime_error("Failed to parse config file"); + } + + for (const auto& sensor : js.items()) + { + SensorInfo info; + std::string sensorPath = sensor.key(); + auto sensorParam = sensor.value(); + info.sensorName = sensorParam["sensorName"]; + info.readingType = sensorParam["sensorReadingType"]; + info.sensorType = sensorParam["sensorType"]; + sensorMessages.insert(std::make_pair(sensorPath, std::move(info))); + } + return sensorMessages; + } + catch (const std::exception& e) + { + throw std::runtime_error("Failed to parse sensor config file"); + } +} + +const SensorMessages& getSensorMessages() +{ + static const SensorMessages sensorMessages = loadSensorFromJson(); + return sensorMessages; +} + +std::optional + getSensorInfo(const std::string& sensorPath) +{ + const auto& sensorMessages = getSensorMessages(); + auto ite = sensorMessages.find(sensorPath); + if (ite == sensorMessages.end()) + { + return std::nullopt; + } + return std::make_optional(ite->second); +} + std::optional initIPMISensorInfo(sdbusplus::bus::bus& bus) { constexpr auto service = "xyz.openbmc_project.Ipmi.Host"; @@ -1212,19 +1291,11 @@ void Add::filterSEL(sdbusplus::bus::bus& bus, const std::string& path) } } - if (!ipmiSensors) + auto sensorInfo = getSensorInfo(sensorPath); + if (sensorInfo != std::nullopt) { - ipmiSensors = initIPMISensorInfo(bus); - } - - if (ipmiSensors) - { - auto iter = ipmiSensors->find(sensorPath); - if (iter != ipmiSensors->end()) - { - sensorType = iter->second[0]; - typeCode = iter->second[1]; - } + sensorType = sensorInfo->sensorType; + typeCode = sensorInfo->readingType; } if (sensorData == "" || typeCode == 0 || eventDir == "" || sensorType == 0) -- 2.34.1