]> granicus.if.org Git - icinga2/blob - lib/icinga/notification.cpp
Make attribute lookups O(1).
[icinga2] / lib / icinga / notification.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-icinga.h"
21
22 using namespace icinga;
23
24 REGISTER_TYPE(Notification, NULL);
25
26 Notification::Notification(const Dictionary::Ptr& properties)
27         : DynamicObject(properties)
28 {
29         RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
30         RegisterAttribute("macros", Attribute_Config, &m_Macros);
31         RegisterAttribute("users", Attribute_Config, &m_Users);
32         RegisterAttribute("host_name", Attribute_Config, &m_HostName);
33         RegisterAttribute("service", Attribute_Config, &m_Service);
34
35         Service::InvalidateNotificationsCache();
36 }
37
38 Notification::~Notification(void)
39 {
40         Service::InvalidateNotificationsCache();
41 }
42
43 bool Notification::Exists(const String& name)
44 {
45         return (DynamicObject::GetObject("Notification", name));
46 }
47
48 Notification::Ptr Notification::GetByName(const String& name)
49 {
50         DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
51
52         if (!configObject)
53                 BOOST_THROW_EXCEPTION(invalid_argument("Notification '" + name + "' does not exist."));
54
55         return dynamic_pointer_cast<Notification>(configObject);
56 }
57
58 Service::Ptr Notification::GetService(void) const
59 {
60         Host::Ptr host = Host::GetByName(m_HostName);
61
62         if (m_Service.IsEmpty())
63                 return host->GetHostCheckService();
64         else
65                 return host->GetServiceByShortName(m_Service);
66 }
67
68 Value Notification::GetNotificationCommand(void) const
69 {
70         return m_NotificationCommand;
71 }
72
73 Dictionary::Ptr Notification::GetMacros(void) const
74 {
75         return m_Macros;
76 }
77
78 set<User::Ptr> Notification::GetUsers(void) const
79 {
80         set<User::Ptr> result;
81
82         Dictionary::Ptr users = m_Users;
83
84         if (users) {
85                 String name;
86                 BOOST_FOREACH(tie(tuples::ignore, name), users) {
87                         result.insert(User::GetByName(name));
88                 }
89         }
90
91         return result;
92 }
93
94 String Notification::NotificationTypeToString(NotificationType type)
95 {
96         switch (type) {
97                 case NotificationDowntimeStart:
98                         return "DOWNTIMESTART";
99                 case NotificationDowntimeEnd:
100                         return "DOWNTIMEEND";
101                 case NotificationDowntimeRemoved:
102                         return "DOWNTIMECANCELLED";
103                 case NotificationCustom:
104                         return "CUSTOM";
105                 case NotificationAcknowledgement:
106                         return "ACKNOWLEDGEMENT";
107                 case NotificationProblem:
108                         return "PROBLEM";
109                 case NotificationRecovery:
110                         return "RECOVERY";
111                 default:
112                         return "UNKNOWN_NOTIFICATION";
113         }
114 }
115
116 void Notification::BeginExecuteNotification(const Notification::Ptr& self, NotificationType type)
117 {
118
119         vector<Dictionary::Ptr> macroDicts;
120
121         Dictionary::Ptr notificationMacros = boost::make_shared<Dictionary>();
122         notificationMacros->Set("NOTIFICATIONTYPE", NotificationTypeToString(type));
123         macroDicts.push_back(notificationMacros);
124
125         Service::Ptr service;
126         set<User::Ptr> users;
127
128         {
129                 ObjectLock olock(self);
130                 macroDicts.push_back(self->GetMacros());
131                 service = self->GetService();
132                 users = self->GetUsers();
133         }
134
135         Host::Ptr host;
136         String service_name;
137
138         {
139                 ObjectLock olock(service);
140                 macroDicts.push_back(service->GetMacros());
141                 service_name = service->GetName();
142                 host = service->GetHost();
143         }
144
145         macroDicts.push_back(Service::CalculateDynamicMacros(service));
146
147         {
148                 ObjectLock olock(host);
149                 macroDicts.push_back(host->GetMacros());
150                 macroDicts.push_back(Host::CalculateDynamicMacros(host));
151         }
152
153         IcingaApplication::Ptr app = IcingaApplication::GetInstance();
154
155         {
156                 ObjectLock olock(app);
157                 macroDicts.push_back(app->GetMacros());
158         }
159
160         macroDicts.push_back(IcingaApplication::CalculateDynamicMacros(app));
161
162         Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
163
164         int count = 0;
165
166         BOOST_FOREACH(const User::Ptr& user, users) {
167                 BeginExecuteNotificationHelper(self, macros, type, user);
168                 count++;
169         }
170
171         if (count == 0) {
172                 /* Send a notification even if there are no users specified. */
173                 BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
174         }
175 }
176
177 void Notification::BeginExecuteNotificationHelper(const Notification::Ptr& self, const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
178 {
179         vector<Dictionary::Ptr> macroDicts;
180
181         if (user) {
182                 {
183                         ObjectLock olock(self);
184                         macroDicts.push_back(user->GetMacros());
185                 }
186
187                 macroDicts.push_back(User::CalculateDynamicMacros(user));
188         }
189
190         macroDicts.push_back(notificationMacros);
191
192         Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
193
194         ScriptTask::Ptr task;
195
196         {
197                 ObjectLock olock(self);
198
199                 vector<Value> arguments;
200                 arguments.push_back(self);
201                 arguments.push_back(macros);
202                 arguments.push_back(type);
203                 task = self->MakeMethodTask("notify", arguments);
204
205                 if (!task) {
206                         Logger::Write(LogWarning, "icinga", "Notification object '" + self->GetName() + "' doesn't have a 'notify' method.");
207
208                         return;
209                 }
210
211                 /* We need to keep the task object alive until the completion handler is called. */
212                 self->m_Tasks.insert(task);
213         }
214
215         task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
216 }
217
218 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
219 {
220         m_Tasks.erase(task);
221
222         try {
223                 {
224                         ObjectLock tlock(task);
225                         (void) task->GetResult();
226                 }
227
228                 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
229         } catch (const exception& ex) {
230                 stringstream msgbuf;
231                 msgbuf << "Exception occured during notification for service '"
232                        << GetService()->GetName() << "': " << diagnostic_information(ex);
233                 String message = msgbuf.str();
234
235                 Logger::Write(LogWarning, "icinga", message);
236         }
237 }
238
239 void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
240 {
241         if (name == "host_name" || name == "service")
242                 Service::InvalidateNotificationsCache();
243 }