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