James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #include <Thresholds.hpp> |
| 2 | #include <VariantVisitors.hpp> |
| 3 | #include <boost/algorithm/string/replace.hpp> |
| 4 | #include <boost/lexical_cast.hpp> |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 5 | #include <cmath> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 6 | #include <fstream> |
| 7 | #include <iostream> |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 8 | #include <sensor.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 9 | |
| 10 | static constexpr bool DEBUG = false; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 11 | static constexpr size_t maxThresholds = 4; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 12 | |
| 13 | namespace thresholds |
| 14 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 15 | unsigned int toBusValue(const Level& level) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 16 | { |
| 17 | switch (level) |
| 18 | { |
| 19 | case (Level::WARNING): |
| 20 | { |
| 21 | return 0; |
| 22 | } |
| 23 | case (Level::CRITICAL): |
| 24 | { |
| 25 | return 1; |
| 26 | } |
| 27 | default: |
| 28 | { |
| 29 | return -1; |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 34 | std::string toBusValue(const Direction& direction) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 35 | { |
| 36 | switch (direction) |
| 37 | { |
| 38 | case (Direction::LOW): |
| 39 | { |
| 40 | return "less than"; |
| 41 | } |
| 42 | case (Direction::HIGH): |
| 43 | { |
| 44 | return "greater than"; |
| 45 | } |
| 46 | default: |
| 47 | { |
| 48 | return "err"; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 53 | bool parseThresholdsFromConfig( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 54 | const SensorData& sensorData, |
| 55 | std::vector<thresholds::Threshold>& thresholdVector, |
| 56 | const std::string* matchLabel) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 57 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 58 | for (const auto& item : sensorData) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 59 | { |
| 60 | if (item.first.find("Thresholds") == std::string::npos) |
| 61 | { |
| 62 | continue; |
| 63 | } |
| 64 | if (matchLabel != nullptr) |
| 65 | { |
| 66 | auto labelFind = item.second.find("Label"); |
| 67 | if (labelFind == item.second.end()) |
| 68 | continue; |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 69 | if (std::visit(VariantToStringVisitor(), labelFind->second) != |
| 70 | *matchLabel) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 71 | continue; |
| 72 | } |
| 73 | auto directionFind = item.second.find("Direction"); |
| 74 | auto severityFind = item.second.find("Severity"); |
| 75 | auto valueFind = item.second.find("Value"); |
| 76 | if (valueFind == item.second.end() || |
| 77 | severityFind == item.second.end() || |
| 78 | directionFind == item.second.end()) |
| 79 | { |
| 80 | std::cerr << "Malformed threshold in configuration\n"; |
| 81 | return false; |
| 82 | } |
| 83 | Level level; |
| 84 | Direction direction; |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 85 | if (std::visit(VariantToUnsignedIntVisitor(), severityFind->second) == |
| 86 | 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 87 | { |
| 88 | level = Level::WARNING; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | level = Level::CRITICAL; |
| 93 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 94 | if (std::visit(VariantToStringVisitor(), directionFind->second) == |
| 95 | "less than") |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 96 | { |
| 97 | direction = Direction::LOW; |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | direction = Direction::HIGH; |
| 102 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 103 | float val = std::visit(VariantToFloatVisitor(), valueFind->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 104 | |
| 105 | thresholdVector.emplace_back(level, direction, val); |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 110 | void persistThreshold(const std::string& path, const std::string& baseInterface, |
| 111 | const thresholds::Threshold& threshold, |
| 112 | std::shared_ptr<sdbusplus::asio::connection>& conn) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 113 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 114 | for (int ii = 0; ii < maxThresholds; ii++) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 115 | { |
| 116 | std::string thresholdInterface = |
| 117 | baseInterface + ".Thresholds" + std::to_string(ii); |
| 118 | conn->async_method_call( |
| 119 | [&, path, threshold, thresholdInterface]( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 120 | const boost::system::error_code& ec, |
| 121 | const boost::container::flat_map<std::string, BasicVariantType>& |
| 122 | result) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 123 | if (ec) |
| 124 | { |
| 125 | return; // threshold not supported |
| 126 | } |
| 127 | |
| 128 | auto directionFind = result.find("Direction"); |
| 129 | auto severityFind = result.find("Severity"); |
| 130 | auto valueFind = result.find("Value"); |
| 131 | if (valueFind == result.end() || severityFind == result.end() || |
| 132 | directionFind == result.end()) |
| 133 | { |
| 134 | std::cerr << "Malformed threshold in configuration\n"; |
| 135 | return; |
| 136 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 137 | unsigned int level = std::visit(VariantToUnsignedIntVisitor(), |
| 138 | severityFind->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 139 | |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 140 | std::string dir = |
| 141 | std::visit(VariantToStringVisitor(), directionFind->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 142 | if ((toBusValue(threshold.level) != level) || |
| 143 | (toBusValue(threshold.direction) != dir)) |
| 144 | { |
| 145 | return; // not the droid we're looking for |
| 146 | } |
| 147 | |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 148 | std::variant<double> value(threshold.value); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 149 | conn->async_method_call( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 150 | [](const boost::system::error_code& ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 151 | if (ec) |
| 152 | { |
| 153 | std::cerr << "Error setting threshold " << ec |
| 154 | << "\n"; |
| 155 | } |
| 156 | }, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 157 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
| 158 | "Set", thresholdInterface, "Value", value); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 159 | }, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 160 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 161 | "GetAll", thresholdInterface); |
| 162 | } |
| 163 | } |
| 164 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 165 | bool checkThresholds(Sensor* sensor) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 166 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 167 | bool status = true; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 168 | |
| 169 | if (sensor->thresholds.empty()) |
| 170 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 171 | return true; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 172 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 173 | for (auto& threshold : sensor->thresholds) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 174 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 175 | if (std::isnan(sensor->value)) |
| 176 | { |
| 177 | threshold.asserted = false; |
| 178 | } |
| 179 | else if (threshold.direction == thresholds::Direction::HIGH) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 180 | { |
| 181 | if (sensor->value > threshold.value && !threshold.asserted) |
| 182 | { |
| 183 | assertThresholds(sensor, threshold.level, threshold.direction, |
| 184 | true); |
| 185 | threshold.asserted = true; |
| 186 | } |
| 187 | else if (sensor->value <= threshold.value && threshold.asserted) |
| 188 | { |
| 189 | assertThresholds(sensor, threshold.level, threshold.direction, |
| 190 | false); |
| 191 | threshold.asserted = false; |
| 192 | } |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | if (sensor->value < threshold.value && !threshold.asserted) |
| 197 | { |
| 198 | assertThresholds(sensor, threshold.level, threshold.direction, |
| 199 | true); |
| 200 | threshold.asserted = true; |
| 201 | } |
| 202 | else if (sensor->value >= threshold.value && threshold.asserted) |
| 203 | { |
| 204 | assertThresholds(sensor, threshold.level, threshold.direction, |
| 205 | false); |
| 206 | threshold.asserted = false; |
| 207 | } |
| 208 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 209 | if (threshold.level == thresholds::Level::CRITICAL && |
| 210 | threshold.asserted) |
| 211 | { |
| 212 | status = false; |
| 213 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 214 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 215 | return status; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 216 | } |
| 217 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 218 | void assertThresholds(Sensor* sensor, thresholds::Level level, |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 219 | thresholds::Direction direction, bool assert) |
| 220 | { |
| 221 | std::string property; |
| 222 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface; |
| 223 | if (level == thresholds::Level::WARNING && |
| 224 | direction == thresholds::Direction::HIGH) |
| 225 | { |
| 226 | property = "WarningAlarmHigh"; |
| 227 | interface = sensor->thresholdInterfaceWarning; |
| 228 | } |
| 229 | else if (level == thresholds::Level::WARNING && |
| 230 | direction == thresholds::Direction::LOW) |
| 231 | { |
| 232 | property = "WarningAlarmLow"; |
| 233 | interface = sensor->thresholdInterfaceWarning; |
| 234 | } |
| 235 | else if (level == thresholds::Level::CRITICAL && |
| 236 | direction == thresholds::Direction::HIGH) |
| 237 | { |
| 238 | property = "CriticalAlarmHigh"; |
| 239 | interface = sensor->thresholdInterfaceCritical; |
| 240 | } |
| 241 | else if (level == thresholds::Level::CRITICAL && |
| 242 | direction == thresholds::Direction::LOW) |
| 243 | { |
| 244 | property = "CriticalAlarmLow"; |
| 245 | interface = sensor->thresholdInterfaceCritical; |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | std::cerr << "Unknown threshold, level " << level << "direction " |
| 250 | << direction << "\n"; |
| 251 | return; |
| 252 | } |
| 253 | if (!interface) |
| 254 | { |
| 255 | std::cout << "trying to set uninitialized interface\n"; |
| 256 | return; |
| 257 | } |
| 258 | interface->set_property(property, assert); |
| 259 | } |
| 260 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 261 | static constexpr std::array<const char*, 4> attrTypes = {"lcrit", "min", "max", |
| 262 | "crit"}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 263 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 264 | bool parseThresholdsFromAttr( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 265 | std::vector<thresholds::Threshold>& thresholdVector, |
| 266 | const std::string& inputPath, const double& scaleFactor) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 267 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 268 | for (auto& type : attrTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 269 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 270 | auto attrPath = boost::replace_all_copy(inputPath, "input", type); |
| 271 | std::ifstream attrFile(attrPath); |
| 272 | if (!attrFile.good()) |
| 273 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 274 | continue; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 275 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 276 | std::string attr; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 277 | std::getline(attrFile, attr); |
| 278 | attrFile.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 279 | |
| 280 | Level level; |
| 281 | Direction direction; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 282 | double val = std::stod(attr) / scaleFactor; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 283 | if (type == "min" || type == "max") |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 284 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 285 | level = Level::WARNING; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 286 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 287 | else |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 288 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 289 | level = Level::CRITICAL; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 290 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 291 | if (type == "min" || type == "lcrit") |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 292 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 293 | direction = Direction::LOW; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 294 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 295 | else |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 296 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 297 | direction = Direction::HIGH; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 298 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 299 | |
| 300 | if (DEBUG) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 301 | { |
| 302 | std::cout << "Threshold: " << attrPath << ": " << val << "\n"; |
| 303 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 304 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 305 | thresholdVector.emplace_back(level, direction, val); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 306 | } |
| 307 | // no thresholds is allowed, not an error so return true always |
| 308 | return true; |
| 309 | } |
| 310 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 311 | bool hasCriticalInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 312 | const std::vector<thresholds::Threshold>& thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 313 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 314 | for (auto& threshold : thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 315 | { |
| 316 | if (threshold.level == Level::CRITICAL) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 317 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 318 | return true; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 319 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 320 | } |
| 321 | return false; |
| 322 | } |
| 323 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 324 | bool hasWarningInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 325 | const std::vector<thresholds::Threshold>& thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 326 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 327 | for (auto& threshold : thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 328 | { |
| 329 | if (threshold.level == Level::WARNING) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 330 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 331 | return true; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 332 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 333 | } |
| 334 | return false; |
| 335 | } |
| 336 | } // namespace thresholds |