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-icinga.h"
22 using namespace icinga;
24 REGISTER_TYPE(Notification, NULL);
26 Notification::Notification(const Dictionary::Ptr& properties)
27 : DynamicObject(properties)
29 Service::InvalidateNotificationsCache();
32 Notification::~Notification(void)
34 Service::InvalidateNotificationsCache();
37 bool Notification::Exists(const String& name)
39 return (DynamicObject::GetObject("Notification", name));
42 Notification::Ptr Notification::GetByName(const String& name)
44 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
47 BOOST_THROW_EXCEPTION(invalid_argument("Notification '" + name + "' does not exist."));
49 return dynamic_pointer_cast<Notification>(configObject);
52 Service::Ptr Notification::GetService(void) const
54 Host::Ptr host = Host::GetByName(Get("host_name"));
55 String service = Get("service");
57 if (service.IsEmpty())
58 return host->GetHostCheckService();
60 return host->GetServiceByShortName(service);
63 Value Notification::GetNotificationCommand(void) const
65 return Get("notification_command");
68 Dictionary::Ptr Notification::GetMacros(void) const
73 set<User::Ptr> Notification::GetUsers(void) const
75 set<User::Ptr> result;
77 Dictionary::Ptr users = Get("users");
81 BOOST_FOREACH(tie(tuples::ignore, name), users) {
82 result.insert(User::GetByName(name));
89 String Notification::NotificationTypeToString(NotificationType type)
92 case NotificationDowntimeStart:
93 return "DOWNTIMESTART";
94 case NotificationDowntimeEnd:
96 case NotificationDowntimeRemoved:
97 return "DOWNTIMECANCELLED";
98 case NotificationCustom:
100 case NotificationAcknowledgement:
101 return "ACKNOWLEDGEMENT";
102 case NotificationProblem:
104 case NotificationRecovery:
107 return "UNKNOWN_NOTIFICATION";
111 void Notification::BeginExecuteNotification(const Notification::Ptr& self, NotificationType type)
114 vector<Dictionary::Ptr> macroDicts;
116 Dictionary::Ptr notificationMacros = boost::make_shared<Dictionary>();
117 notificationMacros->Set("NOTIFICATIONTYPE", NotificationTypeToString(type));
118 macroDicts.push_back(notificationMacros);
120 Service::Ptr service;
121 set<User::Ptr> users;
124 ObjectLock olock(self);
125 macroDicts.push_back(self->GetMacros());
126 service = self->GetService();
127 users = self->GetUsers();
134 ObjectLock olock(service);
135 macroDicts.push_back(service->GetMacros());
136 service_name = service->GetName();
137 host = service->GetHost();
140 macroDicts.push_back(Service::CalculateDynamicMacros(service));
143 ObjectLock olock(host);
144 macroDicts.push_back(host->GetMacros());
145 macroDicts.push_back(Host::CalculateDynamicMacros(host));
148 IcingaApplication::Ptr app = IcingaApplication::GetInstance();
151 ObjectLock olock(app);
152 macroDicts.push_back(app->GetMacros());
155 macroDicts.push_back(IcingaApplication::CalculateDynamicMacros(app));
157 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
161 BOOST_FOREACH(const User::Ptr& user, users) {
162 BeginExecuteNotificationHelper(self, macros, type, user);
167 /* Send a notification even if there are no users specified. */
168 BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
172 void Notification::BeginExecuteNotificationHelper(const Notification::Ptr& self, const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
174 vector<Dictionary::Ptr> macroDicts;
178 ObjectLock olock(self);
179 macroDicts.push_back(user->GetMacros());
182 macroDicts.push_back(User::CalculateDynamicMacros(user));
185 macroDicts.push_back(notificationMacros);
187 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
189 ScriptTask::Ptr task;
192 ObjectLock olock(self);
194 vector<Value> arguments;
195 arguments.push_back(self);
196 arguments.push_back(macros);
197 arguments.push_back(type);
198 task = self->MakeMethodTask("notify", arguments);
201 Logger::Write(LogWarning, "icinga", "Notification object '" + self->GetName() + "' doesn't have a 'notify' method.");
206 /* We need to keep the task object alive until the completion handler is called. */
207 self->m_Tasks.insert(task);
210 task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
213 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
219 ObjectLock tlock(task);
220 (void) task->GetResult();
223 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
224 } catch (const exception& ex) {
226 msgbuf << "Exception occured during notification for service '"
227 << GetService()->GetName() << "': " << diagnostic_information(ex);
228 String message = msgbuf.str();
230 Logger::Write(LogWarning, "icinga", message);
234 void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
236 if (name == "host_name" || name == "service")
237 Service::InvalidateNotificationsCache();