From 163f0a9f5c9f750a2ed6e3e2f51b63ea69ef68c4 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 26 Feb 2013 12:37:25 +0100 Subject: [PATCH] Implemented support for disabling notifications. --- components/compat/compatcomponent.cpp | 67 ++++++++++++++------ lib/icinga/externalcommandprocessor.cpp | 81 +++++++++++++++++++++++++ lib/icinga/externalcommandprocessor.h | 6 ++ lib/icinga/service-check.cpp | 8 +-- lib/icinga/service-notification.cpp | 33 +++++----- lib/icinga/service.cpp | 2 +- lib/icinga/service.h | 8 +-- 7 files changed, 162 insertions(+), 43 deletions(-) diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index bf4490be7..93a0794ec 100644 --- a/components/compat/compatcomponent.cpp +++ b/components/compat/compatcomponent.cpp @@ -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); + Service::Ptr hc; - 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"; + { + ObjectLock olock(host); - set parents = host->GetParentHosts(); + fp << "define host {" << "\n" + << "\t" << "host_name" << "\t" << host->GetName() << "\n" + << "\t" << "display_name" << "\t" << host->GetDisplayName() << "\n"; - if (!parents.empty()) { - fp << "\t" << "parents" << "\t"; - DumpNameList(fp, parents); - fp << "\n"; + set 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(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(object); diff --git a/lib/icinga/externalcommandprocessor.cpp b/lib/icinga/externalcommandprocessor.cpp index 7187ca15f..dcbf24606 100644 --- a/lib/icinga/externalcommandprocessor.cpp +++ b/lib/icinga/externalcommandprocessor.cpp @@ -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& 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& 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& 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& 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& 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& 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); +} diff --git a/lib/icinga/externalcommandprocessor.h b/lib/icinga/externalcommandprocessor.h index 57a9ee6f6..db2ab65aa 100644 --- a/lib/icinga/externalcommandprocessor.h +++ b/lib/icinga/externalcommandprocessor.h @@ -92,6 +92,12 @@ private: static void DelAllSvcComments(double time, const vector& arguments); static void SendCustomHostNotification(double time, const vector& arguments); static void SendCustomSvcNotification(double time, const vector& arguments); + static void DelayHostNotification(double time, const vector& arguments); + static void DelaySvcNotification(double time, const vector& arguments); + static void EnableHostNotifications(double time, const vector& arguments); + static void DisableHostNotifications(double time, const vector& arguments); + static void EnableSvcNotifications(double time, const vector& arguments); + static void DisableSvcNotifications(double time, const vector& arguments); }; } diff --git a/lib/icinga/service-check.cpp b/lib/icinga/service-check.cpp index 78895cf05..f1874d183 100644 --- a/lib/icinga/service-check.cpp +++ b/lib/icinga/service-check.cpp @@ -206,8 +206,8 @@ bool Service::GetEnableActiveChecks(void) const { if (m_EnableActiveChecks.IsEmpty()) return true; - - return static_cast(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(m_EnablePassiveChecks); + else + return m_EnablePassiveChecks; } void Service::SetEnablePassiveChecks(bool enabled) diff --git a/lib/icinga/service-notification.cpp b/lib/icinga/service-notification.cpp index a19180ed2..88d4aa43d 100644 --- a/lib/icinga/service-notification.cpp +++ b/lib/icinga/service-notification.cpp @@ -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 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"); } diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index a5410e549..cf18b6a07 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -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()); } diff --git a/lib/icinga/service.h b/lib/icinga/service.h index 705eb1c2a..f6677041f 100644 --- a/lib/icinga/service.h +++ b/lib/icinga/service.h @@ -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 m_EnableNotifications; Attribute m_LastNotification; - Attribute m_NextNotification; static map > m_NotificationsCache; static bool m_NotificationsCacheValid;