1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "notification/notificationcomponent.hpp"
4 #include "notification/notificationcomponent-ti.cpp"
5 #include "icinga/service.hpp"
6 #include "icinga/icingaapplication.hpp"
7 #include "base/configtype.hpp"
8 #include "base/objectlock.hpp"
9 #include "base/logger.hpp"
10 #include "base/utility.hpp"
11 #include "base/exception.hpp"
12 #include "base/statsfunction.hpp"
14 using namespace icinga;
16 REGISTER_TYPE(NotificationComponent);
18 REGISTER_STATSFUNCTION(NotificationComponent, &NotificationComponent::StatsFunc);
20 void NotificationComponent::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
24 for (const NotificationComponent::Ptr& notification_component : ConfigType::GetObjectsByType<NotificationComponent>()) {
25 nodes.emplace_back(notification_component->GetName(), 1); //add more stats
28 status->Set("notificationcomponent", new Dictionary(std::move(nodes)));
32 * Starts the component.
34 void NotificationComponent::Start(bool runtimeCreated)
36 ObjectImpl<NotificationComponent>::Start(runtimeCreated);
38 Log(LogInformation, "NotificationComponent")
39 << "'" << GetName() << "' started.";
41 Checkable::OnNotificationsRequested.connect(std::bind(&NotificationComponent::SendNotificationsHandler, this, _1,
44 m_NotificationTimer = new Timer();
45 m_NotificationTimer->SetInterval(5);
46 m_NotificationTimer->OnTimerExpired.connect(std::bind(&NotificationComponent::NotificationTimerHandler, this));
47 m_NotificationTimer->Start();
50 void NotificationComponent::Stop(bool runtimeRemoved)
52 Log(LogInformation, "NotificationComponent")
53 << "'" << GetName() << "' stopped.";
55 ObjectImpl<NotificationComponent>::Stop(runtimeRemoved);
59 * Periodically sends notifications.
61 * @param - Event arguments for the timer.
63 void NotificationComponent::NotificationTimerHandler()
65 double now = Utility::GetTime();
67 for (const Notification::Ptr& notification : ConfigType::GetObjectsByType<Notification>()) {
68 if (!notification->IsActive())
71 String notificationName = notification->GetName();
73 if (notification->IsPaused() && GetEnableHA()) {
74 Log(LogNotice, "NotificationComponent")
75 << "Reminder notification '" << notificationName << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
79 Checkable::Ptr checkable = notification->GetCheckable();
81 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !checkable->GetEnableNotifications())
84 if (notification->GetInterval() <= 0 && notification->GetNoMoreNotifications()) {
85 Log(LogNotice, "NotificationComponent")
86 << "Reminder notification '" << notificationName << "': Notification was sent out once and interval=0 disables reminder notifications.";
90 if (notification->GetNextNotification() > now)
93 bool reachable = checkable->IsReachable(DependencyNotification);
96 ObjectLock olock(notification);
97 notification->SetNextNotification(Utility::GetTime() + notification->GetInterval());
102 Service::Ptr service;
103 tie(host, service) = GetHostService(checkable);
105 ObjectLock olock(checkable);
107 if (checkable->GetStateType() == StateTypeSoft)
110 /* Don't send reminder notifications for OK/Up states. */
111 if ((service && service->GetState() == ServiceOK) || (!service && host->GetState() == HostUp))
114 /* Skip in runtime filters. */
115 if (!reachable || checkable->IsInDowntime() || checkable->IsAcknowledged() || checkable->IsFlapping())
120 Log(LogNotice, "NotificationComponent")
121 << "Attempting to send reminder notification '" << notificationName << "'.";
123 notification->BeginExecuteNotification(NotificationProblem, checkable->GetLastCheckResult(), false, true);
124 } catch (const std::exception& ex) {
125 Log(LogWarning, "NotificationComponent")
126 << "Exception occurred during notification for object '"
127 << notificationName << "': " << DiagnosticInformation(ex, false);
133 * Processes icinga::SendNotifications messages.
135 void NotificationComponent::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
136 const CheckResult::Ptr& cr, const String& author, const String& text)
138 checkable->SendNotifications(type, cr, author, text);