]> granicus.if.org Git - icinga2/commitdiff
Implement the 'notification_period' attribute.
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 21 Mar 2013 12:42:46 +0000 (13:42 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 21 Mar 2013 12:42:46 +0000 (13:42 +0100)
components/notification/notificationcomponent.cpp
lib/icinga/icinga-type.conf
lib/icinga/notification.cpp
lib/icinga/notification.h
lib/icinga/service-notification.cpp
lib/icinga/user.cpp
lib/icinga/user.h

index 0ea55fe3c2055ebd3385c3c8c18313c5ae062478..358da30527729cfa88760a16aa33a38e9ffe7741 100644 (file)
@@ -92,12 +92,14 @@ void NotificationComponent::NotificationTimerHandler(void)
                        send_notification = reachable && !service->IsInDowntime() && !service->IsAcknowledged();
                }
 
+               notification->SetNextNotification(Utility::GetTime() + notification->GetNotificationInterval());
+
                if (!send_notification)
                        continue;
 
                try {
                        Log(LogInformation, "notification", "Sending reminder notification for service '" + service->GetName() + "'");
-                       notification->BeginExecuteNotification(NotificationProblem, service->GetLastCheckResult());
+                       notification->BeginExecuteNotification(NotificationProblem, service->GetLastCheckResult(), false);
                } catch (const std::exception& ex) {
                        std::ostringstream msgbuf;
                        msgbuf << "Exception occured during notification for service '"
@@ -106,8 +108,6 @@ void NotificationComponent::NotificationTimerHandler(void)
 
                        Log(LogWarning, "icinga", message);
                }
-
-               notification->SetNextNotification(Utility::GetTime() + notification->GetNotificationInterval());
        }
 }
 
index eae1fbffb305e5e1fa5387fd486270bb87f348f4..1c89e8516c8ff901e95b0a50a60af251681b636f 100644 (file)
@@ -54,6 +54,9 @@ type Host {
                        %attribute number "check_interval",
                        %attribute number "retry_interval",
 
+                       %attribute number "notification_interval",
+                       %attribute string "notification_period",
+
                        %attribute array "servicegroups" {
                                %attribute string "*"
                        },
@@ -99,7 +102,10 @@ type Host {
        %attribute string "check_period",
        %attribute number "check_interval",
        %attribute number "retry_interval",
+
        %attribute number "notification_interval",
+       %attribute string "notification_period",
+
        %attribute dictionary "macros" {
                %attribute string "*"
        },
@@ -145,6 +151,10 @@ type Service {
        %attribute string "check_period",
        %attribute number "check_interval",
        %attribute number "retry_interval",
+
+       %attribute number "notification_interval",
+       %attribute string "notification_period",
+
        %attribute array "hostdependencies" {
                %attribute string "*"
        },
@@ -187,9 +197,7 @@ type Service {
                                %attribute string "*"
                        }
                }
-       },
-
-       %attribute number "notification_interval"
+       }
 }
 
 type ServiceGroup {
@@ -225,7 +233,8 @@ type Notification {
        },
        %attribute string "notification_command",
 
-       %attribute number "notification_interval"
+       %attribute number "notification_interval",
+       %attribute string "notification_period"
 }
 
 type User {
index 1a56b8c3b2bd0d45df3ebabaad4f6f664b4ac5d1..228138348d403677bef72ea76f8a25c18e6a92e8 100644 (file)
@@ -36,6 +36,7 @@ Notification::Notification(const Dictionary::Ptr& serializedUpdate)
 {
        RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
        RegisterAttribute("notification_interval", Attribute_Config, &m_NotificationInterval);
+       RegisterAttribute("notification_period", Attribute_Config, &m_NotificationPeriod);
        RegisterAttribute("last_notification", Attribute_Replicated, &m_LastNotification);
        RegisterAttribute("next_notification", Attribute_Replicated, &m_NextNotification);
        RegisterAttribute("macros", Attribute_Config, &m_Macros);
@@ -153,6 +154,14 @@ double Notification::GetNotificationInterval(void) const
                return m_NotificationInterval;
 }
 
+/**
+ * @threadsafety Always.
+ */
+TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
+{
+       return TimePeriod::GetByName(m_NotificationPeriod);
+}
+
 /**
  * @threadsafety Always.
  */
@@ -222,10 +231,19 @@ String Notification::NotificationTypeToString(NotificationType type)
 /**
  * @threadsafety Always.
  */
-void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr)
+void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool ignore_timeperiod)
 {
        ASSERT(!OwnsLock());
 
+       if (!ignore_timeperiod) {
+               TimePeriod::Ptr tp = GetNotificationPeriod();
+
+               if (tp && !tp->IsInside(Utility::GetTime())) {
+                       Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
+                       return;
+               }
+       }
+
        {
                ObjectLock olock(this);
 
@@ -246,29 +264,33 @@ void Notification::BeginExecuteNotification(NotificationType type, const Diction
 
        BOOST_FOREACH(const User::Ptr& user, allUsers) {
                Log(LogDebug, "icinga", "Sending notification for user " + user->GetName());
-               BeginExecuteNotificationHelper(macros, type, user);
-       }
-
-       if (allUsers.empty()) {
-               /* Send a notification even if there are no users specified. */
-               BeginExecuteNotificationHelper(macros, type, User::Ptr());
+               BeginExecuteNotificationHelper(macros, type, user, ignore_timeperiod);
        }
 }
 
 /**
  * @threadsafety Always.
  */
-void Notification::BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
+void Notification::BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros,
+    NotificationType type, const User::Ptr& user, bool ignore_timeperiod)
 {
        ASSERT(!OwnsLock());
 
-       std::vector<Dictionary::Ptr> macroDicts;
+       if (!ignore_timeperiod) {
+               TimePeriod::Ptr tp = user->GetNotificationPeriod();
 
-       if (user) {
-               macroDicts.push_back(user->GetMacros());
-               macroDicts.push_back(user->CalculateDynamicMacros());
+               if (tp && !tp->IsInside(Utility::GetTime())) {
+                       Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
+                           GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
+                       return;
+               }
        }
 
+       std::vector<Dictionary::Ptr> macroDicts;
+
+       macroDicts.push_back(user->GetMacros());
+       macroDicts.push_back(user->CalculateDynamicMacros());
+
        macroDicts.push_back(notificationMacros);
 
        Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
index 256c260e24867f0203443944815cda4a01d544e1..d9bf03e7dd51aa6027bef08d6f6c3435702a437d 100644 (file)
@@ -23,6 +23,7 @@
 #include "icinga/i2-icinga.h"
 #include "icinga/user.h"
 #include "icinga/usergroup.h"
+#include "icinga/timeperiod.h"
 #include "base/array.h"
 
 namespace icinga
@@ -65,6 +66,7 @@ public:
        shared_ptr<Service> GetService(void) const;
        Value GetNotificationCommand(void) const;
        double GetNotificationInterval(void) const;
+       TimePeriod::Ptr GetNotificationPeriod(void) const;
        Dictionary::Ptr GetMacros(void) const;
        std::set<User::Ptr> GetUsers(void) const;
        std::set<UserGroup::Ptr> GetGroups(void) const;
@@ -75,7 +77,7 @@ public:
        double GetNextNotification(void) const;
        void SetNextNotification(double time);
 
-       void BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr);
+       void BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool ignore_timeperiod);
 
        static String NotificationTypeToString(NotificationType type);
 
@@ -85,6 +87,7 @@ protected:
 private:
        Attribute<Value> m_NotificationCommand;
        Attribute<double> m_NotificationInterval;
+       Attribute<String> m_NotificationPeriod;
        Attribute<double> m_LastNotification;
        Attribute<double> m_NextNotification;
        Attribute<Dictionary::Ptr> m_Macros;
@@ -98,7 +101,7 @@ private:
        void NotificationCompletedHandler(const ScriptTask::Ptr& task);
 
        void BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros,
-           NotificationType type, const User::Ptr& user);
+           NotificationType type, const User::Ptr& user, bool ignore_timeperiod);
 };
 
 }
