blob: e01d81a140176c457ecbb308ec8fdeb02eb6aef3 [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
40class System
41{
42 public:
43 System() = delete;
Matt Spinlerac1efc12020-10-27 10:20:11 -050044 ~System() = default;
Matthew Barthc95c5272020-06-15 19:51:13 -050045 System(const System&) = delete;
46 System(System&&) = delete;
47 System& operator=(const System&) = delete;
48 System& operator=(System&&) = delete;
Matthew Barthc95c5272020-06-15 19:51:13 -050049
50 /**
51 * Constructor
Matthew Barthc95c5272020-06-15 19:51:13 -050052 *
53 * @param[in] mode - mode of fan monitor
54 * @param[in] bus - sdbusplus bus object
55 * @param[in] event - event loop reference
56 */
57 System(Mode mode, sdbusplus::bus::bus& bus,
58 const sdeventplus::Event& event);
59
Matthew Barthd06905c2020-06-12 08:13:06 -050060 /**
61 * @brief Callback function to handle receiving a HUP signal to reload the
62 * JSON configuration.
63 */
64 void sighupHandler(sdeventplus::source::Signal&,
65 const struct signalfd_siginfo*);
66
Matt Spinlerb63aa092020-10-14 09:45:11 -050067 /**
68 * @brief Called from the fan when it changes either
69 * present or functional status to update the
70 * fan health map.
71 *
72 * @param[in] fan - The fan that changed
73 */
74 void fanStatusChange(const Fan& fan);
75
Matt Spinlerf13b42e2020-10-26 15:29:49 -050076 /**
77 * @brief Called when a fan sensor's error timer expires, which
78 * happens when the sensor has been nonfunctional for a
79 * certain amount of time. An event log will be created.
80 *
81 * @param[in] fan - The parent fan of the sensor
82 * @param[in] sensor - The faulted sensor
83 */
84 void sensorErrorTimerExpired(const Fan& fan, const TachSensor& sensor);
85
Matt Spinler27f6b682020-10-27 08:43:37 -050086 /**
87 * @brief Called when the timer that starts when a fan is missing
88 * has expired so an event log needs to be created.
89 *
90 * @param[in] fan - The missing fan.
91 */
92 void fanMissingErrorTimerExpired(const Fan& fan);
93
Matt Spinlerac1efc12020-10-27 10:20:11 -050094 /**
95 * @brief Called by the power off actions to log an error when there is
96 * a power off due to fan problems.
97 *
98 * The error it logs is just the last fan error that occurred.
99 */
100 void logShutdownError();
101
Matt Spinler7d135642021-02-04 12:44:17 -0600102 /**
103 * @brief Returns true if power is on
104 */
105 bool isPowerOn() const
106 {
107 return _powerState->isPowerOn();
108 }
109
110 /**
111 * @brief Parses and populates the fan monitor
112 * trust groups and list of fans
113 *
114 * @param[in] confFile - The config file path
115 */
116 void start(
117#ifdef MONITOR_USE_JSON
118 const std::string& confFile
119#endif
120 );
121
Matthew Barthc95c5272020-06-15 19:51:13 -0500122 private:
123 /* The mode of fan monitor */
124 Mode _mode;
125
126 /* The sdbusplus bus object */
127 sdbusplus::bus::bus& _bus;
128
129 /* The event loop reference */
130 const sdeventplus::Event& _event;
131
132 /* Trust manager of trust groups */
133 std::unique_ptr<phosphor::fan::trust::Manager> _trust;
134
135 /* List of fan objects to monitor */
136 std::vector<std::unique_ptr<Fan>> _fans;
137
138 /**
Matt Spinlerb63aa092020-10-14 09:45:11 -0500139 * @brief The latest health of all the fans
140 */
141 FanHealth _fanHealth;
142
143 /**
Matt Spinlere892e392020-10-14 13:21:31 -0500144 * @brief The object to watch the power state
145 */
146 std::unique_ptr<PowerState> _powerState;
147
148 /**
149 * @brief The power off rules, for shutting down the system
150 * due to fan failures.
151 */
152 std::vector<std::unique_ptr<PowerOffRule>> _powerOffRules;
153
154 /**
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500155 * @brief The number of concurrently nonfunctional fan sensors
156 * there must be for an event log created due to a
157 * nonfunctional fan sensor to have an Error severity as
158 * opposed to an Informational one.
159 */
160 std::optional<size_t> _numNonfuncSensorsBeforeError;
161
162 /**
Matt Spinlerac1efc12020-10-27 10:20:11 -0500163 * @brief The most recently committed fan error.
164 */
165 std::unique_ptr<FanError> _lastError;
166
167 /**
Matt Spinlerc8d3c512021-01-06 14:22:25 -0600168 * @brief The thermal alert D-Bus object
169 */
170 ThermalAlertObject _thermalAlert;
171
172 /**
Matt Spinler7d135642021-02-04 12:44:17 -0600173 * @brief If start() has been called
174 */
175 bool _started = false;
176
177 /**
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500178 * @brief Captures tach sensor data as JSON for use in
179 * fan fault and fan missing event logs.
180 *
181 * @return json - The JSON data
182 */
183 json captureSensorData();
184
185 /**
Matthew Barthc95c5272020-06-15 19:51:13 -0500186 * @brief Retrieve the configured trust groups
187 *
188 * @param[in] jsonObj - JSON object to parse from
189 *
190 * @return List of functions applied on trust groups
191 */
192 const std::vector<CreateGroupFunction> getTrustGroups(const json& jsonObj);
193
194 /**
Matthew Barthd06905c2020-06-12 08:13:06 -0500195 * @brief Set the trust manager's list of trust group functions
196 *
197 * @param[in] groupFuncs - list of trust group functions
198 */
199 void setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs);
200
201 /**
Matthew Barthc95c5272020-06-15 19:51:13 -0500202 * @brief Retrieve the configured fan definitions
203 *
204 * @param[in] jsonObj - JSON object to parse from
205 *
206 * @return List of fan definition data on the fans configured
207 */
208 const std::vector<FanDefinition> getFanDefinitions(const json& jsonObj);
Matthew Barthd06905c2020-06-12 08:13:06 -0500209
210 /**
211 * @brief Set the list of fans to be monitored
212 *
213 * @param[in] fanDefs - list of fan definitions to create fans monitored
214 */
215 void setFans(const std::vector<FanDefinition>& fanDefs);
Matt Spinlerb63aa092020-10-14 09:45:11 -0500216
217 /**
218 * @brief Updates the fan health map entry for the fan passed in
219 *
220 * @param[in] fan - The fan to update the health map with
221 */
222 void updateFanHealth(const Fan& fan);
Matt Spinlere892e392020-10-14 13:21:31 -0500223
224 /**
225 * @brief The function that runs when the power state changes
226 *
227 * @param[in] powerStateOn - If power is now on or not
228 */
229 void powerStateChanged(bool powerStateOn);
230
231 /**
232 * @brief Reads the fault configuration from the JSON config
233 * file, such as the power off rule configuration.
234 *
235 * @param[in] jsonObj - JSON object to parse from
236 */
237 void setFaultConfig(const json& jsonObj);
Matthew Barthc95c5272020-06-15 19:51:13 -0500238};
239
240} // namespace phosphor::fan::monitor