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