Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
fix (anomalydetection): Add unit to anomaly thresholds curves
Browse files Browse the repository at this point in the history
REFS: 10959
  • Loading branch information
rem31 authored and bouda1 committed Oct 13, 2021
1 parent bc841f5 commit a312bf8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

### Bugs

*Downtime*
*Anomalydetection*

Change downtime limits start/end dates to 2145916799
(31/12/2037 23:59:59) max.
Add unit to anomaly thresholds curves

*logs*

Expand Down
11 changes: 6 additions & 5 deletions src/anomalydetection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,8 @@ int anomalydetection::run_async_check(int check_options,
<< " is " << std::get<1>(pd) << std::get<2>(pd);
if (!std::isnan(std::get<3>(pd)) && !std::isnan(std::get<4>(pd)))
oss << " which is outside the forecasting range [" << std::get<3>(pd)
<< " ; " << std::get<4>(pd) << "] |";
<< std::get<2>(pd) << " ; " << std::get<4>(pd) << std::get<2>(pd)
<< "] |";
else
oss << " and the forecasting range is unknown |";
}
Expand Down Expand Up @@ -767,8 +768,7 @@ void anomalydetection::init_thresholds() {
nlohmann::json json;
try {
json = nlohmann::json::parse(buffer.str());
}
catch (const nlohmann::json::parse_error& e) {
} catch (const nlohmann::json::parse_error& e) {
logger(log_config_error, basic) << "Error: the file '" << _thresholds_file
<< "' contains errors: " << e.what();
return;
Expand Down Expand Up @@ -842,8 +842,9 @@ int anomalydetection::update_thresholds(const std::string& filename) {
try {
json = nlohmann::json::parse(buffer.str());
} catch (const nlohmann::json::parse_error& e) {
logger(log_config_error, basic) << "Error: The thresholds file '"
<< filename << "' should be a json file: " << e.what();
logger(log_config_error, basic)
<< "Error: The thresholds file '" << filename
<< "' should be a json file: " << e.what();
return -2;
}

Expand Down
63 changes: 41 additions & 22 deletions tests/checks/anomalydetection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

#include "com/centreon/engine/configuration/applier/anomalydetection.hh"

#include <fmt/format.h>
#include <gtest/gtest.h>
#include <time.h>
#include <fmt/format.h>

#include <cstring>
#include <memory>
Expand Down Expand Up @@ -199,8 +199,10 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {
set_time(51000);

now = std::time(nullptr);
cmd = fmt::format("[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;1;service warning| "
"metric=50;25;60", now);
cmd = fmt::format(
"[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;1;service warning| "
"metric=50;25;60",
now);
process_external_command(cmd.c_str());
checks::checker::instance().reap();
ASSERT_EQ(_svc->get_state_type(), checkable::soft);
Expand All @@ -224,8 +226,11 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {

now = std::time(nullptr);
time_t previous = now;
cmd = fmt::format("[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;2;service critical| "
"metric=110foo;25;60", now);
cmd = fmt::format(
"[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;2;service "
"critical| "
"metric=110foo;25;60",
now);
process_external_command(cmd.c_str());
checks::checker::instance().reap();
ASSERT_EQ(_svc->get_state_type(), checkable::hard);
Expand All @@ -240,7 +245,7 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {
ASSERT_EQ(_ad->get_state_type(), checkable::hard);
ASSERT_EQ(_ad->get_plugin_output(),
"NON-OK: Unusual activity, the actual value of metric is 110.00foo "
"which is outside the forecasting range [71.93 : 81.78]");
"which is outside the forecasting range [71.93foo : 81.78foo]");
ASSERT_EQ(_ad->get_perf_data(),
"metric=110foo metric_lower_thresholds=71.93foo "
"metric_upper_thresholds=81.78foo");
Expand All @@ -249,8 +254,10 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {
set_time(52000);

now = std::time(nullptr);
cmd = fmt::format("[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;1;service warning| "
"metric=30%;25;60", now);
cmd = fmt::format(
"[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;1;service warning| "
"metric=30%;25;60",
now);
process_external_command(cmd.c_str());
checks::checker::instance().reap();
ASSERT_EQ(_svc->get_state_type(), checkable::hard);
Expand All @@ -265,7 +272,7 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {
ASSERT_EQ(_ad->get_last_hard_state_change(), previous);
ASSERT_EQ(_ad->get_plugin_output(),
"NON-OK: Unusual activity, the actual value of metric is 30.00% "
"which is outside the forecasting range [71.24 : 81.04]");
"which is outside the forecasting range [71.24% : 81.04%]");
ASSERT_EQ(_ad->get_perf_data(),
"metric=30% metric_lower_thresholds=71.24% "
"metric_upper_thresholds=81.04%");
Expand All @@ -275,8 +282,10 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {

previous = now;
now = std::time(nullptr);
cmd = fmt::format("[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;1;service warning| "
"metric=35%;25;60", now);
cmd = fmt::format(
"[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;1;service warning| "
"metric=35%;25;60",
now);
process_external_command(cmd.c_str());
checks::checker::instance().reap();
ASSERT_EQ(_svc->get_state_type(), checkable::hard);
Expand All @@ -292,7 +301,7 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {
ASSERT_EQ(_ad->get_current_state(), engine::service::state_critical);
ASSERT_EQ(_ad->get_plugin_output(),
"NON-OK: Unusual activity, the actual value of metric is 35.00% "
"which is outside the forecasting range [70.55 : 80.30]");
"which is outside the forecasting range [70.55% : 80.30%]");
ASSERT_EQ(_ad->get_perf_data(),
"metric=35% metric_lower_thresholds=70.55% "
"metric_upper_thresholds=80.30%");
Expand All @@ -302,8 +311,10 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {

previous = now;
now = std::time(nullptr);
cmd = fmt::format("[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;0;service ok| "
"metric=70%;80;90", now);
cmd = fmt::format(
"[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;0;service ok| "
"metric=70%;80;90",
now);
process_external_command(cmd.c_str());
checks::checker::instance().reap();
ASSERT_EQ(_svc->get_state_type(), checkable::hard);
Expand All @@ -326,8 +337,10 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {

previous = now;
now = std::time(nullptr);
cmd = fmt::format("[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;0;service ok| "
"metric=71%;80;90", now);
cmd = fmt::format(
"[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;0;service ok| "
"metric=71%;80;90",
now);
process_external_command(cmd.c_str());
checks::checker::instance().reap();
ASSERT_EQ(_svc->get_state_type(), checkable::hard);
Expand All @@ -350,7 +363,9 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {

previous = now;
now = std::time(nullptr);
cmd = fmt::format("[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;4;service unknown", now);
cmd = fmt::format(
"[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;4;service unknown",
now);
process_external_command(cmd.c_str());
checks::checker::instance().reap();
ASSERT_EQ(_svc->get_state_type(), checkable::soft);
Expand All @@ -372,8 +387,10 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {

previous = now;
now = std::time(nullptr);
cmd = fmt::format("[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;0;service ok| "
"metric=72%;80;90", now);
cmd = fmt::format(
"[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;0;service ok| "
"metric=72%;80;90",
now);
process_external_command(cmd.c_str());
checks::checker::instance().reap();
ASSERT_EQ(_svc->get_state_type(), checkable::soft);
Expand All @@ -396,8 +413,10 @@ TEST_F(AnomalydetectionCheck, StatusChanges) {

previous = now;
now = std::time(nullptr);
cmd = fmt::format("[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;0;service ok| "
"metric=71.7%;80;90;10;100", now);
cmd = fmt::format(
"[{}] PROCESS_SERVICE_CHECK_RESULT;test_host;test_svc;0;service ok| "
"metric=71.7%;80;90;10;100",
now);
process_external_command(cmd.c_str());
checks::checker::instance().reap();
ASSERT_EQ(_svc->get_state_type(), checkable::hard);
Expand Down Expand Up @@ -476,7 +495,7 @@ TEST_F(AnomalydetectionCheck, MetricWithQuotes) {
ASSERT_EQ(_ad->get_current_attempt(), 1);
ASSERT_EQ(_ad->get_plugin_output(),
"NON-OK: Unusual activity, the actual value of metric is 90.00MT "
"which is outside the forecasting range [73.31 : 83.26]");
"which is outside the forecasting range [73.31MT : 83.26MT]");
ASSERT_EQ(_ad->get_perf_data(),
"'metric'=90MT;;;0;100 metric_lower_thresholds=73.31MT;;;0;100 "
"metric_upper_thresholds=83.26MT;;;0;100");
Expand Down

0 comments on commit a312bf8

Please sign in to comment.