]> granicus.if.org Git - icinga2/commitdiff
Implemented support for disabling notifications.
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 26 Feb 2013 11:37:25 +0000 (12:37 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Tue, 26 Feb 2013 11:37:25 +0000 (12:37 +0100)
components/compat/compatcomponent.cpp
lib/icinga/externalcommandprocessor.cpp
lib/icinga/externalcommandprocessor.h
lib/icinga/service-check.cpp
lib/icinga/service-notification.cpp
lib/icinga/service.cpp
lib/icinga/service.h

index bf4490be75e25551a5fa1589711c250a9ffbcf1a..93a0794ece0ef19c90450455ed49d9d713519e85 100644 (file)
@@ -296,23 +296,38 @@ void CompatComponent::DumpHostStatus(ostream& fp, const Host::Ptr& host)
 
 void CompatComponent::DumpHostObject(ostream& fp, const Host::Ptr& host)
 {
-       ObjectLock olock(host);
-
-       fp << "define host {" << "\n"
-          << "\t" << "host_name" << "\t" << host->GetName() << "\n"
-          << "\t" << "display_name" << "\t" << host->GetDisplayName() << "\n"
-          << "\t" << "check_interval" << "\t" << 1 << "\n"
-          << "\t" << "retry_interval" << "\t" << 1 << "\n"
-          << "\t" << "max_check_attempts" << "\t" << 1 << "\n"
-          << "\t" << "active_checks_enabled" << "\t" << 1 << "\n"
-          << "\t" << "passive_checks_enabled" << "\t" << 1 << "\n";
-
-       set<Host::Ptr> parents = host->GetParentHosts();
-
-       if (!parents.empty()) {
-               fp << "\t" << "parents" << "\t";
-               DumpNameList(fp, parents);
-               fp << "\n";
+       Service::Ptr hc;
+
+       {
+               ObjectLock olock(host);
+
+               fp << "define host {" << "\n"
+                  << "\t" << "host_name" << "\t" << host->GetName() << "\n"
+                  << "\t" << "display_name" << "\t" << host->GetDisplayName() << "\n";
+
+               set<Host::Ptr> parents = host->GetParentHosts();
+
+               if (!parents.empty()) {
+                       fp << "\t" << "parents" << "\t";
+                       DumpNameList(fp, parents);
+                       fp << "\n";
+               }
+
+               hc = host->GetHostCheckService();
+       }
+
+       if (hc) {
+               ObjectLock olock(hc);
+
+               fp << "\t" << "check_interval" << "\t" << hc->GetCheckInterval() << "\n"
+                  << "\t" << "retry_interval" << "\t" << hc->GetRetryInterval() << "\n"
+                  << "\t" << "max_check_attempts" << "\t" << hc->GetMaxCheckAttempts() << "\n"
+                  << "\t" << "active_checks_enabled" << "\t" << (hc->GetEnableActiveChecks() ? 1 : 0) << "\n"
+                  << "\t" << "passive_checks_enabled" << "\t" << (hc->GetEnablePassiveChecks() ? 1 : 0) << "\n"
+                  << "\t" << "notifications_enabled" << "\t" << (hc->GetEnableNotifications() ? 1 : 0) << "\n"
+                  << "\t" << "notification_options" << "\t" << "d,u,r" << "\n"
+                  << "\t" << "notification_interval" << "\t" << "60" << "\n"
+                  << "\t" << "notification_period" << "\t" << "24x7" << "\n";
        }
 
        fp << "\t" << "}" << "\n"
@@ -377,14 +392,14 @@ void CompatComponent::DumpServiceStatusAttrs(ostream& fp, const Service::Ptr& se
                   << "\t" << "last_state_change=" << service->GetLastStateChange() << "\n"
                   << "\t" << "last_hard_state_change=" << service->GetLastHardStateChange() << "\n"
                   << "\t" << "last_update=" << time(NULL) << "\n"
+                  << "\t" << "notifications_enabled=" << (service->GetEnableNotifications() ? 1 : 0) << "\n"
                   << "\t" << "active_checks_enabled=" << (service->GetEnableActiveChecks() ? 1 : 0) <<"\n"
                   << "\t" << "passive_checks_enabled=" << (service->GetEnablePassiveChecks() ? 1 : 0) << "\n"
                   << "\t" << "problem_has_been_acknowledged=" << (service->GetAcknowledgement() != AcknowledgementNone ? 1 : 0) << "\n"
                   << "\t" << "acknowledgement_type=" << static_cast<int>(service->GetAcknowledgement()) << "\n"
                   << "\t" << "acknowledgement_end_time=" << service->GetAcknowledgementExpiry() << "\n"
                   << "\t" << "scheduled_downtime_depth=" << (service->IsInDowntime() ? 1 : 0) << "\n"
-                  << "\t" << "last_notification=" << service->GetLastNotification() << "\n"
-                  << "\t" << "next_notification=" << service->GetNextNotification() << "\n";
+                  << "\t" << "last_notification=" << service->GetLastNotification() << "\n";
        }
 }
 
@@ -448,6 +463,10 @@ void CompatComponent::DumpServiceObject(ostream& fp, const Service::Ptr& service
                   << "\t" << "max_check_attempts" << "\t" << 1 << "\n"
                   << "\t" << "active_checks_enabled" << "\t" << (service->GetEnableActiveChecks() ? 1 : 0) << "\n"
                   << "\t" << "passive_checks_enabled" << "\t" << (service->GetEnablePassiveChecks() ? 1 : 0) << "\n"
+                  << "\t" << "notifications_enabled" << "\t" << (service->GetEnableNotifications() ? 1 : 0) << "\n"
+                  << "\t" << "notification_options" << "\t" << "u,w,c,r" << "\n"
+                  << "\t" << "notification_interval" << "\t" << "60" << "\n"
+                  << "\t" << "notification_period" << "\t" << "24x7" << "\n"
                   << "\t" << "}" << "\n"
                   << "\n";
        }
@@ -508,8 +527,8 @@ void CompatComponent::StatusTimerHandler(void)
                 << "\t" << "program_start=" << startTime << "\n"
                 << "\t" << "active_service_checks_enabled=1" << "\n"
                 << "\t" << "passive_service_checks_enabled=1" << "\n"
-                << "\t" << "active_host_checks_enabled=0" << "\n"
-                << "\t" << "passive_host_checks_enabled=0" << "\n"
+                << "\t" << "active_host_checks_enabled=1" << "\n"
+                << "\t" << "passive_host_checks_enabled=1" << "\n"
                 << "\t" << "check_service_freshness=0" << "\n"
                 << "\t" << "check_host_freshness=0" << "\n"
                 << "\t" << "enable_notifications=1" << "\n"
@@ -531,6 +550,18 @@ void CompatComponent::StatusTimerHandler(void)
                 << "# This file is auto-generated. Do not modify this file." << "\n"
                 << "\n";
 
+       objectfp << "define timeperiod {"
+                << "\t" << "timeperiod_name" << "\t" << "24x7" << "\n"
+                << "\t" << "sunday" << "\t" << "00:00-24:00" << "\n"
+                << "\t" << "monday" << "\t" << "00:00-24:00" << "\n"
+                << "\t" << "tuesday" << "\t" << "00:00-24:00" << "\n"
+                << "\t" << "wednesday" << "\t" << "00:00-24:00" << "\n"
+                << "\t" << "thursday" << "\t" << "00:00-24:00" << "\n"
+                << "\t" << "friday" << "\t" << "00:00-24:00" << "\n"
+                << "\t" << "saturday" << "\t" << "00:00-24:00" << "\n"
+                << "\t" << "}" << "\n";
+
+
        BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Host")) {
                Host::Ptr host = static_pointer_cast<Host>(object);
 
index 7187ca15fc25b981e8ec552ad82aaf0182ded3f0..dcbf24606ef045dd1fc731fe100c193828e0e76c 100644 (file)
@@ -138,6 +138,12 @@ void ExternalCommandProcessor::Initialize(void)
        RegisterCommand("DEL_ALL_SVC_COMMENTS", &ExternalCommandProcessor::DelAllSvcComments);
        RegisterCommand("SEND_CUSTOM_HOST_NOTIFICATION", &ExternalCommandProcessor::SendCustomHostNotification);
        RegisterCommand("SEND_CUSTOM_SVC_NOTIFICATION", &ExternalCommandProcessor::SendCustomSvcNotification);
+       RegisterCommand("DELAY_HOST_NOTIFICATION", &ExternalCommandProcessor::DelayHostNotification);
+       RegisterCommand("DELAY_SVC_NOTIFICATION", &ExternalCommandProcessor::DelaySvcNotification);
+       RegisterCommand("ENABLE_HOST_NOTIFICATIONS", &ExternalCommandProcessor::EnableHostNotifications);
+       RegisterCommand("DISABLE_HOST_NOTIFICATIONS", &ExternalCommandProcessor::DisableHostNotifications);
+       RegisterCommand("ENABLE_SVC_NOTIFICATIONS", &ExternalCommandProcessor::EnableSvcNotifications);
+       RegisterCommand("DISABLE_SVC_NOTIFICATIONS", &ExternalCommandProcessor::DisableSvcNotifications);
 }
 
 /**
@@ -971,3 +977,78 @@ void ExternalCommandProcessor::SendCustomSvcNotification(double time, const vect
        Logger::Write(LogInformation, "icinga", "Sending custom notification for service " + service->GetName());
        service->RequestNotifications(NotificationCustom);
 }
+
+void ExternalCommandProcessor::DelayHostNotification(double time, const vector<String>& arguments)
+{
+       if (arguments.size() < 2)
+               BOOST_THROW_EXCEPTION(invalid_argument("Expected 2 arguments."));
+
+       Host::Ptr host = Host::GetByName(arguments[0]);
+
+       Logger::Write(LogInformation, "icinga", "Delaying notifications for host " + host->GetName());
+       Service::Ptr service = host->GetHostCheckService();
+       if (service) {
+               service->SetLastNotification(Convert::ToDouble(arguments[1]));
+       }
+}
+
+void ExternalCommandProcessor::DelaySvcNotification(double time, const vector<String>& arguments)
+{
+       if (arguments.size() < 3)
+               BOOST_THROW_EXCEPTION(invalid_argument("Expected 3 arguments."));
+
+       Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
+
+       Logger::Write(LogInformation, "icinga", "Delaying notifications for service " + service->GetName());
+       service->SetLastNotification(Convert::ToDouble(arguments[2]));
+}
+
+void ExternalCommandProcessor::EnableHostNotifications(double, const vector<String>& arguments)
+{
+       if (arguments.size() < 1)
+               BOOST_THROW_EXCEPTION(invalid_argument("Expected 1 argument."));
+
+       Host::Ptr host = Host::GetByName(arguments[0]);
+
+       Logger::Write(LogInformation, "icinga", "Enabling notifications for host '" + arguments[0] + "'");
+       Service::Ptr hc = host->GetHostCheckService();
+
+       if (hc)
+               hc->SetEnableNotifications(true);
+}
+
+void ExternalCommandProcessor::DisableHostNotifications(double, const vector<String>& arguments)
+{
+       if (arguments.size() < 1)
+               BOOST_THROW_EXCEPTION(invalid_argument("Expected 1 argument."));
+
+       Host::Ptr host = Host::GetByName(arguments[0]);
+
+       Logger::Write(LogInformation, "icinga", "Disabling notifications for host '" + arguments[0] + "'");
+       Service::Ptr hc = host->GetHostCheckService();
+
+       if (hc)
+               hc->SetEnableNotifications(false);
+}
+
+void ExternalCommandProcessor::EnableSvcNotifications(double, const vector<String>& arguments)
+{
+       if (arguments.size() < 2)
+               BOOST_THROW_EXCEPTION(invalid_argument("Expected 2 arguments."));
+
+       Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
+
+       Logger::Write(LogInformation, "icinga", "Enabling notifications for service '" + arguments[1] + "'");
+       service->SetEnableNotifications(true);
+}
+
+void ExternalCommandProcessor::DisableSvcNotifications(double, const vector<String>& arguments)
+{
+       if (arguments.size() < 2)
+               BOOST_THROW_EXCEPTION(invalid_argument("Expected 2 arguments."));
+
+       Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
+
+       Logger::Write(LogInformation, "icinga", "Disabling notifications for service '" + arguments[1] + "'");
+       service->SetEnableNotifications(false);
+}
index 57a9ee6f6ad7780c31d8cd86d4cc5b9a5e6ba2a8..db2ab65aaddb57c1e755dd78a8ff9b0ae9903f9d 100644 (file)
@@ -92,6 +92,12 @@ private:
        static void DelAllSvcComments(double time, const vector<String>& arguments);
        static void SendCustomHostNotification(double time, const vector<String>& arguments);
        static void SendCustomSvcNotification(double time, const vector<String>& arguments);
+       static void DelayHostNotification(double time, const vector<String>& arguments);
+       static void DelaySvcNotification(double time, const vector<String>& arguments);
+       static void EnableHostNotifications(double time, const vector<String>& arguments);
+       static void DisableHostNotifications(double time, const vector<String>& arguments);
+       static void EnableSvcNotifications(double time, const vector<String>& arguments);
+       static void DisableSvcNotifications(double time, const vector<String>& arguments);
 };
 
 }
index 78895cf05e031406e95046b7932e5e2586565dd6..f1874d183c9a85ab8be2152920ed5c53f0f3c94c 100644 (file)
@@ -206,8 +206,8 @@ bool Service::GetEnableActiveChecks(void) const
 {
        if (m_EnableActiveChecks.IsEmpty())
                return true;
-
-       return static_cast<bool>(m_EnableActiveChecks);
+       else
+               return m_EnableActiveChecks;
 }
 
 void Service::SetEnableActiveChecks(bool enabled)
@@ -220,8 +220,8 @@ bool Service::GetEnablePassiveChecks(void) const
 {
        if (m_EnablePassiveChecks.IsEmpty())
                return true;
-
-       return static_cast<bool>(m_EnablePassiveChecks);
+       else
+               return m_EnablePassiveChecks;
 }
 
 void Service::SetEnablePassiveChecks(bool enabled)
index a19180ed204a1f7c72ab9fb7a34346bc765dc7d3..88d4aa43de19589700a139593ffee8cb99d3beb5 100644 (file)
@@ -41,6 +41,11 @@ void Service::RequestNotifications(NotificationType type) const
 
 void Service::SendNotifications(NotificationType type)
 {
+       if (!GetEnableNotifications()) {
+               Logger::Write(LogInformation, "icinga", "Notifications are disabled for service '" + GetName() + "'.");
+               return;
+       }
+
        Logger::Write(LogInformation, "icinga", "Sending notifications for service '" + GetName() + "'");
 
        set<Notification::Ptr> notifications = GetNotifications();
@@ -239,12 +244,10 @@ void Service::UpdateSlaveNotifications(const Service::Ptr& self)
 
 double Service::GetLastNotification(void) const
 {
-       Value value = m_LastNotification;
-
-       if (value.IsEmpty())
-               value = 0;
-
-       return value;
+       if (m_LastNotification.IsEmpty())
+               return 0;
+       else
+               return m_LastNotification;
 }
 
 void Service::SetLastNotification(double time)
@@ -253,18 +256,16 @@ void Service::SetLastNotification(double time)
        Touch("last_notification");
 }
 
-double Service::GetNextNotification(void) const
+bool Service::GetEnableNotifications(void) const
 {
-       Value value = m_NextNotification;
-
-       if (value.IsEmpty())
-               value = 0;
-
-       return value;
+       if (m_EnableNotifications.IsEmpty())
+               return true;
+       else
+               return m_EnableNotifications;
 }
 
-void Service::SetNextNotification(double time)
+void Service::SetEnableNotifications(bool enabled)
 {
-       m_NextNotification = time;
-       Touch("next_notification");
+       m_EnableNotifications = enabled;
+       Touch("enable_notifications");
 }
index a5410e549b3409624f0e91bb36f04e43d27d2cea..cf18b6a07a695b31902c43a167cc6c188e616af5 100644 (file)
@@ -61,8 +61,8 @@ Service::Service(const Dictionary::Ptr& serializedObject)
 
        RegisterAttribute("downtimes", Attribute_Replicated, &m_Downtimes);
 
+       RegisterAttribute("enable_notifications", Attribute_Replicated, &m_EnableNotifications);
        RegisterAttribute("last_notification", Attribute_Replicated, &m_LastNotification);
-       RegisterAttribute("next_notification", Attribute_Replicated, &m_NextNotification);
 
        SetSchedulingOffset(rand());
 }
index 705eb1c2ac6bfb911252099fb8201e1f3ff8d9aa..f6677041fc9e1e38fba786a70b7f588b419d2920 100644 (file)
@@ -235,6 +235,9 @@ public:
        static void ValidateCommentsCache(void);
 
        /* Notifications */
+       bool GetEnableNotifications(void) const;
+       void SetEnableNotifications(bool enabled);
+
        void RequestNotifications(NotificationType type) const;
        void SendNotifications(NotificationType type);
 
@@ -248,9 +251,6 @@ public:
        double GetLastNotification(void) const;
        void SetLastNotification(double time);
 
-       double GetNextNotification(void) const;
-       void SetNextNotification(double time);
-
 protected:
        virtual void OnRegistrationCompleted(void);
        virtual void OnAttributeChanged(const String& name, const Value& oldValue);
@@ -324,8 +324,8 @@ private:
        void RemoveExpiredComments(void);
 
        /* Notifications */
+       Attribute<bool> m_EnableNotifications;
        Attribute<double> m_LastNotification;
-       Attribute<double> m_NextNotification;
 
        static map<String, set<Notification::WeakPtr> > m_NotificationsCache;
        static bool m_NotificationsCacheValid;