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);
26 Notification::Notification(const Dictionary::Ptr& properties)
27 : DynamicObject(properties)
29 RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
30 RegisterAttribute("macros", Attribute_Config, &m_Macros);
31 RegisterAttribute("users", Attribute_Config, &m_Users);
32 RegisterAttribute("groups", Attribute_Config, &m_Groups);
33 RegisterAttribute("host_name", Attribute_Config, &m_HostName);
34 RegisterAttribute("service", Attribute_Config, &m_Service);
37 Notification::~Notification(void)
39 Service::InvalidateNotificationsCache();
43 * @threadsafety Always.
45 Notification::Ptr Notification::GetByName(const String& name)
47 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
49 return dynamic_pointer_cast<Notification>(configObject);
53 * @threadsafety Always.
55 Service::Ptr Notification::GetService(void) const
57 Host::Ptr host = Host::GetByName(m_HostName);
60 return Service::Ptr();
62 if (m_Service.IsEmpty())
63 return host->GetHostCheckService();
65 return host->GetServiceByShortName(m_Service);
69 * @threadsafety Always.
71 Value Notification::GetNotificationCommand(void) const
73 return m_NotificationCommand;
77 * @threadsafety Always.
79 Dictionary::Ptr Notification::GetMacros(void) const
85 * @threadsafety Always.
87 set<User::Ptr> Notification::GetUsers(void) const
89 set<User::Ptr> result;
91 Dictionary::Ptr users = m_Users;
94 ObjectLock olock(users);
97 BOOST_FOREACH(tie(tuples::ignore, name), users) {
98 User::Ptr user = User::GetByName(name);
111 * @threadsafety Always.
113 set<UserGroup::Ptr> Notification::GetGroups(void) const
115 set<UserGroup::Ptr> result;
117 Dictionary::Ptr groups = m_Groups;
120 ObjectLock olock(groups);
123 BOOST_FOREACH(tie(tuples::ignore, name), groups) {
124 UserGroup::Ptr ug = UserGroup::GetByName(name);
137 * @threadsafety Always.
139 String Notification::NotificationTypeToString(NotificationType type)
142 case NotificationDowntimeStart:
143 return "DOWNTIMESTART";
144 case NotificationDowntimeEnd:
145 return "DOWNTIMEEND";
146 case NotificationDowntimeRemoved:
147 return "DOWNTIMECANCELLED";
148 case NotificationCustom:
150 case NotificationAcknowledgement:
151 return "ACKNOWLEDGEMENT";
152 case NotificationProblem:
154 case NotificationRecovery:
157 return "UNKNOWN_NOTIFICATION";
162 * @threadsafety Always.
164 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr)
168 Dictionary::Ptr macros = cr->Get("macros");
170 set<User::Ptr> allUsers;
172 set<User::Ptr> users = GetUsers();
173 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
175 BOOST_FOREACH(const UserGroup::Ptr& ug, GetGroups()) {
176 set<User::Ptr> members = ug->GetMembers();
177 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
180 BOOST_FOREACH(const User::Ptr& user, allUsers) {
181 Logger::Write(LogDebug, "icinga", "Sending notification for user " + user->GetName());
182 BeginExecuteNotificationHelper(macros, type, user);
185 if (allUsers.empty()) {
186 /* Send a notification even if there are no users specified. */
187 BeginExecuteNotificationHelper(macros, type, User::Ptr());
192 * @threadsafety Always.
194 void Notification::BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
198 vector<Dictionary::Ptr> macroDicts;
201 macroDicts.push_back(user->GetMacros());
202 macroDicts.push_back(user->CalculateDynamicMacros());
205 macroDicts.push_back(notificationMacros);
207 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
209 Notification::Ptr self = GetSelf();
211 vector<Value> arguments;
212 arguments.push_back(self);
213 arguments.push_back(macros);
214 arguments.push_back(type);
216 ScriptTask::Ptr task;
217 task = MakeMethodTask("notify", arguments);
220 Logger::Write(LogWarning, "icinga", "Notification object '" + GetName() + "' doesn't have a 'notify' method.");
226 ObjectLock olock(this);
228 /* We need to keep the task object alive until the completion handler is called. */
229 m_Tasks.insert(task);
232 task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
236 * @threadsafety Always.
238 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
243 ObjectLock olock(this);
251 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
252 } catch (const exception& ex) {
254 msgbuf << "Exception occured during notification for service '"
255 << GetService()->GetName() << "': " << diagnostic_information(ex);
256 String message = msgbuf.str();
258 Logger::Write(LogWarning, "icinga", message);
263 * @threadsafety Always.
265 void Notification::OnAttributeChanged(const String& name)
269 if (name == "host_name" || name == "service")
270 Service::InvalidateNotificationsCache();