]> granicus.if.org Git - icinga2/commitdiff
Implemented the User class.
authorGunnar Beutner <gunnar.beutner@netways.de>
Sun, 24 Feb 2013 07:26:10 +0000 (08:26 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Sun, 24 Feb 2013 07:26:10 +0000 (08:26 +0100)
itl/types.conf
lib/icinga/Makefile.am
lib/icinga/i2-icinga.h
lib/icinga/icinga.vcxproj
lib/icinga/notification.cpp
lib/icinga/notification.h
lib/icinga/service-notification.cpp
lib/icinga/user.cpp [new file with mode: 0644]
lib/icinga/user.h [new file with mode: 0644]

index 9f39568978b64985ab38901d7b44eb92a9a24e8d..0344b3ecdc4d1fd2759b85a681db6996eaba270c 100644 (file)
@@ -113,6 +113,10 @@ type Host {
 
                        %attribute dictionary "macros" {
                                %attribute string "*"
+                       },
+
+                       %attribute dictionary "users" {
+                               %attribute string "*"
                        }
                }
        },
@@ -194,11 +198,16 @@ type Service {
        %attribute dictionary "notifications" {
                %attribute string "*",
                %attribute dictionary "*" {
-                       %require "notification",
-                       %attribute string "notification",
+                       %attribute dictionary "templates" {
+                               %attribute string "*"
+                       },
 
                        %attribute dictionary "macros" {
                                %attribute string "*"
+                       },
+
+                       %attribute dictionary "users" {
+                               %attribute string "*"
                        }
                }
        }
@@ -247,3 +256,10 @@ type Script {
        %require "code",
        %attribute string "code"
 }
+
+type User {
+       %attribute dictionary "macros" {
+               %attribute string "*"
+       }
+}
+
index 9e09a230c00ac27ed2fcc440f8deb00c6f3238f1..9dba6e9f5dea62cd34370f4c22abfcb7210928cf 100644 (file)
@@ -40,7 +40,9 @@ libicinga_la_SOURCES =  \
        service-notification.cpp \
        service.h \
        servicegroup.cpp \
-       servicegroup.h
+       servicegroup.h \
+       user.cpp \
+       user.h
 
 libicinga_la_CPPFLAGS = \
        -DI2_ICINGA_BUILD \
index d31ac7bd01492ab4ff8e6705d994df5b967cf1a7..cb6f9c66d3e94c9ae50cff2657cfc0d403f2caa1 100644 (file)
@@ -46,6 +46,8 @@ using boost::algorithm::is_any_of;
 #include "endpointmanager.h"
 #include "icingaapplication.h"
 
+#include "user.h"
+
 #include "notification.h"
 #include "notificationrequestmessage.h"
 
index a81ea70f7bac8cd51a732fa165fb2a25c21f4bf0..44201f77611f49abacffc35bfd48a8c322780e9d 100644 (file)
@@ -44,6 +44,7 @@
     <ClCompile Include="service-comment.cpp" />
     <ClCompile Include="service-downtime.cpp" />
     <ClCompile Include="servicegroup.cpp" />
+    <ClCompile Include="user.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="api.h" />
@@ -62,6 +63,7 @@
     <ClInclude Include="pluginnotificationtask.h" />
     <ClInclude Include="service.h" />
     <ClInclude Include="servicegroup.h" />
+    <ClInclude Include="user.h" />
   </ItemGroup>
   <PropertyGroup Label="Globals">
     <ProjectGuid>{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}</ProjectGuid>
index dd7d67e98eaaa9cb952ed6bd362c552bd02f956e..4e8e574e9873ef193156d9f86e2c22b2850ae4e6 100644 (file)
@@ -70,6 +70,22 @@ Dictionary::Ptr Notification::GetMacros(void) const
        return Get("macros");
 }
 
