Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 1 | #include "trigger_actions.hpp" |
| 2 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 3 | #include "messages/update_report_ind.hpp" |
Szymon Dompke | f670b02 | 2022-03-16 19:21:11 +0100 | [diff] [blame] | 4 | #include "types/trigger_types.hpp" |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 5 | #include "utils/messanger.hpp" |
| 6 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> |
| 8 | |
| 9 | #include <ctime> |
| 10 | |
| 11 | namespace action |
| 12 | { |
| 13 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 14 | namespace |
| 15 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 16 | std::string timestampToString(Milliseconds timestamp) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 17 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 18 | std::time_t t = static_cast<time_t>(timestamp.count()); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 19 | std::array<char, sizeof("YYYY-MM-DDThh:mm:ssZ")> buf = {}; |
| 20 | size_t size = |
| 21 | std::strftime(buf.data(), buf.size(), "%FT%TZ", std::gmtime(&t)); |
| 22 | if (size == 0) |
| 23 | { |
| 24 | throw std::runtime_error("Failed to parse timestamp to string"); |
| 25 | } |
| 26 | return std::string(buf.data(), size); |
| 27 | } |
| 28 | } // namespace |
| 29 | |
| 30 | namespace numeric |
| 31 | { |
| 32 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 33 | static const char* getDirection(double value, double threshold) |
| 34 | { |
| 35 | if (value < threshold) |
| 36 | { |
| 37 | return "decreasing"; |
| 38 | } |
| 39 | if (value > threshold) |
| 40 | { |
| 41 | return "increasing"; |
| 42 | } |
| 43 | throw std::runtime_error("Invalid value"); |
| 44 | } |
| 45 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 46 | void LogToJournal::commit(const std::string& sensorName, Milliseconds timestamp, |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 47 | double value) |
| 48 | { |
Szymon Dompke | f670b02 | 2022-03-16 19:21:11 +0100 | [diff] [blame] | 49 | std::string msg = ::numeric::typeToString(type) + |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 50 | " numeric threshold condition is met on sensor " + |
| 51 | sensorName + ", recorded value " + std::to_string(value) + |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 52 | ", timestamp " + timestampToString(timestamp) + |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 53 | ", direction " + |
| 54 | std::string(getDirection(value, threshold)); |
| 55 | |
| 56 | phosphor::logging::log<phosphor::logging::level::INFO>(msg.c_str()); |
| 57 | } |
| 58 | |
| 59 | const char* LogToRedfish::getMessageId() const |
| 60 | { |
| 61 | switch (type) |
| 62 | { |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 63 | case ::numeric::Type::upperCritical: |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 64 | return "OpenBMC.0.1.0.NumericThresholdUpperCritical"; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 65 | case ::numeric::Type::lowerCritical: |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 66 | return "OpenBMC.0.1.0.NumericThresholdLowerCritical"; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 67 | case ::numeric::Type::upperWarning: |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 68 | return "OpenBMC.0.1.0.NumericThresholdUpperWarning"; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 69 | case ::numeric::Type::lowerWarning: |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 70 | return "OpenBMC.0.1.0.NumericThresholdLowerWarning"; |
| 71 | } |
| 72 | throw std::runtime_error("Invalid type"); |
| 73 | } |
| 74 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 75 | void LogToRedfish::commit(const std::string& sensorName, Milliseconds timestamp, |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 76 | double value) |
| 77 | { |
| 78 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 79 | "Threshold value is exceeded", |
| 80 | phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", getMessageId()), |
| 81 | phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%f,%llu,%s", |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 82 | sensorName.c_str(), value, timestamp.count(), |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 83 | getDirection(value, threshold))); |
| 84 | } |
| 85 | |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 86 | void fillActions( |
| 87 | std::vector<std::unique_ptr<interfaces::TriggerAction>>& actionsIf, |
| 88 | const std::vector<TriggerAction>& ActionsEnum, ::numeric::Type type, |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 89 | double thresholdValue, boost::asio::io_context& ioc, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 90 | const std::shared_ptr<std::vector<std::string>>& reportIds) |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 91 | { |
| 92 | actionsIf.reserve(ActionsEnum.size()); |
| 93 | for (auto actionType : ActionsEnum) |
| 94 | { |
| 95 | switch (actionType) |
| 96 | { |
| 97 | case TriggerAction::LogToLogService: |
| 98 | { |
| 99 | actionsIf.emplace_back( |
| 100 | std::make_unique<LogToJournal>(type, thresholdValue)); |
| 101 | break; |
| 102 | } |
| 103 | case TriggerAction::RedfishEvent: |
| 104 | { |
| 105 | actionsIf.emplace_back( |
| 106 | std::make_unique<LogToRedfish>(type, thresholdValue)); |
| 107 | break; |
| 108 | } |
| 109 | case TriggerAction::UpdateReport: |
| 110 | { |
| 111 | actionsIf.emplace_back( |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 112 | std::make_unique<UpdateReport>(ioc, reportIds)); |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 113 | break; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 119 | } // namespace numeric |
| 120 | |
| 121 | namespace discrete |
| 122 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 123 | void LogToJournal::commit(const std::string& sensorName, Milliseconds timestamp, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 124 | double value) |
| 125 | { |
Szymon Dompke | f670b02 | 2022-03-16 19:21:11 +0100 | [diff] [blame] | 126 | std::string msg = ::discrete::severityToString(severity) + |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 127 | " discrete threshold condition is met on sensor " + |
| 128 | sensorName + ", recorded value " + std::to_string(value) + |
| 129 | ", timestamp " + timestampToString(timestamp); |
| 130 | |
| 131 | phosphor::logging::log<phosphor::logging::level::INFO>(msg.c_str()); |
| 132 | } |
| 133 | |
| 134 | const char* LogToRedfish::getMessageId() const |
| 135 | { |
| 136 | switch (severity) |
| 137 | { |
| 138 | case ::discrete::Severity::ok: |
| 139 | return "OpenBMC.0.1.0.DiscreteThresholdOk"; |
| 140 | case ::discrete::Severity::warning: |
| 141 | return "OpenBMC.0.1.0.DiscreteThresholdWarning"; |
| 142 | case ::discrete::Severity::critical: |
| 143 | return "OpenBMC.0.1.0.DiscreteThresholdCritical"; |
| 144 | } |
| 145 | throw std::runtime_error("Invalid severity"); |
| 146 | } |
| 147 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 148 | void LogToRedfish::commit(const std::string& sensorName, Milliseconds timestamp, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 149 | double value) |
| 150 | { |
| 151 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 152 | "Discrete treshold condition is met", |
| 153 | phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", getMessageId()), |
| 154 | phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%f,%llu", |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 155 | sensorName.c_str(), value, timestamp.count())); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 158 | void fillActions( |
| 159 | std::vector<std::unique_ptr<interfaces::TriggerAction>>& actionsIf, |
| 160 | const std::vector<TriggerAction>& ActionsEnum, |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 161 | ::discrete::Severity severity, boost::asio::io_context& ioc, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 162 | const std::shared_ptr<std::vector<std::string>>& reportIds) |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 163 | { |
| 164 | actionsIf.reserve(ActionsEnum.size()); |
| 165 | for (auto actionType : ActionsEnum) |
| 166 | { |
| 167 | switch (actionType) |
| 168 | { |
| 169 | case TriggerAction::LogToLogService: |
| 170 | { |
| 171 | actionsIf.emplace_back( |
| 172 | std::make_unique<LogToJournal>(severity)); |
| 173 | break; |
| 174 | } |
| 175 | case TriggerAction::RedfishEvent: |
| 176 | { |
| 177 | actionsIf.emplace_back( |
| 178 | std::make_unique<LogToRedfish>(severity)); |
| 179 | break; |
| 180 | } |
| 181 | case TriggerAction::UpdateReport: |
| 182 | { |
| 183 | actionsIf.emplace_back( |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 184 | std::make_unique<UpdateReport>(ioc, reportIds)); |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 185 | break; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 191 | namespace onChange |
| 192 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 193 | void LogToJournal::commit(const std::string& sensorName, Milliseconds timestamp, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 194 | double value) |
| 195 | { |
| 196 | std::string msg = "Value changed on sensor " + sensorName + |
| 197 | ", recorded value " + std::to_string(value) + |
| 198 | ", timestamp " + timestampToString(timestamp); |
| 199 | |
| 200 | phosphor::logging::log<phosphor::logging::level::INFO>(msg.c_str()); |
| 201 | } |
| 202 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 203 | void LogToRedfish::commit(const std::string& sensorName, Milliseconds timestamp, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 204 | double value) |
| 205 | { |
| 206 | const char* messageId = "OpenBMC.0.1.0.DiscreteThresholdOnChange"; |
| 207 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 208 | "Uncondtional discrete threshold triggered", |
| 209 | phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", messageId), |
| 210 | phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%f,%llu", |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 211 | sensorName.c_str(), value, timestamp.count())); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 212 | } |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 213 | |
| 214 | void fillActions( |
| 215 | std::vector<std::unique_ptr<interfaces::TriggerAction>>& actionsIf, |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 216 | const std::vector<TriggerAction>& ActionsEnum, boost::asio::io_context& ioc, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 217 | const std::shared_ptr<std::vector<std::string>>& reportIds) |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 218 | { |
| 219 | actionsIf.reserve(ActionsEnum.size()); |
| 220 | for (auto actionType : ActionsEnum) |
| 221 | { |
| 222 | switch (actionType) |
| 223 | { |
| 224 | case TriggerAction::LogToLogService: |
| 225 | { |
| 226 | actionsIf.emplace_back(std::make_unique<LogToJournal>()); |
| 227 | break; |
| 228 | } |
| 229 | case TriggerAction::RedfishEvent: |
| 230 | { |
| 231 | actionsIf.emplace_back(std::make_unique<LogToRedfish>()); |
| 232 | break; |
| 233 | } |
| 234 | case TriggerAction::UpdateReport: |
| 235 | { |
| 236 | actionsIf.emplace_back( |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 237 | std::make_unique<UpdateReport>(ioc, reportIds)); |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 238 | break; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 243 | } // namespace onChange |
| 244 | } // namespace discrete |
| 245 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 246 | void UpdateReport::commit(const std::string&, Milliseconds, double) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 247 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 248 | if (reportIds->empty()) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 249 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 250 | return; |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 251 | } |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 252 | |
| 253 | utils::Messanger messanger(ioc); |
| 254 | messanger.send(messages::UpdateReportInd{*reportIds}); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 255 | } |
| 256 | } // namespace action |