From 29b056c848ed9e62f54d19febea88caaa33a7dea Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sat, 9 Feb 2013 01:16:43 +0100 Subject: [PATCH] Remove separate host ack/downtime/comments functionality. Fixes #3663 --- components/compat/compatcomponent.cpp | 93 ++++++--------- components/compat/compatcomponent.h | 12 +- lib/icinga/Makefile.am | 11 +- lib/icinga/acknowledgement.h | 40 ------- lib/icinga/commentprocessor.h | 78 ------------- lib/icinga/downtimeprocessor.h | 74 ------------ lib/icinga/externalcommandprocessor.cpp | 108 ++++++++++-------- lib/icinga/host.cpp | 79 +------------ lib/icinga/host.h | 8 -- lib/icinga/i2-icinga.h | 4 - lib/icinga/icinga.vcxproj | 7 +- ...mmentprocessor.cpp => service-comment.cpp} | 76 ++++++------ ...timeprocessor.cpp => service-downtime.cpp} | 98 ++++++++-------- lib/icinga/service.cpp | 18 +-- lib/icinga/service.h | 91 +++++++++++++++ 15 files changed, 294 insertions(+), 503 deletions(-) delete mode 100644 lib/icinga/acknowledgement.h delete mode 100644 lib/icinga/commentprocessor.h delete mode 100644 lib/icinga/downtimeprocessor.h rename lib/icinga/{commentprocessor.cpp => service-comment.cpp} (67%) rename lib/icinga/{downtimeprocessor.cpp => service-downtime.cpp} (70%) diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index 48a3753cc..9a2028186 100644 --- a/components/compat/compatcomponent.cpp +++ b/components/compat/compatcomponent.cpp @@ -171,21 +171,11 @@ void CompatComponent::ProcessCommand(const String& command) } #endif /* _WIN32 */ -void CompatComponent::DumpComments(ofstream& fp, const DynamicObject::Ptr& owner) +void CompatComponent::DumpComments(ofstream& fp, const Service::Ptr& owner, CompatObjectType type) { Service::Ptr service; Host::Ptr host; - Dictionary::Ptr comments; - - if (owner->GetType() == DynamicType::GetByName("Service")) { - service = dynamic_pointer_cast(owner); - comments = service->GetComments(); - - host = service->GetHost(); - } else { - host = dynamic_pointer_cast(owner); - comments = host->GetComments(); - } + Dictionary::Ptr comments = owner->GetComments(); if (!comments) return; @@ -193,16 +183,16 @@ void CompatComponent::DumpComments(ofstream& fp, const DynamicObject::Ptr& owner String id; Dictionary::Ptr comment; BOOST_FOREACH(tie(id, comment), comments) { - if (CommentProcessor::IsCommentExpired(comment)) + if (Service::IsCommentExpired(comment)) continue; - if (!service) + if (type == CompatTypeHost) fp << "hostcomment {" << "\n"; else fp << "servicecomment {" << "\n" - << "\t" << "service_description=" << service->GetShortName() << "\n"; + << "\t" << "service_description=" << owner->GetShortName() << "\n"; - fp << "\t" << "host_name=" << host->GetName() << "\n" + fp << "\t" << "host_name=" << owner->GetHost()->GetName() << "\n" << "\t" << "comment_id=" << static_cast(comment->Get("legacy_id")) << "\n" << "\t" << "entry_time=" << static_cast(comment->Get("entry_time")) << "\n" << "\t" << "entry_type=" << static_cast(comment->Get("entry_type")) << "\n" @@ -216,21 +206,9 @@ void CompatComponent::DumpComments(ofstream& fp, const DynamicObject::Ptr& owner } } -void CompatComponent::DumpDowntimes(ofstream& fp, const DynamicObject::Ptr& owner) +void CompatComponent::DumpDowntimes(ofstream& fp, const Service::Ptr& owner, CompatObjectType type) { - Service::Ptr service; - Host::Ptr host; - Dictionary::Ptr downtimes; - - if (owner->GetType() == DynamicType::GetByName("Service")) { - service = dynamic_pointer_cast(owner); - downtimes = service->GetDowntimes(); - - host = service->GetHost(); - } else { - host = dynamic_pointer_cast(owner); - downtimes = host->GetDowntimes(); - } + Dictionary::Ptr downtimes = owner->GetDowntimes(); if (!downtimes) return; @@ -238,21 +216,21 @@ void CompatComponent::DumpDowntimes(ofstream& fp, const DynamicObject::Ptr& owne String id; Dictionary::Ptr downtime; BOOST_FOREACH(tie(id, downtime), downtimes) { - if (DowntimeProcessor::IsDowntimeExpired(downtime)) + if (Service::IsDowntimeExpired(downtime)) continue; - if (!service) + if (type == CompatTypeHost) fp << "hostdowntime {" << "\n"; else fp << "servicedowntime {" << "\n" - << "\t" << "service_description=" << service->GetShortName() << "\n"; + << "\t" << "service_description=" << owner->GetShortName() << "\n"; - Dictionary::Ptr triggeredByObj = DowntimeProcessor::GetDowntimeByID(downtime->Get("triggered_by")); + Dictionary::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->Get("triggered_by")); int triggeredByLegacy = 0; if (triggeredByObj) triggeredByLegacy = triggeredByObj->Get("legacy_id"); - fp << "\t" << "host_name=" << host->GetName() << "\n" + fp << "\t" << "host_name=" << owner->GetHost()->GetName() << "\n" << "\t" << "downtime_id=" << static_cast(downtime->Get("legacy_id")) << "\n" << "\t" << "entry_time=" << static_cast(downtime->Get("entry_time")) << "\n" << "\t" << "start_time=" << static_cast(downtime->Get("start_time")) << "\n" @@ -260,7 +238,7 @@ void CompatComponent::DumpDowntimes(ofstream& fp, const DynamicObject::Ptr& owne << "\t" << "triggered_by=" << triggeredByLegacy << "\n" << "\t" << "fixed=" << static_cast(downtime->Get("fixed")) << "\n" << "\t" << "duration=" << static_cast(downtime->Get("duration")) << "\n" - << "\t" << "is_in_effect=" << (DowntimeProcessor::IsDowntimeActive(downtime) ? 1 : 0) << "\n" + << "\t" << "is_in_effect=" << (Service::IsDowntimeActive(downtime) ? 1 : 0) << "\n" << "\t" << "author=" << static_cast(downtime->Get("author")) << "\n" << "\t" << "comment=" << static_cast(downtime->Get("comment")) << "\n" << "\t" << "trigger_time=" << static_cast(downtime->Get("trigger_time")) << "\n" @@ -284,18 +262,17 @@ void CompatComponent::DumpHostStatus(ofstream& fp, const Host::Ptr& host) Service::Ptr hostcheck = host->GetHostCheckService(); - if (hostcheck) - DumpServiceStatusAttrs(fp, hostcheck, CompatStateHost); + if (hostcheck) { + DumpServiceStatusAttrs(fp, hostcheck, CompatTypeHost); + } - fp << "\t" << "problem_has_been_acknowledged=" << (host->GetAcknowledgement() != AcknowledgementNone ? 1 : 0) << "\n" - << "\t" << "acknowledgement_type=" << static_cast(host->GetAcknowledgement()) << "\n" - << "\t" << "acknowledgement_end_time=" << host->GetAcknowledgementExpiry() << "\n" - << "\t" << "scheduled_downtime_depth=" << (host->IsInDowntime() ? 1 : 0) << "\n" - << "\t" << "}" << "\n" + fp << "\t" << "}" << "\n" << "\n"; - DumpDowntimes(fp, host); - DumpComments(fp, host); + if (hostcheck) { + DumpDowntimes(fp, hostcheck, CompatTypeHost); + DumpComments(fp, hostcheck, CompatTypeHost); + } } void CompatComponent::DumpHostObject(ofstream& fp, const Host::Ptr& host) @@ -321,7 +298,7 @@ void CompatComponent::DumpHostObject(ofstream& fp, const Host::Ptr& host) << "\n"; } -void CompatComponent::DumpServiceStatusAttrs(ofstream& fp, const Service::Ptr& service, CompatStateType type) +void CompatComponent::DumpServiceStatusAttrs(ofstream& fp, const Service::Ptr& service, CompatObjectType type) { String output; String perfdata; @@ -346,7 +323,7 @@ void CompatComponent::DumpServiceStatusAttrs(ofstream& fp, const Service::Ptr& s if (state > StateUnknown) state = StateUnknown; - if (type == CompatStateHost) { + if (type == CompatTypeHost) { if (state == StateOK || state == StateWarning) state = 0; else @@ -374,7 +351,11 @@ void CompatComponent::DumpServiceStatusAttrs(ofstream& fp, const Service::Ptr& s << "\t" << "last_hard_state_change=" << service->GetLastHardStateChange() << "\n" << "\t" << "last_update=" << time(NULL) << "\n" << "\t" << "active_checks_enabled=" << (service->GetEnableActiveChecks() ? 1 : 0) <<"\n" - << "\t" << "passive_checks_enabled=" << (service->GetEnablePassiveChecks() ? 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"; } void CompatComponent::DumpServiceStatus(ofstream& fp, const Service::Ptr& service) @@ -383,17 +364,13 @@ void CompatComponent::DumpServiceStatus(ofstream& fp, const Service::Ptr& servic << "\t" << "host_name=" << service->GetHost()->GetName() << "\n" << "\t" << "service_description=" << service->GetShortName() << "\n"; - DumpServiceStatusAttrs(fp, service, CompatStateService); + DumpServiceStatusAttrs(fp, service, CompatTypeService); - fp << "\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" << "}" << "\n" + fp << "\t" << "}" << "\n" << "\n"; - DumpDowntimes(fp, service); - DumpComments(fp, service); + DumpDowntimes(fp, service, CompatTypeService); + DumpComments(fp, service, CompatTypeService); } void CompatComponent::DumpServiceObject(ofstream& fp, const Service::Ptr& service) @@ -465,8 +442,8 @@ void CompatComponent::StatusTimerHandler(void) << "\t" << "enable_failure_prediction=0" << "\n" << "\t" << "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n" << "\t" << "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n" - << "\t" << "next_downtime_id=" << DowntimeProcessor::GetNextDowntimeID() << "\n" - << "\t" << "next_comment_id=" << CommentProcessor::GetNextCommentID() << "\n" + << "\t" << "next_downtime_id=" << Service::GetNextDowntimeID() << "\n" + << "\t" << "next_comment_id=" << Service::GetNextCommentID() << "\n" << "\t" << "}" << "\n" << "\n"; diff --git a/components/compat/compatcomponent.h b/components/compat/compatcomponent.h index c1d15a4b1..27613cc07 100644 --- a/components/compat/compatcomponent.h +++ b/components/compat/compatcomponent.h @@ -23,10 +23,10 @@ namespace icinga { -enum CompatStateType +enum CompatObjectType { - CompatStateService, - CompatStateHost + CompatTypeService, + CompatTypeHost }; /** @@ -53,12 +53,12 @@ private: String GetLogPath(void) const; String GetCommandPath(void) const; - void DumpDowntimes(ofstream& fp, const DynamicObject::Ptr& owner); - void DumpComments(ofstream& fp, const DynamicObject::Ptr& owner); + void DumpDowntimes(ofstream& fp, const Service::Ptr& owner, CompatObjectType type); + void DumpComments(ofstream& fp, const Service::Ptr& owner, CompatObjectType type); void DumpHostStatus(ofstream& fp, const Host::Ptr& host); void DumpHostObject(ofstream& fp, const Host::Ptr& host); - void DumpServiceStatusAttrs(ofstream& fp, const Service::Ptr& service, CompatStateType type); + void DumpServiceStatusAttrs(ofstream& fp, const Service::Ptr& service, CompatObjectType type); template void DumpNameList(ofstream& fp, const T& list) diff --git a/lib/icinga/Makefile.am b/lib/icinga/Makefile.am index 20c3a3a4c..4d7d8d015 100644 --- a/lib/icinga/Makefile.am +++ b/lib/icinga/Makefile.am @@ -5,15 +5,10 @@ pkglib_LTLIBRARIES = \ libicinga.la libicinga_la_SOURCES = \ - acknowledgement.h \ checkresultmessage.cpp \ checkresultmessage.h \ cib.cpp \ cib.h \ - commentprocessor.cpp \ - commentprocessor.h \ - downtimeprocessor.cpp \ - downtimeprocessor.h \ externalcommandprocessor.cpp \ externalcommandprocessor.h \ host.cpp \ @@ -30,9 +25,11 @@ libicinga_la_SOURCES = \ pluginchecktask.cpp \ pluginchecktask.h \ service.cpp \ + service-comment.cpp \ + service-downtime.cpp \ + service.h \ servicegroup.cpp \ - servicegroup.h \ - service.h + servicegroup.h libicinga_la_CPPFLAGS = \ -DI2_ICINGA_BUILD \ diff --git a/lib/icinga/acknowledgement.h b/lib/icinga/acknowledgement.h deleted file mode 100644 index eb0fbf0e5..000000000 --- a/lib/icinga/acknowledgement.h +++ /dev/null @@ -1,40 +0,0 @@ -/****************************************************************************** - * 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/commentprocessor.h b/lib/icinga/commentprocessor.h deleted file mode 100644 index fb39ccdb5..000000000 --- a/lib/icinga/commentprocessor.h +++ /dev/null @@ -1,78 +0,0 @@ -/****************************************************************************** - * 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 COMMENTPROCESSOR_H -#define COMMENTPROCESSOR_H - -namespace icinga -{ - -enum CommentType -{ - Comment_User = 1, - Comment_Downtime = 2, - Comment_Flapping = 3, - Comment_Acknowledgement = 4 -}; - -/** - * Comment processor. - * - * @ingroup icinga - */ -class I2_ICINGA_API CommentProcessor -{ -public: - static int GetNextCommentID(void); - - static String AddComment(const DynamicObject::Ptr& owner, - CommentType entryType, const String& author, const String& text, - double expireTime); - - static void RemoveAllComments(const DynamicObject::Ptr& owner); - static void RemoveComment(const String& id); - - static String GetIDFromLegacyID(int id); - static DynamicObject::Ptr GetOwnerByCommentID(const String& id); - static Dictionary::Ptr GetCommentByID(const String& id); - - static bool IsCommentExpired(const Dictionary::Ptr& comment); - - static void InvalidateCommentCache(void); - static void ValidateCommentCache(void); - -private: - static int m_NextCommentID; - - static map m_LegacyCommentCache; - static map m_CommentCache; - static bool m_CommentCacheValid; - static Timer::Ptr m_CommentExpireTimer; - - CommentProcessor(void); - - static void CommentExpireTimerHandler(void); - - static void AddCommentsToCache(const DynamicObject::Ptr& owner); - static void RemoveExpiredComments(const DynamicObject::Ptr& owner); -}; - -} - -#endif /* DOWNTIMEPROCESSOR_H */ diff --git a/lib/icinga/downtimeprocessor.h b/lib/icinga/downtimeprocessor.h deleted file mode 100644 index 731d3aa5b..000000000 --- a/lib/icinga/downtimeprocessor.h +++ /dev/null @@ -1,74 +0,0 @@ -/****************************************************************************** - * 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 DOWNTIMEPROCESSOR_H -#define DOWNTIMEPROCESSOR_H - -namespace icinga -{ - -/** - * Downtime processor. - * - * @ingroup icinga - */ -class I2_ICINGA_API DowntimeProcessor -{ -public: - static int GetNextDowntimeID(void); - - static String AddDowntime(const DynamicObject::Ptr& owner, - const String& author, const String& comment, - double startTime, double endTime, - bool fixed, const String& triggeredBy, double duration); - - static void RemoveDowntime(const String& id); - - static void TriggerDowntimes(const DynamicObject::Ptr& owner); - static void TriggerDowntime(const String& id); - - static String GetIDFromLegacyID(int id); - static DynamicObject::Ptr GetOwnerByDowntimeID(const String& id); - static Dictionary::Ptr GetDowntimeByID(const String& id); - - static bool IsDowntimeActive(const Dictionary::Ptr& downtime); - static bool IsDowntimeExpired(const Dictionary::Ptr& downtime); - - static void InvalidateDowntimeCache(void); - static void ValidateDowntimeCache(void); - -private: - static int m_NextDowntimeID; - - static map m_LegacyDowntimeCache; - static map m_DowntimeCache; - static bool m_DowntimeCacheValid; - static Timer::Ptr m_DowntimeExpireTimer; - - DowntimeProcessor(void); - - static void DowntimeExpireTimerHandler(void); - - static void AddDowntimesToCache(const DynamicObject::Ptr& owner); - static void RemoveExpiredDowntimes(const DynamicObject::Ptr& owner); -}; - -} - -#endif /* DOWNTIMEPROCESSOR_H */ diff --git a/lib/icinga/externalcommandprocessor.cpp b/lib/icinga/externalcommandprocessor.cpp index 2a9085acd..92ac69738 100644 --- a/lib/icinga/externalcommandprocessor.cpp +++ b/lib/icinga/externalcommandprocessor.cpp @@ -328,8 +328,11 @@ void ExternalCommandProcessor::AcknowledgeHostProblem(double, const vectorGetName() + "'"); - host->SetAcknowledgement(sticky ? AcknowledgementSticky : AcknowledgementNormal); - host->SetAcknowledgementExpiry(0); + Service::Ptr service = host->GetHostCheckService(); + if (service) { + service->SetAcknowledgement(sticky ? AcknowledgementSticky : AcknowledgementNormal); + service->SetAcknowledgementExpiry(0); + } } void ExternalCommandProcessor::AcknowledgeHostProblemExpire(double, const vector& arguments) @@ -346,8 +349,11 @@ void ExternalCommandProcessor::AcknowledgeHostProblemExpire(double, const vector BOOST_THROW_EXCEPTION(invalid_argument("The host '" + arguments[0] + "' is OK.")); Logger::Write(LogInformation, "icinga", "Setting timed acknowledgement for host '" + host->GetName() + "'"); - host->SetAcknowledgement(sticky ? AcknowledgementSticky : AcknowledgementNormal); - host->SetAcknowledgementExpiry(timestamp); + Service::Ptr service = host->GetHostCheckService(); + if (service) { + service->SetAcknowledgement(sticky ? AcknowledgementSticky : AcknowledgementNormal); + service->SetAcknowledgementExpiry(timestamp); + } } void ExternalCommandProcessor::RemoveHostAcknowledgement(double, const vector& arguments) @@ -358,8 +364,11 @@ void ExternalCommandProcessor::RemoveHostAcknowledgement(double, const vectorGetName() + "'"); - host->SetAcknowledgement(AcknowledgementNone); - host->SetAcknowledgementExpiry(0); + Service::Ptr service = host->GetHostCheckService(); + if (service) { + service->SetAcknowledgement(AcknowledgementNone); + service->SetAcknowledgementExpiry(0); + } } void ExternalCommandProcessor::EnableHostgroupSvcChecks(double, const vector& arguments) @@ -540,10 +549,10 @@ void ExternalCommandProcessor::ScheduleSvcDowntime(double, const vector& String triggeredBy; int triggeredByLegacy = Convert::ToLong(arguments[5]); if (triggeredByLegacy != 0) - triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy); + triggeredBy = Service::GetDowntimeIDFromLegacyID(triggeredByLegacy); Logger::Write(LogInformation, "icinga", "Creating downtime for service " + service->GetName()); - (void) DowntimeProcessor::AddDowntime(service, arguments[7], arguments[8], + (void) service->AddDowntime(arguments[7], arguments[8], Convert::ToDouble(arguments[2]), Convert::ToDouble(arguments[3]), Convert::ToBool(arguments[4]), triggeredBy, Convert::ToDouble(arguments[6])); } @@ -555,8 +564,8 @@ void ExternalCommandProcessor::DelSvcDowntime(double, const vector& argu int id = Convert::ToLong(arguments[0]); Logger::Write(LogInformation, "icinga", "Removing downtime ID " + arguments[0]); - String rid = DowntimeProcessor::GetIDFromLegacyID(id); - DowntimeProcessor::RemoveDowntime(rid); + String rid = Service::GetDowntimeIDFromLegacyID(id); + Service::RemoveDowntime(rid); } void ExternalCommandProcessor::ScheduleHostDowntime(double, const vector& arguments) @@ -569,12 +578,15 @@ void ExternalCommandProcessor::ScheduleHostDowntime(double, const vector String triggeredBy; int triggeredByLegacy = Convert::ToLong(arguments[4]); if (triggeredByLegacy != 0) - triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy); + triggeredBy = Service::GetDowntimeIDFromLegacyID(triggeredByLegacy); Logger::Write(LogInformation, "icinga", "Creating downtime for host " + host->GetName()); - (void) DowntimeProcessor::AddDowntime(host, arguments[6], arguments[7], - Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), - Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5])); + Service::Ptr service = host->GetHostCheckService(); + if (service) { + (void) service->AddDowntime(arguments[6], arguments[7], + Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), + Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5])); + } } void ExternalCommandProcessor::DelHostDowntime(double, const vector& arguments) @@ -584,8 +596,8 @@ void ExternalCommandProcessor::DelHostDowntime(double, const vector& arg int id = Convert::ToLong(arguments[0]); Logger::Write(LogInformation, "icinga", "Removing downtime ID " + arguments[0]); - String rid = DowntimeProcessor::GetIDFromLegacyID(id); - DowntimeProcessor::RemoveDowntime(rid); + String rid = Service::GetDowntimeIDFromLegacyID(id); + Service::RemoveDowntime(rid); } void ExternalCommandProcessor::ScheduleHostSvcDowntime(double, const vector& arguments) @@ -598,16 +610,11 @@ void ExternalCommandProcessor::ScheduleHostSvcDowntime(double, const vectorGetName()); - (void) DowntimeProcessor::AddDowntime(host, arguments[6], arguments[7], - Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), - Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5])); + triggeredBy = Service::GetDowntimeIDFromLegacyID(triggeredByLegacy); BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { Logger::Write(LogInformation, "icinga", "Creating downtime for service " + service->GetName()); - (void) DowntimeProcessor::AddDowntime(service, arguments[6], arguments[7], + (void) service->AddDowntime(arguments[6], arguments[7], Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5])); } @@ -623,13 +630,16 @@ void ExternalCommandProcessor::ScheduleHostgroupHostDowntime(double, const vecto String triggeredBy; int triggeredByLegacy = Convert::ToLong(arguments[4]); if (triggeredByLegacy != 0) - triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy); + triggeredBy = Service::GetDowntimeIDFromLegacyID(triggeredByLegacy); BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { Logger::Write(LogInformation, "icinga", "Creating downtime for host " + host->GetName()); - (void) DowntimeProcessor::AddDowntime(host, arguments[6], arguments[7], - Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), - Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5])); + Service::Ptr service = host->GetHostCheckService(); + if (service) { + (void) service->AddDowntime(arguments[6], arguments[7], + Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), + Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5])); + } } } @@ -643,7 +653,7 @@ void ExternalCommandProcessor::ScheduleHostgroupSvcDowntime(double, const vector String triggeredBy; int triggeredByLegacy = Convert::ToLong(arguments[4]); if (triggeredByLegacy != 0) - triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy); + triggeredBy = Service::GetDowntimeIDFromLegacyID(triggeredByLegacy); /* Note: we can't just directly create downtimes for all the services by iterating * over all hosts in the host group - otherwise we might end up creating multiple @@ -659,7 +669,7 @@ void ExternalCommandProcessor::ScheduleHostgroupSvcDowntime(double, const vector BOOST_FOREACH(const Service::Ptr& service, services) { Logger::Write(LogInformation, "icinga", "Creating downtime for service " + service->GetName()); - (void) DowntimeProcessor::AddDowntime(service, arguments[6], arguments[7], + (void) service->AddDowntime(arguments[6], arguments[7], Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5])); } @@ -675,21 +685,23 @@ void ExternalCommandProcessor::ScheduleServicegroupHostDowntime(double, const ve String triggeredBy; int triggeredByLegacy = Convert::ToLong(arguments[4]); if (triggeredByLegacy != 0) - triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy); + triggeredBy = Service::GetDowntimeIDFromLegacyID(triggeredByLegacy); /* Note: we can't just directly create downtimes for all the hosts by iterating * over all services in the service group - otherwise we might end up creating multiple * downtimes for some hosts. */ - set hosts; + set services; BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { - hosts.insert(service->GetHost()); + Service::Ptr hcService = service->GetHost()->GetHostCheckService(); + if (hcService) + services.insert(hcService); } - BOOST_FOREACH(const Host::Ptr& host, hosts) { - Logger::Write(LogInformation, "icinga", "Creating downtime for host " + host->GetName()); - (void) DowntimeProcessor::AddDowntime(host, arguments[6], arguments[7], + BOOST_FOREACH(const Service::Ptr& service, services) { + Logger::Write(LogInformation, "icinga", "Creating downtime for service " + service->GetName()); + (void) service->AddDowntime(arguments[6], arguments[7], Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5])); } @@ -705,11 +717,11 @@ void ExternalCommandProcessor::ScheduleServicegroupSvcDowntime(double, const vec String triggeredBy; int triggeredByLegacy = Convert::ToLong(arguments[4]); if (triggeredByLegacy != 0) - triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy); + triggeredBy = Service::GetDowntimeIDFromLegacyID(triggeredByLegacy); BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { Logger::Write(LogInformation, "icinga", "Creating downtime for service " + service->GetName()); - (void) DowntimeProcessor::AddDowntime(service, arguments[6], arguments[7], + (void) service->AddDowntime(arguments[6], arguments[7], Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5])); } @@ -723,7 +735,10 @@ void ExternalCommandProcessor::AddHostComment(double, const vector& argu Host::Ptr host = Host::GetByName(arguments[0]); Logger::Write(LogInformation, "icinga", "Creating comment for host " + host->GetName()); - (void) CommentProcessor::AddComment(host, Comment_User, arguments[2], arguments[3], 0); + Service::Ptr service = host->GetHostCheckService(); + if (service) { + (void) service->AddComment(CommentUser, arguments[2], arguments[3], 0); + } } void ExternalCommandProcessor::DelHostComment(double, const vector& arguments) @@ -733,8 +748,8 @@ void ExternalCommandProcessor::DelHostComment(double, const vector& argu int id = Convert::ToLong(arguments[0]); Logger::Write(LogInformation, "icinga", "Removing comment ID " + arguments[0]); - String rid = CommentProcessor::GetIDFromLegacyID(id); - CommentProcessor::RemoveComment(rid); + String rid = Service::GetCommentIDFromLegacyID(id); + Service::RemoveComment(rid); } void ExternalCommandProcessor::AddSvcComment(double, const vector& arguments) @@ -745,7 +760,7 @@ void ExternalCommandProcessor::AddSvcComment(double, const vector& argum Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]); Logger::Write(LogInformation, "icinga", "Creating comment for service " + service->GetName()); - (void) CommentProcessor::AddComment(service, Comment_User, arguments[3], arguments[4], 0); + (void) service->AddComment(CommentUser, arguments[3], arguments[4], 0); } void ExternalCommandProcessor::DelSvcComment(double, const vector& arguments) @@ -756,8 +771,8 @@ void ExternalCommandProcessor::DelSvcComment(double, const vector& argum int id = Convert::ToLong(arguments[0]); Logger::Write(LogInformation, "icinga", "Removing comment ID " + arguments[0]); - String rid = CommentProcessor::GetIDFromLegacyID(id); - CommentProcessor::RemoveComment(rid); + String rid = Service::GetCommentIDFromLegacyID(id); + Service::RemoveComment(rid); } void ExternalCommandProcessor::DelAllHostComments(double, const vector& arguments) @@ -768,7 +783,10 @@ void ExternalCommandProcessor::DelAllHostComments(double, const vector& Host::Ptr host = Host::GetByName(arguments[0]); Logger::Write(LogInformation, "icinga", "Removing all comments for host " + host->GetName()); - CommentProcessor::RemoveAllComments(host); + Service::Ptr service = host->GetHostCheckService(); + if (service) { + service->RemoveAllComments(); + } } void ExternalCommandProcessor::DelAllSvcComments(double, const vector& arguments) @@ -779,5 +797,5 @@ void ExternalCommandProcessor::DelAllSvcComments(double, const vector& a Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]); Logger::Write(LogInformation, "icinga", "Removing all comments for service " + service->GetName()); - CommentProcessor::RemoveAllComments(service); + service->RemoveAllComments(); } diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp index 3e05f896f..f265283d6 100644 --- a/lib/icinga/host.cpp +++ b/lib/icinga/host.cpp @@ -27,10 +27,6 @@ bool Host::m_ServicesCacheValid = true; REGISTER_SCRIPTFUNCTION("native::ValidateServiceDictionary", &Host::ValidateServiceDictionary); static AttributeDescription hostAttributes[] = { - { "acknowledgement", Attribute_Replicated }, - { "acknowledgement_expiry", Attribute_Replicated }, - { "downtimes", Attribute_Replicated }, - { "comments", Attribute_Replicated }, { "convenience_services", Attribute_Transient } }; @@ -43,7 +39,6 @@ Host::Host(const Dictionary::Ptr& properties) void Host::OnInitCompleted(void) { HostGroup::InvalidateMembersCache(); - DowntimeProcessor::InvalidateDowntimeCache(); UpdateSlaveServices(); } @@ -51,7 +46,6 @@ void Host::OnInitCompleted(void) Host::~Host(void) { HostGroup::InvalidateMembersCache(); - DowntimeProcessor::InvalidateDowntimeCache(); Dictionary::Ptr services = Get("convenience_services"); @@ -97,20 +91,6 @@ Dictionary::Ptr Host::GetMacros(void) const return Get("macros"); } -Dictionary::Ptr Host::GetDowntimes(void) const -{ - DowntimeProcessor::ValidateDowntimeCache(); - - return Get("downtimes"); -} - -Dictionary::Ptr Host::GetComments(void) const -{ - CommentProcessor::ValidateCommentCache(); - - return Get("comments"); -} - Dictionary::Ptr Host::GetHostDependencies(void) const { return Get("hostdependencies"); @@ -158,18 +138,8 @@ bool Host::IsReachable(void) bool Host::IsInDowntime(void) const { - Dictionary::Ptr downtimes = GetDowntimes(); - - if (!downtimes) - return false; - - Dictionary::Ptr downtime; - BOOST_FOREACH(tie(tuples::ignore, downtime), downtimes) { - if (DowntimeProcessor::IsDowntimeActive(downtime)) - return true; - } - - return false; + Service::Ptr service = GetHostCheckService(); + return (service || service->IsInDowntime()); } bool Host::IsUp(void) const @@ -289,8 +259,6 @@ void Host::OnAttributeChanged(const String& name, const Value&) { if (name == "hostgroups") HostGroup::InvalidateMembersCache(); - else if (name == "downtimes") - DowntimeProcessor::InvalidateDowntimeCache(); else if (name == "services") UpdateSlaveServices(); } @@ -315,49 +283,6 @@ 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 6e60d0bc6..41b0f2d71 100644 --- a/lib/icinga/host.h +++ b/lib/icinga/host.h @@ -47,18 +47,10 @@ public: Dictionary::Ptr GetGroups(void) const; Dictionary::Ptr GetMacros(void) const; - Dictionary::Ptr GetDowntimes(void) const; - Dictionary::Ptr GetComments(void) const; Dictionary::Ptr GetHostDependencies(void) const; Dictionary::Ptr GetServiceDependencies(void) const; String GetHostCheck(void) const; - AcknowledgementType GetAcknowledgement(void); - void SetAcknowledgement(AcknowledgementType acknowledgement); - - double GetAcknowledgementExpiry(void) const; - void SetAcknowledgementExpiry(double timestamp); - shared_ptr GetHostCheckService(void) const; set GetParentHosts(void) const; set > GetParentServices(void) const; diff --git a/lib/icinga/i2-icinga.h b/lib/icinga/i2-icinga.h index cb40679ce..914217090 100644 --- a/lib/icinga/i2-icinga.h +++ b/lib/icinga/i2-icinga.h @@ -46,10 +46,6 @@ using boost::algorithm::is_any_of; #include "endpointmanager.h" #include "icingaapplication.h" -#include "acknowledgement.h" -#include "downtimeprocessor.h" -#include "commentprocessor.h" - #include "host.h" #include "hostgroup.h" #include "service.h" diff --git a/lib/icinga/icinga.vcxproj b/lib/icinga/icinga.vcxproj index ca28caf60..1d625a71e 100644 --- a/lib/icinga/icinga.vcxproj +++ b/lib/icinga/icinga.vcxproj @@ -21,8 +21,6 @@ - - @@ -37,14 +35,13 @@ + + - - - diff --git a/lib/icinga/commentprocessor.cpp b/lib/icinga/service-comment.cpp similarity index 67% rename from lib/icinga/commentprocessor.cpp rename to lib/icinga/service-comment.cpp index d6aa23999..321e10fa4 100644 --- a/lib/icinga/commentprocessor.cpp +++ b/lib/icinga/service-comment.cpp @@ -21,20 +21,19 @@ using namespace icinga; -int CommentProcessor::m_NextCommentID = 1; -map CommentProcessor::m_LegacyCommentCache; -map CommentProcessor::m_CommentCache; -bool CommentProcessor::m_CommentCacheValid; -Timer::Ptr CommentProcessor::m_CommentExpireTimer; +int Service::m_NextCommentID = 1; +map Service::m_LegacyCommentCache; +map Service::m_CommentCache; +bool Service::m_CommentCacheValid; +Timer::Ptr Service::m_CommentExpireTimer; -int CommentProcessor::GetNextCommentID(void) +int Service::GetNextCommentID(void) { return m_NextCommentID; } -String CommentProcessor::AddComment(const DynamicObject::Ptr& owner, - CommentType entryType, const String& author, const String& text, - double expireTime) +String Service::AddComment(CommentType entryType, const String& author, + const String& text, double expireTime) { Dictionary::Ptr comment = boost::make_shared(); comment->Set("entry_time", Utility::GetTime()); @@ -44,26 +43,26 @@ String CommentProcessor::AddComment(const DynamicObject::Ptr& owner, comment->Set("expire_time", expireTime); comment->Set("legacy_id", m_NextCommentID++); - Dictionary::Ptr comments = owner->Get("comments"); + Dictionary::Ptr comments = Get("comments"); if (!comments) comments = boost::make_shared(); String id = Utility::NewUUID(); comments->Set(id, comment); - owner->Set("comments", comments); + Set("comments", comments); return id; } -void CommentProcessor::RemoveAllComments(const DynamicObject::Ptr& owner) +void Service::RemoveAllComments(void) { - owner->Set("comments", Empty); + Set("comments", Empty); } -void CommentProcessor::RemoveComment(const String& id) +void Service::RemoveComment(const String& id) { - DynamicObject::Ptr owner = GetOwnerByCommentID(id); + Service::Ptr owner = GetOwnerByCommentID(id); if (!owner) return; @@ -76,7 +75,7 @@ void CommentProcessor::RemoveComment(const String& id) } } -String CommentProcessor::GetIDFromLegacyID(int id) +String Service::GetCommentIDFromLegacyID(int id) { map::iterator it = m_LegacyCommentCache.find(id); @@ -86,16 +85,16 @@ String CommentProcessor::GetIDFromLegacyID(int id) return it->second; } -DynamicObject::Ptr CommentProcessor::GetOwnerByCommentID(const String& id) +Service::Ptr Service::GetOwnerByCommentID(const String& id) { ValidateCommentCache(); return m_CommentCache[id].lock(); } -Dictionary::Ptr CommentProcessor::GetCommentByID(const String& id) +Dictionary::Ptr Service::GetCommentByID(const String& id) { - DynamicObject::Ptr owner = GetOwnerByCommentID(id); + Service::Ptr owner = GetOwnerByCommentID(id); if (!owner) return Dictionary::Ptr(); @@ -110,23 +109,23 @@ Dictionary::Ptr CommentProcessor::GetCommentByID(const String& id) return Dictionary::Ptr(); } -bool CommentProcessor::IsCommentExpired(const Dictionary::Ptr& comment) +bool Service::IsCommentExpired(const Dictionary::Ptr& comment) { double expire_time = comment->Get("expire_time"); return (expire_time != 0 && expire_time < Utility::GetTime()); } -void CommentProcessor::InvalidateCommentCache(void) +void Service::InvalidateCommentCache(void) { m_CommentCacheValid = false; m_CommentCache.clear(); m_LegacyCommentCache.clear(); } -void CommentProcessor::AddCommentsToCache(const DynamicObject::Ptr& owner) +void Service::AddCommentsToCache(void) { - Dictionary::Ptr comments = owner->Get("comments"); + Dictionary::Ptr comments = Get("comments"); if (!comments) return; @@ -145,15 +144,15 @@ void CommentProcessor::AddCommentsToCache(const DynamicObject::Ptr& owner) legacy_id = m_NextCommentID++; comment->Set("legacy_id", legacy_id); - owner->Touch("comments"); + Touch("comments"); } m_LegacyCommentCache[legacy_id] = id; - m_CommentCache[id] = owner; + m_CommentCache[id] = GetSelf(); } } -void CommentProcessor::ValidateCommentCache(void) +void Service::ValidateCommentCache(void) { if (m_CommentCacheValid) return; @@ -162,12 +161,9 @@ void CommentProcessor::ValidateCommentCache(void) m_LegacyCommentCache.clear(); DynamicObject::Ptr object; - BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Host")->GetObjects()) { - AddCommentsToCache(object); - } - BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) { - AddCommentsToCache(object); + Service::Ptr service = dynamic_pointer_cast(object); + service->AddCommentsToCache(); } m_CommentCacheValid = true; @@ -175,14 +171,14 @@ void CommentProcessor::ValidateCommentCache(void) if (!m_CommentExpireTimer) { m_CommentExpireTimer = boost::make_shared(); m_CommentExpireTimer->SetInterval(300); - m_CommentExpireTimer->OnTimerExpired.connect(boost::bind(&CommentProcessor::CommentExpireTimerHandler)); + m_CommentExpireTimer->OnTimerExpired.connect(boost::bind(&Service::CommentExpireTimerHandler)); m_CommentExpireTimer->Start(); } } -void CommentProcessor::RemoveExpiredComments(const DynamicObject::Ptr& owner) +void Service::RemoveExpiredComments(void) { - Dictionary::Ptr comments = owner->Get("comments"); + Dictionary::Ptr comments = Get("comments"); if (!comments) return; @@ -201,19 +197,15 @@ void CommentProcessor::RemoveExpiredComments(const DynamicObject::Ptr& owner) comments->Remove(id); } - owner->Touch("comments"); + Touch("comments"); } } -void CommentProcessor::CommentExpireTimerHandler(void) +void Service::CommentExpireTimerHandler(void) { DynamicObject::Ptr object; - BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Host")->GetObjects()) { - RemoveExpiredComments(object); - } - BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) { - RemoveExpiredComments(object); + Service::Ptr service = dynamic_pointer_cast(object); + service->RemoveExpiredComments(); } } - diff --git a/lib/icinga/downtimeprocessor.cpp b/lib/icinga/service-downtime.cpp similarity index 70% rename from lib/icinga/downtimeprocessor.cpp rename to lib/icinga/service-downtime.cpp index 243f36186..b60ee2360 100644 --- a/lib/icinga/downtimeprocessor.cpp +++ b/lib/icinga/service-downtime.cpp @@ -21,21 +21,20 @@ using namespace icinga; -int DowntimeProcessor::m_NextDowntimeID = 1; -map DowntimeProcessor::m_LegacyDowntimeCache; -map DowntimeProcessor::m_DowntimeCache; -bool DowntimeProcessor::m_DowntimeCacheValid; -Timer::Ptr DowntimeProcessor::m_DowntimeExpireTimer; +int Service::m_NextDowntimeID = 1; +map Service::m_LegacyDowntimeCache; +map Service::m_DowntimeCache; +bool Service::m_DowntimeCacheValid; +Timer::Ptr Service::m_DowntimeExpireTimer; -int DowntimeProcessor::GetNextDowntimeID(void) +int Service::GetNextDowntimeID(void) { return m_NextDowntimeID; } -String DowntimeProcessor::AddDowntime(const DynamicObject::Ptr& owner, - const String& author, const String& comment, - double startTime, double endTime, - bool fixed, const String& triggeredBy, double duration) +String Service::AddDowntime(const String& author, const String& comment, + double startTime, double endTime, bool fixed, + const String& triggeredBy, double duration) { Dictionary::Ptr downtime = boost::make_shared(); downtime->Set("entry_time", Utility::GetTime()); @@ -51,33 +50,33 @@ String DowntimeProcessor::AddDowntime(const DynamicObject::Ptr& owner, downtime->Set("legacy_id", m_NextDowntimeID++); if (!triggeredBy.IsEmpty()) { - DynamicObject::Ptr otherOwner = GetOwnerByDowntimeID(triggeredBy); + Service::Ptr otherOwner = GetOwnerByDowntimeID(triggeredBy); Dictionary::Ptr otherDowntimes = otherOwner->Get("downtimes"); Dictionary::Ptr otherDowntime = otherDowntimes->Get(triggeredBy); Dictionary::Ptr triggers = otherDowntime->Get("triggers"); triggers->Set(triggeredBy, triggeredBy); otherOwner->Touch("downtimes"); } - - Dictionary::Ptr downtimes = owner->Get("downtimes"); + + Dictionary::Ptr downtimes = Get("downtimes"); if (!downtimes) downtimes = boost::make_shared(); String id = Utility::NewUUID(); downtimes->Set(id, downtime); - owner->Set("downtimes", downtimes); + Set("downtimes", downtimes); return id; } -void DowntimeProcessor::RemoveDowntime(const String& id) +void Service::RemoveDowntime(const String& id) { - DynamicObject::Ptr owner = GetOwnerByDowntimeID(id); + Service::Ptr owner = GetOwnerByDowntimeID(id); if (!owner) return; - + Dictionary::Ptr downtimes = owner->Get("downtimes"); if (!downtimes) @@ -87,22 +86,22 @@ void DowntimeProcessor::RemoveDowntime(const String& id) owner->Touch("downtimes"); } -void DowntimeProcessor::TriggerDowntimes(const DynamicObject::Ptr& owner) +void Service::TriggerDowntimes(void) { - Dictionary::Ptr downtimes = owner->Get("downtimes"); + Dictionary::Ptr downtimes = Get("downtimes"); if (!downtimes) return; - + String id; BOOST_FOREACH(tie(id, tuples::ignore), downtimes) { TriggerDowntime(id); } } -void DowntimeProcessor::TriggerDowntime(const String& id) +void Service::TriggerDowntime(const String& id) { - DynamicObject::Ptr owner = GetOwnerByDowntimeID(id); + Service::Ptr owner = GetOwnerByDowntimeID(id); Dictionary::Ptr downtime = GetDowntimeByID(id); double now = Utility::GetTime(); @@ -113,18 +112,20 @@ void DowntimeProcessor::TriggerDowntime(const String& id) if (downtime->Get("trigger_time") == 0) downtime->Set("trigger_time", now); - + Dictionary::Ptr triggers = downtime->Get("triggers"); String tid; BOOST_FOREACH(tie(tid, tuples::ignore), triggers) { TriggerDowntime(tid); } - + owner->Touch("downtimes"); } -String DowntimeProcessor::GetIDFromLegacyID(int id) +String Service::GetDowntimeIDFromLegacyID(int id) { + ValidateDowntimeCache(); + map::iterator it = m_LegacyDowntimeCache.find(id); if (it == m_LegacyDowntimeCache.end()) @@ -133,16 +134,16 @@ String DowntimeProcessor::GetIDFromLegacyID(int id) return it->second; } -DynamicObject::Ptr DowntimeProcessor::GetOwnerByDowntimeID(const String& id) +Service::Ptr Service::GetOwnerByDowntimeID(const String& id) { ValidateDowntimeCache(); return m_DowntimeCache[id].lock(); } -Dictionary::Ptr DowntimeProcessor::GetDowntimeByID(const String& id) +Dictionary::Ptr Service::GetDowntimeByID(const String& id) { - DynamicObject::Ptr owner = GetOwnerByDowntimeID(id); + Service::Ptr owner = GetOwnerByDowntimeID(id); if (!owner) return Dictionary::Ptr(); @@ -157,7 +158,7 @@ Dictionary::Ptr DowntimeProcessor::GetDowntimeByID(const String& id) return Dictionary::Ptr(); } -bool DowntimeProcessor::IsDowntimeActive(const Dictionary::Ptr& downtime) +bool Service::IsDowntimeActive(const Dictionary::Ptr& downtime) { double now = Utility::GetTime(); @@ -176,21 +177,21 @@ bool DowntimeProcessor::IsDowntimeActive(const Dictionary::Ptr& downtime) return (triggerTime + downtime->Get("duration") < now); } -bool DowntimeProcessor::IsDowntimeExpired(const Dictionary::Ptr& downtime) +bool Service::IsDowntimeExpired(const Dictionary::Ptr& downtime) { return (downtime->Get("end_time") < Utility::GetTime()); } -void DowntimeProcessor::InvalidateDowntimeCache(void) +void Service::InvalidateDowntimeCache(void) { m_DowntimeCacheValid = false; m_DowntimeCache.clear(); m_LegacyDowntimeCache.clear(); } -void DowntimeProcessor::AddDowntimesToCache(const DynamicObject::Ptr& owner) +void Service::AddDowntimesToCache(void) { - Dictionary::Ptr downtimes = owner->Get("downtimes"); + Dictionary::Ptr downtimes = Get("downtimes"); if (!downtimes) return; @@ -208,15 +209,15 @@ void DowntimeProcessor::AddDowntimesToCache(const DynamicObject::Ptr& owner) * this shouldn't usually happen - assign it a new ID. */ legacy_id = m_NextDowntimeID++; downtime->Set("legacy_id", legacy_id); - owner->Touch("downtimes"); + Touch("downtimes"); } m_LegacyDowntimeCache[legacy_id] = id; - m_DowntimeCache[id] = owner; + m_DowntimeCache[id] = GetSelf(); } } -void DowntimeProcessor::ValidateDowntimeCache(void) +void Service::ValidateDowntimeCache(void) { if (m_DowntimeCacheValid) return; @@ -225,12 +226,9 @@ void DowntimeProcessor::ValidateDowntimeCache(void) m_LegacyDowntimeCache.clear(); DynamicObject::Ptr object; - BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Host")->GetObjects()) { - AddDowntimesToCache(object); - } - BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) { - AddDowntimesToCache(object); + Service::Ptr service = dynamic_pointer_cast(object); + service->AddDowntimesToCache(); } m_DowntimeCacheValid = true; @@ -238,14 +236,14 @@ void DowntimeProcessor::ValidateDowntimeCache(void) if (!m_DowntimeExpireTimer) { m_DowntimeExpireTimer = boost::make_shared(); m_DowntimeExpireTimer->SetInterval(300); - m_DowntimeExpireTimer->OnTimerExpired.connect(boost::bind(&DowntimeProcessor::DowntimeExpireTimerHandler)); + m_DowntimeExpireTimer->OnTimerExpired.connect(boost::bind(&Service::DowntimeExpireTimerHandler)); m_DowntimeExpireTimer->Start(); } } -void DowntimeProcessor::RemoveExpiredDowntimes(const DynamicObject::Ptr& owner) +void Service::RemoveExpiredDowntimes(void) { - Dictionary::Ptr downtimes = owner->Get("downtimes"); + Dictionary::Ptr downtimes = Get("downtimes"); if (!downtimes) return; @@ -264,19 +262,15 @@ void DowntimeProcessor::RemoveExpiredDowntimes(const DynamicObject::Ptr& owner) downtimes->Remove(id); } - owner->Touch("downtimes"); + Touch("downtimes"); } } -void DowntimeProcessor::DowntimeExpireTimerHandler(void) +void Service::DowntimeExpireTimerHandler(void) { DynamicObject::Ptr object; - BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Host")->GetObjects()) { - RemoveExpiredDowntimes(object); - } - BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) { - RemoveExpiredDowntimes(object); + Service::Ptr service = dynamic_pointer_cast(object); + service->RemoveExpiredDowntimes(); } } - diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index 663a06e01..46f53286c 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -56,14 +56,16 @@ Service::Service(const Dictionary::Ptr& serializedObject) { ServiceGroup::InvalidateMembersCache(); Host::InvalidateServicesCache(); - DowntimeProcessor::InvalidateDowntimeCache(); + Service::InvalidateDowntimeCache(); + Service::InvalidateCommentCache(); } Service::~Service(void) { ServiceGroup::InvalidateMembersCache(); Host::InvalidateServicesCache(); - DowntimeProcessor::InvalidateDowntimeCache(); + Service::InvalidateDowntimeCache(); + Service::InvalidateCommentCache(); } String Service::GetAlias(void) const @@ -118,14 +120,14 @@ Dictionary::Ptr Service::GetMacros(void) const Dictionary::Ptr Service::GetDowntimes(void) const { - DowntimeProcessor::ValidateDowntimeCache(); + Service::ValidateDowntimeCache(); return Get("downtimes"); } Dictionary::Ptr Service::GetComments(void) const { - CommentProcessor::ValidateCommentCache(); + Service::ValidateCommentCache(); return Get("comments"); } @@ -234,7 +236,7 @@ bool Service::IsInDowntime(void) const Dictionary::Ptr downtime; BOOST_FOREACH(tie(tuples::ignore, downtime), downtimes) { - if (DowntimeProcessor::IsDowntimeActive(downtime)) + if (Service::IsDowntimeActive(downtime)) return true; } @@ -559,7 +561,7 @@ void Service::ApplyCheckResult(const Dictionary::Ptr& cr) } if (GetState() != StateOK) - DowntimeProcessor::TriggerDowntimes(GetSelf()); + TriggerDowntimes(); } ServiceState Service::StateFromString(const String& state) @@ -636,7 +638,9 @@ void Service::OnAttributeChanged(const String& name, const Value& oldValue) else if (name == "host_name" || name == "short_name") Host::InvalidateServicesCache(); else if (name == "downtimes") - DowntimeProcessor::InvalidateDowntimeCache(); + Service::InvalidateDowntimeCache(); + else if (name == "comments") + Service::InvalidateCommentCache(); } void Service::BeginExecuteCheck(const function& callback) diff --git a/lib/icinga/service.h b/lib/icinga/service.h index 77d9d4299..fee3c75e7 100644 --- a/lib/icinga/service.h +++ b/lib/icinga/service.h @@ -48,6 +48,31 @@ enum ServiceStateType StateTypeHard }; +/** + * The acknowledgement type of a service. + * + * @ingroup icinga + */ +enum AcknowledgementType +{ + AcknowledgementNone = 0, + AcknowledgementNormal = 1, + AcknowledgementSticky = 2 +}; + +/** + * The type of a service comment. + * + * @ingroup icinga + */ +enum CommentType +{ + CommentUser = 1, + CommentDowntime = 2, + CommentFlapping = 3, + CommentAcknowledgement = 4 +}; + class CheckResultMessage; /** @@ -157,10 +182,76 @@ public: static boost::signal OnCheckerChanged; static boost::signal OnNextCheckChanged; + /* Downtimes */ + static int GetNextDowntimeID(void); + + String AddDowntime(const String& author, const String& comment, + double startTime, double endTime, bool fixed, + const String& triggeredBy, double duration); + + static void RemoveDowntime(const String& id); + + void TriggerDowntimes(void); + static void TriggerDowntime(const String& id); + + static String GetDowntimeIDFromLegacyID(int id); + static Service::Ptr GetOwnerByDowntimeID(const String& id); + static Dictionary::Ptr GetDowntimeByID(const String& id); + + static bool IsDowntimeActive(const Dictionary::Ptr& downtime); + static bool IsDowntimeExpired(const Dictionary::Ptr& downtime); + + static void InvalidateDowntimeCache(void); + static void ValidateDowntimeCache(void); + + /* Comments */ + static int GetNextCommentID(void); + + String AddComment(CommentType entryType, const String& author, + const String& text, double expireTime); + + void RemoveAllComments(void); + static void RemoveComment(const String& id); + + static String GetCommentIDFromLegacyID(int id); + static Service::Ptr GetOwnerByCommentID(const String& id); + static Dictionary::Ptr GetCommentByID(const String& id); + + static bool IsCommentExpired(const Dictionary::Ptr& comment); + + static void InvalidateCommentCache(void); + static void ValidateCommentCache(void); + protected: virtual void OnAttributeChanged(const String& name, const Value& oldValue); private: + /* Downtimes */ + static int m_NextDowntimeID; + + static map m_LegacyDowntimeCache; + static map m_DowntimeCache; + static bool m_DowntimeCacheValid; + static Timer::Ptr m_DowntimeExpireTimer; + + static void DowntimeExpireTimerHandler(void); + + void AddDowntimesToCache(void); + void RemoveExpiredDowntimes(void); + + /* Comments */ + static int m_NextCommentID; + + static map m_LegacyCommentCache; + static map m_CommentCache; + static bool m_CommentCacheValid; + static Timer::Ptr m_CommentExpireTimer; + + static void CommentExpireTimerHandler(void); + + void AddCommentsToCache(void); + void RemoveExpiredComments(void); + void CheckCompletedHandler(const Dictionary::Ptr& scheduleInfo, const ScriptTask::Ptr& task, const function& callback); }; -- 2.49.0