2013-02-07 21:08:16 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2013-02-07 21:08:16 +01:00
|
|
|
* *
|
|
|
|
* 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"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "notification/notificationcomponent.tcpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/service.hpp"
|
2014-06-20 10:34:00 +02:00
|
|
|
#include "icinga/icingaapplication.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#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-02-07 21:08:16 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-12 13:45:54 +01:00
|
|
|
REGISTER_TYPE(NotificationComponent);
|
|
|
|
|
2015-09-21 11:44:58 +02:00
|
|
|
REGISTER_STATSFUNCTION(NotificationComponent, &NotificationComponent::StatsFunc);
|
2014-02-17 16:34:18 +01:00
|
|
|
|
2015-02-07 22:36:17 +01:00
|
|
|
void NotificationComponent::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
|
2014-02-17 16:34:18 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr nodes = new Dictionary();
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const NotificationComponent::Ptr& notification_component : ConfigType::GetObjectsByType<NotificationComponent>()) {
|
2014-02-18 10:53:44 +01:00
|
|
|
nodes->Set(notification_component->GetName(), 1); //add more stats
|
|
|
|
}
|
|
|
|
|
|
|
|
status->Set("notificationcomponent", nodes);
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
|
|
|
|
2013-02-07 21:08:16 +01:00
|
|
|
/**
|
|
|
|
* Starts the component.
|
|
|
|
*/
|
2015-08-20 17:18:48 +02:00
|
|
|
void NotificationComponent::Start(bool runtimeCreated)
|
2013-02-07 21:08:16 +01:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<NotificationComponent>::Start(runtimeCreated);
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
Log(LogInformation, "NotificationComponent")
|
|
|
|
<< "'" << GetName() << "' started.";
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationComponent::SendNotificationsHandler, this, _1,
|
2013-08-20 11:06:04 +02:00
|
|
|
_2, _3, _4, _5));
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
m_NotificationTimer = new Timer();
|
2013-02-07 21:08:16 +01:00
|
|
|
m_NotificationTimer->SetInterval(5);
|
|
|
|
m_NotificationTimer->OnTimerExpired.connect(boost::bind(&NotificationComponent::NotificationTimerHandler, this));
|
|
|
|
m_NotificationTimer->Start();
|
|
|
|
}
|
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
void NotificationComponent::Stop(bool runtimeRemoved)
|
|
|
|
{
|
|
|
|
Log(LogInformation, "NotificationComponent")
|
|
|
|
<< "'" << GetName() << "' stopped.";
|
|
|
|
|
|
|
|
ObjectImpl<NotificationComponent>::Stop(runtimeRemoved);
|
|
|
|
}
|
|
|
|
|
2013-02-07 21:08:16 +01:00
|
|
|
/**
|
2013-03-20 10:08:27 +01:00
|
|
|
* Periodically sends notifications.
|
2013-02-07 21:08:16 +01:00
|
|
|
*
|
|
|
|
* @param - Event arguments for the timer.
|
|
|
|
*/
|
|
|
|
void NotificationComponent::NotificationTimerHandler(void)
|
|
|
|
{
|
2013-02-27 12:44:51 +01:00
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Notification::Ptr& notification : ConfigType::GetObjectsByType<Notification>()) {
|
2016-04-12 12:04:38 +02:00
|
|
|
if (!notification->IsActive())
|
|
|
|
continue;
|
|
|
|
|
2016-07-28 17:39:52 +02:00
|
|
|
if (notification->IsPaused() && GetEnableHA())
|
2014-08-22 12:23:45 +02:00
|
|
|
continue;
|
|
|
|
|
2016-07-28 17:39:52 +02:00
|
|
|
Checkable::Ptr checkable = notification->GetCheckable();
|
|
|
|
|
2014-06-20 10:34:00 +02:00
|
|
|
if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !checkable->GetEnableNotifications())
|
|
|
|
continue;
|
|
|
|
|
2016-08-16 13:47:48 +02:00
|
|
|
if (notification->GetInterval() <= 0 && notification->GetNoMoreNotifications())
|
2013-03-20 10:08:27 +01:00
|
|
|
continue;
|
|
|
|
|
2013-11-06 08:36:22 +01:00
|
|
|
if (notification->GetNextNotification() > now)
|
2013-08-05 08:56:36 +02:00
|
|
|
continue;
|
|
|
|
|
2014-04-09 15:51:10 +02:00
|
|
|
bool reachable = checkable->IsReachable(DependencyNotification);
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-06-13 11:33:00 +02:00
|
|
|
{
|
|
|
|
ObjectLock olock(notification);
|
2014-04-09 10:25:23 +02:00
|
|
|
notification->SetNextNotification(Utility::GetTime() + notification->GetInterval());
|
2013-06-13 11:33:00 +02:00
|
|
|
}
|
2013-03-21 15:24:00 +01:00
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
{
|
2014-04-09 15:51:10 +02:00
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
ObjectLock olock(checkable);
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (checkable->GetStateType() == StateTypeSoft)
|
2013-03-06 11:03:50 +01:00
|
|
|
continue;
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2014-04-09 15:51:10 +02:00
|
|
|
if ((service && service->GetState() == ServiceOK) || (!service && host->GetState() == HostUp))
|
|
|
|
continue;
|
2013-02-27 18:15:01 +01:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (!reachable || checkable->IsInDowntime() || checkable->IsAcknowledged())
|
2013-03-21 15:24:00 +01:00
|
|
|
continue;
|
2013-03-20 10:08:27 +01:00
|
|
|
}
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-03-20 10:08:27 +01:00
|
|
|
try {
|
2015-02-12 14:51:17 +01:00
|
|
|
Log(LogNotice, "NotificationComponent")
|
2016-08-15 18:32:51 +02:00
|
|
|
<< "Attempting to send reminder notification '" << notification->GetName() << "'";
|
2016-08-12 14:49:29 +02:00
|
|
|
notification->BeginExecuteNotification(NotificationProblem, checkable->GetLastCheckResult(), false, true);
|
2013-03-20 10:08:27 +01:00
|
|
|
} 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
|
|
|
}
|
2013-02-27 12:44:51 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-09 15:20:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes icinga::SendNotifications messages.
|
|
|
|
*/
|
2014-04-03 15:36:13 +02:00
|
|
|
void NotificationComponent::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
|
2013-11-09 14:22:38 +01:00
|
|
|
const CheckResult::Ptr& cr, const String& author, const String& text)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2014-04-03 15:36:13 +02:00
|
|
|
checkable->SendNotifications(type, cr, author, text);
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|