]> granicus.if.org Git - icinga2/blob - components/notification/notificationcomponent.cpp
Implemented LAST*STATE* macros.
[icinga2] / components / notification / notificationcomponent.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "i2-notification.h"
21
22 using namespace icinga;
23
24 REGISTER_COMPONENT("notification", NotificationComponent);
25
26 /**
27  * Starts the component.
28  */
29 void NotificationComponent::Start(void)
30 {
31         m_Endpoint = Endpoint::MakeEndpoint("notification", false);
32         m_Endpoint->RegisterTopicHandler("icinga::SendNotifications",
33             boost::bind(&NotificationComponent::SendNotificationsRequestHandler, this, _2,
34             _3));
35
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();
40 }
41
42 /**
43  * Stops the component.
44  */
45 void NotificationComponent::Stop(void)
46 {
47         m_Endpoint->Unregister();
48 }
49
50 /**
51  * Periodically sends a notification::HelloWorld message.
52  *
53  * @param - Event arguments for the timer.
54  */
55 void NotificationComponent::NotificationTimerHandler(void)
56 {
57         double now = Utility::GetTime();
58
59         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
60                 Service::Ptr service = dynamic_pointer_cast<Service>(object);
61                 bool reachable = service->IsReachable();
62
63                 bool send_notification;
64
65                 {
66                         ObjectLock olock(service);
67
68                         if (service->GetStateType() == StateTypeSoft)
69                                 continue;
70
71                         if (service->GetState() == StateOK)
72                                 continue;
73
74                         if (service->GetNotificationInterval() <= 0)
75                                 continue;
76
77                         if (service->GetLastNotification() > now - service->GetNotificationInterval())
78                                 continue;
79
80                         send_notification = reachable && !service->IsInDowntime() && !service->IsAcknowledged();
81                 }
82
83                 if (send_notification)
84                         service->RequestNotifications(NotificationProblem, service->GetLastCheckResult());
85         }
86 }
87
88 /**
89  * Processes icinga::SendNotifications messages.
90  */
91 void NotificationComponent::SendNotificationsRequestHandler(const Endpoint::Ptr& sender,
92     const RequestMessage& request)
93 {
94         MessagePart params;
95         if (!request.GetParams(&params))
96                 return;
97
98         String svc;
99         if (!params.Get("service", &svc))
100                 return;
101
102         int type;
103         if (!params.Get("type", &type))
104                 return;
105
106         Dictionary::Ptr cr;
107         if (!params.Get("check_result", &cr))
108                 return;
109
110         Service::Ptr service = Service::GetByName(svc);
111
112         if (!service)
113                 return;
114
115         service->SendNotifications(static_cast<NotificationType>(type), cr);
116 }