Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "error_reporter.hpp" |
| 17 | |
Matt Spinler | 76e73c2 | 2021-04-21 11:03:05 -0500 | [diff] [blame] | 18 | #include "get_power_state.hpp" |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 19 | #include "logging.hpp" |
| 20 | #include "psensor.hpp" |
| 21 | #include "utility.hpp" |
| 22 | |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
Matt Spinler | 39fcd50 | 2020-09-24 14:23:11 -0500 | [diff] [blame] | 25 | #include <xyz/openbmc_project/Logging/Create/server.hpp> |
| 26 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 27 | |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 28 | #include <format> |
| 29 | |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 30 | namespace phosphor::fan::presence |
| 31 | { |
| 32 | |
| 33 | using json = nlohmann::json; |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 34 | using namespace sdbusplus::bus::match; |
| 35 | using namespace std::literals::string_literals; |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 36 | using namespace std::chrono; |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 37 | namespace fs = std::filesystem; |
| 38 | |
| 39 | const auto itemIface = "xyz.openbmc_project.Inventory.Item"s; |
| 40 | const auto invPrefix = "/xyz/openbmc_project/inventory"s; |
Matt Spinler | 39fcd50 | 2020-09-24 14:23:11 -0500 | [diff] [blame] | 41 | const auto loggingPath = "/xyz/openbmc_project/logging"; |
| 42 | const auto loggingCreateIface = "xyz.openbmc_project.Logging.Create"; |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 43 | |
| 44 | ErrorReporter::ErrorReporter( |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 45 | sdbusplus::bus_t& bus, |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 46 | const std::vector< |
| 47 | std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>>& fans) : |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 48 | _bus(bus), _event(sdeventplus::Event::get_default()), |
Matt Spinler | 76e73c2 | 2021-04-21 11:03:05 -0500 | [diff] [blame] | 49 | _powerState(getPowerStateObject()) |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 50 | { |
Matt Spinler | 76e73c2 | 2021-04-21 11:03:05 -0500 | [diff] [blame] | 51 | _powerState->addCallback("errorReporter", |
| 52 | std::bind(&ErrorReporter::powerStateChanged, this, |
| 53 | std::placeholders::_1)); |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 54 | |
| 55 | for (const auto& fan : fans) |
| 56 | { |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 57 | const auto& fanData = std::get<0>(fan); |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 58 | |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 59 | // Only deal with fans that have an error time defined. |
| 60 | if (std::get<std::optional<size_t>>(fanData)) |
| 61 | { |
| 62 | auto path = invPrefix + std::get<1>(fanData); |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 63 | |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 64 | // Register for fan presence changes, get their initial states, |
| 65 | // and create the fan missing timers. |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 66 | |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 67 | _matches.emplace_back( |
| 68 | _bus, rules::propertiesChanged(path, itemIface), |
| 69 | std::bind(std::mem_fn(&ErrorReporter::presenceChanged), this, |
| 70 | std::placeholders::_1)); |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 71 | |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 72 | _fanStates.emplace(path, getPresence(fanData)); |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 73 | |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 74 | auto timer = std::make_unique< |
| 75 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>( |
| 76 | _event, |
| 77 | std::bind(std::mem_fn(&ErrorReporter::fanMissingTimerExpired), |
| 78 | this, path)); |
| 79 | |
| 80 | seconds errorTime{std::get<std::optional<size_t>>(fanData).value()}; |
| 81 | |
| 82 | _fanMissingTimers.emplace( |
| 83 | path, std::make_tuple(std::move(timer), std::move(errorTime))); |
| 84 | } |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // If power is already on, check for currently missing fans. |
| 88 | if (_powerState->isPowerOn()) |
| 89 | { |
| 90 | powerStateChanged(true); |
| 91 | } |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 94 | void ErrorReporter::presenceChanged(sdbusplus::message_t& msg) |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 95 | { |
| 96 | bool present; |
| 97 | auto fanPath = msg.get_path(); |
| 98 | std::string interface; |
| 99 | std::map<std::string, std::variant<bool>> properties; |
| 100 | |
| 101 | msg.read(interface, properties); |
| 102 | |
| 103 | auto presentProp = properties.find("Present"); |
| 104 | if (presentProp != properties.end()) |
| 105 | { |
| 106 | present = std::get<bool>(presentProp->second); |
| 107 | if (_fanStates[fanPath] != present) |
| 108 | { |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 109 | getLogger().log(std::format("Fan {} presence state change to {}", |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 110 | fanPath, present)); |
| 111 | |
| 112 | _fanStates[fanPath] = present; |
| 113 | checkFan(fanPath); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void ErrorReporter::checkFan(const std::string& fanPath) |
| 119 | { |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 120 | auto& timer = std::get<0>(_fanMissingTimers[fanPath]); |
| 121 | |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 122 | if (!_fanStates[fanPath]) |
| 123 | { |
| 124 | // Fan is missing. If power is on, start the timer. |
| 125 | // If power is off, stop a running timer. |
| 126 | if (_powerState->isPowerOn()) |
| 127 | { |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 128 | timer->restartOnce(std::get<seconds>(_fanMissingTimers[fanPath])); |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 129 | } |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 130 | else if (timer->isEnabled()) |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 131 | { |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 132 | timer->setEnabled(false); |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | // Fan is present. Stop a running timer. |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 138 | if (timer->isEnabled()) |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 139 | { |
Matt Spinler | 9e9f599 | 2020-09-30 08:29:24 -0500 | [diff] [blame] | 140 | timer->setEnabled(false); |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void ErrorReporter::fanMissingTimerExpired(const std::string& fanPath) |
Matt Spinler | 39fcd50 | 2020-09-24 14:23:11 -0500 | [diff] [blame] | 146 | { |
| 147 | getLogger().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 148 | std::format("Creating event log for missing fan {}", fanPath), |
Matt Spinler | 39fcd50 | 2020-09-24 14:23:11 -0500 | [diff] [blame] | 149 | Logger::error); |
| 150 | |
| 151 | std::map<std::string, std::string> additionalData; |
| 152 | additionalData.emplace("_PID", std::to_string(getpid())); |
| 153 | additionalData.emplace("CALLOUT_INVENTORY_PATH", fanPath); |
| 154 | |
| 155 | auto severity = |
| 156 | sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage( |
| 157 | sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level:: |
| 158 | Error); |
| 159 | |
Matt Spinler | c674510 | 2020-10-22 10:52:02 -0500 | [diff] [blame] | 160 | // Save our logs to a temp file and get the file descriptor |
Matt Spinler | 39fcd50 | 2020-09-24 14:23:11 -0500 | [diff] [blame] | 161 | // so it can be passed in as FFDC data. |
| 162 | auto logFile = getLogger().saveToTempFile(); |
| 163 | util::FileDescriptor fd{-1}; |
| 164 | fd.open(logFile, O_RDONLY); |
| 165 | |
| 166 | std::vector<std::tuple< |
| 167 | sdbusplus::xyz::openbmc_project::Logging::server::Create::FFDCFormat, |
| 168 | uint8_t, uint8_t, sdbusplus::message::unix_fd>> |
| 169 | ffdc; |
| 170 | |
| 171 | ffdc.emplace_back(sdbusplus::xyz::openbmc_project::Logging::server::Create:: |
Matt Spinler | c674510 | 2020-10-22 10:52:02 -0500 | [diff] [blame] | 172 | FFDCFormat::Text, |
Matt Spinler | 39fcd50 | 2020-09-24 14:23:11 -0500 | [diff] [blame] | 173 | 0x01, 0x01, fd()); |
| 174 | |
| 175 | try |
| 176 | { |
| 177 | util::SDBusPlus::lookupAndCallMethod( |
| 178 | loggingPath, loggingCreateIface, "CreateWithFFDCFiles", |
| 179 | "xyz.openbmc_project.Fan.Error.Missing", severity, additionalData, |
| 180 | ffdc); |
| 181 | } |
| 182 | catch (const util::DBusError& e) |
| 183 | { |
| 184 | getLogger().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 185 | std::format( |
Matt Spinler | 39fcd50 | 2020-09-24 14:23:11 -0500 | [diff] [blame] | 186 | "Call to create an error log for missing fan {} failed: {}", |
| 187 | fanPath, e.what()), |
| 188 | Logger::error); |
| 189 | fs::remove(logFile); |
| 190 | throw; |
| 191 | } |
| 192 | |
| 193 | fs::remove(logFile); |
| 194 | } |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 195 | |
| 196 | void ErrorReporter::powerStateChanged(bool powerState) |
| 197 | { |
| 198 | if (powerState) |
| 199 | { |
| 200 | // If there are fans already missing, log it. |
Patrick Williams | 5e15c3b | 2023-10-20 11:18:11 -0500 | [diff] [blame] | 201 | auto missing = std::count_if( |
| 202 | _fanStates.begin(), _fanStates.end(), |
| 203 | [](const auto& fanState) { return fanState.second == false; }); |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 204 | |
| 205 | if (missing) |
| 206 | { |
| 207 | getLogger().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 208 | std::format("At power on, there are {} missing fans", missing)); |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 212 | std::for_each(_fanStates.begin(), _fanStates.end(), |
| 213 | [this](const auto& fanState) { |
| 214 | this->checkFan(fanState.first); |
| 215 | }); |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 218 | } // namespace phosphor::fan::presence |