blob: 5b57abb9a8e9082131314b255d4744cefa55a408 [file] [log] [blame]
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01001#include "trigger_actions.hpp"
2
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01003#include "messages/update_report_ind.hpp"
Szymon Dompkef670b022022-03-16 19:21:11 +01004#include "types/trigger_types.hpp"
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +02005#include "utils/clock.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01006#include "utils/messanger.hpp"
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +00007#include "utils/to_short_enum.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01008
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01009#include <phosphor-logging/log.hpp>
10
11#include <ctime>
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020012#include <iomanip>
13#include <sstream>
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010014
15namespace action
16{
17
Szymon Dompkef763c9e2021-03-12 09:19:22 +010018namespace
19{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010020std::string timestampToString(Milliseconds timestamp)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010021{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020022 std::time_t t = static_cast<time_t>(
23 std::chrono::duration_cast<std::chrono::seconds>(timestamp).count());
24 std::stringstream ss;
25 ss << std::put_time(std::gmtime(&t), "%FT%T.") << std::setw(3)
26 << std::setfill('0') << timestamp.count() % 1000 << 'Z';
27 return ss.str();
Szymon Dompkef763c9e2021-03-12 09:19:22 +010028}
29} // namespace
30
31namespace numeric
32{
33
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010034static const char* getDirection(double value, double threshold)
35{
36 if (value < threshold)
37 {
38 return "decreasing";
39 }
40 if (value > threshold)
41 {
42 return "increasing";
43 }
44 throw std::runtime_error("Invalid value");
45}
46
Patrick Williams583ba442025-02-03 14:28:19 -050047void LogToJournal::commit(
48 const std::string& triggerId, const ThresholdName thresholdNameInIn,
49 const std::string& sensorName, const Milliseconds timestamp,
50 const TriggerValue triggerValue)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010051{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020052 double value = std::get<double>(triggerValue);
53 std::string thresholdName = ::numeric::typeToString(type);
54 auto direction = getDirection(value, threshold);
55
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000056 std::string msg =
57 "Numeric threshold '" + std::string(utils::toShortEnum(thresholdName)) +
58 "' of trigger '" + triggerId + "' is crossed on sensor " + sensorName +
59 ", recorded value: " + std::to_string(value) +
60 ", crossing direction: " + std::string(utils::toShortEnum(direction)) +
61 ", timestamp: " + timestampToString(timestamp);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010062
63 phosphor::logging::log<phosphor::logging::level::INFO>(msg.c_str());
64}
65
Michal Orzel8018a3b2024-06-26 14:14:16 +020066const char* LogToRedfishEventLog::getRedfishMessageId(const double value) const
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010067{
Michal Orzel8018a3b2024-06-26 14:14:16 +020068 std::string direction(getDirection(value, threshold));
69
70 if (direction == "decreasing")
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010071 {
Michal Orzel8018a3b2024-06-26 14:14:16 +020072 switch (type)
73 {
74 case ::numeric::Type::upperCritical:
75 return redfish_message_ids::TriggerNumericBelowUpperCritical;
76 case ::numeric::Type::lowerCritical:
77 return redfish_message_ids::TriggerNumericBelowLowerCritical;
78 case ::numeric::Type::upperWarning:
79 return redfish_message_ids::TriggerNumericReadingNormal;
80 case ::numeric::Type::lowerWarning:
81 return redfish_message_ids::TriggerNumericBelowLowerWarning;
82 }
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010083 }
Michal Orzel8018a3b2024-06-26 14:14:16 +020084
85 if (direction == "increasing")
86 {
87 switch (type)
88 {
89 case ::numeric::Type::upperCritical:
90 return redfish_message_ids::TriggerNumericAboveUpperCritical;
91 case ::numeric::Type::lowerCritical:
92 return redfish_message_ids::TriggerNumericAboveLowerCritical;
93 case ::numeric::Type::upperWarning:
94 return redfish_message_ids::TriggerNumericAboveUpperWarning;
95 case ::numeric::Type::lowerWarning:
96 return redfish_message_ids::TriggerNumericReadingNormal;
97 }
98 }
99
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100100 throw std::runtime_error("Invalid type");
101}
102
Patrick Williams583ba442025-02-03 14:28:19 -0500103void LogToRedfishEventLog::commit(
104 const std::string& triggerId, const ThresholdName thresholdNameInIn,
105 const std::string& sensorName, const Milliseconds timestamp,
106 const TriggerValue triggerValue)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100107{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200108 double value = std::get<double>(triggerValue);
Michal Orzel8018a3b2024-06-26 14:14:16 +0200109 auto messageId = getRedfishMessageId(value);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200110
Michal Orzel8018a3b2024-06-26 14:14:16 +0200111 if (messageId == redfish_message_ids::TriggerNumericReadingNormal)
112 {
113 phosphor::logging::log<phosphor::logging::level::INFO>(
114 "Logging numeric trigger action to Redfish Event Log.",
115 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", messageId),
116 phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%f,%s",
117 sensorName.c_str(), value,
118 triggerId.c_str()));
119 }
120 else
121 {
122 phosphor::logging::log<phosphor::logging::level::INFO>(
123 "Logging numeric trigger action to Redfish Event Log.",
124 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", messageId),
125 phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%f,%f,%s",
126 sensorName.c_str(), value, threshold,
127 triggerId.c_str()));
128 }
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100129}
130
Szymon Dompke20013012021-07-23 09:54:20 +0200131void fillActions(
132 std::vector<std::unique_ptr<interfaces::TriggerAction>>& actionsIf,
133 const std::vector<TriggerAction>& ActionsEnum, ::numeric::Type type,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100134 double thresholdValue, boost::asio::io_context& ioc,
Szymon Dompke94f71c52021-12-10 07:16:33 +0100135 const std::shared_ptr<std::vector<std::string>>& reportIds)
Szymon Dompke20013012021-07-23 09:54:20 +0200136{
137 actionsIf.reserve(ActionsEnum.size());
138 for (auto actionType : ActionsEnum)
139 {
140 switch (actionType)
141 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200142 case TriggerAction::LogToJournal:
Szymon Dompke20013012021-07-23 09:54:20 +0200143 {
144 actionsIf.emplace_back(
145 std::make_unique<LogToJournal>(type, thresholdValue));
146 break;
147 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200148 case TriggerAction::LogToRedfishEventLog:
Szymon Dompke20013012021-07-23 09:54:20 +0200149 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200150 actionsIf.emplace_back(std::make_unique<LogToRedfishEventLog>(
151 type, thresholdValue));
Szymon Dompke20013012021-07-23 09:54:20 +0200152 break;
153 }
154 case TriggerAction::UpdateReport:
155 {
156 actionsIf.emplace_back(
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100157 std::make_unique<UpdateReport>(ioc, reportIds));
Szymon Dompke20013012021-07-23 09:54:20 +0200158 break;
159 }
160 }
161 }
162}
163
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100164} // namespace numeric
165
166namespace discrete
167{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200168
Patrick Williams583ba442025-02-03 14:28:19 -0500169void LogToJournal::commit(
170 const std::string& triggerId, const ThresholdName thresholdNameIn,
171 const std::string& sensorName, const Milliseconds timestamp,
172 const TriggerValue triggerValue)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100173{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200174 auto value = std::get<std::string>(triggerValue);
175
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000176 std::string msg =
177 "Discrete condition '" + thresholdNameIn->get() + "' of trigger '" +
178 triggerId + "' is met on sensor " + sensorName +
179 ", recorded value: " + value + ", severity: " +
180 std::string(utils::toShortEnum(utils::enumToString(severity))) +
181 ", timestamp: " + timestampToString(timestamp);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100182
183 phosphor::logging::log<phosphor::logging::level::INFO>(msg.c_str());
184}
185
Patrick Williams583ba442025-02-03 14:28:19 -0500186void LogToRedfishEventLog::commit(
187 const std::string& triggerId, const ThresholdName thresholdNameIn,
188 const std::string& sensorName, const Milliseconds timestamp,
189 const TriggerValue triggerValue)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100190{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200191 auto value = std::get<std::string>(triggerValue);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200192
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100193 phosphor::logging::log<phosphor::logging::level::INFO>(
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200194 "Logging discrete trigger action to Redfish Event Log.",
Michal Orzel8018a3b2024-06-26 14:14:16 +0200195 phosphor::logging::entry(
196 "REDFISH_MESSAGE_ID=%s",
197 redfish_message_ids::TriggerDiscreteConditionMet),
198 phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%s,%s",
199 sensorName.c_str(), value.c_str(),
200 triggerId.c_str()));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100201}
202
Szymon Dompke20013012021-07-23 09:54:20 +0200203void fillActions(
204 std::vector<std::unique_ptr<interfaces::TriggerAction>>& actionsIf,
205 const std::vector<TriggerAction>& ActionsEnum,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100206 ::discrete::Severity severity, boost::asio::io_context& ioc,
Szymon Dompke94f71c52021-12-10 07:16:33 +0100207 const std::shared_ptr<std::vector<std::string>>& reportIds)
Szymon Dompke20013012021-07-23 09:54:20 +0200208{
209 actionsIf.reserve(ActionsEnum.size());
210 for (auto actionType : ActionsEnum)
211 {
212 switch (actionType)
213 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200214 case TriggerAction::LogToJournal:
Szymon Dompke20013012021-07-23 09:54:20 +0200215 {
216 actionsIf.emplace_back(
217 std::make_unique<LogToJournal>(severity));
218 break;
219 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200220 case TriggerAction::LogToRedfishEventLog:
Szymon Dompke20013012021-07-23 09:54:20 +0200221 {
222 actionsIf.emplace_back(
Michal Orzel8018a3b2024-06-26 14:14:16 +0200223 std::make_unique<LogToRedfishEventLog>());
Szymon Dompke20013012021-07-23 09:54:20 +0200224 break;
225 }
226 case TriggerAction::UpdateReport:
227 {
228 actionsIf.emplace_back(
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100229 std::make_unique<UpdateReport>(ioc, reportIds));
Szymon Dompke20013012021-07-23 09:54:20 +0200230 break;
231 }
232 }
233 }
234}
235
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100236namespace onChange
237{
Patrick Williams583ba442025-02-03 14:28:19 -0500238void LogToJournal::commit(
239 const std::string& triggerId, const ThresholdName thresholdNameIn,
240 const std::string& sensorName, const Milliseconds timestamp,
241 const TriggerValue triggerValue)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100242{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200243 auto value = triggerValueToString(triggerValue);
Patrick Williams583ba442025-02-03 14:28:19 -0500244 std::string msg =
245 "Discrete condition 'OnChange' of trigger '" + triggerId +
246 "' is met on sensor: " + sensorName + ", recorded value: " + value +
247 ", timestamp: " + timestampToString(timestamp);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100248
249 phosphor::logging::log<phosphor::logging::level::INFO>(msg.c_str());
250}
251
Patrick Williams583ba442025-02-03 14:28:19 -0500252void LogToRedfishEventLog::commit(
253 const std::string& triggerId, const ThresholdName thresholdNameIn,
254 const std::string& sensorName, const Milliseconds timestamp,
255 const TriggerValue triggerValue)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100256{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200257 auto value = triggerValueToString(triggerValue);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200258
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100259 phosphor::logging::log<phosphor::logging::level::INFO>(
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200260 "Logging onChange discrete trigger action to Redfish Event Log.",
Michal Orzel8018a3b2024-06-26 14:14:16 +0200261 phosphor::logging::entry(
262 "REDFISH_MESSAGE_ID=%s",
263 redfish_message_ids::TriggerDiscreteConditionMet),
264 phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%s,%s",
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200265 sensorName.c_str(), value.c_str(),
Michal Orzel8018a3b2024-06-26 14:14:16 +0200266 triggerId.c_str()));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100267}
Szymon Dompke20013012021-07-23 09:54:20 +0200268
269void fillActions(
270 std::vector<std::unique_ptr<interfaces::TriggerAction>>& actionsIf,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100271 const std::vector<TriggerAction>& ActionsEnum, boost::asio::io_context& ioc,
Szymon Dompke94f71c52021-12-10 07:16:33 +0100272 const std::shared_ptr<std::vector<std::string>>& reportIds)
Szymon Dompke20013012021-07-23 09:54:20 +0200273{
274 actionsIf.reserve(ActionsEnum.size());
275 for (auto actionType : ActionsEnum)
276 {
277 switch (actionType)
278 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200279 case TriggerAction::LogToJournal:
Szymon Dompke20013012021-07-23 09:54:20 +0200280 {
281 actionsIf.emplace_back(std::make_unique<LogToJournal>());
282 break;
283 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200284 case TriggerAction::LogToRedfishEventLog:
Szymon Dompke20013012021-07-23 09:54:20 +0200285 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200286 actionsIf.emplace_back(
287 std::make_unique<LogToRedfishEventLog>());
Szymon Dompke20013012021-07-23 09:54:20 +0200288 break;
289 }
290 case TriggerAction::UpdateReport:
291 {
292 actionsIf.emplace_back(
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100293 std::make_unique<UpdateReport>(ioc, reportIds));
Szymon Dompke20013012021-07-23 09:54:20 +0200294 break;
295 }
296 }
297 }
298}
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100299} // namespace onChange
300} // namespace discrete
301
Patrick Williams583ba442025-02-03 14:28:19 -0500302void UpdateReport::commit(
303 const std::string& triggerId, const ThresholdName thresholdNameIn,
304 const std::string& sensorName, const Milliseconds timestamp,
305 const TriggerValue triggerValue)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100306{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100307 if (reportIds->empty())
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100308 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100309 return;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100310 }
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100311
312 utils::Messanger messanger(ioc);
313 messanger.send(messages::UpdateReportInd{*reportIds});
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100314}
315} // namespace action