+set<User::Ptr> Notification::GetUsers(void) const
+{
+       set<User::Ptr> result;
+
+       Dictionary::Ptr users = Get("users");
+
+       if (users) {
+               String name;
+               BOOST_FOREACH(tie(tuples::ignore, name), users) {
+                       result.insert(User::GetByName(name));
+               }
+       }
+
+       return result;
+}
+
 String Notification::NotificationTypeToString(NotificationType type)
 {
        switch (type) {
@@ -100,11 +116,13 @@ void Notification::BeginExecuteNotification(const Notification::Ptr& self, Notif
        macroDicts.push_back(notificationMacros);
 
        Service::Ptr service;
+       set<User::Ptr> users;
 
        {
                ObjectLock olock(self);
                macroDicts.push_back(self->GetMacros());
                service = self->GetService();
+               users = self->GetUsers();
        }
 
        Host::Ptr host;
@@ -136,15 +154,45 @@ void Notification::BeginExecuteNotification(const Notification::Ptr& self, Notif
 
        Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
 
-       vector<Value> arguments;
-       arguments.push_back(self);
-       arguments.push_back(macros);
-       arguments.push_back(type);
+       int count = 0;
+
+       BOOST_FOREACH(const User::Ptr& user, users) {
+               BeginExecuteNotificationHelper(self, macros, type, user);
+               count++;
+       }
+
+       if (count == 0) {
+               /* Send a notification even if there are no users specified. */
+               BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
+       }
+}
+
+void Notification::BeginExecuteNotificationHelper(const Notification::Ptr& self, const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
+{
+       vector<Dictionary::Ptr> macroDicts;
+
+       if (user) {
+               {
+                       ObjectLock olock(self);
+                       macroDicts.push_back(user->GetMacros());
+               }
+
+               macroDicts.push_back(User::CalculateDynamicMacros(user));
+       }
+
+       macroDicts.push_back(notificationMacros);
+
+       Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
 
        ScriptTask::Ptr task;
 
        {
                ObjectLock olock(self);
+
+               vector<Value> arguments;
+               arguments.push_back(self);
+               arguments.push_back(macros);
+               arguments.push_back(type);
                task = self->MakeMethodTask("notify", arguments);
 
                if (!task) {
index 4aa16a1a2cef2b31fcde3c67273039672fa7c8e2..176fbee8f76f2d3251bf5a5cce3568300d89125d 100644 (file)
@@ -60,6 +60,7 @@ public:
        shared_ptr<Service> GetService(void) const;
        Value GetNotificationCommand(void) const;
        Dictionary::Ptr GetMacros(void) const;
+       set<User::Ptr> GetUsers(void) const;
 
        static void BeginExecuteNotification(const Notification::Ptr& self, NotificationType type);
 
@@ -72,6 +73,9 @@ private:
        set<ScriptTask::Ptr> m_Tasks;
 
        void NotificationCompletedHandler(const ScriptTask::Ptr& task);
+
+       static void BeginExecuteNotificationHelper(const Notification::Ptr& self,
+           const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user);
 };
 
 }
index 36ade2b666def32627353cf8b85f64a597d86b06..e2a97ff6a834e307af57df1ff66e364f4f269222 100644 (file)
@@ -49,7 +49,16 @@ void Service::SendNotifications(NotificationType type)
                Logger::Write(LogInformation, "icinga", "Service '" + GetName() + "' does not have any notifications.");
 
        BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
-               Notification::BeginExecuteNotification(notification, type);
+               try {
+                       Notification::BeginExecuteNotification(notification, type);
+               } catch (const exception& ex) {
+                       stringstream msgbuf;
+                       msgbuf << "Exception occured during notification for service '"
+                              << GetName() << "': " << diagnostic_information(ex);
+                       String message = msgbuf.str();
+
+                       Logger::Write(LogWarning, "icinga", message);
+               }
        }
 
        SetLastNotification(Utility::GetTime());
@@ -105,6 +114,10 @@ static void CopyNotificationAttributes(TDict notificationDesc, const ConfigItemB
        if (!macros.IsEmpty())
                builder->AddExpression("macros", OperatorPlus, macros);
 
+       Value users = notificationDesc->Get("users");
+       if (!users.IsEmpty())
+               builder->AddExpression("users", OperatorPlus, users);
+
        /*Value notificationInterval = notificationDesc->Get("notification_interval");
        if (!notificationInterval.IsEmpty())
                builder->AddExpression("notification_interval", OperatorSet, notificationInterval);*/
diff --git a/lib/icinga/user.cpp b/lib/icinga/user.cpp
new file mode 100644 (file)
index 0000000..5c5ba85
--- /dev/null
@@ -0,0 +1,63 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#include "i2-icinga.h"
+
+using namespace icinga;
+
+REGISTER_TYPE(User, NULL);
+
+User::User(const Dictionary::Ptr& properties)
+       : DynamicObject(properties)
+{ }
+
+bool User::Exists(const String& name)
+{
+       return (DynamicObject::GetObject("User", name));
+}
+
+User::Ptr User::GetByName(const String& name)
+{
+       DynamicObject::Ptr configObject = DynamicObject::GetObject("User", name);
+
+       if (!configObject)
+               BOOST_THROW_EXCEPTION(invalid_argument("User '" + name + "' does not exist."));
+
+       return dynamic_pointer_cast<User>(configObject);
+}
+
+Dictionary::Ptr User::GetMacros(void) const
+{
+       return Get("macros");
+}
+
+Dictionary::Ptr User::CalculateDynamicMacros(const User::Ptr& self)
+{
+       Dictionary::Ptr macros = boost::make_shared<Dictionary>();
+
+       {
+               ObjectLock olock(self);
+               macros->Set("CONTACTNAME", self->GetName());
+               macros->Set("CONTACTALIAS", self->GetName());
+       }
+
+       macros->Seal();
+
+       return macros;
+}
diff --git a/lib/icinga/user.h b/lib/icinga/user.h
new file mode 100644 (file)
index 0000000..70dad24
--- /dev/null
@@ -0,0 +1,48 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#ifndef USER_H
+#define USER_H
+
+namespace icinga
+{
+
+/**
+ * A User.
+ *
+ * @ingroup icinga
+ */
+class I2_ICINGA_API User : public DynamicObject
+{
+public:
+       typedef shared_ptr<User> Ptr;
+       typedef weak_ptr<User> WeakPtr;
+
+       User(const Dictionary::Ptr& properties);
+
+       static bool Exists(const String& name);
+       static User::Ptr GetByName(const String& name);
+
+       Dictionary::Ptr GetMacros(void) const;
+       static Dictionary::Ptr CalculateDynamicMacros(const User::Ptr& self);
+};
+
+}
+
+#endif /* USER_H */