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"
21 #include "base/dynamictype.h"
22 #include "base/objectlock.h"
23 #include "base/logger_fwd.h"
24 #include <boost/tuple/tuple.hpp>
25 #include <boost/foreach.hpp>
26 #include <boost/exception/diagnostic_information.hpp>
28 using namespace icinga;
30 REGISTER_TYPE(Notification);
32 Notification::Notification(const Dictionary::Ptr& serializedUpdate)
33 : DynamicObject(serializedUpdate)
35 RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
36 RegisterAttribute("macros", Attribute_Config, &m_Macros);
37 RegisterAttribute("users", Attribute_Config, &m_Users);
38 RegisterAttribute("groups", Attribute_Config, &m_Groups);
39 RegisterAttribute("host_name", Attribute_Config, &m_HostName);
40 RegisterAttribute("service", Attribute_Config, &m_Service);
43 Notification::~Notification(void)
45 Service::InvalidateNotificationsCache();
49 * @threadsafety Always.
51 Notification::Ptr Notification::GetByName(const String& name)
53 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
55 return dynamic_pointer_cast<Notification>(configObject);
59 * @threadsafety Always.
61 Service::Ptr Notification::GetService(void) const
63 Host::Ptr host = Host::GetByName(m_HostName);
66 return Service::Ptr();
68 if (m_Service.IsEmpty())
69 return host->GetHostCheckService();
71 return host->GetServiceByShortName(m_Service);
75 * @threadsafety Always.
77 Value Notification::GetNotificationCommand(void) const
79 return m_NotificationCommand;
83 * @threadsafety Always.
85 Dictionary::Ptr Notification::GetMacros(void) const
91 * @threadsafety Always.
93 std::set<User::Ptr> Notification::GetUsers(void) const
95 std::set<User::Ptr> result;
97 Array::Ptr users = m_Users;
100 ObjectLock olock(users);
102 BOOST_FOREACH(const String& name, users) {
103 User::Ptr user = User::GetByName(name);
116 * @threadsafety Always.
118 std::set<UserGroup::Ptr> Notification::GetGroups(void) const
120 std::set<UserGroup::Ptr> result;
122 Array::Ptr groups = m_Groups;
125 ObjectLock olock(groups);
127 BOOST_FOREACH(const String& name, groups) {
128 UserGroup::Ptr ug = UserGroup::GetByName(name);
141 * @threadsafety Always.
143 String Notification::NotificationTypeToString(NotificationType type)
146 case NotificationDowntimeStart:
147 return "DOWNTIMESTART";
148 case NotificationDowntimeEnd:
149 return "DOWNTIMEEND";
150 case NotificationDowntimeRemoved:
151 return "DOWNTIMECANCELLED";
152 case NotificationCustom:
154 case NotificationAcknowledgement:
155 return "ACKNOWLEDGEMENT";
156 case NotificationProblem:
158 case NotificationRecovery:
161 return "UNKNOWN_NOTIFICATION";
166 * @threadsafety Always.
168 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr)
172 Dictionary::Ptr macros = cr->Get("macros");
174 std::set<User::Ptr> allUsers;
176 std::set<User::Ptr> users = GetUsers();
177 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
179 BOOST_FOREACH(const UserGroup::Ptr& ug, GetGroups()) {
180 std::set<User::Ptr> members = ug->GetMembers();
181 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
184 BOOST_FOREACH(const User::Ptr& user, allUsers) {
185 Log(LogDebug, "icinga", "Sending notification for user " + user->GetName());
186 BeginExecuteNotificationHelper(macros, type, user);
189 if (allUsers.empty()) {
190 /* Send a notification even if there are no users specified. */
191 BeginExecuteNotificationHelper(macros, type, User::Ptr());
196 * @threadsafety Always.
198 void Notification::BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
202 std::vector<Dictionary::Ptr> macroDicts;
205 macroDicts.push_back(user->GetMacros());
206 macroDicts.push_back(user->CalculateDynamicMacros());
209 macroDicts.push_back(notificationMacros);
211 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
213 Notification::Ptr self = GetSelf();
215 std::vector<Value> arguments;
216 arguments.push_back(self);
217 arguments.push_back(macros);
218 arguments.push_back(type);
220 ScriptTask::Ptr task = MakeMethodTask("notify", arguments);
223 Log(LogWarning, "icinga", "Notification object '" + GetName() + "' doesn't have a 'notify' method.");
229 ObjectLock olock(this);
231 /* We need to keep the task object alive until the completion handler is called. */
232 m_Tasks.insert(task);
235 task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
239 * @threadsafety Always.
241 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
246 ObjectLock olock(this);
254 Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
255 } catch (const std::exception& ex) {
256 std::ostringstream msgbuf;
257 msgbuf << "Exception occured during notification for service '"
258 << GetService()->GetName() << "': " << boost::diagnostic_information(ex);
259 String message = msgbuf.str();
261 Log(LogWarning, "icinga", message);
266 * @threadsafety Always.
268 void Notification::OnAttributeChanged(const String& name)
272 if (name == "host_name" || name == "service")
273 Service::InvalidateNotificationsCache();