blob: a74ec4a838eb464fba0aa4dfbf8b74e8394f6fc9 [file] [log] [blame]
Ed Tanous8a57ec02020-10-09 12:46:52 -07001#include <Thresholds.hpp>
2#include <VariantVisitors.hpp>
James Feist6714a252018-09-10 15:26:18 -07003#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -07004#include <boost/container/flat_map.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -07005#include <sensor.hpp>
James Feist38fb5982020-05-28 10:09:54 -07006
7#include <array>
James Feistdc6c55f2018-10-31 12:53:20 -07008#include <cmath>
James Feist6714a252018-09-10 15:26:18 -07009#include <fstream>
10#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070011#include <memory>
12#include <stdexcept>
13#include <string>
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020014#include <tuple>
Patrick Venture96e97db2019-10-31 13:44:38 -070015#include <utility>
16#include <variant>
17#include <vector>
James Feist6714a252018-09-10 15:26:18 -070018
Ed Tanous8a57ec02020-10-09 12:46:52 -070019static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070020namespace thresholds
21{
James Feistd8705872019-02-08 13:26:09 -080022unsigned int toBusValue(const Level& level)
James Feist6714a252018-09-10 15:26:18 -070023{
24 switch (level)
25 {
26 case (Level::WARNING):
27 {
28 return 0;
29 }
30 case (Level::CRITICAL):
31 {
32 return 1;
33 }
34 default:
35 {
36 return -1;
37 }
38 }
39}
40
James Feistd8705872019-02-08 13:26:09 -080041std::string toBusValue(const Direction& direction)
James Feist6714a252018-09-10 15:26:18 -070042{
43 switch (direction)
44 {
45 case (Direction::LOW):
46 {
47 return "less than";
48 }
49 case (Direction::HIGH):
50 {
51 return "greater than";
52 }
53 default:
54 {
55 return "err";
56 }
57 }
58}
59
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070060bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -080061 const SensorData& sensorData,
62 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -050063 const std::string* matchLabel, const int* sensorIndex)
James Feist6714a252018-09-10 15:26:18 -070064{
James Feistd8705872019-02-08 13:26:09 -080065 for (const auto& item : sensorData)
James Feist6714a252018-09-10 15:26:18 -070066 {
67 if (item.first.find("Thresholds") == std::string::npos)
68 {
69 continue;
70 }
71 if (matchLabel != nullptr)
72 {
73 auto labelFind = item.second.find("Label");
74 if (labelFind == item.second.end())
Ed Tanous8a57ec02020-10-09 12:46:52 -070075 {
James Feist6714a252018-09-10 15:26:18 -070076 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070077 }
James Feist3eb82622019-02-08 13:10:22 -080078 if (std::visit(VariantToStringVisitor(), labelFind->second) !=
79 *matchLabel)
Ed Tanous8a57ec02020-10-09 12:46:52 -070080 {
James Feist6714a252018-09-10 15:26:18 -070081 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070082 }
James Feist6714a252018-09-10 15:26:18 -070083 }
Matt Spinler5636d522021-03-17 14:52:18 -050084
85 if (sensorIndex != nullptr)
86 {
87 auto indexFind = item.second.find("Index");
88
89 // If we're checking for index 1, a missing Index is OK.
90 if ((indexFind == item.second.end()) && (*sensorIndex != 1))
91 {
92 continue;
93 }
94
95 if ((indexFind != item.second.end()) &&
96 (std::visit(VariantToIntVisitor(), indexFind->second) !=
97 *sensorIndex))
98 {
99 continue;
100 }
101 }
102
James Feist6714a252018-09-10 15:26:18 -0700103 auto directionFind = item.second.find("Direction");
104 auto severityFind = item.second.find("Severity");
105 auto valueFind = item.second.find("Value");
106 if (valueFind == item.second.end() ||
107 severityFind == item.second.end() ||
108 directionFind == item.second.end())
109 {
Matt Spinler5636d522021-03-17 14:52:18 -0500110 std::cerr << "Malformed threshold on configuration interface "
111 << item.first << "\n";
James Feist6714a252018-09-10 15:26:18 -0700112 return false;
113 }
114 Level level;
115 Direction direction;
James Feist3eb82622019-02-08 13:10:22 -0800116 if (std::visit(VariantToUnsignedIntVisitor(), severityFind->second) ==
117 0)
James Feist6714a252018-09-10 15:26:18 -0700118 {
119 level = Level::WARNING;
120 }
121 else
122 {
123 level = Level::CRITICAL;
124 }
James Feist3eb82622019-02-08 13:10:22 -0800125 if (std::visit(VariantToStringVisitor(), directionFind->second) ==
126 "less than")
James Feist6714a252018-09-10 15:26:18 -0700127 {
128 direction = Direction::LOW;
129 }
130 else
131 {
132 direction = Direction::HIGH;
133 }
James Feist13f340b2019-03-07 16:36:11 -0800134 double val = std::visit(VariantToDoubleVisitor(), valueFind->second);
James Feist6714a252018-09-10 15:26:18 -0700135
136 thresholdVector.emplace_back(level, direction, val);
137 }
138 return true;
139}
140
James Feistd8705872019-02-08 13:26:09 -0800141void persistThreshold(const std::string& path, const std::string& baseInterface,
142 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800143 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800144 size_t thresholdCount, const std::string& labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700145{
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500146 for (size_t ii = 0; ii < thresholdCount; ii++)
James Feist6714a252018-09-10 15:26:18 -0700147 {
148 std::string thresholdInterface =
149 baseInterface + ".Thresholds" + std::to_string(ii);
150 conn->async_method_call(
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800151 [&, path, threshold, thresholdInterface, labelMatch](
James Feistd8705872019-02-08 13:26:09 -0800152 const boost::system::error_code& ec,
153 const boost::container::flat_map<std::string, BasicVariantType>&
154 result) {
James Feist6714a252018-09-10 15:26:18 -0700155 if (ec)
156 {
157 return; // threshold not supported
158 }
159
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800160 if (!labelMatch.empty())
161 {
162 auto labelFind = result.find("Label");
163 if (labelFind == result.end())
164 {
165 std::cerr << "No label in threshold configuration\n";
166 return;
167 }
168 std::string label =
169 std::visit(VariantToStringVisitor(), labelFind->second);
170 if (label != labelMatch)
171 {
172 return;
173 }
174 }
175
James Feist6714a252018-09-10 15:26:18 -0700176 auto directionFind = result.find("Direction");
177 auto severityFind = result.find("Severity");
178 auto valueFind = result.find("Value");
179 if (valueFind == result.end() || severityFind == result.end() ||
180 directionFind == result.end())
181 {
182 std::cerr << "Malformed threshold in configuration\n";
183 return;
184 }
James Feist3eb82622019-02-08 13:10:22 -0800185 unsigned int level = std::visit(VariantToUnsignedIntVisitor(),
186 severityFind->second);
James Feist6714a252018-09-10 15:26:18 -0700187
James Feist3eb82622019-02-08 13:10:22 -0800188 std::string dir =
189 std::visit(VariantToStringVisitor(), directionFind->second);
James Feist6714a252018-09-10 15:26:18 -0700190 if ((toBusValue(threshold.level) != level) ||
191 (toBusValue(threshold.direction) != dir))
192 {
193 return; // not the droid we're looking for
194 }
195
James Feist3eb82622019-02-08 13:10:22 -0800196 std::variant<double> value(threshold.value);
James Feist6714a252018-09-10 15:26:18 -0700197 conn->async_method_call(
James Feistd8705872019-02-08 13:26:09 -0800198 [](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700199 if (ec)
200 {
201 std::cerr << "Error setting threshold " << ec
202 << "\n";
203 }
204 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700205 entityManagerName, path, "org.freedesktop.DBus.Properties",
206 "Set", thresholdInterface, "Value", value);
James Feist6714a252018-09-10 15:26:18 -0700207 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700208 entityManagerName, path, "org.freedesktop.DBus.Properties",
James Feist6714a252018-09-10 15:26:18 -0700209 "GetAll", thresholdInterface);
210 }
211}
212
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800213void updateThresholds(Sensor* sensor)
214{
215 if (sensor->thresholds.empty())
216 {
217 return;
218 }
219
220 for (const auto& threshold : sensor->thresholds)
221 {
222 std::shared_ptr<sdbusplus::asio::dbus_interface> interface;
223 std::string property;
224 if (threshold.level == thresholds::Level::CRITICAL)
225 {
226 interface = sensor->thresholdInterfaceCritical;
227 if (threshold.direction == thresholds::Direction::HIGH)
228 {
229 property = "CriticalHigh";
230 }
231 else
232 {
233 property = "CriticalLow";
234 }
235 }
236 else if (threshold.level == thresholds::Level::WARNING)
237 {
238 interface = sensor->thresholdInterfaceWarning;
239 if (threshold.direction == thresholds::Direction::HIGH)
240 {
241 property = "WarningHigh";
242 }
243 else
244 {
245 property = "WarningLow";
246 }
247 }
248 else
249 {
250 continue;
251 }
252 if (!interface)
253 {
254 continue;
255 }
256 interface->set_property(property, threshold.value);
257 }
258}
259
Josh Lehan883fb3a2020-02-27 14:41:39 -0800260// Debugging counters
261static int cHiTrue = 0;
262static int cHiFalse = 0;
263static int cHiMidstate = 0;
264static int cLoTrue = 0;
265static int cLoFalse = 0;
266static int cLoMidstate = 0;
267static int cDebugThrottle = 0;
Zhikui Rend3da1282020-09-11 17:02:01 -0700268static constexpr int assertLogCount = 10;
Josh Lehan883fb3a2020-02-27 14:41:39 -0800269
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700270struct ChangeParam
James Feist251c7822018-09-12 12:54:15 -0700271{
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700272 ChangeParam(Threshold whichThreshold, bool status, double value) :
273 threshold(whichThreshold), asserted(status), assertValue(value)
274 {}
275
276 Threshold threshold;
277 bool asserted;
278 double assertValue;
279};
280
281static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value)
282{
283 std::vector<ChangeParam> thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700284 if (sensor->thresholds.empty())
285 {
James Feist46342ec2019-03-06 14:03:41 -0800286 return thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700287 }
James Feist46342ec2019-03-06 14:03:41 -0800288
James Feistd8705872019-02-08 13:26:09 -0800289 for (auto& threshold : sensor->thresholds)
James Feist251c7822018-09-12 12:54:15 -0700290 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800291 // Use "Schmitt trigger" logic to avoid threshold trigger spam,
292 // if value is noisy while hovering very close to a threshold.
293 // When a threshold is crossed, indicate true immediately,
294 // but require more distance to be crossed the other direction,
295 // before resetting the indicator back to false.
James Feist46342ec2019-03-06 14:03:41 -0800296 if (threshold.direction == thresholds::Direction::HIGH)
James Feistdc6c55f2018-10-31 12:53:20 -0700297 {
James Feist551087a2019-12-09 11:17:12 -0800298 if (value >= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700299 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700300 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700301 if (++cHiTrue < assertLogCount)
302 {
303 std::cerr << "Sensor " << sensor->name << " high threshold "
304 << threshold.value << " assert: value " << value
305 << " raw data " << sensor->rawValue << "\n";
306 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800307 }
308 else if (value < (threshold.value - sensor->hysteresisTrigger))
309 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700310 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800311 ++cHiFalse;
James Feist251c7822018-09-12 12:54:15 -0700312 }
Patrick Venture66235d42019-10-11 08:31:27 -0700313 else
James Feist251c7822018-09-12 12:54:15 -0700314 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800315 ++cHiMidstate;
James Feist251c7822018-09-12 12:54:15 -0700316 }
317 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800318 else if (threshold.direction == thresholds::Direction::LOW)
James Feist251c7822018-09-12 12:54:15 -0700319 {
James Feist551087a2019-12-09 11:17:12 -0800320 if (value <= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700321 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700322 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700323 if (++cLoTrue < assertLogCount)
324 {
325 std::cerr << "Sensor " << sensor->name << " low threshold "
326 << threshold.value << " assert: value "
327 << sensor->value << " raw data "
328 << sensor->rawValue << "\n";
329 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800330 }
331 else if (value > (threshold.value + sensor->hysteresisTrigger))
332 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700333 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800334 ++cLoFalse;
James Feist251c7822018-09-12 12:54:15 -0700335 }
Patrick Venture66235d42019-10-11 08:31:27 -0700336 else
James Feist251c7822018-09-12 12:54:15 -0700337 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800338 ++cLoMidstate;
James Feist251c7822018-09-12 12:54:15 -0700339 }
340 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800341 else
342 {
343 std::cerr << "Error determining threshold direction\n";
344 }
James Feist251c7822018-09-12 12:54:15 -0700345 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800346
Ed Tanous8a57ec02020-10-09 12:46:52 -0700347 if constexpr (debug)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800348 {
349 // Throttle debug output, so that it does not continuously spam
350 ++cDebugThrottle;
351 if (cDebugThrottle >= 1000)
352 {
353 cDebugThrottle = 0;
354 std::cerr << "checkThresholds: High T=" << cHiTrue
355 << " F=" << cHiFalse << " M=" << cHiMidstate
356 << ", Low T=" << cLoTrue << " F=" << cLoFalse
357 << " M=" << cLoMidstate << "\n";
358 }
359 }
360
James Feist46342ec2019-03-06 14:03:41 -0800361 return thresholdChanges;
362}
363
Jeff Lind9cd7042020-11-20 15:49:28 +0800364void ThresholdTimer::startTimer(const Threshold& threshold, bool assert,
365 double assertValue)
366{
367 struct TimerUsed timerUsed = {};
368 constexpr const size_t waitTime = 5;
369 TimerPair* pair = nullptr;
370
371 for (TimerPair& timer : timers)
372 {
373 if (!timer.first.used)
374 {
375 pair = &timer;
376 break;
377 }
378 }
379 if (pair == nullptr)
380 {
381 pair = &timers.emplace_back(timerUsed, boost::asio::deadline_timer(io));
382 }
383
384 pair->first.used = true;
385 pair->first.level = threshold.level;
386 pair->first.direction = threshold.direction;
387 pair->first.assert = assert;
388 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
389 pair->second.async_wait([this, pair, threshold, assert,
390 assertValue](boost::system::error_code ec) {
391 pair->first.used = false;
392
393 if (ec == boost::asio::error::operation_aborted)
394 {
395 return; // we're being canceled
396 }
397 if (ec)
398 {
399 std::cerr << "timer error: " << ec.message() << "\n";
400 return;
401 }
402 if (sensor->readingStateGood())
403 {
404 assertThresholds(sensor, assertValue, threshold.level,
405 threshold.direction, assert);
406 }
407 });
408}
409
James Feist46342ec2019-03-06 14:03:41 -0800410bool checkThresholds(Sensor* sensor)
411{
James Feist7b18b1e2019-05-14 13:42:09 -0700412 bool status = true;
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700413 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
414 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800415 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700416 assertThresholds(sensor, change.assertValue, change.threshold.level,
417 change.threshold.direction, change.asserted);
418 if (change.threshold.level == thresholds::Level::CRITICAL &&
419 change.asserted)
James Feist46342ec2019-03-06 14:03:41 -0800420 {
James Feist7b18b1e2019-05-14 13:42:09 -0700421 status = false;
James Feist46342ec2019-03-06 14:03:41 -0800422 }
423 }
424
James Feistdc6c55f2018-10-31 12:53:20 -0700425 return status;
James Feist251c7822018-09-12 12:54:15 -0700426}
427
James Feist46342ec2019-03-06 14:03:41 -0800428void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer)
429{
430
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700431 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
432 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800433 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700434 // When CPU is powered off, some volatges are expected to
435 // go below low thresholds. Filter these events with thresholdTimer.
436 // 1. always delay the assertion of low events to see if they are
437 // caused by power off event.
438 // 2. conditional delay the de-assertion of low events if there is
439 // an existing timer for assertion.
440 // 3. no delays for de-assert of low events if there is an existing
441 // de-assert for low event. This means 2nd de-assert would happen
442 // first and when timer expires for the previous one, no additional
443 // signal will be logged.
444 // 4. no delays for all high events.
445 if (change.threshold.direction == thresholds::Direction::LOW)
James Feist46342ec2019-03-06 14:03:41 -0800446 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700447 if (change.asserted || thresholdTimer.hasActiveTimer(
448 change.threshold, !change.asserted))
449 {
450 thresholdTimer.startTimer(change.threshold, change.asserted,
451 change.assertValue);
452 continue;
453 }
James Feist46342ec2019-03-06 14:03:41 -0800454 }
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700455 assertThresholds(sensor, change.assertValue, change.threshold.level,
456 change.threshold.direction, change.asserted);
James Feist46342ec2019-03-06 14:03:41 -0800457 }
458}
459
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700460void assertThresholds(Sensor* sensor, double assertValue,
461 thresholds::Level level, thresholds::Direction direction,
462 bool assert)
James Feist251c7822018-09-12 12:54:15 -0700463{
464 std::string property;
465 std::shared_ptr<sdbusplus::asio::dbus_interface> interface;
466 if (level == thresholds::Level::WARNING &&
467 direction == thresholds::Direction::HIGH)
468 {
469 property = "WarningAlarmHigh";
470 interface = sensor->thresholdInterfaceWarning;
471 }
472 else if (level == thresholds::Level::WARNING &&
473 direction == thresholds::Direction::LOW)
474 {
475 property = "WarningAlarmLow";
476 interface = sensor->thresholdInterfaceWarning;
477 }
478 else if (level == thresholds::Level::CRITICAL &&
479 direction == thresholds::Direction::HIGH)
480 {
481 property = "CriticalAlarmHigh";
482 interface = sensor->thresholdInterfaceCritical;
483 }
484 else if (level == thresholds::Level::CRITICAL &&
485 direction == thresholds::Direction::LOW)
486 {
487 property = "CriticalAlarmLow";
488 interface = sensor->thresholdInterfaceCritical;
489 }
490 else
491 {
492 std::cerr << "Unknown threshold, level " << level << "direction "
493 << direction << "\n";
494 return;
495 }
496 if (!interface)
497 {
498 std::cout << "trying to set uninitialized interface\n";
499 return;
500 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700501
502 if (interface->set_property<bool, true>(property, assert))
503 {
504 try
505 {
506 // msg.get_path() is interface->get_object_path()
507 sdbusplus::message::message msg =
508 interface->new_signal("ThresholdAsserted");
509
510 msg.append(sensor->name, interface->get_interface_name(), property,
511 assert, assertValue);
512 msg.signal_send();
513 }
514 catch (const sdbusplus::exception::exception& e)
515 {
516 std::cerr
517 << "Failed to send thresholdAsserted signal with assertValue\n";
518 }
519 }
James Feist251c7822018-09-12 12:54:15 -0700520}
521
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700522bool parseThresholdsFromAttr(
James Feistd8705872019-02-08 13:26:09 -0800523 std::vector<thresholds::Threshold>& thresholdVector,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700524 const std::string& inputPath, const double& scaleFactor,
525 const double& offset)
James Feist6714a252018-09-10 15:26:18 -0700526{
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200527 const boost::container::flat_map<
528 std::string, std::vector<std::tuple<const char*, thresholds::Level,
529 thresholds::Direction, double>>>
530 map = {
531 {"average",
532 {
533 std::make_tuple("average_min", Level::WARNING, Direction::LOW,
534 0.0),
535 std::make_tuple("average_max", Level::WARNING, Direction::HIGH,
536 0.0),
537 }},
538 {"input",
539 {
540 std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0),
541 std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0),
542 std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0),
543 std::make_tuple("crit", Level::CRITICAL, Direction::HIGH,
544 offset),
545 }},
546 };
547
548 if (auto fileParts = splitFileName(inputPath))
James Feist6714a252018-09-10 15:26:18 -0700549 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200550 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200551 if (map.count(item) != 0)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700552 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200553 for (const auto& t : map.at(item))
554 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200555 auto& [suffix, level, direction, offset] = t;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200556 auto attrPath =
557 boost::replace_all_copy(inputPath, item, suffix);
558 if (auto val = readFile(attrPath, scaleFactor))
559 {
560 *val += offset;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700561 if (debug)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200562 {
563 std::cout << "Threshold: " << attrPath << ": " << *val
564 << "\n";
565 }
566 thresholdVector.emplace_back(level, direction, *val);
567 }
568 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700569 }
James Feist6714a252018-09-10 15:26:18 -0700570 }
James Feist6714a252018-09-10 15:26:18 -0700571 return true;
572}
573
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700574bool hasCriticalInterface(
James Feistd8705872019-02-08 13:26:09 -0800575 const std::vector<thresholds::Threshold>& thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700576{
James Feistd8705872019-02-08 13:26:09 -0800577 for (auto& threshold : thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700578 {
579 if (threshold.level == Level::CRITICAL)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700580 {
James Feist6714a252018-09-10 15:26:18 -0700581 return true;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700582 }
James Feist6714a252018-09-10 15:26:18 -0700583 }
584 return false;
585}
586
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700587bool hasWarningInterface(
James Feistd8705872019-02-08 13:26:09 -0800588 const std::vector<thresholds::Threshold>& thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700589{
James Feistd8705872019-02-08 13:26:09 -0800590 for (auto& threshold : thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700591 {
592 if (threshold.level == Level::WARNING)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700593 {
James Feist6714a252018-09-10 15:26:18 -0700594 return true;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700595 }
James Feist6714a252018-09-10 15:26:18 -0700596 }
597 return false;
598}
599} // namespace thresholds