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 String Notification::NotificationTypeToString(NotificationType type)
76 case NotificationDowntimeStart:
77 return "DOWNTIMESTART";
78 case NotificationDowntimeEnd:
80 case NotificationDowntimeRemoved:
81 return "DOWNTIMECANCELLED";
82 case NotificationCustom:
83 return "DOWNTIMECUSTOM";
84 case NotificationProblem:
86 case NotificationRecovery:
89 return "UNKNOWN_NOTIFICATION";
93 void Notification::BeginExecuteNotification(const Notification::Ptr& self, NotificationType type)
96 vector<Dictionary::Ptr> macroDicts;
98 Dictionary::Ptr notificationMacros = boost::make_shared<Dictionary>();
99 notificationMacros->Set("NOTIFICATIONTYPE", NotificationTypeToString(type));
100 macroDicts.push_back(notificationMacros);
102 Service::Ptr service;
105 ObjectLock olock(self);
106 macroDicts.push_back(self->GetMacros());
107 service = self->GetService();
114 ObjectLock olock(service);
115 macroDicts.push_back(service->GetMacros());
116 service_name = service->GetName();
117 host = service->GetHost();
120 macroDicts.push_back(Service::CalculateDynamicMacros(service));
123 ObjectLock olock(host);
124 macroDicts.push_back(host->GetMacros());
125 macroDicts.push_back(Host::CalculateDynamicMacros(host));
128 IcingaApplication::Ptr app = IcingaApplication::GetInstance();
131 ObjectLock olock(app);
132 macroDicts.push_back(app->GetMacros());
135 macroDicts.push_back(IcingaApplication::CalculateDynamicMacros(app));
137 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
139 vector<Value> arguments;
140 arguments.push_back(self);
141 arguments.push_back(macros);
142 arguments.push_back(type);
144 ScriptTask::Ptr task;
147 ObjectLock olock(self);
148 task = self->MakeMethodTask("notify", arguments);
151 Logger::Write(LogWarning, "icinga", "Notification object '" + self->GetName() + "' doesn't have a 'notify' method.");
156 /* We need to keep the task object alive until the completion handler is called. */
157 self->m_Tasks.insert(task);
160 task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
163 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
169 ObjectLock tlock(task);
170 (void) task->GetResult();
173 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
174 } catch (const exception& ex) {
176 msgbuf << "Exception occured during notification for service '"
177 << GetService()->GetName() << "': " << diagnostic_information(ex);
178 String message = msgbuf.str();
180 Logger::Write(LogWarning, "icinga", message);
184 void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
186 if (name == "host_name" || name == "service")
187 Service::InvalidateNotificationsCache();