index 2da027df43aeb2e56cb25b205a4cbcb75652f559..016c43e46c34a0e39b6a4442350b7d2a1a5a6388 100644 (file)
@@ -60,12 +60,15 @@ void Service::RequestNotifications(NotificationType type, const Dictionary::Ptr&
  */
 void Service::SendNotifications(NotificationType type, const Dictionary::Ptr& cr)
 {
+       bool force = false;
+
        if (!GetEnableNotifications()) {
                if (!GetForceNextNotification()) {
                        Log(LogInformation, "icinga", "Notifications are disabled for service '" + GetName() + "'.");
                        return;
                }
 
+               force = true;
                SetForceNextNotification(false);
        }
 
@@ -78,7 +81,7 @@ void Service::SendNotifications(NotificationType type, const Dictionary::Ptr& cr
 
        BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
                try {
-                       notification->BeginExecuteNotification(type, cr);
+                       notification->BeginExecuteNotification(type, cr, force);
                } catch (const std::exception& ex) {
                        std::ostringstream msgbuf;
                        msgbuf << "Exception occured during notification for service '"
@@ -254,6 +257,7 @@ void Service::UpdateSlaveNotifications(void)
                        keys.insert("users");
                        keys.insert("groups");
                        keys.insert("notification_interval");
+                       keys.insert("notification_period");
 
                        ExpressionList::Ptr svc_exprl = boost::make_shared<ExpressionList>();
                        item->GetLinkedExpressionList()->ExtractFiltered(keys, svc_exprl);
index 41a843b767880a1e3e1bca65f81ecc12d9a535f2..d384a435a5ec1b060265b6a9bb4a098ab19806c9 100644 (file)
@@ -32,6 +32,7 @@ User::User(const Dictionary::Ptr& serializedUpdate)
        RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
        RegisterAttribute("macros", Attribute_Config, &m_Macros);
        RegisterAttribute("groups", Attribute_Config, &m_Groups);
+       RegisterAttribute("notification_period", Attribute_Config, &m_NotificationPeriod);
 }
 
 User::~User(void)
@@ -87,6 +88,11 @@ Dictionary::Ptr User::GetMacros(void) const
        return m_Macros;
 }
 
+TimePeriod::Ptr User::GetNotificationPeriod(void) const
+{
+       return TimePeriod::GetByName(m_NotificationPeriod);
+}
+
 /**
  * @threadsafety Always.
  */
index dd0e849b6d60e1e504a37c4a0115f7f05ba42d1f..53d33d243c3b4124791ef872229a2655bd725b96 100644 (file)
@@ -21,6 +21,7 @@
 #define USER_H
 
 #include "icinga/i2-icinga.h"
+#include "icinga/timeperiod.h"
 #include "base/dynamicobject.h"
 #include "base/array.h"
 
@@ -45,6 +46,7 @@ public:
 
        String GetDisplayName(void) const;
        Array::Ptr GetGroups(void) const;
+       TimePeriod::Ptr GetNotificationPeriod(void) const;
 
        Dictionary::Ptr GetMacros(void) const;
        Dictionary::Ptr CalculateDynamicMacros(void) const;
@@ -55,6 +57,7 @@ protected:
 private:
        Attribute<String> m_DisplayName;
        Attribute<Dictionary::Ptr> m_Macros;
+       Attribute<String> m_NotificationPeriod;
        Attribute<Array::Ptr> m_Groups;
 };