blob: 1d984e59be469b66d30bd20bd06c19062f0dba10 [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
Piotr Sulewskic1dbac12025-11-12 14:14:09 +0100111 if (std::string_view(messageId) ==
112 redfish_message_ids::TriggerNumericReadingNormal)
Michal Orzel8018a3b2024-06-26 14:14:16 +0200113 {
114 phosphor::logging::log<phosphor::logging::level::INFO>(
115 "Logging numeric trigger action to Redfish Event Log.",
116 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", messageId),
117 phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%f,%s",
118 sensorName.c_str(), value,
119 triggerId.c_str()));
120 }
121 else
122 {
123 phosphor::logging::log<phosphor::logging::level::INFO>(
124 "Logging numeric trigger action to Redfish Event Log.",
125 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", messageId),
126 phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%f,%f,%s",
127 sensorName.c_str(), value, threshold,
128 triggerId.c_str()));
129 }
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100130}
131
Szymon Dompke20013012021-07-23 09:54:20 +0200132void fillActions(
133 std::vector<std::unique_ptr<interfaces::TriggerAction>>& actionsIf,
134 const std::vector<TriggerAction>& ActionsEnum, ::numeric::Type type,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100135 double thresholdValue, boost::asio::io_context& ioc,
Szymon Dompke94f71c52021-12-10 07:16:33 +0100136 const std::shared_ptr<std::vector<std::string>>& reportIds)
Szymon Dompke20013012021-07-23 09:54:20 +0200137{
138 actionsIf.reserve(ActionsEnum.size());
139 for (auto actionType : ActionsEnum)
140 {
141 switch (actionType)
142 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200143 case TriggerAction::LogToJournal:
Szymon Dompke20013012021-07-23 09:54:20 +0200144 {
145 actionsIf.emplace_back(
146 std::make_unique<LogToJournal>(type, thresholdValue));
147 break;
148 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200149 case TriggerAction::LogToRedfishEventLog:
Szymon Dompke20013012021-07-23 09:54:20 +0200150 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200151 actionsIf.emplace_back(std::make_unique<LogToRedfishEventLog>(
152 type, thresholdValue));
Szymon Dompke20013012021-07-23 09:54:20 +0200153 break;
154 }
155 case TriggerAction::UpdateReport:
156 {
157 actionsIf.emplace_back(
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100158 std::make_unique<UpdateReport>(ioc, reportIds));
Szymon Dompke20013012021-07-23 09:54:20 +0200159 break;
160 }
161 }
162 }
163}
164
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100165} // namespace numeric
166
167namespace discrete
168{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200169
Patrick Williams583ba442025-02-03 14:28:19 -0500170void LogToJournal::commit(
171 const std::string& triggerId, const ThresholdName thresholdNameIn,
172 const std::string& sensorName, const Milliseconds timestamp,
173 const TriggerValue triggerValue)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100174{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200175 auto value = std::get<std::string>(triggerValue);
176
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000177 std::string msg =
178 "Discrete condition '" + thresholdNameIn->get() + "' of trigger '" +
179 triggerId + "' is met on sensor " + sensorName +
180 ", recorded value: " + value + ", severity: " +
181 std::string(utils::toShortEnum(utils::enumToString(severity))) +
182 ", timestamp: " + timestampToString(timestamp);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100183
184 phosphor::logging::log<phosphor::logging::level::INFO>(msg.c_str());
185}
186
Patrick Williams583ba442025-02-03 14:28:19 -0500187void LogToRedfishEventLog::commit(
188 const std::string& triggerId, const ThresholdName thresholdNameIn,
189 const std::string& sensorName, const Milliseconds timestamp,
190 const TriggerValue triggerValue)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100191{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200192 auto value = std::get<std::string>(triggerValue);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200193
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100194 phosphor::logging::log<phosphor::logging::level::INFO>(
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200195 "Logging discrete trigger action to Redfish Event Log.",
Michal Orzel8018a3b2024-06-26 14:14:16 +0200196 phosphor::logging::entry(
197 "REDFISH_MESSAGE_ID=%s",
198 redfish_message_ids::TriggerDiscreteConditionMet),
199 phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%s,%s",
200 sensorName.c_str(), value.c_str(),
201 triggerId.c_str()));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100202}
203
Szymon Dompke20013012021-07-23 09:54:20 +0200204void fillActions(
205 std::vector<std::unique_ptr<interfaces::TriggerAction>>& actionsIf,
206 const std::vector<TriggerAction>& ActionsEnum,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100207 ::discrete::Severity severity, boost::asio::io_context& ioc,
Szymon Dompke94f71c52021-12-10 07:16:33 +0100208 const std::shared_ptr<std::vector<std::string>>& reportIds)
Szymon Dompke20013012021-07-23 09:54:20 +0200209{
210 actionsIf.reserve(ActionsEnum.size());
211 for (auto actionType : ActionsEnum)
212 {
213 switch (actionType)
214 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200215 case TriggerAction::LogToJournal:
Szymon Dompke20013012021-07-23 09:54:20 +0200216 {
217 actionsIf.emplace_back(
218 std::make_unique<LogToJournal>(severity));
219 break;
220 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200221 case TriggerAction::LogToRedfishEventLog:
Szymon Dompke20013012021-07-23 09:54:20 +0200222 {
223 actionsIf.emplace_back(
Michal Orzel8018a3b2024-06-26 14:14:16 +0200224 std::make_unique<LogToRedfishEventLog>());
Szymon Dompke20013012021-07-23 09:54:20 +0200225 break;
226 }
227 case TriggerAction::UpdateReport:
228 {
229 actionsIf.emplace_back(
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100230 std::make_unique<UpdateReport>(ioc, reportIds));
Szymon Dompke20013012021-07-23 09:54:20 +0200231 break;
232 }
233 }
234 }
235}
236
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100237namespace onChange
238{
Patrick Williams583ba442025-02-03 14:28:19 -0500239void LogToJournal::commit(
240 const std::string& triggerId, const ThresholdName thresholdNameIn,
241 const std::string& sensorName, const Milliseconds timestamp,
242 const TriggerValue triggerValue)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100243{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200244 auto value = triggerValueToString(triggerValue);
Patrick Williams583ba442025-02-03 14:28:19 -0500245 std::string msg =
246 "Discrete condition 'OnChange' of trigger '" + triggerId +
247 "' is met on sensor: " + sensorName + ", recorded value: " + value +
248 ", timestamp: " + timestampToString(timestamp);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100249
250 phosphor::logging::log<phosphor::logging::level::INFO>(msg.c_str());
251}
252
Patrick Williams583ba442025-02-03 14:28:19 -0500253void LogToRedfishEventLog::commit(
254 const std::string& triggerId, const ThresholdName thresholdNameIn,
255 const std::string& sensorName, const Milliseconds timestamp,
256 const TriggerValue triggerValue)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100257{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200258 auto value = triggerValueToString(triggerValue);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200259
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100260 phosphor::logging::log<phosphor::logging::level::INFO>(
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200261 "Logging onChange discrete trigger action to Redfish Event Log.",
Michal Orzel8018a3b2024-06-26 14:14:16 +0200262 phosphor::logging::entry(
263 "REDFISH_MESSAGE_ID=%s",
264 redfish_message_ids::TriggerDiscreteConditionMet),
265 phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%s,%s",
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200266 sensorName.c_str(), value.c_str(),
Michal Orzel8018a3b2024-06-26 14:14:16 +0200267 triggerId.c_str()));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100268}
Szymon Dompke20013012021-07-23 09:54:20 +0200269
270void fillActions(
271 std::vector<std::unique_ptr<interfaces::TriggerAction>>& actionsIf,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100272 const std::vector<TriggerAction>& ActionsEnum, boost::asio::io_context& ioc,
Szymon Dompke94f71c52021-12-10 07:16:33 +0100273 const std::shared_ptr<std::vector<std::string>>& reportIds)
Szymon Dompke20013012021-07-23 09:54:20 +0200274{
275 actionsIf.reserve(ActionsEnum.size());
276 for (auto actionType : ActionsEnum)
277 {
278 switch (actionType)
279 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200280 case TriggerAction::LogToJournal:
Szymon Dompke20013012021-07-23 09:54:20 +0200281 {
282 actionsIf.emplace_back(std::make_unique<LogToJournal>());
283 break;
284 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200285 case TriggerAction::LogToRedfishEventLog:
Szymon Dompke20013012021-07-23 09:54:20 +0200286 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200287 actionsIf.emplace_back(
288 std::make_unique<LogToRedfishEventLog>());
Szymon Dompke20013012021-07-23 09:54:20 +0200289 break;
290 }
291 case TriggerAction::UpdateReport:
292 {
293 actionsIf.emplace_back(
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100294 std::make_unique<UpdateReport>(ioc, reportIds));
Szymon Dompke20013012021-07-23 09:54:20 +0200295 break;
296 }
297 }
298 }
299}
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100300} // namespace onChange
301} // namespace discrete
302
Patrick Williams583ba442025-02-03 14:28:19 -0500303void UpdateReport::commit(
304 const std::string& triggerId, const ThresholdName thresholdNameIn,
305 const std::string& sensorName, const Milliseconds timestamp,
306 const TriggerValue triggerValue)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100307{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100308 if (reportIds->empty())
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100309 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100310 return;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100311 }
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100312
313 utils::Messanger messanger(ioc);
314 messanger.send(messages::UpdateReportInd{*reportIds});
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100315}
316} // namespace action