1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
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 "i2-notification.h"
22 using namespace icinga;
24 REGISTER_COMPONENT("notification", NotificationComponent);
27 * Starts the component.
29 void NotificationComponent::Start(void)
31 m_Endpoint = Endpoint::MakeEndpoint("notification", false);
32 m_Endpoint->RegisterTopicHandler("icinga::SendNotifications",
33 boost::bind(&NotificationComponent::SendNotificationsRequestHandler, this, _2,
36 m_NotificationTimer = boost::make_shared<Timer>();
37 m_NotificationTimer->SetInterval(5);
38 m_NotificationTimer->OnTimerExpired.connect(boost::bind(&NotificationComponent::NotificationTimerHandler, this));
39 m_NotificationTimer->Start();
43 * Stops the component.
45 void NotificationComponent::Stop(void)
47 m_Endpoint->Unregister();
51 * Periodically sends a notification::HelloWorld message.
53 * @param - Event arguments for the timer.
55 void NotificationComponent::NotificationTimerHandler(void)
57 double now = Utility::GetTime();
59 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
60 Service::Ptr service = dynamic_pointer_cast<Service>(object);
61 bool reachable = service->IsReachable();
63 bool send_notification;
66 ObjectLock olock(service);
68 if (service->GetStateType() == StateTypeSoft)
71 if (service->GetState() == StateOK)
74 if (service->GetNotificationInterval() <= 0)
77 if (service->GetLastNotification() > now - service->GetNotificationInterval())
80 send_notification = reachable && !service->IsInDowntime() && !service->IsAcknowledged();
83 if (send_notification)
84 service->RequestNotifications(NotificationProblem, service->GetLastCheckResult());
89 * Processes icinga::SendNotifications messages.
91 void NotificationComponent::SendNotificationsRequestHandler(const Endpoint::Ptr& sender,
92 const RequestMessage& request)
95 if (!request.GetParams(¶ms))
99 if (!params.Get("service", &svc))
103 if (!params.Get("type", &type))
107 if (!params.Get("check_result", &cr))
110 Service::Ptr service = Service::GetByName(svc);
115 service->SendNotifications(static_cast<NotificationType>(type), cr);