]> granicus.if.org Git - icinga2/blob - lib/icinga/notification.cpp
Implemented dictionary support for commands.
[icinga2] / lib / icinga / notification.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "i2-icinga.h"
21
22 using namespace icinga;
23
24 REGISTER_TYPE(Notification, NULL);
25
26 Notification::Notification(const Dictionary::Ptr& properties)
27         : DynamicObject(properties)
28 {
29         Service::InvalidateNotificationsCache();
30 }
31
32 Notification::~Notification(void)
33 {
34         Service::InvalidateNotificationsCache();
35 }
36
37 bool Notification::Exists(const String& name)
38 {
39         return (DynamicObject::GetObject("Notification", name));
40 }
41
42 Notification::Ptr Notification::GetByName(const String& name)
43 {
44         DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
45
46         if (!configObject)
47                 BOOST_THROW_EXCEPTION(invalid_argument("Notification '" + name + "' does not exist."));
48
49         return dynamic_pointer_cast<Notification>(configObject);
50 }
51
52 Service::Ptr Notification::GetService(void) const
53 {
54         Host::Ptr host = Host::GetByName(Get("host_name"));
55         String service = Get("service");
56
57         if (service.IsEmpty())
58                 return host->GetHostCheckService();
59         else
60                 return host->GetServiceByShortName(service);
61 }
62
63 Value Notification::GetNotificationCommand(void) const
64 {
65         return Get("notification_command");
66 }
67
68 Dictionary::Ptr Notification::GetMacros(void) const
69 {
70         return Get("macros");
71 }
72
73 void Notification::SendNotification(NotificationType type)
74 {
75         vector<Value> arguments;
76         arguments.push_back(static_cast<Notification::Ptr>(GetSelf()));
77         arguments.push_back(type);
78         ScriptTask::Ptr task;
79         task = InvokeMethod("notify", arguments, boost::bind(&Notification::NotificationCompletedHandler, this, _1));
80
81         if (!task) {
82                 Logger::Write(LogWarning, "icinga", "Notification object '" + GetName() + "' doesn't have a 'notify' method.");
83
84                 return;
85         }
86
87         if (!task->IsFinished()) {
88                 /* We need to keep the task object alive until the completion handler is called. */
89
90                 m_Tasks.insert(task);
91         }
92 }
93
94 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
95 {
96         m_Tasks.erase(task);
97
98         try {
99                 (void) task->GetResult();
100
101                 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
102         } catch (const exception& ex) {
103                 stringstream msgbuf;
104                 msgbuf << "Exception occured during notification for service '"
105                        << GetService()->GetName() << "': " << diagnostic_information(ex);
106                 String message = msgbuf.str();
107
108                 Logger::Write(LogWarning, "icinga", message);
109         }
110 }
111
112 void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
113 {
114         if (name == "host_name" || name == "service")
115                 Service::InvalidateNotificationsCache();
116 }