Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 1 | /** |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame] | 2 | * Copyright © 2022 IBM Corporation |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 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 | */ |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 16 | #include "system.hpp" |
| 17 | |
Mike Capps | bf8e56f | 2022-06-29 14:23:07 -0400 | [diff] [blame] | 18 | #include "dbus_paths.hpp" |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 19 | #include "fan.hpp" |
| 20 | #include "fan_defs.hpp" |
| 21 | #include "tach_sensor.hpp" |
| 22 | #include "trust_manager.hpp" |
| 23 | #include "types.hpp" |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 24 | #include "utility.hpp" |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 25 | #ifdef MONITOR_USE_JSON |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 26 | #include "json_config.hpp" |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 27 | #include "json_parser.hpp" |
| 28 | #endif |
| 29 | |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 30 | #include "config.h" |
| 31 | |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 32 | #include "hwmon_ffdc.hpp" |
| 33 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 34 | #include <nlohmann/json.hpp> |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 35 | #include <phosphor-logging/log.hpp> |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 36 | #include <sdbusplus/bus.hpp> |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 37 | #include <sdbusplus/bus/match.hpp> |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 38 | #include <sdeventplus/event.hpp> |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 39 | #include <sdeventplus/source/signal.hpp> |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 40 | |
| 41 | namespace phosphor::fan::monitor |
| 42 | { |
| 43 | |
| 44 | using json = nlohmann::json; |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 45 | using Severity = sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level; |
| 46 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 47 | using namespace phosphor::logging; |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 48 | |
Matt Spinler | 4f472a8 | 2022-08-26 13:55:34 -0500 | [diff] [blame] | 49 | const std::string System::dumpFile = "/tmp/fan_monitor_dump.json"; |
| 50 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 51 | System::System(Mode mode, sdbusplus::bus_t& bus, |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 52 | const sdeventplus::Event& event) : |
| 53 | _mode(mode), |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 54 | _bus(bus), _event(event), |
| 55 | _powerState(std::make_unique<PGoodState>( |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 56 | bus, std::bind(std::mem_fn(&System::powerStateChanged), this, |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 57 | std::placeholders::_1))), |
| 58 | _thermalAlert(bus, THERMAL_ALERT_OBJPATH) |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 59 | {} |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 60 | |
Matthew Barth | 823bc49 | 2021-06-21 14:19:09 -0500 | [diff] [blame] | 61 | void System::start() |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 62 | { |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 63 | namespace match = sdbusplus::bus::match; |
| 64 | |
| 65 | // must be done before service detection |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 66 | _inventoryMatch = std::make_unique<sdbusplus::bus::match_t>( |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 67 | _bus, match::rules::nameOwnerChanged(util::INVENTORY_SVC), |
| 68 | std::bind(&System::inventoryOnlineCb, this, std::placeholders::_1)); |
| 69 | |
| 70 | bool invServiceRunning = util::SDBusPlus::callMethodAndRead<bool>( |
| 71 | _bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", |
| 72 | "org.freedesktop.DBus", "NameHasOwner", util::INVENTORY_SVC); |
| 73 | |
| 74 | if (invServiceRunning) |
| 75 | { |
| 76 | _inventoryMatch.reset(); |
| 77 | |
| 78 | if (!_loaded) |
| 79 | { |
| 80 | load(); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void System::load() |
| 86 | { |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 87 | json jsonObj = json::object(); |
| 88 | #ifdef MONITOR_USE_JSON |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 89 | try |
| 90 | { |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 91 | jsonObj = getJsonObj(); |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 92 | #endif |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 93 | auto trustGrps = getTrustGroups(jsonObj); |
| 94 | auto fanDefs = getFanDefinitions(jsonObj); |
| 95 | // Retrieve and set trust groups within the trust manager |
| 96 | setTrustMgr(getTrustGroups(jsonObj)); |
| 97 | // Clear/set configured fan definitions |
| 98 | _fans.clear(); |
| 99 | _fanHealth.clear(); |
| 100 | // Retrieve fan definitions and create fan objects to be monitored |
| 101 | setFans(fanDefs); |
| 102 | setFaultConfig(jsonObj); |
| 103 | log<level::INFO>("Configuration loaded"); |
| 104 | |
| 105 | _loaded = true; |
| 106 | #ifdef MONITOR_USE_JSON |
| 107 | } |
| 108 | catch (const phosphor::fan::NoConfigFound&) |
| 109 | {} |
| 110 | #endif |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 111 | |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 112 | if (_powerState->isPowerOn()) |
| 113 | { |
Matt Spinler | 752f24e | 2022-07-06 15:57:54 -0500 | [diff] [blame] | 114 | // Fans could be missing on startup, so check the power off rules. |
| 115 | // Tach sensors default to functional, so they wouldn't cause a power |
| 116 | // off here. |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 117 | std::for_each(_powerOffRules.begin(), _powerOffRules.end(), |
| 118 | [this](auto& rule) { |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 119 | rule->check(PowerRuleState::runtime, _fanHealth); |
| 120 | }); |
| 121 | } |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 122 | |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 123 | subscribeSensorsToServices(); |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 126 | void System::subscribeSensorsToServices() |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 127 | { |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 128 | namespace match = sdbusplus::bus::match; |
| 129 | |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 130 | _sensorMatch.clear(); |
| 131 | |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 132 | SensorMapType sensorMap; |
| 133 | |
| 134 | // build a list of all interfaces, always including the value interface |
| 135 | // using set automatically guards against duplicates |
| 136 | std::set<std::string> unique_interfaces{util::FAN_SENSOR_VALUE_INTF}; |
| 137 | |
| 138 | for (const auto& fan : _fans) |
| 139 | { |
| 140 | for (const auto& sensor : fan->sensors()) |
| 141 | { |
| 142 | unique_interfaces.insert(sensor->getInterface()); |
| 143 | } |
| 144 | } |
| 145 | // convert them to vector to pass into getSubTreeRaw |
| 146 | std::vector<std::string> interfaces(unique_interfaces.begin(), |
| 147 | unique_interfaces.end()); |
| 148 | |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 149 | try |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 150 | { |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 151 | // get service information for all service names that are |
| 152 | // hosting these interfaces |
| 153 | auto serviceObjects = util::SDBusPlus::getSubTreeRaw( |
| 154 | _bus, FAN_SENSOR_PATH, interfaces, 0); |
| 155 | |
| 156 | for (const auto& fan : _fans) |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 157 | { |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 158 | // For every sensor in each fan |
| 159 | for (const auto& sensor : fan->sensors()) |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 160 | { |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 161 | const auto itServ = serviceObjects.find(sensor->name()); |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 162 | |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 163 | if (serviceObjects.end() == itServ || itServ->second.empty()) |
| 164 | { |
| 165 | getLogger().log( |
| 166 | fmt::format("Fan sensor entry {} not found in D-Bus", |
| 167 | sensor->name()), |
| 168 | Logger::error); |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | for (const auto& [serviceName, unused] : itServ->second) |
| 173 | { |
| 174 | // associate service name with sensor |
| 175 | sensorMap[serviceName].insert(sensor); |
| 176 | } |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 177 | } |
| 178 | } |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 179 | |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 180 | // only create 1 match per service |
| 181 | for (const auto& [serviceName, unused] : sensorMap) |
| 182 | { |
| 183 | // map its service name to the sensor |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 184 | _sensorMatch.emplace_back(std::make_unique<sdbusplus::bus::match_t>( |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 185 | _bus, match::rules::nameOwnerChanged(serviceName), |
| 186 | std::bind(&System::tachSignalOffline, this, |
| 187 | std::placeholders::_1, sensorMap))); |
| 188 | } |
| 189 | } |
| 190 | catch (const util::DBusError&) |
| 191 | { |
| 192 | // catch exception from getSubTreeRaw() when fan sensor paths don't |
| 193 | // exist yet |
| 194 | } |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 197 | void System::inventoryOnlineCb(sdbusplus::message_t& msg) |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 198 | { |
| 199 | namespace match = sdbusplus::bus::match; |
| 200 | |
| 201 | std::string iface; |
| 202 | msg.read(iface); |
| 203 | |
| 204 | if (util::INVENTORY_INTF != iface) |
| 205 | { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | std::string oldName; |
| 210 | msg.read(oldName); |
| 211 | |
| 212 | std::string newName; |
| 213 | msg.read(newName); |
| 214 | |
| 215 | // newName should never be empty since match was reset on the first |
| 216 | // nameOwnerChanged signal received from the service. |
| 217 | if (!_loaded && !newName.empty()) |
| 218 | { |
| 219 | load(); |
| 220 | } |
| 221 | |
| 222 | // cancel any further notifications about the service state |
| 223 | _inventoryMatch.reset(); |
| 224 | } |
| 225 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 226 | void System::sighupHandler(sdeventplus::source::Signal&, |
| 227 | const struct signalfd_siginfo*) |
| 228 | { |
| 229 | try |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 230 | { |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 231 | load(); |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 232 | } |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 233 | catch (std::runtime_error& re) |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 234 | { |
| 235 | log<level::ERR>("Error reloading config, no config changes made", |
| 236 | entry("LOAD_ERROR=%s", re.what())); |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | const std::vector<CreateGroupFunction> |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 241 | System::getTrustGroups([[maybe_unused]] const json& jsonObj) |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 242 | { |
| 243 | #ifdef MONITOR_USE_JSON |
| 244 | return getTrustGrps(jsonObj); |
| 245 | #else |
| 246 | return trustGroups; |
| 247 | #endif |
| 248 | } |
| 249 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 250 | void System::setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs) |
| 251 | { |
| 252 | _trust = std::make_unique<trust::Manager>(groupFuncs); |
| 253 | } |
| 254 | |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 255 | const std::vector<FanDefinition> |
| 256 | System::getFanDefinitions([[maybe_unused]] const json& jsonObj) |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 257 | { |
| 258 | #ifdef MONITOR_USE_JSON |
| 259 | return getFanDefs(jsonObj); |
| 260 | #else |
| 261 | return fanDefinitions; |
| 262 | #endif |
| 263 | } |
| 264 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 265 | void System::setFans(const std::vector<FanDefinition>& fanDefs) |
| 266 | { |
| 267 | for (const auto& fanDef : fanDefs) |
| 268 | { |
| 269 | // Check if a condition exists on the fan |
| 270 | auto condition = std::get<conditionField>(fanDef); |
| 271 | if (condition) |
| 272 | { |
| 273 | // Condition exists, skip adding fan if it fails |
| 274 | if (!(*condition)(_bus)) |
| 275 | { |
| 276 | continue; |
| 277 | } |
| 278 | } |
| 279 | _fans.emplace_back( |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 280 | std::make_unique<Fan>(_mode, _bus, _event, _trust, fanDef, *this)); |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 281 | |
| 282 | updateFanHealth(*(_fans.back())); |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 286 | // callback indicating a service went [on|off]line. |
| 287 | // Determine on/offline status, set all sensors for that service |
| 288 | // to new state |
| 289 | // |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 290 | void System::tachSignalOffline(sdbusplus::message_t& msg, |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 291 | SensorMapType const& sensorMap) |
| 292 | { |
| 293 | std::string serviceName, oldOwner, newOwner; |
| 294 | |
| 295 | msg.read(serviceName); |
| 296 | msg.read(oldOwner); |
| 297 | msg.read(newOwner); |
| 298 | |
| 299 | // true if sensor server came back online, false -> went offline |
| 300 | bool hasOwner = !newOwner.empty() && oldOwner.empty(); |
| 301 | |
| 302 | std::string stateStr(hasOwner ? "online" : "offline"); |
| 303 | getLogger().log(fmt::format("Changing sensors for service {} to {}", |
| 304 | serviceName, stateStr), |
| 305 | Logger::info); |
| 306 | |
| 307 | auto sensorItr(sensorMap.find(serviceName)); |
| 308 | |
| 309 | if (sensorItr != sensorMap.end()) |
| 310 | { |
| 311 | // set all sensors' owner state to not-owned |
| 312 | for (auto& sensor : sensorItr->second) |
| 313 | { |
| 314 | sensor->setOwner(hasOwner); |
| 315 | sensor->getFan().process(*sensor); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 320 | void System::updateFanHealth(const Fan& fan) |
| 321 | { |
| 322 | std::vector<bool> sensorStatus; |
| 323 | for (const auto& sensor : fan.sensors()) |
| 324 | { |
| 325 | sensorStatus.push_back(sensor->functional()); |
| 326 | } |
| 327 | |
| 328 | _fanHealth[fan.getName()] = |
| 329 | std::make_tuple(fan.present(), std::move(sensorStatus)); |
| 330 | } |
| 331 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 332 | void System::fanStatusChange(const Fan& fan, bool skipRulesCheck) |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 333 | { |
| 334 | updateFanHealth(fan); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 335 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 336 | if (_powerState->isPowerOn() && !skipRulesCheck) |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 337 | { |
| 338 | std::for_each(_powerOffRules.begin(), _powerOffRules.end(), |
| 339 | [this](auto& rule) { |
| 340 | rule->check(PowerRuleState::runtime, _fanHealth); |
| 341 | }); |
| 342 | } |
| 343 | } |
| 344 | |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 345 | void System::setFaultConfig([[maybe_unused]] const json& jsonObj) |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 346 | { |
| 347 | #ifdef MONITOR_USE_JSON |
| 348 | std::shared_ptr<PowerInterfaceBase> powerInterface = |
Matt Spinler | ba3ee9a | 2021-01-06 14:45:50 -0600 | [diff] [blame] | 349 | std::make_shared<PowerInterface>(_thermalAlert); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 350 | |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 351 | PowerOffAction::PrePowerOffFunc func = |
| 352 | std::bind(std::mem_fn(&System::logShutdownError), this); |
| 353 | |
| 354 | _powerOffRules = getPowerOffRules(jsonObj, powerInterface, func); |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 355 | |
| 356 | _numNonfuncSensorsBeforeError = getNumNonfuncRotorsBeforeError(jsonObj); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 357 | #endif |
| 358 | } |
| 359 | |
| 360 | void System::powerStateChanged(bool powerStateOn) |
| 361 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 362 | std::for_each(_fans.begin(), _fans.end(), [powerStateOn](auto& fan) { |
| 363 | fan->powerStateChanged(powerStateOn); |
| 364 | }); |
| 365 | |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 366 | if (powerStateOn) |
| 367 | { |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 368 | if (!_loaded) |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 369 | { |
| 370 | log<level::ERR>("No conf file found at power on"); |
Matthew Barth | ba53d3e | 2021-02-24 07:48:37 -0600 | [diff] [blame] | 371 | throw std::runtime_error("No conf file found at power on"); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 372 | } |
| 373 | |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 374 | // If no fan has its sensors on D-Bus, then there is a problem |
| 375 | // with the fan controller. Log an error and shut down. |
| 376 | if (std::all_of(_fans.begin(), _fans.end(), [](const auto& fan) { |
| 377 | return fan->numSensorsOnDBusAtPowerOn() == 0; |
| 378 | })) |
| 379 | { |
Chau Ly | 751c8be | 2023-01-13 08:21:03 +0000 | [diff] [blame^] | 380 | #if DELAY_HOST_CONTROL > 0 |
| 381 | sleep(DELAY_HOST_CONTROL); |
| 382 | std::for_each(_fans.begin(), _fans.end(), |
| 383 | [powerStateOn](auto& fan) { |
| 384 | fan->powerStateChanged(powerStateOn); |
| 385 | }); |
| 386 | if (std::all_of(_fans.begin(), _fans.end(), [](const auto& fan) { |
| 387 | return fan->numSensorsOnDBusAtPowerOn() == 0; |
| 388 | })) |
| 389 | { |
| 390 | handleOfflineFanController(); |
| 391 | return; |
| 392 | } |
| 393 | #else |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 394 | handleOfflineFanController(); |
| 395 | return; |
Chau Ly | 751c8be | 2023-01-13 08:21:03 +0000 | [diff] [blame^] | 396 | #endif |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 397 | } |
| 398 | |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 399 | if (_sensorMatch.empty()) |
| 400 | { |
| 401 | subscribeSensorsToServices(); |
| 402 | } |
| 403 | |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 404 | std::for_each(_powerOffRules.begin(), _powerOffRules.end(), |
| 405 | [this](auto& rule) { |
| 406 | rule->check(PowerRuleState::atPgood, _fanHealth); |
| 407 | }); |
| 408 | std::for_each(_powerOffRules.begin(), _powerOffRules.end(), |
| 409 | [this](auto& rule) { |
| 410 | rule->check(PowerRuleState::runtime, _fanHealth); |
| 411 | }); |
| 412 | } |
| 413 | else |
| 414 | { |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 415 | _thermalAlert.enabled(false); |
| 416 | |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 417 | // Cancel any in-progress power off actions |
| 418 | std::for_each(_powerOffRules.begin(), _powerOffRules.end(), |
| 419 | [this](auto& rule) { rule->cancel(); }); |
| 420 | } |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 421 | } |
| 422 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 423 | void System::sensorErrorTimerExpired(const Fan& fan, const TachSensor& sensor) |
| 424 | { |
| 425 | std::string fanPath{util::INVENTORY_PATH + fan.getName()}; |
| 426 | |
| 427 | getLogger().log( |
| 428 | fmt::format("Creating event log for faulted fan {} sensor {}", fanPath, |
| 429 | sensor.name()), |
| 430 | Logger::error); |
| 431 | |
| 432 | // In order to know if the event log should have a severity of error or |
| 433 | // informational, count the number of existing nonfunctional sensors and |
| 434 | // compare it to _numNonfuncSensorsBeforeError. |
| 435 | size_t nonfuncSensors = 0; |
| 436 | for (const auto& fan : _fans) |
| 437 | { |
| 438 | for (const auto& s : fan->sensors()) |
| 439 | { |
| 440 | // Don't count nonfunctional sensors that still have their |
| 441 | // error timer running as nonfunctional since they haven't |
| 442 | // had event logs created for those errors yet. |
| 443 | if (!s->functional() && !s->errorTimerRunning()) |
| 444 | { |
| 445 | nonfuncSensors++; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | Severity severity = Severity::Error; |
| 451 | if (nonfuncSensors < _numNonfuncSensorsBeforeError) |
| 452 | { |
| 453 | severity = Severity::Informational; |
| 454 | } |
| 455 | |
| 456 | auto error = |
| 457 | std::make_unique<FanError>("xyz.openbmc_project.Fan.Error.Fault", |
| 458 | fanPath, sensor.name(), severity); |
| 459 | |
| 460 | auto sensorData = captureSensorData(); |
| 461 | error->commit(sensorData); |
| 462 | |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 463 | // Save the error so it can be committed again on a power off. |
| 464 | _lastError = std::move(error); |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 465 | } |
| 466 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 467 | void System::fanMissingErrorTimerExpired(const Fan& fan) |
| 468 | { |
| 469 | std::string fanPath{util::INVENTORY_PATH + fan.getName()}; |
| 470 | |
| 471 | getLogger().log( |
| 472 | fmt::format("Creating event log for missing fan {}", fanPath), |
| 473 | Logger::error); |
| 474 | |
| 475 | auto error = std::make_unique<FanError>( |
| 476 | "xyz.openbmc_project.Fan.Error.Missing", fanPath, "", Severity::Error); |
| 477 | |
| 478 | auto sensorData = captureSensorData(); |
| 479 | error->commit(sensorData); |
| 480 | |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 481 | // Save the error so it can be committed again on a power off. |
| 482 | _lastError = std::move(error); |
| 483 | } |
| 484 | |
| 485 | void System::logShutdownError() |
| 486 | { |
| 487 | if (_lastError) |
| 488 | { |
| 489 | getLogger().log("Re-committing previous fan error before power off"); |
| 490 | |
| 491 | // Still use the latest sensor data |
| 492 | auto sensorData = captureSensorData(); |
Matt Spinler | f435eb1 | 2021-05-11 14:44:25 -0500 | [diff] [blame] | 493 | _lastError->commit(sensorData, true); |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 494 | } |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 495 | } |
| 496 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 497 | json System::captureSensorData() |
| 498 | { |
| 499 | json data; |
| 500 | |
| 501 | for (const auto& fan : _fans) |
| 502 | { |
| 503 | for (const auto& sensor : fan->sensors()) |
| 504 | { |
| 505 | json values; |
| 506 | values["present"] = fan->present(); |
| 507 | values["functional"] = sensor->functional(); |
Matt Spinler | d16d464 | 2022-08-26 13:32:07 -0500 | [diff] [blame] | 508 | values["in_range"] = !fan->outOfRange(*sensor); |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 509 | values["tach"] = sensor->getInput(); |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame] | 510 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 511 | if (sensor->hasTarget()) |
| 512 | { |
| 513 | values["target"] = sensor->getTarget(); |
| 514 | } |
| 515 | |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame] | 516 | // convert between string/json to remove newlines |
| 517 | values["prev_tachs"] = json(sensor->getPrevTach()).dump(); |
| 518 | |
| 519 | if (sensor->hasTarget()) |
| 520 | { |
| 521 | values["prev_targets"] = json(sensor->getPrevTarget()).dump(); |
| 522 | } |
| 523 | |
Matt Spinler | 87f9adc | 2022-08-11 13:17:09 -0500 | [diff] [blame] | 524 | if (sensor->getMethod() == MethodMode::count) |
| 525 | { |
| 526 | values["ticks"] = sensor->getCounter(); |
| 527 | } |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 528 | data["sensors"][sensor->name()] = values; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | return data; |
| 533 | } |
| 534 | |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 535 | void System::handleOfflineFanController() |
| 536 | { |
| 537 | getLogger().log("The fan controller appears to be offline. Shutting down.", |
| 538 | Logger::error); |
| 539 | |
| 540 | auto ffdc = collectHwmonFFDC(); |
| 541 | |
| 542 | FanError error{"xyz.openbmc_project.Fan.Error.FanControllerOffline", |
| 543 | Severity::Critical}; |
| 544 | error.commit(ffdc, true); |
| 545 | |
| 546 | PowerInterface::executeHardPowerOff(); |
Mike Capps | 683a96c | 2022-04-27 16:46:06 -0400 | [diff] [blame] | 547 | |
| 548 | createBmcDump(); |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * @brief Create a BMC Dump |
| 553 | */ |
| 554 | void System::createBmcDump() const |
| 555 | { |
| 556 | try |
| 557 | { |
| 558 | util::SDBusPlus::callMethod( |
| 559 | "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump/bmc", |
| 560 | "xyz.openbmc_project.Dump.Create", "CreateDump", |
| 561 | std::vector< |
| 562 | std::pair<std::string, std::variant<std::string, uint64_t>>>()); |
| 563 | } |
Mike Capps | 477b13b | 2022-07-11 10:45:46 -0400 | [diff] [blame] | 564 | catch (const std::exception& e) |
| 565 | { |
| 566 | getLogger().log( |
| 567 | fmt::format("Caught exception while creating BMC dump: {}", |
| 568 | e.what()), |
| 569 | Logger::error); |
| 570 | } |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 571 | } |
| 572 | |
Matt Spinler | 4f472a8 | 2022-08-26 13:55:34 -0500 | [diff] [blame] | 573 | void System::dumpDebugData(sdeventplus::source::Signal&, |
| 574 | const struct signalfd_siginfo*) |
| 575 | { |
| 576 | json output; |
| 577 | |
| 578 | if (_loaded) |
| 579 | { |
| 580 | output["logs"] = getLogger().getLogs(); |
| 581 | output["sensors"] = captureSensorData(); |
| 582 | } |
| 583 | else |
| 584 | { |
| 585 | output["error"] = "Fan monitor not loaded yet. Try again later."; |
| 586 | } |
| 587 | |
| 588 | std::ofstream file{System::dumpFile}; |
| 589 | if (!file) |
| 590 | { |
| 591 | log<level::ERR>("Could not open file for fan monitor dump"); |
| 592 | } |
| 593 | else |
| 594 | { |
| 595 | file << std::setw(4) << output; |
| 596 | } |
| 597 | } |
| 598 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 599 | } // namespace phosphor::fan::monitor |