icinga2/lib/notification/notificationcomponent.cpp

136 lines
4.9 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2015-01-22 12:00:23 +01:00
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2014-05-25 16:23:35 +02:00
#include "notification/notificationcomponent.hpp"
#include "notification/notificationcomponent.tcpp"
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"
2013-03-16 21:18:53 +01:00
#include <boost/foreach.hpp>
using namespace icinga;
REGISTER_TYPE(NotificationComponent);
REGISTER_STATSFUNCTION(NotificationComponentStats, &NotificationComponent::StatsFunc);
void NotificationComponent::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
{
Dictionary::Ptr nodes = new Dictionary();
BOOST_FOREACH(const NotificationComponent::Ptr& notification_component, ConfigType::GetObjectsByType<NotificationComponent>()) {
nodes->Set(notification_component->GetName(), 1); //add more stats
}
status->Set("notificationcomponent", nodes);
}
/**
* Starts the component.
*/
void NotificationComponent::Start(void)
{
ObjectImpl<NotificationComponent>::Start();
2014-04-03 15:36:13 +02:00
Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationComponent::SendNotificationsHandler, this, _1,
_2, _3, _4, _5));
m_NotificationTimer = new Timer();
m_NotificationTimer->SetInterval(5);
m_NotificationTimer->OnTimerExpired.connect(boost::bind(&NotificationComponent::NotificationTimerHandler, this));
m_NotificationTimer->Start();
}
/**
* Periodically sends notifications.
*
* @param - Event arguments for the timer.
*/
void NotificationComponent::NotificationTimerHandler(void)
{
double now = Utility::GetTime();
BOOST_FOREACH(const Notification::Ptr& notification, ConfigType::GetObjectsByType<Notification>()) {
2014-04-03 15:36:13 +02:00
Checkable::Ptr checkable = notification->GetCheckable();
if (checkable->IsPaused() && GetEnableHA())
continue;
if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !checkable->GetEnableNotifications())
continue;
if (notification->GetInterval() <= 0 && notification->GetLastProblemNotification() > checkable->GetLastHardStateChange())
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;
if ((service && service->GetState() == ServiceOK) || (!service && host->GetState() == HostUp))
continue;
2014-04-03 15:36:13 +02:00
if (!reachable || checkable->IsInDowntime() || checkable->IsAcknowledged())
2013-03-21 15:24:00 +01:00
continue;
}
try {
Log(LogNotice, "NotificationComponent")
<< "Attempting to send reminder notification for object '" << checkable->GetName() << "'";
2014-04-03 15:36:13 +02:00
notification->BeginExecuteNotification(NotificationProblem, checkable->GetLastCheckResult(), false);
} catch (const std::exception& ex) {
2014-10-19 17:52:17 +02:00
Log(LogWarning, "NotificationComponent")
<< "Exception occured during notification for object '"
<< GetName() << "': " << DiagnosticInformation(ex);
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)
{
if (checkable->IsPaused() && GetEnableHA())
return;
2014-04-03 15:36:13 +02:00
checkable->SendNotifications(type, cr, author, text);
}