blob: 8c1bb44c22690e0d7be78d32b40e1f5b07b808dd [file] [log] [blame]
Matthew Barthc95c5272020-06-15 19:51:13 -05001/**
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#pragma once
17
18#include "fan.hpp"
Matt Spinlerac1efc12020-10-27 10:20:11 -050019#include "fan_error.hpp"
Matt Spinlere892e392020-10-14 13:21:31 -050020#include "power_off_rule.hpp"
21#include "power_state.hpp"
Matthew Barthc95c5272020-06-15 19:51:13 -050022#include "tach_sensor.hpp"
23#include "trust_manager.hpp"
24#include "types.hpp"
25
26#include <nlohmann/json.hpp>
27#include <sdbusplus/bus.hpp>
28#include <sdeventplus/event.hpp>
Matthew Barthd06905c2020-06-12 08:13:06 -050029#include <sdeventplus/source/signal.hpp>
Matthew Barthc95c5272020-06-15 19:51:13 -050030
31#include <memory>
32#include <optional>
33#include <vector>
34
35namespace phosphor::fan::monitor
36{
37
38using json = nlohmann::json;
39
Mike Cappsfdcd5db2021-05-20 12:47:10 -040040// Mapping from service name to sensor
41using SensorMapType =
42 std::map<std::string, std::set<std::shared_ptr<TachSensor>>>;
43
Matthew Barthc95c5272020-06-15 19:51:13 -050044class System
45{
46 public:
47 System() = delete;
Matt Spinlerac1efc12020-10-27 10:20:11 -050048 ~System() = default;
Matthew Barthc95c5272020-06-15 19:51:13 -050049 System(const System&) = delete;
50 System(System&&) = delete;
51 System& operator=(const System&) = delete;
52 System& operator=(System&&) = delete;
Matthew Barthc95c5272020-06-15 19:51:13 -050053
54 /**
55 * Constructor
Matthew Barthc95c5272020-06-15 19:51:13 -050056 *
57 * @param[in] mode - mode of fan monitor
58 * @param[in] bus - sdbusplus bus object
59 * @param[in] event - event loop reference
60 */
61 System(Mode mode, sdbusplus::bus::bus& bus,
62 const sdeventplus::Event& event);
63
Matthew Barthd06905c2020-06-12 08:13:06 -050064 /**
65 * @brief Callback function to handle receiving a HUP signal to reload the
66 * JSON configuration.
67 */
68 void sighupHandler(sdeventplus::source::Signal&,
69 const struct signalfd_siginfo*);
70
Matt Spinlerb63aa092020-10-14 09:45:11 -050071 /**
72 * @brief Called from the fan when it changes either
73 * present or functional status to update the
74 * fan health map.
75 *
76 * @param[in] fan - The fan that changed
Matt Spinler4283c5d2021-03-01 15:56:00 -060077 * @param[in] skipRulesCheck - If the rules checks should be done now.
Matt Spinlerb63aa092020-10-14 09:45:11 -050078 */
Matt Spinler4283c5d2021-03-01 15:56:00 -060079 void fanStatusChange(const Fan& fan, bool skipRulesCheck = false);
Matt Spinlerb63aa092020-10-14 09:45:11 -050080
Matt Spinlerf13b42e2020-10-26 15:29:49 -050081 /**
82 * @brief Called when a fan sensor's error timer expires, which
83 * happens when the sensor has been nonfunctional for a
84 * certain amount of time. An event log will be created.
85 *
86 * @param[in] fan - The parent fan of the sensor
87 * @param[in] sensor - The faulted sensor
88 */
89 void sensorErrorTimerExpired(const Fan& fan, const TachSensor& sensor);
90
Matt Spinler27f6b682020-10-27 08:43:37 -050091 /**
92 * @brief Called when the timer that starts when a fan is missing
93 * has expired so an event log needs to be created.
94 *
95 * @param[in] fan - The missing fan.
96 */
97 void fanMissingErrorTimerExpired(const Fan& fan);
98
Matt Spinlerac1efc12020-10-27 10:20:11 -050099 /**
100 * @brief Called by the power off actions to log an error when there is
101 * a power off due to fan problems.
102 *
103 * The error it logs is just the last fan error that occurred.
104 */
105 void logShutdownError();
106
Matt Spinler7d135642021-02-04 12:44:17 -0600107 /**
108 * @brief Returns true if power is on
109 */
110 bool isPowerOn() const
111 {
112 return _powerState->isPowerOn();
113 }
114
115 /**
Mike Cappsb4379a12021-10-11 14:18:06 -0400116 * @brief tests the presence of Inventory and calls load() if present, else
117 * waits for Inventory asynchronously and has a callback to load() when
118 * present
Matt Spinler7d135642021-02-04 12:44:17 -0600119 */
Matthew Barth823bc492021-06-21 14:19:09 -0500120 void start();
Matt Spinler7d135642021-02-04 12:44:17 -0600121
Mike Cappsb4379a12021-10-11 14:18:06 -0400122 /**
123 * @brief Parses and populates the fan monitor trust groups and list of fans
124 */
125 void load();
126
Matthew Barthc95c5272020-06-15 19:51:13 -0500127 private:
Mike Cappsb4379a12021-10-11 14:18:06 -0400128 /**
129 * @brief Callback from D-Bus when Inventory service comes online
130 *
131 * @param[in] msg - Service details.
132 */
133 void inventoryOnlineCb(sdbusplus::message::message& msg);
134
Mike Capps683a96c2022-04-27 16:46:06 -0400135 /**
136 * @brief Create a BMC Dump
137 */
138 void createBmcDump() const;
139
Matthew Barthc95c5272020-06-15 19:51:13 -0500140 /* The mode of fan monitor */
141 Mode _mode;
142
143 /* The sdbusplus bus object */
144 sdbusplus::bus::bus& _bus;
145
146 /* The event loop reference */
147 const sdeventplus::Event& _event;
148
149 /* Trust manager of trust groups */
150 std::unique_ptr<phosphor::fan::trust::Manager> _trust;
151
Mike Cappsb4379a12021-10-11 14:18:06 -0400152 /* match object to detect Inventory service */
153 std::unique_ptr<sdbusplus::bus::match::match> _inventoryMatch;
154
Matthew Barthc95c5272020-06-15 19:51:13 -0500155 /* List of fan objects to monitor */
156 std::vector<std::unique_ptr<Fan>> _fans;
157
158 /**
Matt Spinlerb63aa092020-10-14 09:45:11 -0500159 * @brief The latest health of all the fans
160 */
161 FanHealth _fanHealth;
162
163 /**
Matt Spinlere892e392020-10-14 13:21:31 -0500164 * @brief The object to watch the power state
165 */
166 std::unique_ptr<PowerState> _powerState;
167
168 /**
169 * @brief The power off rules, for shutting down the system
170 * due to fan failures.
171 */
172 std::vector<std::unique_ptr<PowerOffRule>> _powerOffRules;
173
174 /**
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500175 * @brief The number of concurrently nonfunctional fan sensors
176 * there must be for an event log created due to a
177 * nonfunctional fan sensor to have an Error severity as
178 * opposed to an Informational one.
179 */
180 std::optional<size_t> _numNonfuncSensorsBeforeError;
181
182 /**
Matt Spinlerac1efc12020-10-27 10:20:11 -0500183 * @brief The most recently committed fan error.
184 */
185 std::unique_ptr<FanError> _lastError;
186
187 /**
Matt Spinlerc8d3c512021-01-06 14:22:25 -0600188 * @brief The thermal alert D-Bus object
189 */
190 ThermalAlertObject _thermalAlert;
191
192 /**
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400193 * @brief The tach sensors D-Bus match objects
194 */
195 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> _sensorMatch;
196
197 /**
Mike Cappsb4379a12021-10-11 14:18:06 -0400198 * @brief true if config files have been loaded
Matt Spinler7d135642021-02-04 12:44:17 -0600199 */
Mike Cappsb4379a12021-10-11 14:18:06 -0400200 bool _loaded = false;
Matt Spinler7d135642021-02-04 12:44:17 -0600201
202 /**
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500203 * @brief Captures tach sensor data as JSON for use in
204 * fan fault and fan missing event logs.
205 *
206 * @return json - The JSON data
207 */
208 json captureSensorData();
209
210 /**
Mike Capps25f03272021-09-13 13:38:44 -0400211 * @brief creates a subscription (service->sensor) to take sensors
212 * on/offline when D-Bus starts/stops updating values
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400213 *
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400214 */
Mike Capps25f03272021-09-13 13:38:44 -0400215 void subscribeSensorsToServices();
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400216
217 /**
Matthew Barthc95c5272020-06-15 19:51:13 -0500218 * @brief Retrieve the configured trust groups
219 *
220 * @param[in] jsonObj - JSON object to parse from
221 *
222 * @return List of functions applied on trust groups
223 */
224 const std::vector<CreateGroupFunction> getTrustGroups(const json& jsonObj);
225
226 /**
Matthew Barthd06905c2020-06-12 08:13:06 -0500227 * @brief Set the trust manager's list of trust group functions
228 *
229 * @param[in] groupFuncs - list of trust group functions
230 */
231 void setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs);
232
233 /**
Matthew Barthc95c5272020-06-15 19:51:13 -0500234 * @brief Retrieve the configured fan definitions
235 *
236 * @param[in] jsonObj - JSON object to parse from
237 *
238 * @return List of fan definition data on the fans configured
239 */
240 const std::vector<FanDefinition> getFanDefinitions(const json& jsonObj);
Matthew Barthd06905c2020-06-12 08:13:06 -0500241
242 /**
243 * @brief Set the list of fans to be monitored
244 *
245 * @param[in] fanDefs - list of fan definitions to create fans monitored
246 */
247 void setFans(const std::vector<FanDefinition>& fanDefs);
Matt Spinlerb63aa092020-10-14 09:45:11 -0500248
249 /**
250 * @brief Updates the fan health map entry for the fan passed in
251 *
252 * @param[in] fan - The fan to update the health map with
253 */
254 void updateFanHealth(const Fan& fan);
Matt Spinlere892e392020-10-14 13:21:31 -0500255
256 /**
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400257 * @brief callback when a tach sensor signal goes offline
258 *
259 * @param[in] msg - D-Bus message containing details (inc. service name)
260 *
261 * @param[in] sensorMap - map providing sensor access for each service
262 */
263 void tachSignalOffline(sdbusplus::message::message& msg,
264 const SensorMapType& sensorMap);
265
266 /**
Matt Spinlere892e392020-10-14 13:21:31 -0500267 * @brief The function that runs when the power state changes
268 *
269 * @param[in] powerStateOn - If power is now on or not
270 */
271 void powerStateChanged(bool powerStateOn);
272
273 /**
274 * @brief Reads the fault configuration from the JSON config
275 * file, such as the power off rule configuration.
276 *
277 * @param[in] jsonObj - JSON object to parse from
278 */
279 void setFaultConfig(const json& jsonObj);
Matt Spinlerbb449c12021-06-14 11:45:28 -0600280
281 /**
282 * @brief Log an error and shut down due to an offline fan controller
283 */
284 void handleOfflineFanController();
Matthew Barthc95c5272020-06-15 19:51:13 -0500285};
286
287} // namespace phosphor::fan::monitor