Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 1 | #include "Thresholds.hpp" |
| 2 | |
| 3 | #include "VariantVisitors.hpp" |
| 4 | #include "sensor.hpp" |
| 5 | |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 6 | #include <array> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 7 | #include <boost/algorithm/string/replace.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 8 | #include <boost/container/flat_map.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 9 | #include <boost/lexical_cast.hpp> |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 10 | #include <cmath> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 11 | #include <fstream> |
| 12 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | #include <stdexcept> |
| 15 | #include <string> |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 16 | #include <tuple> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 17 | #include <utility> |
| 18 | #include <variant> |
| 19 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 20 | |
| 21 | static constexpr bool DEBUG = false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 22 | namespace thresholds |
| 23 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 24 | unsigned int toBusValue(const Level& level) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 25 | { |
| 26 | switch (level) |
| 27 | { |
| 28 | case (Level::WARNING): |
| 29 | { |
| 30 | return 0; |
| 31 | } |
| 32 | case (Level::CRITICAL): |
| 33 | { |
| 34 | return 1; |
| 35 | } |
| 36 | default: |
| 37 | { |
| 38 | return -1; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 43 | std::string toBusValue(const Direction& direction) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 44 | { |
| 45 | switch (direction) |
| 46 | { |
| 47 | case (Direction::LOW): |
| 48 | { |
| 49 | return "less than"; |
| 50 | } |
| 51 | case (Direction::HIGH): |
| 52 | { |
| 53 | return "greater than"; |
| 54 | } |
| 55 | default: |
| 56 | { |
| 57 | return "err"; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 62 | bool parseThresholdsFromConfig( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 63 | const SensorData& sensorData, |
| 64 | std::vector<thresholds::Threshold>& thresholdVector, |
| 65 | const std::string* matchLabel) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 66 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 67 | for (const auto& item : sensorData) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 68 | { |
| 69 | if (item.first.find("Thresholds") == std::string::npos) |
| 70 | { |
| 71 | continue; |
| 72 | } |
| 73 | if (matchLabel != nullptr) |
| 74 | { |
| 75 | auto labelFind = item.second.find("Label"); |
| 76 | if (labelFind == item.second.end()) |
| 77 | continue; |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 78 | if (std::visit(VariantToStringVisitor(), labelFind->second) != |
| 79 | *matchLabel) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 80 | continue; |
| 81 | } |
| 82 | auto directionFind = item.second.find("Direction"); |
| 83 | auto severityFind = item.second.find("Severity"); |
| 84 | auto valueFind = item.second.find("Value"); |
| 85 | if (valueFind == item.second.end() || |
| 86 | severityFind == item.second.end() || |
| 87 | directionFind == item.second.end()) |
| 88 | { |
| 89 | std::cerr << "Malformed threshold in configuration\n"; |
| 90 | return false; |
| 91 | } |
| 92 | Level level; |
| 93 | Direction direction; |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 94 | if (std::visit(VariantToUnsignedIntVisitor(), severityFind->second) == |
| 95 | 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 96 | { |
| 97 | level = Level::WARNING; |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | level = Level::CRITICAL; |
| 102 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 103 | if (std::visit(VariantToStringVisitor(), directionFind->second) == |
| 104 | "less than") |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 105 | { |
| 106 | direction = Direction::LOW; |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | direction = Direction::HIGH; |
| 111 | } |
James Feist | 13f340b | 2019-03-07 16:36:11 -0800 | [diff] [blame] | 112 | double val = std::visit(VariantToDoubleVisitor(), valueFind->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 113 | |
| 114 | thresholdVector.emplace_back(level, direction, val); |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 119 | void persistThreshold(const std::string& path, const std::string& baseInterface, |
| 120 | const thresholds::Threshold& threshold, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 121 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 122 | size_t thresholdCount, const std::string& labelMatch) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 123 | { |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 124 | for (size_t ii = 0; ii < thresholdCount; ii++) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 125 | { |
| 126 | std::string thresholdInterface = |
| 127 | baseInterface + ".Thresholds" + std::to_string(ii); |
| 128 | conn->async_method_call( |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 129 | [&, path, threshold, thresholdInterface, labelMatch]( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 130 | const boost::system::error_code& ec, |
| 131 | const boost::container::flat_map<std::string, BasicVariantType>& |
| 132 | result) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 133 | if (ec) |
| 134 | { |
| 135 | return; // threshold not supported |
| 136 | } |
| 137 | |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 138 | if (!labelMatch.empty()) |
| 139 | { |
| 140 | auto labelFind = result.find("Label"); |
| 141 | if (labelFind == result.end()) |
| 142 | { |
| 143 | std::cerr << "No label in threshold configuration\n"; |
| 144 | return; |
| 145 | } |
| 146 | std::string label = |
| 147 | std::visit(VariantToStringVisitor(), labelFind->second); |
| 148 | if (label != labelMatch) |
| 149 | { |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 154 | auto directionFind = result.find("Direction"); |
| 155 | auto severityFind = result.find("Severity"); |
| 156 | auto valueFind = result.find("Value"); |
| 157 | if (valueFind == result.end() || severityFind == result.end() || |
| 158 | directionFind == result.end()) |
| 159 | { |
| 160 | std::cerr << "Malformed threshold in configuration\n"; |
| 161 | return; |
| 162 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 163 | unsigned int level = std::visit(VariantToUnsignedIntVisitor(), |
| 164 | severityFind->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 165 | |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 166 | std::string dir = |
| 167 | std::visit(VariantToStringVisitor(), directionFind->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 168 | if ((toBusValue(threshold.level) != level) || |
| 169 | (toBusValue(threshold.direction) != dir)) |
| 170 | { |
| 171 | return; // not the droid we're looking for |
| 172 | } |
| 173 | |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 174 | std::variant<double> value(threshold.value); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 175 | conn->async_method_call( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 176 | [](const boost::system::error_code& ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 177 | if (ec) |
| 178 | { |
| 179 | std::cerr << "Error setting threshold " << ec |
| 180 | << "\n"; |
| 181 | } |
| 182 | }, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 183 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
| 184 | "Set", thresholdInterface, "Value", value); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 185 | }, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 186 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 187 | "GetAll", thresholdInterface); |
| 188 | } |
| 189 | } |
| 190 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 191 | void updateThresholds(Sensor* sensor) |
| 192 | { |
| 193 | if (sensor->thresholds.empty()) |
| 194 | { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | for (const auto& threshold : sensor->thresholds) |
| 199 | { |
| 200 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface; |
| 201 | std::string property; |
| 202 | if (threshold.level == thresholds::Level::CRITICAL) |
| 203 | { |
| 204 | interface = sensor->thresholdInterfaceCritical; |
| 205 | if (threshold.direction == thresholds::Direction::HIGH) |
| 206 | { |
| 207 | property = "CriticalHigh"; |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | property = "CriticalLow"; |
| 212 | } |
| 213 | } |
| 214 | else if (threshold.level == thresholds::Level::WARNING) |
| 215 | { |
| 216 | interface = sensor->thresholdInterfaceWarning; |
| 217 | if (threshold.direction == thresholds::Direction::HIGH) |
| 218 | { |
| 219 | property = "WarningHigh"; |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | property = "WarningLow"; |
| 224 | } |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | continue; |
| 229 | } |
| 230 | if (!interface) |
| 231 | { |
| 232 | continue; |
| 233 | } |
| 234 | interface->set_property(property, threshold.value); |
| 235 | } |
| 236 | } |
| 237 | |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 238 | // Debugging counters |
| 239 | static int cHiTrue = 0; |
| 240 | static int cHiFalse = 0; |
| 241 | static int cHiMidstate = 0; |
| 242 | static int cLoTrue = 0; |
| 243 | static int cLoFalse = 0; |
| 244 | static int cLoMidstate = 0; |
| 245 | static int cDebugThrottle = 0; |
| 246 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 247 | static std::vector<std::pair<Threshold, bool>> checkThresholds(Sensor* sensor, |
| 248 | double value) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 249 | { |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 250 | std::vector<std::pair<Threshold, bool>> thresholdChanges; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 251 | if (sensor->thresholds.empty()) |
| 252 | { |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 253 | return thresholdChanges; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 254 | } |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 255 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 256 | for (auto& threshold : sensor->thresholds) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 257 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 258 | // Use "Schmitt trigger" logic to avoid threshold trigger spam, |
| 259 | // if value is noisy while hovering very close to a threshold. |
| 260 | // When a threshold is crossed, indicate true immediately, |
| 261 | // but require more distance to be crossed the other direction, |
| 262 | // before resetting the indicator back to false. |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 263 | if (threshold.direction == thresholds::Direction::HIGH) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 264 | { |
James Feist | 551087a | 2019-12-09 11:17:12 -0800 | [diff] [blame] | 265 | if (value >= threshold.value) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 266 | { |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 267 | thresholdChanges.push_back(std::make_pair(threshold, true)); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 268 | ++cHiTrue; |
| 269 | } |
| 270 | else if (value < (threshold.value - sensor->hysteresisTrigger)) |
| 271 | { |
| 272 | thresholdChanges.push_back(std::make_pair(threshold, false)); |
| 273 | ++cHiFalse; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 274 | } |
Patrick Venture | 66235d4 | 2019-10-11 08:31:27 -0700 | [diff] [blame] | 275 | else |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 276 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 277 | ++cHiMidstate; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 280 | else if (threshold.direction == thresholds::Direction::LOW) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 281 | { |
James Feist | 551087a | 2019-12-09 11:17:12 -0800 | [diff] [blame] | 282 | if (value <= threshold.value) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 283 | { |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 284 | thresholdChanges.push_back(std::make_pair(threshold, true)); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 285 | ++cLoTrue; |
| 286 | } |
| 287 | else if (value > (threshold.value + sensor->hysteresisTrigger)) |
| 288 | { |
| 289 | thresholdChanges.push_back(std::make_pair(threshold, false)); |
| 290 | ++cLoFalse; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 291 | } |
Patrick Venture | 66235d4 | 2019-10-11 08:31:27 -0700 | [diff] [blame] | 292 | else |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 293 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 294 | ++cLoMidstate; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 297 | else |
| 298 | { |
| 299 | std::cerr << "Error determining threshold direction\n"; |
| 300 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 301 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 302 | |
| 303 | if constexpr (DEBUG) |
| 304 | { |
| 305 | // Throttle debug output, so that it does not continuously spam |
| 306 | ++cDebugThrottle; |
| 307 | if (cDebugThrottle >= 1000) |
| 308 | { |
| 309 | cDebugThrottle = 0; |
| 310 | std::cerr << "checkThresholds: High T=" << cHiTrue |
| 311 | << " F=" << cHiFalse << " M=" << cHiMidstate |
| 312 | << ", Low T=" << cLoTrue << " F=" << cLoFalse |
| 313 | << " M=" << cLoMidstate << "\n"; |
| 314 | } |
| 315 | } |
| 316 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 317 | return thresholdChanges; |
| 318 | } |
| 319 | |
| 320 | bool checkThresholds(Sensor* sensor) |
| 321 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 322 | bool status = true; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 323 | std::vector<std::pair<Threshold, bool>> changes = |
| 324 | checkThresholds(sensor, sensor->value); |
| 325 | for (const auto& [threshold, asserted] : changes) |
| 326 | { |
| 327 | assertThresholds(sensor, threshold.level, threshold.direction, |
| 328 | asserted); |
| 329 | if (threshold.level == thresholds::Level::CRITICAL && asserted) |
| 330 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 331 | status = false; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 335 | return status; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 336 | } |
| 337 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 338 | void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer) |
| 339 | { |
| 340 | |
| 341 | std::vector<std::pair<Threshold, bool>> changes = |
| 342 | checkThresholds(sensor, sensor->value); |
| 343 | for (const auto& [threshold, asserted] : changes) |
| 344 | { |
| 345 | if (asserted) |
| 346 | { |
| 347 | thresholdTimer.startTimer(threshold); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | assertThresholds(sensor, threshold.level, threshold.direction, |
| 352 | false); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 357 | void assertThresholds(Sensor* sensor, thresholds::Level level, |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 358 | thresholds::Direction direction, bool assert) |
| 359 | { |
| 360 | std::string property; |
| 361 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface; |
| 362 | if (level == thresholds::Level::WARNING && |
| 363 | direction == thresholds::Direction::HIGH) |
| 364 | { |
| 365 | property = "WarningAlarmHigh"; |
| 366 | interface = sensor->thresholdInterfaceWarning; |
| 367 | } |
| 368 | else if (level == thresholds::Level::WARNING && |
| 369 | direction == thresholds::Direction::LOW) |
| 370 | { |
| 371 | property = "WarningAlarmLow"; |
| 372 | interface = sensor->thresholdInterfaceWarning; |
| 373 | } |
| 374 | else if (level == thresholds::Level::CRITICAL && |
| 375 | direction == thresholds::Direction::HIGH) |
| 376 | { |
| 377 | property = "CriticalAlarmHigh"; |
| 378 | interface = sensor->thresholdInterfaceCritical; |
| 379 | } |
| 380 | else if (level == thresholds::Level::CRITICAL && |
| 381 | direction == thresholds::Direction::LOW) |
| 382 | { |
| 383 | property = "CriticalAlarmLow"; |
| 384 | interface = sensor->thresholdInterfaceCritical; |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | std::cerr << "Unknown threshold, level " << level << "direction " |
| 389 | << direction << "\n"; |
| 390 | return; |
| 391 | } |
| 392 | if (!interface) |
| 393 | { |
| 394 | std::cout << "trying to set uninitialized interface\n"; |
| 395 | return; |
| 396 | } |
| 397 | interface->set_property(property, assert); |
| 398 | } |
| 399 | |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 400 | std::optional<double> readFile(const std::string& thresholdFile, |
| 401 | const double& scaleFactor) |
| 402 | { |
| 403 | std::string line; |
| 404 | std::ifstream labelFile(thresholdFile); |
| 405 | if (labelFile.good()) |
| 406 | { |
| 407 | std::getline(labelFile, line); |
| 408 | labelFile.close(); |
| 409 | |
| 410 | try |
| 411 | { |
| 412 | return std::stod(line) / scaleFactor; |
| 413 | } |
| 414 | catch (const std::invalid_argument&) |
| 415 | { |
| 416 | return std::nullopt; |
| 417 | } |
| 418 | } |
| 419 | return std::nullopt; |
| 420 | } |
| 421 | |
| 422 | std::optional<std::tuple<std::string, std::string, std::string>> |
| 423 | splitFileName(const std::filesystem::path& filePath) |
| 424 | { |
| 425 | if (filePath.has_filename()) |
| 426 | { |
| 427 | const auto fileName = filePath.filename().string(); |
| 428 | const std::regex rx(R"((\w+)(\d+)_(.*))"); |
| 429 | std::smatch mr; |
| 430 | |
| 431 | if (std::regex_search(fileName, mr, rx)) |
| 432 | { |
| 433 | if (mr.size() == 4) |
| 434 | { |
| 435 | return std::make_optional(std::make_tuple(mr[1], mr[2], mr[3])); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | return std::nullopt; |
| 440 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 441 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 442 | bool parseThresholdsFromAttr( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 443 | std::vector<thresholds::Threshold>& thresholdVector, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 444 | const std::string& inputPath, const double& scaleFactor, |
| 445 | const double& offset) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 446 | { |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 447 | const boost::container::flat_map< |
| 448 | std::string, std::vector<std::tuple<const char*, thresholds::Level, |
| 449 | thresholds::Direction, double>>> |
| 450 | map = { |
| 451 | {"average", |
| 452 | { |
| 453 | std::make_tuple("average_min", Level::WARNING, Direction::LOW, |
| 454 | 0.0), |
| 455 | std::make_tuple("average_max", Level::WARNING, Direction::HIGH, |
| 456 | 0.0), |
| 457 | }}, |
| 458 | {"input", |
| 459 | { |
| 460 | std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0), |
| 461 | std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0), |
| 462 | std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0), |
| 463 | std::make_tuple("crit", Level::CRITICAL, Direction::HIGH, |
| 464 | offset), |
| 465 | }}, |
| 466 | }; |
| 467 | |
| 468 | if (auto fileParts = splitFileName(inputPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 469 | { |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 470 | auto [type, nr, item] = *fileParts; |
| 471 | if (map.count(item) != 0) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 472 | { |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 473 | for (const auto& t : map.at(item)) |
| 474 | { |
| 475 | auto [suffix, level, direction, offset] = t; |
| 476 | auto attrPath = |
| 477 | boost::replace_all_copy(inputPath, item, suffix); |
| 478 | if (auto val = readFile(attrPath, scaleFactor)) |
| 479 | { |
| 480 | *val += offset; |
| 481 | if (DEBUG) |
| 482 | { |
| 483 | std::cout << "Threshold: " << attrPath << ": " << *val |
| 484 | << "\n"; |
| 485 | } |
| 486 | thresholdVector.emplace_back(level, direction, *val); |
| 487 | } |
| 488 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 489 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 490 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 491 | return true; |
| 492 | } |
| 493 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 494 | bool hasCriticalInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 495 | const std::vector<thresholds::Threshold>& thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 496 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 497 | for (auto& threshold : thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 498 | { |
| 499 | if (threshold.level == Level::CRITICAL) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 500 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 501 | return true; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 502 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 503 | } |
| 504 | return false; |
| 505 | } |
| 506 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 507 | bool hasWarningInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 508 | const std::vector<thresholds::Threshold>& thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 509 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 510 | for (auto& threshold : thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 511 | { |
| 512 | if (threshold.level == Level::WARNING) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 513 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 514 | return true; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 515 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 516 | } |
| 517 | return false; |
| 518 | } |
| 519 | } // namespace thresholds |