icinga2/lib/notification/notificationcomponent.cpp

140 lines
4.3 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2014-05-25 16:23:35 +02:00
#include "notification/notificationcomponent.hpp"
2018-01-18 13:50:38 +01:00
#include "notification/notificationcomponent-ti.cpp"
2014-05-25 16:23:35 +02:00
#include "icinga/service.hpp"
#include "icinga/icingaapplication.hpp"
#include "base/configtype.hpp"
2014-05-25 16:23:35 +02:00
#include "base/objectlock.hpp"
2014-10-19 14:21:12 +02:00
#include "base/logger.hpp"
2014-05-25 16:23:35 +02:00
#include "base/utility.hpp"
#include "base/exception.hpp"
#include "base/statsfunction.hpp"
using namespace icinga;
REGISTER_TYPE(NotificationComponent);
REGISTER_STATSFUNCTION(NotificationComponent, &NotificationComponent::StatsFunc);
void NotificationComponent::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
{
DictionaryData nodes;
for (const NotificationComponent::Ptr& notification_component : ConfigType::GetObjectsByType<NotificationComponent>()) {
nodes.emplace_back(notification_component->GetName(), 1); //add more stats
}
status->Set("notificationcomponent", new Dictionary(std::move(nodes)));
}
/**
* Starts the component.
*/
void NotificationComponent::Start(bool runtimeCreated)
{
ObjectImpl<NotificationComponent>::Start(runtimeCreated);
Log(LogInformation, "NotificationComponent")
<< "'" << GetName() << "' started.";
Checkable::OnNotificationsRequested.connect(std::bind(&NotificationComponent::SendNotificationsHandler, this, _1,
_2, _3, _4, _5));
m_NotificationTimer = new Timer();
m_NotificationTimer->SetInterval(5);
m_NotificationTimer->OnTimerExpired.connect(std::bind(&NotificationComponent::NotificationTimerHandler, this));
m_NotificationTimer->Start();
}
void NotificationComponent::Stop(bool runtimeRemoved)
{
Log(LogInformation, "NotificationComponent")
<< "'" << GetName() << "' stopped.";
ObjectImpl<NotificationComponent>::Stop(runtimeRemoved);
}
/**
* Periodically sends notifications.
*
* @param - Event arguments for the timer.
*/
void NotificationComponent::NotificationTimerHandler()
{
double now = Utility::GetTime();
for (const Notification::Ptr& notification : ConfigType::GetObjectsByType<Notification>()) {
if (!notification->IsActive())
continue;
String notificationName = notification->GetName();
if (notification->IsPaused() && GetEnableHA()) {
Log(LogNotice, "NotificationComponent")
<< "Reminder notification '" << notificationName << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
continue;
}
Checkable::Ptr checkable = notification->GetCheckable();
if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !checkable->GetEnableNotifications())
continue;
if (notification->GetInterval() <= 0 && notification->GetNoMoreNotifications()) {
Log(LogNotice, "NotificationComponent")
<< "Reminder notification '" << notificationName << "': Notification was sent out once and interval=0 disables reminder notifications.";
continue;
}
if (notification->GetNextNotification() > now)
continue;
bool reachable = checkable->IsReachable(DependencyNotification);
2013-03-02 09:07:47 +01:00
{
ObjectLock olock(notification);
notification->SetNextNotification(Utility::GetTime() + notification->GetInterval());
}
2013-03-21 15:24:00 +01:00
2013-03-06 11:03:50 +01:00
{
Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
2014-04-03 15:36:13 +02:00
ObjectLock olock(checkable);
2014-04-03 15:36:13 +02:00
if (checkable->GetStateType() == StateTypeSoft)
2013-03-06 11:03:50 +01:00
continue;
/* Don't send reminder notifications for OK/Up states. */
if ((service && service->GetState() == ServiceOK) || (!service && host->GetState() == HostUp))
continue;
/* Skip in runtime filters. */
if (!reachable || checkable->IsInDowntime() || checkable->IsAcknowledged() || checkable->IsFlapping())
2013-03-21 15:24:00 +01:00
continue;
}
try {
Log(LogNotice, "NotificationComponent")
<< "Attempting to send reminder notification '" << notificationName << "'.";
notification->BeginExecuteNotification(NotificationProblem, checkable->GetLastCheckResult(), false, true);
} catch (const std::exception& ex) {
2014-10-19 17:52:17 +02:00
Log(LogWarning, "NotificationComponent")
<< "Exception occurred during notification for object '"
<< notificationName << "': " << DiagnosticInformation(ex, false);
2013-03-06 11:03:50 +01:00
}
}
}
/**
* Processes icinga::SendNotifications messages.
*/
2014-04-03 15:36:13 +02:00
void NotificationComponent::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
const CheckResult::Ptr& cr, const String& author, const String& text)
{
2014-04-03 15:36:13 +02:00
checkable->SendNotifications(type, cr, author, text);
}