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_SEVERITY = "warning"; |
| 21 | 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] | 22 | const auto ALARM_THRESHOLD_CROSSING_LOW = "velia-alarms:sensor-low-value-alarm"; |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 23 | const auto ALARM_THRESHOLD_CROSSING_LOW_DESCRIPTION = "Sensor value crossed low threshold."; |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 24 | const auto ALARM_THRESHOLD_CROSSING_HIGH = "velia-alarms:sensor-high-value-alarm"; |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 25 | const auto ALARM_THRESHOLD_CROSSING_HIGH_DESCRIPTION = "Sensor value crossed high threshold."; |
Tomáš Pecka | 5a4c035 | 2023-12-12 12:29:28 +0100 | [diff] [blame] | 26 | const auto ALARM_SENSOR_NONOPERATIONAL = "velia-alarms:sensor-nonoperational"; |
| 27 | const auto ALARM_SENSOR_NONOPERATIONAL_SEVERITY = "warning"; |
| 28 | 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] | 29 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 30 | /** @brief Extracts component path prefix from an XPath under /ietf-hardware/component node |
| 31 | * |
| 32 | * Example input: /ietf-hardware:hardware/component[name='ne:psu:child']/oper-state/disabled |
| 33 | * Example output: /ietf-hardware:hardware/component[name='ne:psu:child'] |
| 34 | */ |
| 35 | std::string extractComponentPrefix(const std::string& componentXPath) |
| 36 | { |
| 37 | static const std::regex regex(R"((/ietf-hardware:hardware/component\[name=('|").*?(\2)\]).*)"); |
| 38 | std::smatch match; |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 39 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 40 | if (std::regex_match(componentXPath, match, regex)) { |
| 41 | return match.str(1); |
| 42 | } |
| 43 | |
| 44 | throw std::logic_error("Invalid xPath provided ('" + componentXPath + "')"); |
| 45 | } |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 46 | |
| 47 | void logAlarm(velia::Log logger, const std::string_view sensor, const std::string_view alarm, const std::string_view severity) |
| 48 | { |
| 49 | logger->info("Alarm {}: {} for {}", alarm, severity, sensor); |
| 50 | } |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 51 | |
| 52 | bool isThresholdCrossingLow(velia::ietf_hardware::State state) |
| 53 | { |
| 54 | return state == velia::ietf_hardware::State::WarningLow || state == velia::ietf_hardware::State::CriticalLow; |
| 55 | } |
| 56 | |
| 57 | bool isThresholdCrossingHigh(velia::ietf_hardware::State state) |
| 58 | { |
| 59 | return state == velia::ietf_hardware::State::WarningHigh || state == velia::ietf_hardware::State::CriticalHigh; |
| 60 | } |
| 61 | |
| 62 | std::string toYangAlarmSeverity(velia::ietf_hardware::State state) |
| 63 | { |
| 64 | switch (state) { |
| 65 | case velia::ietf_hardware::State::WarningLow: |
| 66 | case velia::ietf_hardware::State::WarningHigh: |
| 67 | return "warning"; |
| 68 | case velia::ietf_hardware::State::CriticalLow: |
| 69 | case velia::ietf_hardware::State::CriticalHigh: |
| 70 | return "critical"; |
| 71 | default: { |
| 72 | std::ostringstream oss; |
| 73 | oss << "No severity associated with sensor threshold State " << state; |
| 74 | throw std::logic_error(oss.str()); |
| 75 | } |
| 76 | } |
| 77 | } |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 80 | namespace velia::ietf_hardware::sysrepo { |
| 81 | |
| 82 | /** @brief The constructor expects the HardwareState instance which will provide the actual hardware state data and the poll interval */ |
| 83 | Sysrepo::Sysrepo(::sysrepo::Session session, std::shared_ptr<IETFHardware> hwState, std::chrono::microseconds pollInterval) |
| 84 | : m_log(spdlog::get("hardware")) |
| 85 | , m_pollInterval(std::move(pollInterval)) |
| 86 | , m_session(std::move(session)) |
| 87 | , m_hwState(std::move(hwState)) |
| 88 | , m_quit(false) |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 89 | { |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 90 | m_pollThread = std::thread([&]() { |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 91 | auto conn = m_session.getConnection(); |
| 92 | |
| 93 | DataTree prevValues; |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 94 | std::set<std::string> seenSensors; |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 95 | std::map<std::string, State> thresholdsStates; |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 96 | std::set<std::pair<std::string, std::string>> activeSideLoadedAlarms; |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 97 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame] | 98 | alarms::pushInventory( |
| 99 | m_session, |
| 100 | { |
| 101 | {ALARM_THRESHOLD_CROSSING_LOW, "Sensor value is below the low threshold."}, |
| 102 | {ALARM_THRESHOLD_CROSSING_HIGH, "Sensor value is above the high threshold."}, |
| 103 | {ALARM_SENSOR_MISSING, "Sensor is missing."}, |
| 104 | {ALARM_SENSOR_NONOPERATIONAL, "Sensor is flagged as nonoperational."}, |
| 105 | }); |
Tomáš Pecka | 2848fd0 | 2024-01-30 12:05:59 +0100 | [diff] [blame] | 106 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 107 | while (!m_quit) { |
| 108 | m_log->trace("IetfHardware poll"); |
| 109 | |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 110 | auto [hwStateValues, thresholds, activeSensors, sideLoadedAlarms] = m_hwState->process(); |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 111 | std::set<std::string> deletedComponents; |
Tomáš Pecka | 8784429 | 2024-01-30 15:20:21 +0100 | [diff] [blame] | 112 | std::vector<std::string> newSensors; |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 113 | |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 114 | for (const auto& sensorXPath : activeSensors) { |
| 115 | if (!seenSensors.contains(sensorXPath)) { |
Tomáš Pecka | 8784429 | 2024-01-30 15:20:21 +0100 | [diff] [blame] | 116 | newSensors.emplace_back(extractComponentPrefix(sensorXPath)); |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | seenSensors.merge(activeSensors); |
| 120 | |
Tomáš Pecka | 8784429 | 2024-01-30 15:20:21 +0100 | [diff] [blame] | 121 | if (!newSensors.empty()) { |
Tomáš Pecka | bbfc1c3 | 2024-01-31 13:58:11 +0100 | [diff] [blame] | 122 | alarms::addResourcesToInventory(m_session, { |
| 123 | {ALARM_THRESHOLD_CROSSING_LOW, newSensors}, |
| 124 | {ALARM_THRESHOLD_CROSSING_HIGH, newSensors}, |
| 125 | {ALARM_SENSOR_MISSING, newSensors}, |
| 126 | {ALARM_SENSOR_NONOPERATIONAL, newSensors}, |
| 127 | }); |
Tomáš Pecka | 8784429 | 2024-01-30 15:20:21 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 130 | /* Some data readers can stop returning data in some cases (e.g. ejected PSU). |
| 131 | * Prune tree components that were removed before updating to avoid having not current data from previous invocations. |
| 132 | */ |
| 133 | for (const auto& [k, v] : prevValues) { |
| 134 | if (!hwStateValues.contains(k)) { |
| 135 | deletedComponents.emplace(extractComponentPrefix(k)); |
| 136 | } |
| 137 | } |
| 138 | |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame] | 139 | std::vector<std::string> discards; |
| 140 | discards.reserve(deletedComponents.size()); |
| 141 | std::copy(deletedComponents.begin(), deletedComponents.end(), std::back_inserter(discards)); |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 142 | |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame] | 143 | utils::valuesPush(hwStateValues, {}, discards, m_session, ::sysrepo::Datastore::Operational); |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 144 | |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 145 | /* Publish sideloaded alarms */ |
| 146 | for (const auto& [alarm, resource, severity, text] : sideLoadedAlarms) { |
| 147 | // Sideloaded alarms are not registered using the code above, let's register those too |
Tomáš Pecka | bbfc1c3 | 2024-01-31 13:58:11 +0100 | [diff] [blame] | 148 | alarms::addResourcesToInventory(m_session, {{ALARM_SENSOR_MISSING, {resource}}}); |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 149 | |
| 150 | bool isActive = activeSideLoadedAlarms.contains({alarm, resource}); |
| 151 | if (isActive && severity == "cleared") { |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 152 | alarms::push(m_session, alarm, resource, "cleared", text); |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 153 | activeSideLoadedAlarms.erase({alarm, resource}); |
| 154 | } else if (!isActive && severity != "cleared") { |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 155 | alarms::push(m_session, alarm, resource, severity, text); |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 156 | activeSideLoadedAlarms.insert({alarm, resource}); |
| 157 | } |
| 158 | } |
| 159 | |
Tomáš Pecka | 5a4c035 | 2023-12-12 12:29:28 +0100 | [diff] [blame] | 160 | /* Look for nonoperational sensors to set alarms */ |
| 161 | for (const auto& [leaf, value] : hwStateValues) { |
| 162 | if (boost::ends_with(leaf, "/sensor-data/oper-status")) { |
| 163 | std::optional<std::string> oldValue; |
| 164 | |
| 165 | if (auto it = prevValues.find(leaf); it != prevValues.end()) { |
| 166 | oldValue = it->second; |
| 167 | } |
| 168 | |
| 169 | if (value == "nonoperational" && oldValue != "nonoperational") { |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 170 | alarms::push(m_session, ALARM_SENSOR_NONOPERATIONAL, extractComponentPrefix(leaf), ALARM_SENSOR_NONOPERATIONAL_SEVERITY, ALARM_SENSOR_NONOPERATIONAL_DESCRIPTION); |
Tomáš Pecka | 5a4c035 | 2023-12-12 12:29:28 +0100 | [diff] [blame] | 171 | } 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 */) { |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 172 | alarms::push(m_session, ALARM_SENSOR_NONOPERATIONAL, extractComponentPrefix(leaf), ALARM_CLEARED, ALARM_SENSOR_NONOPERATIONAL_DESCRIPTION); |
Tomáš Pecka | 5a4c035 | 2023-12-12 12:29:28 +0100 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 177 | for (const auto& [sensorXPath, state] : thresholds) { |
| 178 | // missing prevState can be considered as Normal |
| 179 | const State prevState = [&, sensorXPath = sensorXPath] { |
| 180 | if (auto it = thresholdsStates.find(sensorXPath); it != thresholdsStates.end()) { |
| 181 | return it->second; |
| 182 | } |
| 183 | return State::Normal; |
| 184 | }(); |
| 185 | const auto componentXPath = extractComponentPrefix(sensorXPath); |
| 186 | |
| 187 | if (state == State::NoValue) { |
Jan Kundrát | 2ec3748 | 2024-01-11 21:38:20 +0100 | [diff] [blame] | 188 | logAlarm(m_log, componentXPath, ALARM_SENSOR_MISSING, ALARM_MISSING_SEVERITY); |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 189 | alarms::push(m_session, ALARM_SENSOR_MISSING, componentXPath, ALARM_MISSING_SEVERITY, ALARM_MISSING_DESCRIPTION); |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 190 | } else if (prevState == State::NoValue) { |
Jan Kundrát | 2ec3748 | 2024-01-11 21:38:20 +0100 | [diff] [blame] | 191 | logAlarm(m_log, componentXPath, ALARM_SENSOR_MISSING, ALARM_CLEARED); |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 192 | /* The alarm message is same for both setting and clearing the alarm. RFC8632 says that it is |
| 193 | * "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.", |
| 194 | * 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. |
| 195 | */ |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 196 | alarms::push(m_session, ALARM_SENSOR_MISSING, componentXPath, ALARM_CLEARED, ALARM_MISSING_DESCRIPTION); |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 199 | /* |
| 200 | * 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. |
| 201 | * |
| 202 | * In case new state corresponds to threshold crossing (wither lower bound or upper bound) we set the alarm. |
| 203 | * Since we receive only changes to states it should be sufficient to just check if the new state crossed the threshold. |
| 204 | * 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. |
| 205 | * We can however receive a change from critical threshold to warning threshold (or warning to critical) but that is no problem. |
| 206 | * We only need to set the same alarm again with the new severity. |
| 207 | */ |
| 208 | if (isThresholdCrossingLow(state)) { |
| 209 | logAlarm(m_log, componentXPath, ALARM_THRESHOLD_CROSSING_LOW, toYangAlarmSeverity(state)); |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 210 | alarms::push(m_session, ALARM_THRESHOLD_CROSSING_LOW, componentXPath, toYangAlarmSeverity(state), ALARM_THRESHOLD_CROSSING_LOW_DESCRIPTION); |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 211 | } else if (isThresholdCrossingHigh(state)) { |
| 212 | logAlarm(m_log, componentXPath, ALARM_THRESHOLD_CROSSING_HIGH, toYangAlarmSeverity(state)); |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 213 | alarms::push(m_session, ALARM_THRESHOLD_CROSSING_HIGH, componentXPath, toYangAlarmSeverity(state), ALARM_THRESHOLD_CROSSING_HIGH_DESCRIPTION); |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* 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. */ |
| 217 | if (!isThresholdCrossingLow(state) && isThresholdCrossingLow(prevState)) { |
| 218 | logAlarm(m_log, componentXPath, ALARM_THRESHOLD_CROSSING_LOW, ALARM_CLEARED); |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 219 | alarms::push(m_session, ALARM_THRESHOLD_CROSSING_LOW, componentXPath, ALARM_CLEARED, ALARM_THRESHOLD_CROSSING_LOW_DESCRIPTION); |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 220 | } else if (!isThresholdCrossingHigh(state) && isThresholdCrossingHigh(prevState)) { |
| 221 | logAlarm(m_log, componentXPath, ALARM_THRESHOLD_CROSSING_HIGH, ALARM_CLEARED); |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 222 | alarms::push(m_session, ALARM_THRESHOLD_CROSSING_HIGH, componentXPath, ALARM_CLEARED, ALARM_THRESHOLD_CROSSING_HIGH_DESCRIPTION); |
Tomáš Pecka | 9af4739 | 2023-05-23 14:56:48 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Tomáš Pecka | 1b3c173 | 2023-05-12 11:45:01 +0200 | [diff] [blame] | 225 | thresholdsStates[sensorXPath] = state; |
| 226 | } |
| 227 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 228 | prevValues = std::move(hwStateValues); |
| 229 | std::this_thread::sleep_for(m_pollInterval); |
Tomáš Pecka | bbfc1c3 | 2024-01-31 13:58:11 +0100 | [diff] [blame] | 230 | } |
Tomáš Pecka | 2117ce5 | 2023-05-12 11:28:34 +0200 | [diff] [blame] | 231 | }); |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 232 | } |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 233 | |
Tomáš Pecka | 43ef7ba | 2023-04-13 15:56:48 +0200 | [diff] [blame] | 234 | Sysrepo::~Sysrepo() |
| 235 | { |
| 236 | m_log->trace("Requesting poll thread stop"); |
| 237 | m_quit = true; |
| 238 | m_pollThread.join(); |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 239 | } |
| 240 | } |