1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://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-ti.cpp"
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&)
41 for (const NotificationComponent::Ptr& notification_component : ConfigType::GetObjectsByType<NotificationComponent>()) {
42 nodes.emplace_back(notification_component->GetName(), 1); //add more stats
45 status->Set("notificationcomponent", new Dictionary(std::move(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()
82 double now = Utility::GetTime();
84 for (const Notification::Ptr& notification : ConfigType::GetObjectsByType<Notification>()) {
85 if (!notification->IsActive())
88 String notificationName = notification->GetName();
90 if (notification->IsPaused() && GetEnableHA()) {
91 Log(LogNotice, "NotificationComponent")
92 << "Reminder notification '" << notificationName << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
96 Checkable::Ptr checkable = notification->GetCheckable();
98 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !checkable->GetEnableNotifications())
101 if (notification->GetInterval() <= 0 && notification->GetNoMoreNotifications()) {
102 Log(LogNotice, "NotificationComponent")
103 << "Reminder notification '" << notificationName << "': Notification was sent out once and interval=0 disables reminder notifications.";
107 if (notification->GetNextNotification() > now)
110 bool reachable = checkable->IsReachable(DependencyNotification);
113 ObjectLock olock(notification);
114 notification->SetNextNotification(Utility::GetTime() + notification->GetInterval());
119 Service::Ptr service;
120 tie(host, service) = GetHostService(checkable);
122 ObjectLock olock(checkable);
124 if (checkable->GetStateType() == StateTypeSoft)
127 /* Don't send reminder notifications for OK/Up states. */
128 if ((service && service->GetState() == ServiceOK) || (!service && host->GetState() == HostUp))
131 /* Skip in runtime filters. */
132 if (!reachable || checkable->IsInDowntime() || checkable->IsAcknowledged() || checkable->IsFlapping())
137 Log(LogNotice, "NotificationComponent")
138 << "Attempting to send reminder notification '" << notificationName << "'.";
140 notification->BeginExecuteNotification(NotificationProblem, checkable->GetLastCheckResult(), false, true);
141 } catch (const std::exception& ex) {
142 Log(LogWarning, "NotificationComponent")
143 << "Exception occurred during notification for object '"
144 << notificationName << "': " << DiagnosticInformation(ex, false);
150 * Processes icinga::SendNotifications messages.
152 void NotificationComponent::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
153 const CheckResult::Ptr& cr, const String& author, const String& text)
155 checkable->SendNotifications(type, cr, author, text);