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 /* Function already checks whether 'api' feature is enabled. */
68 Endpoint::Ptr myEndpoint = Endpoint::GetLocalEndpoint();
70 for (const Notification::Ptr& notification : ConfigType::GetObjectsByType<Notification>()) {
71 if (!notification->IsActive())
74 String notificationName = notification->GetName();
76 /* Skip notification if paused, in a cluster setup & HA feature is enabled. */
77 if (notification->IsPaused() && myEndpoint && GetEnableHA()) {
78 Log(LogNotice, "NotificationComponent")
79 << "Reminder notification '" << notificationName << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
83 Checkable::Ptr checkable = notification->GetCheckable();
85 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !checkable->GetEnableNotifications())
88 if (notification->GetInterval() <= 0 && notification->GetNoMoreNotifications()) {
89 Log(LogNotice, "NotificationComponent")
90 << "Reminder notification '" << notificationName << "': Notification was sent out once and interval=0 disables reminder notifications.";
94 if (notification->GetNextNotification() > now)
97 bool reachable = checkable->IsReachable(DependencyNotification);
100 ObjectLock olock(notification);
101 notification->SetNextNotification(Utility::GetTime() + notification->GetInterval());
106 Service::Ptr service;
107 tie(host, service) = GetHostService(checkable);
109 ObjectLock olock(checkable);
111 if (checkable->GetStateType() == StateTypeSoft)
114 /* Don't send reminder notifications for OK/Up states. */
115 if ((service && service->GetState() == ServiceOK) || (!service && host->GetState() == HostUp))
118 /* Skip in runtime filters. */
119 if (!reachable || checkable->IsInDowntime() || checkable->IsAcknowledged() || checkable->IsFlapping())
124 Log(LogNotice, "NotificationComponent")
125 << "Attempting to send reminder notification '" << notificationName << "'.";
127 notification->BeginExecuteNotification(NotificationProblem, checkable->GetLastCheckResult(), false, true);
128 } catch (const std::exception& ex) {
129 Log(LogWarning, "NotificationComponent")
130 << "Exception occurred during notification for object '"
131 << notificationName << "': " << DiagnosticInformation(ex, false);
137 * Processes icinga::SendNotifications messages.
139 void NotificationComponent::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
140 const CheckResult::Ptr& cr, const String& author, const String& text)
142 checkable->SendNotifications(type, cr, author, text);