Notification#BeginExecuteNotification(): discard likely duplicate problem notifications

This commit is contained in:
Alexander A. Klimov 2023-06-14 17:00:56 +02:00
parent 2cff763295
commit 44e9c6f40d
4 changed files with 111 additions and 1 deletions

View File

@ -440,6 +440,22 @@ void Notification::BeginExecuteNotification(NotificationType type, const CheckRe
}
}
if (type == NotificationProblem && !reminder && !checkable->GetVolatile()) {
auto [host, service] = GetHostService(checkable);
uint_fast8_t state = service ? service->GetState() : host->GetState();
if (state == (uint_fast8_t)GetLastNotifiedStatePerUser()->Get(userName)) {
auto stateStr (service ? NotificationServiceStateToString(service->GetState()) : NotificationHostStateToString(host->GetState()));
Log(LogNotice, "Notification")
<< "Notification object '" << notificationName << "': We already notified user '" << userName << "' for a " << stateStr
<< " problem. Likely after that another state change notification was filtered out by config. Not sending duplicate '"
<< stateStr << "' notification.";
continue;
}
}
Log(LogInformation, "Notification")
<< "Sending " << (reminder ? "reminder " : "") << "'" << NotificationTypeToString(type) << "' notification '"
<< notificationName << "' for user '" << userName << "'";

View File

@ -107,7 +107,6 @@ public:
static const std::map<String, int>& GetStateFilterMap();
static const std::map<String, int>& GetTypeFilterMap();
protected:
void OnConfigLoaded() override;
void OnAllConfigLoaded() override;
void Start(bool runtimeCreated) override;

View File

@ -138,6 +138,9 @@ add_boost_test(base
icinga_notification/strings
icinga_notification/state_filter
icinga_notification/type_filter
icinga_notification/no_filter_problem_no_duplicate
icinga_notification/filter_problem_no_duplicate
icinga_notification/volatile_filter_problem_duplicate
icinga_macros/simple
icinga_legacytimeperiod/simple
icinga_legacytimeperiod/advanced

View File

@ -1,11 +1,74 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "icinga/host.hpp"
#include "icinga/notification.hpp"
#include "icinga/notificationcommand.hpp"
#include "icinga/service.hpp"
#include "icinga/user.hpp"
#include <BoostTestTargetConfig.h>
#include <iostream>
using namespace icinga;
struct DuplicateDueToFilterHelper
{
Host::Ptr h = new Host();
Service::Ptr s = new Service();
User::Ptr u = new User();
NotificationCommand::Ptr nc = new NotificationCommand();
Notification::Ptr n = new Notification();
unsigned int called = 0;
DuplicateDueToFilterHelper(int typeFilter, int stateFilter)
{
h->SetName("example.com", true);
h->Register();
s->SetShortName("disk", true);
h->AddService(s);
u->SetName("jdoe", true);
u->SetTypeFilter(~0);
u->SetStateFilter(~0);
u->Register();
nc->SetName("mail", true);
nc->SetExecute(new Function("", [this]() { ++called; }), true);
nc->Register();
n->SetFieldByName("host_name", "example.com", false, DebugInfo());
n->SetFieldByName("service_name", "disk", false, DebugInfo());
n->SetFieldByName("command", "mail", false, DebugInfo());
n->SetUsersRaw(new Array({"jdoe"}), true);
n->SetTypeFilter(typeFilter);
n->SetStateFilter(stateFilter);
n->OnAllConfigLoaded(); // link Service
}
~DuplicateDueToFilterHelper()
{
h->Unregister();
u->Unregister();
nc->Unregister();
}
void SendStateNotification(ServiceState state, bool isSent)
{
auto calledBefore (called);
s->SetStateRaw(state, true);
Application::GetTP().Start();
n->BeginExecuteNotification(
state == ServiceOK ? NotificationRecovery : NotificationProblem,
nullptr, false, false, "", ""
);
Application::GetTP().Stop();
BOOST_CHECK_EQUAL(called > calledBefore, isSent);
}
};
BOOST_AUTO_TEST_SUITE(icinga_notification)
BOOST_AUTO_TEST_CASE(strings)
@ -102,4 +165,33 @@ BOOST_AUTO_TEST_CASE(type_filter)
std::cout << "#4 Notification type: " << ftype << " against " << notification->GetTypeFilter() << " must fail." << std::endl;
BOOST_CHECK(!(notification->GetTypeFilter() & ftype));
}
BOOST_AUTO_TEST_CASE(no_filter_problem_no_duplicate)
{
DuplicateDueToFilterHelper helper (~0, ~0);
helper.SendStateNotification(ServiceCritical, true);
helper.SendStateNotification(ServiceWarning, true);
helper.SendStateNotification(ServiceCritical, true);
}
BOOST_AUTO_TEST_CASE(filter_problem_no_duplicate)
{
DuplicateDueToFilterHelper helper (~0, ~StateFilterWarning);
helper.SendStateNotification(ServiceCritical, true);
helper.SendStateNotification(ServiceWarning, false);
helper.SendStateNotification(ServiceCritical, false);
}
BOOST_AUTO_TEST_CASE(volatile_filter_problem_duplicate)
{
DuplicateDueToFilterHelper helper (~0, ~StateFilterWarning);
helper.s->SetVolatile(true, true);
helper.SendStateNotification(ServiceCritical, true);
helper.SendStateNotification(ServiceWarning, false);
helper.SendStateNotification(ServiceCritical, true);
}
BOOST_AUTO_TEST_SUITE_END()