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 "icinga/service.h"
21 #include "icinga/notificationrequestmessage.h"
22 #include "remoting/endpointmanager.h"
23 #include "base/dynamictype.h"
24 #include "base/objectlock.h"
25 #include "base/logger_fwd.h"
26 #include "config/configitembuilder.h"
27 #include <boost/tuple/tuple.hpp>
28 #include <boost/smart_ptr/make_shared.hpp>
29 #include <boost/foreach.hpp>
30 #include <boost/exception/diagnostic_information.hpp>
32 using namespace icinga;
34 static boost::mutex l_NotificationMutex;
35 static std::map<String, std::set<Notification::WeakPtr> > l_NotificationsCache;
36 static bool l_NotificationsCacheNeedsUpdate = false;
37 static Timer::Ptr l_NotificationsCacheTimer;
39 void Service::RequestNotifications(NotificationType type, const Dictionary::Ptr& cr, const String& author, const String& text)
42 msg.SetMethod("icinga::SendNotifications");
44 NotificationRequestMessage params;
45 msg.SetParams(params);
47 params.SetService(GetName());
49 params.SetCheckResult(cr);
50 params.SetAuthor(author);
53 Log(LogDebug, "icinga", "Sending notification anycast request for service '" + GetName() + "'");
54 EndpointManager::GetInstance()->SendAnycastMessage(Endpoint::Ptr(), msg);
57 void Service::SendNotifications(NotificationType type, const Dictionary::Ptr& cr, const String& author, const String& text)
61 if (!GetEnableNotifications()) {
62 if (!GetForceNextNotification()) {
63 Log(LogInformation, "icinga", "Notifications are disabled for service '" + GetName() + "'.");
68 SetForceNextNotification(false);
71 Log(LogInformation, "icinga", "Sending notifications for service '" + GetName() + "'");
73 std::set<Notification::Ptr> notifications = GetNotifications();
75 if (notifications.empty())
76 Log(LogInformation, "icinga", "Service '" + GetName() + "' does not have any notifications.");
78 BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
80 notification->BeginExecuteNotification(type, cr, force, author, text);
81 } catch (const std::exception& ex) {
82 std::ostringstream msgbuf;
83 msgbuf << "Exception occured during notification for service '"
84 << GetName() << "': " << boost::diagnostic_information(ex);
85 String message = msgbuf.str();
87 Log(LogWarning, "icinga", message);
92 void Service::InvalidateNotificationsCache(void)
94 boost::mutex::scoped_lock lock(l_NotificationMutex);
96 if (l_NotificationsCacheNeedsUpdate)
97 return; /* Someone else has already requested a refresh. */
99 if (!l_NotificationsCacheTimer) {
100 l_NotificationsCacheTimer = boost::make_shared<Timer>();
101 l_NotificationsCacheTimer->SetInterval(0.5);
102 l_NotificationsCacheTimer->OnTimerExpired.connect(boost::bind(&Service::RefreshNotificationsCache));
103 l_NotificationsCacheTimer->Start();
106 l_NotificationsCacheNeedsUpdate = true;
109 void Service::RefreshNotificationsCache(void)
112 boost::mutex::scoped_lock lock(l_NotificationMutex);
114 if (!l_NotificationsCacheNeedsUpdate)
117 l_NotificationsCacheNeedsUpdate = false;
120 Log(LogDebug, "icinga", "Updating Service notifications cache.");
122 std::map<String, std::set<Notification::WeakPtr> > newNotificationsCache;
124 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Notification")) {
125 const Notification::Ptr& notification = static_pointer_cast<Notification>(object);
127 Service::Ptr service = notification->GetService();
132 newNotificationsCache[service->GetName()].insert(notification);
135 boost::mutex::scoped_lock lock(l_NotificationMutex);
136 l_NotificationsCache.swap(newNotificationsCache);
139 std::set<Notification::Ptr> Service::GetNotifications(void) const
141 std::set<Notification::Ptr> notifications;
144 boost::mutex::scoped_lock lock(l_NotificationMutex);
146 BOOST_FOREACH(const Notification::WeakPtr& wservice, l_NotificationsCache[GetName()]) {
147 Notification::Ptr notification = wservice.lock();
152 notifications.insert(notification);
156 return notifications;
159 void Service::UpdateSlaveNotifications(void)
161 Dictionary::Ptr oldNotifications;
162 ConfigItem::Ptr serviceItem, hostItem;
164 serviceItem = ConfigItem::GetObject("Service", GetName());
166 /* Don't create slave notifications unless we own this object */
170 Host::Ptr host = GetHost();
175 hostItem = ConfigItem::GetObject("Host", host->GetName());
177 /* Don't create slave notifications unless we own the host */
182 ObjectLock olock(this);
183 oldNotifications = m_SlaveNotifications;
186 std::vector<Dictionary::Ptr> descLists;
188 descLists.push_back(Get("notifications"));
190 Dictionary::Ptr newNotifications;
191 newNotifications = boost::make_shared<Dictionary>();
193 descLists.push_back(host->Get("notifications"));
195 for (int i = 0; i < 2; i++) {
196 Dictionary::Ptr descs;
197 ConfigItem::Ptr item;
200 /* Host notification descs */
201 descs = host->Get("notifications");
204 /* Service notification descs */
205 descs = Get("notifications");
212 ObjectLock olock(descs);
216 BOOST_FOREACH(boost::tie(nfcname, nfcdesc), descs) {
217 std::ostringstream namebuf;
218 namebuf << GetName() << ":" << nfcname;
219 String name = namebuf.str();
221 ConfigItemBuilder::Ptr builder = boost::make_shared<ConfigItemBuilder>(item->GetDebugInfo());
222 builder->SetType("Notification");
223 builder->SetName(name);
224 builder->AddExpression("host_name", OperatorSet, host->GetName());
225 builder->AddExpression("service", OperatorSet, GetShortName());
227 if (!nfcdesc.IsObjectType<Dictionary>())
228 BOOST_THROW_EXCEPTION(std::invalid_argument("Notification description must be a dictionary."));
230 Dictionary::Ptr notification = nfcdesc;
232 Array::Ptr templates = notification->Get("templates");
235 ObjectLock tlock(templates);
237 BOOST_FOREACH(const Value& tmpl, templates) {
238 builder->AddParent(tmpl);
242 /* Clone attributes from the host/service object. */
243 std::set<String, string_iless> keys;
244 keys.insert("users");
245 keys.insert("groups");
246 keys.insert("notification_interval");
247 keys.insert("notification_period");
248 keys.insert("notification_type_filter");
249 keys.insert("notification_state_filter");
250 keys.insert("export_macros");
252 ExpressionList::Ptr svc_exprl = boost::make_shared<ExpressionList>();
253 item->GetLinkedExpressionList()->ExtractFiltered(keys, svc_exprl);
254 builder->AddExpressionList(svc_exprl);
256 /* Clone attributes from the notification expression list. */
257 std::vector<String> path;
258 path.push_back("notifications");
259 path.push_back(nfcname);
261 ExpressionList::Ptr nfc_exprl = boost::make_shared<ExpressionList>();
262 item->GetLinkedExpressionList()->ExtractPath(path, nfc_exprl);
263 builder->AddExpressionList(nfc_exprl);
265 ConfigItem::Ptr notificationItem = builder->Compile();
266 notificationItem->Commit();
268 newNotifications->Set(name, notificationItem);
272 if (oldNotifications) {
273 ObjectLock olock(oldNotifications);
275 ConfigItem::Ptr notification;
276 BOOST_FOREACH(boost::tie(boost::tuples::ignore, notification), oldNotifications) {
280 if (!newNotifications->Contains(notification->GetName()))
281 notification->Unregister();
286 ObjectLock olock(this);
287 m_SlaveNotifications = newNotifications;
291 bool Service::GetEnableNotifications(void) const
293 if (m_EnableNotifications.IsEmpty())
296 return m_EnableNotifications;
299 void Service::SetEnableNotifications(bool enabled)
301 m_EnableNotifications = enabled;
302 Touch("enable_notifications");
305 bool Service::GetForceNextNotification(void) const
307 if (m_ForceNextNotification.IsEmpty())
310 return static_cast<bool>(m_ForceNextNotification);
313 void Service::SetForceNextNotification(bool forced)
315 m_ForceNextNotification = forced ? 1 : 0;
316 Touch("force_next_notification");