From 464344491372ec4189004adff8190e88a13c3d35 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 29 Jan 2013 14:50:51 +0100 Subject: [PATCH] Added missing files. --- components/compat/compatcomponent.cpp | 2 +- lib/icinga/downtimeprocessor.cpp | 156 ++++++++++++++++++++++++++ lib/icinga/downtimeprocessor.h | 65 +++++++++++ 3 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 lib/icinga/downtimeprocessor.cpp create mode 100644 lib/icinga/downtimeprocessor.h diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index 9c1c1747e..7112ba5d0 100644 --- a/components/compat/compatcomponent.cpp +++ b/components/compat/compatcomponent.cpp @@ -198,7 +198,7 @@ void CompatComponent::DumpDowntimes(ofstream& fp, const DynamicObject::Ptr& owne << "\t" << "is_in_effect=" << (DowntimeProcessor::IsDowntimeActive(downtime) ? 1 : 0) << "\n" << "\t" << "author=" << static_cast(downtime->Get("author")) << "\n" << "\t" << "comment=" << static_cast(downtime->Get("comment")) << "\n" - << "\t" << "trigger_time=" << 0 << "\n" + << "\t" << "trigger_time=" << static_cast(downtime->Get("trigger_time")) << "\n" << "\t" << "}" << "\n" << "\n"; } diff --git a/lib/icinga/downtimeprocessor.cpp b/lib/icinga/downtimeprocessor.cpp new file mode 100644 index 000000000..ab9580f46 --- /dev/null +++ b/lib/icinga/downtimeprocessor.cpp @@ -0,0 +1,156 @@ +/****************************************************************************** + * 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. * + ******************************************************************************/ + +#include "i2-icinga.h" + +using namespace icinga; + +int DowntimeProcessor::m_NextDowntimeID = 1; +map DowntimeProcessor::m_DowntimeCache; +bool DowntimeProcessor::m_DowntimeCacheValid; + +int DowntimeProcessor::GetNextDowntimeID(void) +{ + return m_NextDowntimeID; +} + +int DowntimeProcessor::AddDowntime(const DynamicObject::Ptr& owner, + const String& author, const String& comment, + double startTime, double endTime, + bool fixed, int triggeredBy, double duration) +{ + Dictionary::Ptr downtime = boost::make_shared(); + downtime->Set("entry_time", Utility::GetTime()); + downtime->Set("author", author); + downtime->Set("comment", comment); + downtime->Set("start_time", startTime); + downtime->Set("end_time", endTime); + downtime->Set("fixed", fixed); + downtime->Set("duration", duration); + downtime->Set("triggered_by", triggeredBy); + downtime->Set("trigger_time", 0); + + Dictionary::Ptr downtimes = owner->Get("downtimes"); + + if (!downtimes) + downtimes = boost::make_shared(); + + int id = m_NextDowntimeID; + m_NextDowntimeID++; + + downtimes->Set(Convert::ToString(id), downtime); + owner->Set("downtimes", downtimes); +} + +void DowntimeProcessor::RemoveDowntime(int id) +{ + DynamicObject::Ptr dto = GetOwnerByDowntimeID(id); + + Dictionary::Ptr downtimes = dto->Get("downtimes"); + + if (downtimes) { + downtimes->Remove(Convert::ToString(id)); + dto->Touch("downtimes"); + } +} + +DynamicObject::Ptr DowntimeProcessor::GetOwnerByDowntimeID(int id) +{ + ValidateDowntimeCache(); + + DynamicObject::Ptr dto = m_DowntimeCache[id].lock(); + return dto; +} + +Dictionary::Ptr DowntimeProcessor::GetDowntimeByID(int id) +{ + DynamicObject::Ptr dto = GetOwnerByDowntimeID(id); + + Dictionary::Ptr downtimes = dto->Get("downtimes"); + + if (downtimes) { + Dictionary::Ptr downtime = downtimes->Get(Convert::ToString(id)); + return downtime; + } + + return Dictionary::Ptr(); +} + +bool DowntimeProcessor::IsDowntimeActive(const Dictionary::Ptr& downtime) +{ + double now = Utility::GetTime(); + + if (now < static_cast(downtime->Get("start_time")) || + now > static_cast(downtime->Get("end_time"))) + return false; + + if (static_cast(downtime->Get("fixed"))) + return true; + + double triggerTime = static_cast(downtime->Get("trigger_time")); + + if (triggerTime == 0) + return false; + + return (triggerTime + static_cast(downtime->Get("duration")) < now); +} + +void DowntimeProcessor::InvalidateDowntimeCache(void) +{ + m_DowntimeCacheValid = false; + m_DowntimeCache.clear(); +} + +void DowntimeProcessor::AddDowntimesToCache(const DynamicObject::Ptr& owner) +{ + Dictionary::Ptr downtimes = owner->Get("downtimes"); + + if (!downtimes) + return; + + String sid; + BOOST_FOREACH(tie(sid, tuples::ignore), downtimes) { + int id = Convert::ToLong(sid); + + if (id > m_NextDowntimeID) + m_NextDowntimeID = id; + + m_DowntimeCache[id] = owner; + } +} + +void DowntimeProcessor::ValidateDowntimeCache(void) +{ + if (m_DowntimeCacheValid) + return; + + m_DowntimeCache.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); + } + + m_DowntimeCacheValid = true; +} + diff --git a/lib/icinga/downtimeprocessor.h b/lib/icinga/downtimeprocessor.h new file mode 100644 index 000000000..d74807338 --- /dev/null +++ b/lib/icinga/downtimeprocessor.h @@ -0,0 +1,65 @@ +/****************************************************************************** + * 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 +{ + +/** + * Common Information Base class. Holds some statistics (and will likely be + * removed/refactored). + * + * @ingroup icinga + */ +class I2_ICINGA_API DowntimeProcessor +{ +public: + static int GetNextDowntimeID(void); + + static int AddDowntime(const DynamicObject::Ptr& owner, + const String& author, const String& comment, + double startTime, double endTime, + bool fixed, int triggeredBy, double duration); + + static void RemoveDowntime(int id); + + static DynamicObject::Ptr GetOwnerByDowntimeID(int id); + static Dictionary::Ptr GetDowntimeByID(int id); + + static bool IsDowntimeActive(const Dictionary::Ptr& downtime); + + static void InvalidateDowntimeCache(void); + +private: + static int m_NextDowntimeID; + + static map m_DowntimeCache; + static bool m_DowntimeCacheValid; + + DowntimeProcessor(void); + + static void AddDowntimesToCache(const DynamicObject::Ptr& owner); + static void ValidateDowntimeCache(void); +}; + +} + +#endif /* DOWNTIMEPROCESSOR_H */