Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 1 | #include "trigger_actions.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | |
| 5 | #include <ctime> |
| 6 | |
| 7 | namespace action |
| 8 | { |
| 9 | |
| 10 | static const char* getDirection(double value, double threshold) |
| 11 | { |
| 12 | if (value < threshold) |
| 13 | { |
| 14 | return "decreasing"; |
| 15 | } |
| 16 | if (value > threshold) |
| 17 | { |
| 18 | return "increasing"; |
| 19 | } |
| 20 | throw std::runtime_error("Invalid value"); |
| 21 | } |
| 22 | |
| 23 | const char* LogToJournal::getType() const |
| 24 | { |
| 25 | switch (type) |
| 26 | { |
| 27 | case numeric::Type::upperCritical: |
| 28 | return "UpperCritical"; |
| 29 | case numeric::Type::lowerCritical: |
| 30 | return "LowerCritical"; |
| 31 | case numeric::Type::upperWarning: |
| 32 | return "UpperWarning"; |
| 33 | case numeric::Type::lowerWarning: |
| 34 | return "LowerWarning"; |
| 35 | } |
| 36 | throw std::runtime_error("Invalid type"); |
| 37 | } |
| 38 | |
| 39 | void LogToJournal::commit(const std::string& sensorName, uint64_t timestamp, |
| 40 | double value) |
| 41 | { |
| 42 | std::time_t t = static_cast<time_t>(timestamp); |
| 43 | std::array<char, sizeof("YYYY-MM-DDThh:mm:ssZ")> buf = {}; |
| 44 | size_t size = |
| 45 | std::strftime(buf.data(), buf.size(), "%FT%TZ", std::gmtime(&t)); |
| 46 | if (size == 0) |
| 47 | { |
| 48 | throw std::runtime_error("Failed to parse timestamp to string"); |
| 49 | } |
| 50 | |
| 51 | std::string msg = std::string(getType()) + |
| 52 | " numeric threshold condition is met on sensor " + |
| 53 | sensorName + ", recorded value " + std::to_string(value) + |
| 54 | ", timestamp " + std::string(buf.data(), size) + |
| 55 | ", direction " + |
| 56 | std::string(getDirection(value, threshold)); |
| 57 | |
| 58 | phosphor::logging::log<phosphor::logging::level::INFO>(msg.c_str()); |
| 59 | } |
| 60 | |
| 61 | const char* LogToRedfish::getMessageId() const |
| 62 | { |
| 63 | switch (type) |
| 64 | { |
| 65 | case numeric::Type::upperCritical: |
| 66 | return "OpenBMC.0.1.0.NumericThresholdUpperCritical"; |
| 67 | case numeric::Type::lowerCritical: |
| 68 | return "OpenBMC.0.1.0.NumericThresholdLowerCritical"; |
| 69 | case numeric::Type::upperWarning: |
| 70 | return "OpenBMC.0.1.0.NumericThresholdUpperWarning"; |
| 71 | case numeric::Type::lowerWarning: |
| 72 | return "OpenBMC.0.1.0.NumericThresholdLowerWarning"; |
| 73 | } |
| 74 | throw std::runtime_error("Invalid type"); |
| 75 | } |
| 76 | |
| 77 | void LogToRedfish::commit(const std::string& sensorName, uint64_t timestamp, |
| 78 | double value) |
| 79 | { |
| 80 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 81 | "Threshold value is exceeded", |
| 82 | phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", getMessageId()), |
| 83 | phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s,%f,%llu,%s", |
| 84 | sensorName.c_str(), value, timestamp, |
| 85 | getDirection(value, threshold))); |
| 86 | } |
| 87 | |
| 88 | void UpdateReport::commit(const std::string&, uint64_t, double) |
| 89 | { |
| 90 | for (const auto& name : reportNames) |
| 91 | { |
| 92 | reportManager.updateReport(name); |
| 93 | } |
| 94 | } |
| 95 | } // namespace action |