blob: bbe8e20972812bc4f2a99335b0d995c663f7bf50 [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
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700364void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor,
365 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800366 double assertValue)
367{
368 struct TimerUsed timerUsed = {};
369 constexpr const size_t waitTime = 5;
370 TimerPair* pair = nullptr;
371
372 for (TimerPair& timer : timers)
373 {
374 if (!timer.first.used)
375 {
376 pair = &timer;
377 break;
378 }
379 }
380 if (pair == nullptr)
381 {
382 pair = &timers.emplace_back(timerUsed, boost::asio::deadline_timer(io));
383 }
384
385 pair->first.used = true;
386 pair->first.level = threshold.level;
387 pair->first.direction = threshold.direction;
388 pair->first.assert = assert;
389 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700390 pair->second.async_wait([weakSensor, pair, threshold, assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800391 assertValue](boost::system::error_code ec) {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700392 auto sensorPtr = weakSensor.lock();
393 if (!sensorPtr)
394 {
395 return; // owner sensor has been destructed
396 }
397 // pair is valid as long as sensor is valid
Jeff Lind9cd7042020-11-20 15:49:28 +0800398 pair->first.used = false;
399
400 if (ec == boost::asio::error::operation_aborted)
401 {
402 return; // we're being canceled
403 }
404 if (ec)
405 {
406 std::cerr << "timer error: " << ec.message() << "\n";
407 return;
408 }
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700409 if (sensorPtr->readingStateGood())
Jeff Lind9cd7042020-11-20 15:49:28 +0800410 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700411 assertThresholds(sensorPtr.get(), assertValue, threshold.level,
Jeff Lind9cd7042020-11-20 15:49:28 +0800412 threshold.direction, assert);
413 }
414 });
415}
416
James Feist46342ec2019-03-06 14:03:41 -0800417bool checkThresholds(Sensor* sensor)
418{
James Feist7b18b1e2019-05-14 13:42:09 -0700419 bool status = true;
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700420 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
421 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800422 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700423 assertThresholds(sensor, change.assertValue, change.threshold.level,
424 change.threshold.direction, change.asserted);
425 if (change.threshold.level == thresholds::Level::CRITICAL &&
426 change.asserted)
James Feist46342ec2019-03-06 14:03:41 -0800427 {
James Feist7b18b1e2019-05-14 13:42:09 -0700428 status = false;
James Feist46342ec2019-03-06 14:03:41 -0800429 }
430 }
431
James Feistdc6c55f2018-10-31 12:53:20 -0700432 return status;
James Feist251c7822018-09-12 12:54:15 -0700433}
434
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700435void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
436 ThresholdTimer& thresholdTimer)
James Feist46342ec2019-03-06 14:03:41 -0800437{
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700438 auto sensorPtr = weakSensor.lock();
439 if (!sensorPtr)
440 {
441 return; // sensor is destructed, should never be here
442 }
James Feist46342ec2019-03-06 14:03:41 -0800443
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700444 Sensor* sensor = sensorPtr.get();
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700445 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
446 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800447 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700448 // When CPU is powered off, some volatges are expected to
449 // go below low thresholds. Filter these events with thresholdTimer.
450 // 1. always delay the assertion of low events to see if they are
451 // caused by power off event.
452 // 2. conditional delay the de-assertion of low events if there is
453 // an existing timer for assertion.
454 // 3. no delays for de-assert of low events if there is an existing
455 // de-assert for low event. This means 2nd de-assert would happen
456 // first and when timer expires for the previous one, no additional
457 // signal will be logged.
458 // 4. no delays for all high events.
459 if (change.threshold.direction == thresholds::Direction::LOW)
James Feist46342ec2019-03-06 14:03:41 -0800460 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700461 if (change.asserted || thresholdTimer.hasActiveTimer(
462 change.threshold, !change.asserted))
463 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700464 thresholdTimer.startTimer(weakSensor, change.threshold,
465 change.asserted, change.assertValue);
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700466 continue;
467 }
James Feist46342ec2019-03-06 14:03:41 -0800468 }
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700469 assertThresholds(sensor, change.assertValue, change.threshold.level,
470 change.threshold.direction, change.asserted);
James Feist46342ec2019-03-06 14:03:41 -0800471 }
472}
473
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700474void assertThresholds(Sensor* sensor, double assertValue,
475 thresholds::Level level, thresholds::Direction direction,
476 bool assert)
James Feist251c7822018-09-12 12:54:15 -0700477{
478 std::string property;
479 std::shared_ptr<sdbusplus::asio::dbus_interface> interface;
480 if (level == thresholds::Level::WARNING &&
481 direction == thresholds::Direction::HIGH)
482 {
483 property = "WarningAlarmHigh";
484 interface = sensor->thresholdInterfaceWarning;
485 }
486 else if (level == thresholds::Level::WARNING &&
487 direction == thresholds::Direction::LOW)
488 {
489 property = "WarningAlarmLow";
490 interface = sensor->thresholdInterfaceWarning;
491 }
492 else if (level == thresholds::Level::CRITICAL &&
493 direction == thresholds::Direction::HIGH)
494 {
495 property = "CriticalAlarmHigh";
496 interface = sensor->thresholdInterfaceCritical;
497 }
498 else if (level == thresholds::Level::CRITICAL &&
499 direction == thresholds::Direction::LOW)
500 {
501 property = "CriticalAlarmLow";
502 interface = sensor->thresholdInterfaceCritical;
503 }
504 else
505 {
506 std::cerr << "Unknown threshold, level " << level << "direction "
507 << direction << "\n";
508 return;
509 }
510 if (!interface)
511 {
512 std::cout << "trying to set uninitialized interface\n";
513 return;
514 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700515
516 if (interface->set_property<bool, true>(property, assert))
517 {
518 try
519 {
520 // msg.get_path() is interface->get_object_path()
521 sdbusplus::message::message msg =
522 interface->new_signal("ThresholdAsserted");
523
524 msg.append(sensor->name, interface->get_interface_name(), property,
525 assert, assertValue);
526 msg.signal_send();
527 }
528 catch (const sdbusplus::exception::exception& e)
529 {
530 std::cerr
531 << "Failed to send thresholdAsserted signal with assertValue\n";
532 }
533 }
James Feist251c7822018-09-12 12:54:15 -0700534}
535
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700536bool parseThresholdsFromAttr(
James Feistd8705872019-02-08 13:26:09 -0800537 std::vector<thresholds::Threshold>& thresholdVector,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700538 const std::string& inputPath, const double& scaleFactor,
539 const double& offset)
James Feist6714a252018-09-10 15:26:18 -0700540{
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200541 const boost::container::flat_map<
542 std::string, std::vector<std::tuple<const char*, thresholds::Level,
543 thresholds::Direction, double>>>
544 map = {
545 {"average",
546 {
547 std::make_tuple("average_min", Level::WARNING, Direction::LOW,
548 0.0),
549 std::make_tuple("average_max", Level::WARNING, Direction::HIGH,
550 0.0),
551 }},
552 {"input",
553 {
554 std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0),
555 std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0),
556 std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0),
557 std::make_tuple("crit", Level::CRITICAL, Direction::HIGH,
558 offset),
559 }},
560 };
561
562 if (auto fileParts = splitFileName(inputPath))
James Feist6714a252018-09-10 15:26:18 -0700563 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200564 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200565 if (map.count(item) != 0)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700566 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200567 for (const auto& t : map.at(item))
568 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200569 auto& [suffix, level, direction, offset] = t;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200570 auto attrPath =
571 boost::replace_all_copy(inputPath, item, suffix);
572 if (auto val = readFile(attrPath, scaleFactor))
573 {
574 *val += offset;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700575 if (debug)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200576 {
577 std::cout << "Threshold: " << attrPath << ": " << *val
578 << "\n";
579 }
580 thresholdVector.emplace_back(level, direction, *val);
581 }
582 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700583 }
James Feist6714a252018-09-10 15:26:18 -0700584 }
James Feist6714a252018-09-10 15:26:18 -0700585 return true;
586}
587
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700588bool hasCriticalInterface(
James Feistd8705872019-02-08 13:26:09 -0800589 const std::vector<thresholds::Threshold>& thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700590{
James Feistd8705872019-02-08 13:26:09 -0800591 for (auto& threshold : thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700592 {
593 if (threshold.level == Level::CRITICAL)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700594 {
James Feist6714a252018-09-10 15:26:18 -0700595 return true;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700596 }
James Feist6714a252018-09-10 15:26:18 -0700597 }
598 return false;
599}
600
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700601bool hasWarningInterface(
James Feistd8705872019-02-08 13:26:09 -0800602 const std::vector<thresholds::Threshold>& thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700603{
James Feistd8705872019-02-08 13:26:09 -0800604 for (auto& threshold : thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700605 {
606 if (threshold.level == Level::WARNING)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700607 {
James Feist6714a252018-09-10 15:26:18 -0700608 return true;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700609 }
James Feist6714a252018-09-10 15:26:18 -0700610 }
611 return false;
612}
613} // namespace thresholds