From ae1787859682228efc02c9467e564374858cb200 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sun, 27 Jan 2013 11:35:47 +0100 Subject: [PATCH] Implemented host acknowledgements. --- lib/icinga/Makefile.am | 1 + lib/icinga/acknowledgement.h | 40 ++++++++++++++++++++ lib/icinga/externalcommand.cpp | 61 ++++++++++++++++++++++++++++++- lib/icinga/externalcommand.h | 3 ++ lib/icinga/host.cpp | 47 +++++++++++++++++++++++- lib/icinga/host.h | 7 ++++ lib/icinga/i2-icinga.h | 2 + lib/icinga/icinga.vcxproj | 1 + lib/icinga/icinga.vcxproj.filters | 3 ++ lib/icinga/service.h | 12 ------ 10 files changed, 163 insertions(+), 14 deletions(-) create mode 100644 lib/icinga/acknowledgement.h diff --git a/lib/icinga/Makefile.am b/lib/icinga/Makefile.am index e6c9a6dd7..0650d2e29 100644 --- a/lib/icinga/Makefile.am +++ b/lib/icinga/Makefile.am @@ -5,6 +5,7 @@ pkglib_LTLIBRARIES = \ libicinga.la libicinga_la_SOURCES = \ + acknowledgement.h \ cib.cpp \ cib.h \ externalcommand.cpp \ diff --git a/lib/icinga/acknowledgement.h b/lib/icinga/acknowledgement.h new file mode 100644 index 000000000..eb0fbf0e5 --- /dev/null +++ b/lib/icinga/acknowledgement.h @@ -0,0 +1,40 @@ +/****************************************************************************** + * 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 ACKNOWLEDGEMENT_H +#define ACKNOWLEDGEMENT_H + +namespace icinga +{ + +/** + * The acknowledgement type of a host/service. + * + * @ingroup icinga + */ +enum AcknowledgementType +{ + AcknowledgementNone = 0, + AcknowledgementNormal = 1, + AcknowledgementSticky = 2 +}; + +} + +#endif /* ACKNOWLEDGEMENT_H */ diff --git a/lib/icinga/externalcommand.cpp b/lib/icinga/externalcommand.cpp index c0d9883aa..79a4ed13e 100644 --- a/lib/icinga/externalcommand.cpp +++ b/lib/icinga/externalcommand.cpp @@ -70,7 +70,10 @@ void ExternalCommand::Execute(double time, const String& command, const vector service->SetAcknowledgementExpiry(0); } +void ExternalCommand::AcknowledgeHostProblem(double time, const vector& arguments) +{ + if (arguments.size() < 7) + throw_exception(invalid_argument("Expected 7 arguments.")); + + if (!Host::Exists(arguments[1])) + throw_exception(invalid_argument("The host '" + arguments[1] + "' does not exist.")); + + int sticky = arguments[2].ToDouble(); + + Host::Ptr host = Host::GetByName(arguments[1]); + + if (host->IsUp()) + throw_exception(invalid_argument("The host '" + arguments[1] + "' is OK.")); + + Logger::Write(LogInformation, "icinga", "Setting acknowledgement for host '" + host->GetName() + "'"); + host->SetAcknowledgement(sticky ? AcknowledgementSticky : AcknowledgementNormal); + host->SetAcknowledgementExpiry(0); +} + +void ExternalCommand::AcknowledgeHostProblemExpire(double time, const vector& arguments) +{ + if (arguments.size() < 8) + throw_exception(invalid_argument("Expected 8 arguments.")); + + if (!Host::Exists(arguments[1])) + throw_exception(invalid_argument("The host '" + arguments[1] + "' does not exist.")); + + int sticky = arguments[2].ToDouble(); + double timestamp = arguments[5].ToDouble(); + + Host::Ptr host = Host::GetByName(arguments[1]); + + if (host->IsUp()) + throw_exception(invalid_argument("The host '" + arguments[1] + "' is OK.")); + + Logger::Write(LogInformation, "icinga", "Setting timed acknowledgement for host '" + host->GetName() + "'"); + host->SetAcknowledgement(sticky ? AcknowledgementSticky : AcknowledgementNormal); + host->SetAcknowledgementExpiry(timestamp); +} + +void ExternalCommand::RemoveHostAcknowledgement(double time, const vector& arguments) +{ + if (arguments.size() < 2) + throw_exception(invalid_argument("Expected 2 arguments.")); + + if (!Host::Exists(arguments[1])) + throw_exception(invalid_argument("The host '" + arguments[1] + "' does not exist.")); + + Host::Ptr host = Host::GetByName(arguments[1]); + + Logger::Write(LogInformation, "icinga", "Removing acknowledgement for host '" + host->GetName() + "'"); + host->SetAcknowledgement(AcknowledgementNone); + host->SetAcknowledgementExpiry(0); +} + void ExternalCommand::EnableHostgroupSvcChecks(double time, const vector& arguments) { if (arguments.size() < 1) diff --git a/lib/icinga/externalcommand.h b/lib/icinga/externalcommand.h index 973113bdf..fd24708ee 100644 --- a/lib/icinga/externalcommand.h +++ b/lib/icinga/externalcommand.h @@ -42,6 +42,9 @@ public: static void AcknowledgeSvcProblem(double time, const vector& arguments); static void AcknowledgeSvcProblemExpire(double time, const vector& arguments); static void RemoveSvcAcknowledgement(double time, const vector& arguments); + static void AcknowledgeHostProblem(double time, const vector& arguments); + static void AcknowledgeHostProblemExpire(double time, const vector& arguments); + static void RemoveHostAcknowledgement(double time, const vector& arguments); static void EnableHostgroupSvcChecks(double time, const vector& arguments); static void DisableHostgroupSvcChecks(double time, const vector& arguments); static void EnableServicegroupSvcChecks(double time, const vector& arguments); diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp index bea0aee98..e359fdc80 100644 --- a/lib/icinga/host.cpp +++ b/lib/icinga/host.cpp @@ -26,7 +26,9 @@ bool Host::m_ServicesCacheValid = true; static AttributeDescription hostAttributes[] = { { "alias", Attribute_Config }, - { "hostgroups", Attribute_Config } + { "hostgroups", Attribute_Config }, + { "acknowledgement", Attribute_Replicated }, + { "acknowledgement_expiry", Attribute_Replicated } }; REGISTER_TYPE(Host, hostAttributes); @@ -307,6 +309,49 @@ set Host::GetServices(void) const return services; } +AcknowledgementType Host::GetAcknowledgement(void) +{ + Value value = Get("acknowledgement"); + + if (value.IsEmpty()) + return AcknowledgementNone; + + int ivalue = static_cast(value); + AcknowledgementType avalue = static_cast(ivalue); + + if (avalue != AcknowledgementNone) { + double expiry = GetAcknowledgementExpiry(); + + if (expiry != 0 && expiry < Utility::GetTime()) { + avalue = AcknowledgementNone; + SetAcknowledgement(avalue); + SetAcknowledgementExpiry(0); + } + } + + return avalue; +} + +void Host::SetAcknowledgement(AcknowledgementType acknowledgement) +{ + Set("acknowledgement", static_cast(acknowledgement)); +} + +double Host::GetAcknowledgementExpiry(void) const +{ + Value value = Get("acknowledgement_expiry"); + + if (value.IsEmpty()) + return 0; + + return static_cast(value); +} + +void Host::SetAcknowledgementExpiry(double timestamp) +{ + Set("acknowledgement_expiry", timestamp); +} + void Host::InvalidateServicesCache(void) { m_ServicesCacheValid = false; diff --git a/lib/icinga/host.h b/lib/icinga/host.h index f57bc1a66..a3797b5d3 100644 --- a/lib/icinga/host.h +++ b/lib/icinga/host.h @@ -44,9 +44,16 @@ public: String GetAlias(void) const; Dictionary::Ptr GetGroups(void) const; + set GetParents(void); Dictionary::Ptr GetMacros(void) const; + AcknowledgementType GetAcknowledgement(void); + void SetAcknowledgement(AcknowledgementType acknowledgement); + + double GetAcknowledgementExpiry(void) const; + void SetAcknowledgementExpiry(double timestamp); + bool IsReachable(void); bool IsUp(void); diff --git a/lib/icinga/i2-icinga.h b/lib/icinga/i2-icinga.h index 62a952d2e..bfb6a0ba2 100644 --- a/lib/icinga/i2-icinga.h +++ b/lib/icinga/i2-icinga.h @@ -48,6 +48,8 @@ using boost::algorithm::is_any_of; #include "timeperiod.h" +#include "acknowledgement.h" + #include "host.h" #include "hostgroup.h" #include "service.h" diff --git a/lib/icinga/icinga.vcxproj b/lib/icinga/icinga.vcxproj index 6ee43a2cd..69e6adf11 100644 --- a/lib/icinga/icinga.vcxproj +++ b/lib/icinga/icinga.vcxproj @@ -39,6 +39,7 @@ + diff --git a/lib/icinga/icinga.vcxproj.filters b/lib/icinga/icinga.vcxproj.filters index 6775eca2f..4bd00b018 100644 --- a/lib/icinga/icinga.vcxproj.filters +++ b/lib/icinga/icinga.vcxproj.filters @@ -81,6 +81,9 @@ Headerdateien + + Headerdateien + diff --git a/lib/icinga/service.h b/lib/icinga/service.h index 791852650..a9a5980c4 100644 --- a/lib/icinga/service.h +++ b/lib/icinga/service.h @@ -48,18 +48,6 @@ enum ServiceStateType StateTypeHard }; -/** - * The acknowledgement type of a service. - * - * @ingroup icinga - */ -enum AcknowledgementType -{ - AcknowledgementNone = 0, - AcknowledgementNormal = 1, - AcknowledgementSticky = 2 -}; - class CheckResultMessage; class ServiceStatusMessage; -- 2.40.0