1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "notification/notificationcomponent.hpp"
21 #include "notification/notificationcomponent.tcpp"
22 #include "icinga/service.hpp"
23 #include "icinga/icingaapplication.hpp"
24 #include "base/configtype.hpp"
25 #include "base/objectlock.hpp"
26 #include "base/logger.hpp"
27 #include "base/utility.hpp"
28 #include "base/exception.hpp"
29 #include "base/statsfunction.hpp"
31 using namespace icinga;
33 REGISTER_TYPE(NotificationComponent);
35 REGISTER_STATSFUNCTION(NotificationComponent, &NotificationComponent::StatsFunc);
37 void NotificationComponent::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
39 Dictionary::Ptr nodes = new Dictionary();
41 for (const NotificationComponent::Ptr& notification_component : ConfigType::GetObjectsByType<NotificationComponent>()) {
42 nodes->Set(notification_component->GetName(), 1); //add more stats
45 status->Set("notificationcomponent", nodes);
49 * Starts the component.
51 void NotificationComponent::Start(bool runtimeCreated)
53 ObjectImpl<NotificationComponent>::Start(runtimeCreated);
55 Log(LogInformation, "NotificationComponent")
56 << "'" << GetName() << "' started.";
58 Checkable::OnNotificationsRequested.connect(std::bind(&NotificationComponent::SendNotificationsHandler, this, _1,
61 m_NotificationTimer = new Timer();
62 m_NotificationTimer->SetInterval(5);
63 m_NotificationTimer->OnTimerExpired.connect(std::bind(&NotificationComponent::NotificationTimerHandler, this));
64 m_NotificationTimer->Start();
67 void NotificationComponent::Stop(bool runtimeRemoved)
69 Log(LogInformation, "NotificationComponent")
70 << "'" << GetName() << "' stopped.";
72 ObjectImpl<NotificationComponent>::Stop(runtimeRemoved);
76 * Periodically sends notifications.
78 * @param - Event arguments for the timer.
80 void NotificationComponent::NotificationTimerHandler(void)
82 double now = Utility::GetTime();
84 for (const Notification::Ptr& notification : ConfigType::GetObjectsByType<Notification>()) {
85 if (!notification->IsActive())
88 if (notification->IsPaused() && GetEnableHA())
91 Checkable::Ptr checkable = notification->GetCheckable();
93 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !checkable->GetEnableNotifications())
96 if (notification->GetInterval() <= 0 && notification->GetNoMoreNotifications())
99 if (notification->GetNextNotification() > now)
102 bool reachable = checkable->IsReachable(DependencyNotification);
105 ObjectLock olock(notification);
106 notification->SetNextNotification(Utility::GetTime() + notification->GetInterval());
111 Service::Ptr service;
112 tie(host, service) = GetHostService(checkable);
114 ObjectLock olock(checkable);
116 if (checkable->GetStateType() == StateTypeSoft)
119 if ((service && service->GetState() == ServiceOK) || (!service && host->GetState() == HostUp))
122 if (!reachable || checkable->IsInDowntime() || checkable->IsAcknowledged())
127 Log(LogNotice, "NotificationComponent")
128 << "Attempting to send reminder notification '" << notification->GetName() << "'";
129 notification->BeginExecuteNotification(NotificationProblem, checkable->GetLastCheckResult(), false, true);
130 } catch (const std::exception& ex) {
131 Log(LogWarning, "NotificationComponent")
132 << "Exception occured during notification for object '"
133 << GetName() << "': " << DiagnosticInformation(ex);
139 * Processes icinga::SendNotifications messages.
141 void NotificationComponent::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
142 const CheckResult::Ptr& cr, const String& author, const String& text)
144 checkable->SendNotifications(type, cr, author, text);