blob: 04a0547f1b2812f44256664771688312b84cd2ca [file] [log] [blame]
Bob King833b8e02020-06-11 14:56:38 +08001/**
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
17#include "sensor_monitoring.hpp"
18
19#include "action_environment.hpp"
20#include "action_utils.hpp"
21#include "chassis.hpp"
22#include "device.hpp"
Shawn McCarney81a2f902021-03-23 21:41:34 -050023#include "error_logging_utils.hpp"
Bob King833b8e02020-06-11 14:56:38 +080024#include "exception_utils.hpp"
Bob King833b8e02020-06-11 14:56:38 +080025#include "rail.hpp"
Shawn McCarney17bac892021-05-08 07:55:52 -050026#include "sensors.hpp"
Bob King833b8e02020-06-11 14:56:38 +080027#include "system.hpp"
28
29#include <exception>
30
31namespace phosphor::power::regulators
32{
33
Shawn McCarneyfa2734d2022-03-30 21:16:40 -050034/**
35 * Maximum number of consecutive errors before an error log entry is created.
36 * This provides "de-glitching" to ignore transient hardware problems.
37 *
38 * Also the maximum number of consecutive errors that will be logged to the
39 * journal.
40 */
41constexpr unsigned short maxErrorCount{6};
42
Bob King8a552922020-08-05 17:02:31 +080043void SensorMonitoring::execute(Services& services, System& system,
Shawn McCarney17bac892021-05-08 07:55:52 -050044 Chassis& chassis, Device& device, Rail& rail)
Bob King833b8e02020-06-11 14:56:38 +080045{
Shawn McCarney17bac892021-05-08 07:55:52 -050046 // Notify sensors service that monitoring is starting for this rail
47 Sensors& sensors = services.getSensors();
48 sensors.startRail(rail.getID(), device.getFRU(),
49 chassis.getInventoryPath());
50
51 // Read all sensors defined for this rail
Bob King833b8e02020-06-11 14:56:38 +080052 try
53 {
54 // Create ActionEnvironment
Bob King73eacee2020-10-23 13:58:02 +080055 ActionEnvironment environment{system.getIDMap(), device.getID(),
56 services};
Bob King833b8e02020-06-11 14:56:38 +080057
58 // Execute the actions
59 action_utils::execute(actions, environment);
Shawn McCarneyfa2734d2022-03-30 21:16:40 -050060
61 // Reset consecutive error count since sensors were read successfully
62 errorCount = 0;
Bob King833b8e02020-06-11 14:56:38 +080063 }
64 catch (const std::exception& e)
65 {
Shawn McCarneyfa2734d2022-03-30 21:16:40 -050066 // If we haven't hit the maximum consecutive error count yet
67 if (errorCount < maxErrorCount)
Shawn McCarney17bac892021-05-08 07:55:52 -050068 {
Shawn McCarneyfa2734d2022-03-30 21:16:40 -050069 // Log error messages in journal
Shawn McCarney17bac892021-05-08 07:55:52 -050070 services.getJournal().logError(exception_utils::getMessages(e));
71 services.getJournal().logError(
72 "Unable to monitor sensors for rail " + rail.getID());
Shawn McCarney17bac892021-05-08 07:55:52 -050073
Shawn McCarneyfa2734d2022-03-30 21:16:40 -050074 // Increment error count. If now at max, create error log entry.
75 if (++errorCount >= maxErrorCount)
76 {
77 error_logging_utils::logError(std::current_exception(),
78 Entry::Level::Warning, services,
79 errorHistory);
80 }
81 }
Bob King833b8e02020-06-11 14:56:38 +080082 }
Shawn McCarney17bac892021-05-08 07:55:52 -050083
84 // Notify sensors service that monitoring has ended for this rail
Shawn McCarneyfa2734d2022-03-30 21:16:40 -050085 bool errorOccurred = (errorCount > 0);
Shawn McCarney17bac892021-05-08 07:55:52 -050086 sensors.endRail(errorOccurred);
Bob King833b8e02020-06-11 14:56:38 +080087}
88
89} // namespace phosphor::power::regulators