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 boost::mutex Service::m_NotificationMutex;
25 map<String, set<Notification::WeakPtr> > Service::m_NotificationsCache;
26 bool Service::m_NotificationsCacheNeedsUpdate = false;
27 Timer::Ptr Service::m_NotificationsCacheTimer;
30 * @threadsafety Always.
32 void Service::RequestNotifications(NotificationType type, const Dictionary::Ptr& cr)
35 ObjectLock olock(this);
36 SetLastNotification(Utility::GetTime());
40 msg.SetMethod("icinga::SendNotifications");
42 NotificationRequestMessage params;
43 msg.SetParams(params);
45 params.SetService(GetName());
47 params.SetCheckResult(cr);
49 Logger::Write(LogDebug, "icinga", "Sending notification anycast request for service '" + GetName() + "'");
50 EndpointManager::GetInstance()->SendAnycastMessage(Endpoint::Ptr(), msg);
54 * @threadsafety Always.
56 void Service::SendNotifications(NotificationType type, const Dictionary::Ptr& cr)
58 if (!GetEnableNotifications()) {
59 Logger::Write(LogInformation, "icinga", "Notifications are disabled for service '" + GetName() + "'.");
63 Logger::Write(LogInformation, "icinga", "Sending notifications for service '" + GetName() + "'");
65 set<Notification::Ptr> notifications = GetNotifications();
67 if (notifications.empty())
68 Logger::Write(LogInformation, "icinga", "Service '" + GetName() + "' does not have any notifications.");
70 BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
72 notification->BeginExecuteNotification(type, cr);
73 } catch (const exception& ex) {
75 msgbuf << "Exception occured during notification for service '"
76 << GetName() << "': " << diagnostic_information(ex);
77 String message = msgbuf.str();
79 Logger::Write(LogWarning, "icinga", message);
85 * @threadsafety Always.
87 void Service::InvalidateNotificationsCache(void)
89 boost::mutex::scoped_lock lock(m_NotificationMutex);
91 if (m_NotificationsCacheNeedsUpdate)
92 return; /* Someone else has already requested a refresh. */
94 if (!m_NotificationsCacheTimer) {
95 m_NotificationsCacheTimer = boost::make_shared<Timer>();
96 m_NotificationsCacheTimer->SetInterval(0.5);
97 m_NotificationsCacheTimer->OnTimerExpired.connect(boost::bind(&Service::RefreshNotificationsCache));
98 m_NotificationsCacheTimer->Start();
101 m_NotificationsCacheNeedsUpdate = true;
105 * @threadsafety Always.
107 void Service::RefreshNotificationsCache(void)
110 boost::mutex::scoped_lock lock(m_NotificationMutex);
112 if (!m_NotificationsCacheNeedsUpdate)
115 m_NotificationsCacheNeedsUpdate = false;
118 Logger::Write(LogInformation, "icinga", "Updating Service notifications cache.");
120 map<String, set<Notification::WeakPtr> > newNotificationsCache;
122 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Notification")) {
123 const Notification::Ptr& notification = static_pointer_cast<Notification>(object);
125 Service::Ptr service = notification->GetService();
130 newNotificationsCache[service->GetName()].insert(notification);
133 boost::mutex::scoped_lock lock(m_NotificationMutex);
134 m_NotificationsCache.swap(newNotificationsCache);
138 * @threadsafety Always.
140 set<Notification::Ptr> Service::GetNotifications(void) const
142 set<Notification::Ptr> notifications;
145 boost::mutex::scoped_lock lock(m_NotificationMutex);
147 BOOST_FOREACH(const Notification::WeakPtr& wservice, m_NotificationsCache[GetName()]) {
148 Notification::Ptr notification = wservice.lock();
153 notifications.insert(notification);
157 return notifications;
161 * @threadsafety Always.
163 template<typename TDict>
164 static void CopyNotificationAttributes(TDict notificationDesc, const ConfigItemBuilder::Ptr& builder)
166 /* TODO: we only need to copy macros if this is an inline definition,
167 * i.e. "typeid(notificationDesc)" != Notification, however for now we just
168 * copy them anyway. */
169 Value macros = notificationDesc->Get("macros");
170 if (!macros.IsEmpty())
171 builder->AddExpression("macros", OperatorPlus, macros);
173 Value users = notificationDesc->Get("users");
174 if (!users.IsEmpty())
175 builder->AddExpression("users", OperatorPlus, users);
177 Value groups = notificationDesc->Get("groups");
178 if (!groups.IsEmpty())
179 builder->AddExpression("groups", OperatorPlus, groups);
181 /*Value notificationInterval = notificationDesc->Get("notification_interval");
182 if (!notificationInterval.IsEmpty())
183 builder->AddExpression("notification_interval", OperatorSet, notificationInterval);*/
186 void Service::UpdateSlaveNotifications(void)
188 Dictionary::Ptr oldNotifications;
189 vector<Dictionary::Ptr> notificationDescsList;
190 ConfigItem::Ptr item;
192 item = ConfigItem::GetObject("Service", GetName());
194 /* Don't create slave notifications unless we own this object
195 * and it's not a template. */
196 if (!item || IsAbstract())
200 ObjectLock olock(this);
201 oldNotifications = m_SlaveNotifications;
204 notificationDescsList.push_back(Get("notifications"));
206 Dictionary::Ptr newNotifications;
207 newNotifications = boost::make_shared<Dictionary>();
209 Host::Ptr host = GetHost();
214 notificationDescsList.push_back(host->Get("notifications"));
216 BOOST_FOREACH(const Dictionary::Ptr& notificationDescs, notificationDescsList) {
217 if (!notificationDescs)
220 ObjectLock olock(notificationDescs);
224 BOOST_FOREACH(tie(nfcname, nfcdesc), notificationDescs) {
225 if (nfcdesc.IsScalar())
228 stringstream namebuf;
229 namebuf << GetName() << "-" << nfcname;
230 String name = namebuf.str();
232 ConfigItemBuilder::Ptr builder = boost::make_shared<ConfigItemBuilder>(item->GetDebugInfo());
233 builder->SetType("Notification");
234 builder->SetName(name);
235 builder->AddExpression("host_name", OperatorSet, host->GetName());
236 builder->AddExpression("service", OperatorSet, GetShortName());
238 CopyNotificationAttributes(this, builder);
240 if (nfcdesc.IsScalar()) {
241 builder->AddParent(nfcdesc);
242 } else if (nfcdesc.IsObjectType<Dictionary>()) {
243 Dictionary::Ptr notification = nfcdesc;
245 Dictionary::Ptr templates = notification->Get("templates");
248 ObjectLock tlock(templates);
251 BOOST_FOREACH(tie(tuples::ignore, tmpl), templates) {
252 builder->AddParent(tmpl);
255 builder->AddParent(nfcname);
258 CopyNotificationAttributes(notification, builder);
260 BOOST_THROW_EXCEPTION(invalid_argument("Notification description must be either a string or a dictionary."));
263 ConfigItem::Ptr notificationItem = builder->Compile();
264 notificationItem->Commit();
266 newNotifications->Set(name, notificationItem);
270 if (oldNotifications) {
271 ObjectLock olock(oldNotifications);
273 ConfigItem::Ptr notification;
274 BOOST_FOREACH(tie(tuples::ignore, notification), oldNotifications) {
278 if (!newNotifications->Contains(notification->GetName()))
279 notification->Unregister();
284 ObjectLock olock(this);
285 m_SlaveNotifications = newNotifications;
290 * @threadsafety Always.
292 double Service::GetLastNotification(void) const
294 if (m_LastNotification.IsEmpty())
297 return m_LastNotification;
301 * @threadsafety Always.
303 void Service::SetLastNotification(double time)
305 m_LastNotification = time;
306 Touch("last_notification");
310 * @threadsafety Always.
312 bool Service::GetEnableNotifications(void) const
314 if (m_EnableNotifications.IsEmpty())
317 return m_EnableNotifications;
321 * @threadsafety Always.
323 void Service::SetEnableNotifications(bool enabled)
325 m_EnableNotifications = enabled;
326 Touch("enable_notifications");
330 * @threadsafety Always.
332 double Service::GetNotificationInterval(void) const
334 if (m_NotificationInterval.IsEmpty())
337 return m_NotificationInterval;