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