Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 1 | /* |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 2 | * Copyright (C) 2020-2023 CESNET, https://photonics.cesnet.cz/ |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
Tomáš Pecka | 5a4c035 | 2023-12-12 12:29:28 +0100 | [diff] [blame] | 8 | #include <boost/algorithm/string.hpp> |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 9 | #include <regex> |
| 10 | #include <sysrepo-cpp/Connection.hpp> |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 11 | #include "Sysrepo.h" |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 12 | #include "utils/alarms.h" |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 13 | #include "utils/log.h" |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 14 | #include "utils/sysrepo.h" |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 15 | |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 16 | namespace { |
| 17 | |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 18 | const auto ALARM_CLEARED = "cleared"; |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 19 | const auto ALARM_SENSOR_MISSING = "velia-alarms:sensor-missing-alarm"; |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 20 | const auto ALARM_MISSING = "velia-alarms:sensor-missing-alarm"; |
| 21 | const auto ALARM_MISSING_SEVERITY = "warning"; |
| 22 | const auto ALARM_MISSING_DESCRIPTION = "Sensor value not reported. Maybe the sensor was unplugged?"; |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 23 | const auto ALARM_THRESHOLD_CROSSING_LOW = "velia-alarms:sensor-low-value-alarm"; |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 24 | const auto ALARM_THRESHOLD_CROSSING_LOW_DESCRIPTION = "Sensor value crossed low threshold."; |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 25 | const auto ALARM_THRESHOLD_CROSSING_HIGH = "velia-alarms:sensor-high-value-alarm"; |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 26 | const auto ALARM_THRESHOLD_CROSSING_HIGH_DESCRIPTION = "Sensor value crossed high threshold."; |
Tomáš Pecka | 5a4c035 | 2023-12-12 12:29:28 +0100 | [diff] [blame] | 27 | const auto ALARM_SENSOR_NONOPERATIONAL = "velia-alarms:sensor-nonoperational"; |
| 28 | const auto ALARM_SENSOR_NONOPERATIONAL_SEVERITY = "warning"; |
| 29 | const auto ALARM_SENSOR_NONOPERATIONAL_DESCRIPTION = "Sensor is nonoperational. The values it reports may not be relevant."; |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 30 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 31 | /** @brief Extracts component path prefix from an XPath under /ietf-hardware/component node |
| 32 | * |
| 33 | * Example input: /ietf-hardware:hardware/component[name='ne:psu:child']/oper-state/disabled |
| 34 | * Example output: /ietf-hardware:hardware/component[name='ne:psu:child'] |
| 35 | */ |
| 36 | std::string extractComponentPrefix(const std::string& componentXPath) |
| 37 | { |
| 38 | static const std::regex regex(R"((/ietf-hardware:hardware/component\[name=('|").*?(\2)\]).*)"); |
| 39 | std::smatch match; |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 40 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 41 | if (std::regex_match(componentXPath, match, regex)) { |
| 42 | return match.str(1); |
| 43 | } |
| 44 | |
| 45 | throw std::logic_error("Invalid xPath provided ('" + componentXPath + "')"); |
| 46 | } |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 47 | |
| 48 | void logAlarm(velia::Log logger, const std::string_view sensor, const std::string_view alarm, const std::string_view severity) |
| 49 | { |
| 50 | logger->info("Alarm {}: {} for {}", alarm, severity, sensor); |
| 51 | } |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 52 | |
| 53 | bool isThresholdCrossingLow(velia::ietf_hardware::State state) |
| 54 | { |
| 55 | return state == velia::ietf_hardware::State::WarningLow || state == velia::ietf_hardware::State::CriticalLow; |
| 56 | } |
| 57 | |
| 58 | bool isThresholdCrossingHigh(velia::ietf_hardware::State state) |
| 59 | { |
| 60 | return state == velia::ietf_hardware::State::WarningHigh || state == velia::ietf_hardware::State::CriticalHigh; |
| 61 | } |
| 62 | |
| 63 | std::string toYangAlarmSeverity(velia::ietf_hardware::State state) |
| 64 | { |
| 65 | switch (state) { |
| 66 | case velia::ietf_hardware::State::WarningLow: |
| 67 | case velia::ietf_hardware::State::WarningHigh: |
| 68 | return "warning"; |
| 69 | case velia::ietf_hardware::State::CriticalLow: |
| 70 | case velia::ietf_hardware::State::CriticalHigh: |
| 71 | return "critical"; |
| 72 | default: { |
| 73 | std::ostringstream oss; |
| 74 | oss << "No severity associated with sensor threshold State " << state; |
| 75 | throw std::logic_error(oss.str()); |
| 76 | } |
| 77 | } |
| 78 | } |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 81 | namespace velia::ietf_hardware::sysrepo { |
| 82 | |
| 83 | /** @brief The constructor expects the HardwareState instance which will provide the actual hardware state data and the poll interval */ |
| 84 | Sysrepo::Sysrepo(::sysrepo::Session session, std::shared_ptr<IETFHardware> hwState, std::chrono::microseconds pollInterval) |
| 85 | : m_log(spdlog::get("hardware")) |
| 86 | , m_pollInterval(std::move(pollInterval)) |
| 87 | , m_session(std::move(session)) |
| 88 | , m_hwState(std::move(hwState)) |
| 89 | , m_quit(false) |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 90 | { |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 91 | m_pollThread = std::thread([&]() { |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 92 | auto conn = m_session.getConnection(); |
| 93 | |
| 94 | DataTree prevValues; |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame^] | 95 | std::set<std::string> seenSensors; |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 96 | std::map<std::string, State> thresholdsStates; |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 97 | |
| 98 | while (!m_quit) { |
| 99 | m_log->trace("IetfHardware poll"); |
| 100 | |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame^] | 101 | auto [hwStateValues, thresholds, activeSensors] = m_hwState->process(); |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 102 | std::set<std::string> deletedComponents; |
| 103 | |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame^] | 104 | for (const auto& sensorXPath : activeSensors) { |
| 105 | if (!seenSensors.contains(sensorXPath)) { |
| 106 | auto componentXPath = extractComponentPrefix(sensorXPath); |
| 107 | utils::addResourceToAlarmInventoryEntry(m_session, ALARM_THRESHOLD_CROSSING_LOW, std::nullopt, componentXPath); |
| 108 | utils::addResourceToAlarmInventoryEntry(m_session, ALARM_THRESHOLD_CROSSING_HIGH, std::nullopt, componentXPath); |
| 109 | utils::addResourceToAlarmInventoryEntry(m_session, ALARM_SENSOR_MISSING, std::nullopt, componentXPath); |
| 110 | utils::addResourceToAlarmInventoryEntry(m_session, ALARM_SENSOR_NONOPERATIONAL, std::nullopt, componentXPath); |
| 111 | } |
| 112 | } |
| 113 | seenSensors.merge(activeSensors); |
| 114 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 115 | /* Some data readers can stop returning data in some cases (e.g. ejected PSU). |
| 116 | * Prune tree components that were removed before updating to avoid having not current data from previous invocations. |
| 117 | */ |
| 118 | for (const auto& [k, v] : prevValues) { |
| 119 | if (!hwStateValues.contains(k)) { |
| 120 | deletedComponents.emplace(extractComponentPrefix(k)); |
| 121 | } |
| 122 | } |
| 123 | |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame] | 124 | std::vector<std::string> discards; |
| 125 | discards.reserve(deletedComponents.size()); |
| 126 | std::copy(deletedComponents.begin(), deletedComponents.end(), std::back_inserter(discards)); |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 127 | |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame] | 128 | utils::valuesPush(hwStateValues, {}, discards, m_session, ::sysrepo::Datastore::Operational); |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 129 | |
Tomáš Pecka | 5a4c035 | 2023-12-12 12:29:28 +0100 | [diff] [blame] | 130 | /* Look for nonoperational sensors to set alarms */ |
| 131 | for (const auto& [leaf, value] : hwStateValues) { |
| 132 | if (boost::ends_with(leaf, "/sensor-data/oper-status")) { |
| 133 | std::optional<std::string> oldValue; |
| 134 | |
| 135 | if (auto it = prevValues.find(leaf); it != prevValues.end()) { |
| 136 | oldValue = it->second; |
| 137 | } |
| 138 | |
| 139 | if (value == "nonoperational" && oldValue != "nonoperational") { |
| 140 | utils::createOrUpdateAlarm(m_session, ALARM_SENSOR_NONOPERATIONAL, std::nullopt, extractComponentPrefix(leaf), ALARM_SENSOR_NONOPERATIONAL_SEVERITY, ALARM_SENSOR_NONOPERATIONAL_DESCRIPTION); |
| 141 | } else if (value == "ok" && oldValue && oldValue != "ok" /* don't call clear-alarm if we see this node for the first time, i.e., oldvalue is nullopt */) { |
| 142 | utils::createOrUpdateAlarm(m_session, ALARM_SENSOR_NONOPERATIONAL, std::nullopt, extractComponentPrefix(leaf), ALARM_CLEARED, ALARM_SENSOR_NONOPERATIONAL_DESCRIPTION); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 147 | for (const auto& [sensorXPath, state] : thresholds) { |
| 148 | // missing prevState can be considered as Normal |
| 149 | const State prevState = [&, sensorXPath = sensorXPath] { |
| 150 | if (auto it = thresholdsStates.find(sensorXPath); it != thresholdsStates.end()) { |
| 151 | return it->second; |
| 152 | } |
| 153 | return State::Normal; |
| 154 | }(); |
| 155 | const auto componentXPath = extractComponentPrefix(sensorXPath); |
| 156 | |
| 157 | if (state == State::NoValue) { |
| 158 | logAlarm(m_log, componentXPath, ALARM_MISSING, ALARM_MISSING_SEVERITY); |
| 159 | utils::createOrUpdateAlarm(m_session, ALARM_MISSING, std::nullopt, componentXPath, ALARM_MISSING_SEVERITY, ALARM_MISSING_DESCRIPTION); |
| 160 | } else if (prevState == State::NoValue) { |
| 161 | logAlarm(m_log, componentXPath, ALARM_MISSING, ALARM_CLEARED); |
| 162 | /* The alarm message is same for both setting and clearing the alarm. RFC8632 says that it is |
| 163 | * "The string used to inform operators about the alarm. This MUST contain enough information for an operator to be able to understand the problem and how to resolve it.", |
| 164 | * i.e., from my POV it does not make sense to say something like "cleared" when clearing the alarm as this would not be beneficial for the operator to understand what happened. |
| 165 | */ |
| 166 | utils::createOrUpdateAlarm(m_session, ALARM_MISSING, std::nullopt, componentXPath, ALARM_CLEARED, ALARM_MISSING_DESCRIPTION); |
| 167 | } |
| 168 | |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 169 | /* |
| 170 | * We set new threshold alarms first. In case the sensor value transitions from high to low (or low to high) we don't want to lose any active alarm on the resource. |
| 171 | * |
| 172 | * In case new state corresponds to threshold crossing (wither lower bound or upper bound) we set the alarm. |
| 173 | * Since we receive only changes to states it should be sufficient to just check if the new state crossed the threshold. |
| 174 | * We shouldn't receive any "no-op" state change (e.g. warning low to warning low) and even if we did receive such change, we would only set the same alarm again. |
| 175 | * We can however receive a change from critical threshold to warning threshold (or warning to critical) but that is no problem. |
| 176 | * We only need to set the same alarm again with the new severity. |
| 177 | */ |
| 178 | if (isThresholdCrossingLow(state)) { |
| 179 | logAlarm(m_log, componentXPath, ALARM_THRESHOLD_CROSSING_LOW, toYangAlarmSeverity(state)); |
| 180 | utils::createOrUpdateAlarm(m_session, ALARM_THRESHOLD_CROSSING_LOW, std::nullopt, componentXPath, toYangAlarmSeverity(state), ALARM_THRESHOLD_CROSSING_LOW_DESCRIPTION); |
| 181 | } else if (isThresholdCrossingHigh(state)) { |
| 182 | logAlarm(m_log, componentXPath, ALARM_THRESHOLD_CROSSING_HIGH, toYangAlarmSeverity(state)); |
| 183 | utils::createOrUpdateAlarm(m_session, ALARM_THRESHOLD_CROSSING_HIGH, std::nullopt, componentXPath, toYangAlarmSeverity(state), ALARM_THRESHOLD_CROSSING_HIGH_DESCRIPTION); |
| 184 | } |
| 185 | |
| 186 | /* Now we can clear the old threshold alarms that are no longer active, i.e., we transition away from the CriticalLow/WarningLow or CriticalHigh/WarningHigh. */ |
| 187 | if (!isThresholdCrossingLow(state) && isThresholdCrossingLow(prevState)) { |
| 188 | logAlarm(m_log, componentXPath, ALARM_THRESHOLD_CROSSING_LOW, ALARM_CLEARED); |
| 189 | utils::createOrUpdateAlarm(m_session, ALARM_THRESHOLD_CROSSING_LOW, std::nullopt, componentXPath, ALARM_CLEARED, ALARM_THRESHOLD_CROSSING_LOW_DESCRIPTION); |
| 190 | } else if (!isThresholdCrossingHigh(state) && isThresholdCrossingHigh(prevState)) { |
| 191 | logAlarm(m_log, componentXPath, ALARM_THRESHOLD_CROSSING_HIGH, ALARM_CLEARED); |
| 192 | utils::createOrUpdateAlarm(m_session, ALARM_THRESHOLD_CROSSING_HIGH, std::nullopt, componentXPath, ALARM_CLEARED, ALARM_THRESHOLD_CROSSING_HIGH_DESCRIPTION); |
| 193 | } |
| 194 | |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 195 | thresholdsStates[sensorXPath] = state; |
| 196 | } |
| 197 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 198 | prevValues = std::move(hwStateValues); |
| 199 | std::this_thread::sleep_for(m_pollInterval); |
| 200 | } |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 201 | }); |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 202 | } |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 203 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 204 | Sysrepo::~Sysrepo() |
| 205 | { |
| 206 | m_log->trace("Requesting poll thread stop"); |
| 207 | m_quit = true; |
| 208 | m_pollThread.join(); |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 209 | } |
| 210 | } |