2013-03-13 16:04:53 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
2013-03-13 16:04:53 +01:00
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "icinga/timeperiod.h"
|
2013-03-18 11:02:18 +01:00
|
|
|
#include "config/configitem.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/scriptfunction.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/logger_fwd.h"
|
2013-03-18 11:02:18 +01:00
|
|
|
#include "base/timer.h"
|
2013-03-25 18:36:15 +01:00
|
|
|
#include "base/utility.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
2013-03-13 16:04:53 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(TimePeriod);
|
|
|
|
REGISTER_SCRIPTFUNCTION(EmptyTimePeriod, &TimePeriod::EmptyTimePeriodUpdate);
|
|
|
|
REGISTER_SCRIPTFUNCTION(EvenMinutesTimePeriod, &TimePeriod::EvenMinutesTimePeriodUpdate);
|
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
static Timer::Ptr l_UpdateTimer;
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void TimePeriod::Start(void)
|
2013-03-13 16:04:53 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Start();
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
if (!l_UpdateTimer) {
|
|
|
|
l_UpdateTimer = boost::make_shared<Timer>();
|
|
|
|
l_UpdateTimer->SetInterval(300);
|
|
|
|
l_UpdateTimer->OnTimerExpired.connect(boost::bind(&TimePeriod::UpdateTimerHandler));
|
|
|
|
l_UpdateTimer->Start();
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pre-fill the time period for the next 24 hours. */
|
|
|
|
double now = Utility::GetTime();
|
2013-04-16 10:12:53 +02:00
|
|
|
UpdateRegion(now, now + 24 * 3600, true);
|
|
|
|
Dump();
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
|
|
|
|
2013-07-18 15:30:17 +02:00
|
|
|
String TimePeriod::GetDisplayName(void) const
|
|
|
|
{
|
|
|
|
if (!m_DisplayName.IsEmpty())
|
|
|
|
return m_DisplayName;
|
|
|
|
else
|
|
|
|
return GetName();
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Dictionary::Ptr TimePeriod::GetRanges(void) const
|
2013-03-13 16:04:53 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
return m_Ranges;
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TimePeriod::AddSegment(double begin, double end)
|
|
|
|
{
|
|
|
|
ASSERT(OwnsLock());
|
|
|
|
|
2013-09-19 00:01:18 +02:00
|
|
|
Log(LogDebug, "icinga", "Adding segment '" + Utility::FormatDateTime("%c", begin) + "' <-> '" + Utility::FormatDateTime("%c", end) + "' to TimePeriod '" + GetName() + "'");
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (m_ValidBegin.IsEmpty() || begin < m_ValidBegin)
|
2013-03-13 16:04:53 +01:00
|
|
|
m_ValidBegin = begin;
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (m_ValidEnd.IsEmpty() || end > m_ValidEnd)
|
2013-03-13 16:04:53 +01:00
|
|
|
m_ValidEnd = end;
|
|
|
|
|
|
|
|
Array::Ptr segments = m_Segments;
|
|
|
|
|
|
|
|
if (segments) {
|
|
|
|
/* Try to merge the new segment into an existing segment. */
|
|
|
|
ObjectLock dlock(segments);
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& segment, segments) {
|
|
|
|
if (segment->Get("begin") <= begin && segment->Get("end") >= end)
|
|
|
|
return; /* New segment is fully contained in this segment. */
|
|
|
|
|
|
|
|
if (segment->Get("begin") < begin && segment->Get("end") > begin) {
|
|
|
|
segment->Set("end", end); /* Extend an existing segment. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (segment->Get("begin") > begin && segment->Get("begin") < end) {
|
|
|
|
segment->Set("begin", begin); /* Extend an existing segment. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create new segment if we weren't able to merge this into an existing segment. */
|
|
|
|
Dictionary::Ptr segment = boost::make_shared<Dictionary>();
|
|
|
|
segment->Set("begin", begin);
|
|
|
|
segment->Set("end", end);
|
|
|
|
|
|
|
|
if (!segments) {
|
|
|
|
segments = boost::make_shared<Array>();
|
|
|
|
m_Segments = segments;
|
|
|
|
}
|
|
|
|
|
|
|
|
segments->Add(segment);
|
|
|
|
}
|
|
|
|
|
2013-03-15 09:54:06 +01:00
|
|
|
void TimePeriod::AddSegment(const Dictionary::Ptr& segment)
|
|
|
|
{
|
|
|
|
AddSegment(segment->Get("begin"), segment->Get("end"));
|
|
|
|
}
|
|
|
|
|
2013-03-13 16:04:53 +01:00
|
|
|
void TimePeriod::RemoveSegment(double begin, double end)
|
|
|
|
{
|
|
|
|
ASSERT(OwnsLock());
|
|
|
|
|
2013-09-19 00:01:18 +02:00
|
|
|
Log(LogDebug, "icinga", "Removing segment '" + Utility::FormatDateTime("%c", begin) + "' <-> '" + Utility::FormatDateTime("%c", end) + "' from TimePeriod '" + GetName() + "'");
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (m_ValidBegin.IsEmpty() || begin < m_ValidBegin)
|
2013-03-13 16:04:53 +01:00
|
|
|
m_ValidBegin = begin;
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (m_ValidEnd.IsEmpty() || end > m_ValidEnd)
|
2013-03-13 16:04:53 +01:00
|
|
|
m_ValidEnd = end;
|
|
|
|
|
|
|
|
Array::Ptr segments = m_Segments;
|
|
|
|
|
|
|
|
if (!segments)
|
|
|
|
return;
|
|
|
|
|
2013-03-14 07:29:53 +01:00
|
|
|
Array::Ptr newSegments = boost::make_shared<Array>();
|
|
|
|
|
2013-03-13 16:04:53 +01:00
|
|
|
/* Try to split or adjust an existing segment. */
|
|
|
|
ObjectLock dlock(segments);
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& segment, segments) {
|
2013-03-14 07:29:53 +01:00
|
|
|
/* Fully contained in the specified range? */
|
|
|
|
if (segment->Get("begin") >= begin && segment->Get("end") <= end)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Not overlapping at all? */
|
|
|
|
if (segment->Get("end") < begin || segment->Get("begin") > end) {
|
|
|
|
newSegments->Add(segment);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-03-14 08:18:19 +01:00
|
|
|
/* Adjust the begin/end timestamps so as to not overlap with the specified range. */
|
2013-09-19 00:01:18 +02:00
|
|
|
if (segment->Get("begin") > begin && segment->Get("begin") < end)
|
2013-03-14 08:18:19 +01:00
|
|
|
segment->Set("begin", end);
|
2013-03-14 07:29:53 +01:00
|
|
|
|
2013-09-19 00:01:18 +02:00
|
|
|
if (segment->Get("end") > begin && segment->Get("end") < end)
|
2013-03-14 08:18:19 +01:00
|
|
|
segment->Set("end", begin);
|
|
|
|
|
|
|
|
newSegments->Add(segment);
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
|
|
|
|
2013-03-14 07:29:53 +01:00
|
|
|
m_Segments = newSegments;
|
2013-09-19 00:01:18 +02:00
|
|
|
|
|
|
|
Dump();
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TimePeriod::PurgeSegments(double end)
|
|
|
|
{
|
|
|
|
ASSERT(OwnsLock());
|
|
|
|
|
2013-09-19 00:01:18 +02:00
|
|
|
Log(LogDebug, "icinga", "Purging segments older than '" + Utility::FormatDateTime("%c", end) + "' from TimePeriod '" + GetName() + "'");
|
|
|
|
|
2013-03-13 16:04:53 +01:00
|
|
|
if (m_ValidBegin.IsEmpty() || end < m_ValidBegin)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_ValidBegin = end;
|
|
|
|
|
|
|
|
Array::Ptr segments = m_Segments;
|
|
|
|
|
|
|
|
if (!segments)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Array::Ptr newSegments = boost::make_shared<Array>();
|
|
|
|
|
|
|
|
/* Remove old segments. */
|
|
|
|
ObjectLock dlock(segments);
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& segment, segments) {
|
|
|
|
if (segment->Get("end") >= end)
|
|
|
|
newSegments->Add(segment);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Segments = newSegments;
|
|
|
|
}
|
|
|
|
|
2013-04-16 10:12:53 +02:00
|
|
|
void TimePeriod::UpdateRegion(double begin, double end, bool clearExisting)
|
2013-03-13 16:04:53 +01:00
|
|
|
{
|
2013-04-16 10:12:53 +02:00
|
|
|
if (!clearExisting) {
|
|
|
|
if (begin < m_ValidEnd)
|
|
|
|
begin = m_ValidEnd;
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-04-16 10:12:53 +02:00
|
|
|
if (end < m_ValidEnd)
|
|
|
|
return;
|
|
|
|
}
|
2013-03-13 16:04:53 +01:00
|
|
|
|
|
|
|
TimePeriod::Ptr self = GetSelf();
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<Value> arguments;
|
2013-03-13 16:04:53 +01:00
|
|
|
arguments.push_back(self);
|
|
|
|
arguments.push_back(begin);
|
|
|
|
arguments.push_back(end);
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
Array::Ptr segments = InvokeMethod("update", arguments);
|
2013-03-15 09:54:06 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
RemoveSegment(begin, end);
|
|
|
|
|
2013-03-25 20:47:02 +01:00
|
|
|
if (segments) {
|
|
|
|
ObjectLock dlock(segments);
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& segment, segments) {
|
|
|
|
AddSegment(segment);
|
|
|
|
}
|
2013-03-15 09:54:06 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TimePeriod::IsInside(double ts) const
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
if (m_ValidBegin.IsEmpty() || ts < m_ValidBegin || m_ValidEnd.IsEmpty() || ts > m_ValidEnd)
|
|
|
|
return true; /* Assume that all invalid regions are "inside". */
|
|
|
|
|
|
|
|
Array::Ptr segments = m_Segments;
|
|
|
|
|
|
|
|
if (segments) {
|
|
|
|
ObjectLock dlock(segments);
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& segment, segments) {
|
|
|
|
if (ts > segment->Get("begin") && ts < segment->Get("end"))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
double TimePeriod::FindNextTransition(double begin)
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
Array::Ptr segments = m_Segments;
|
|
|
|
|
|
|
|
double closestTransition = -1;
|
|
|
|
|
|
|
|
if (segments) {
|
|
|
|
ObjectLock dlock(segments);
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& segment, segments) {
|
|
|
|
if (segment->Get("begin") > begin && (segment->Get("begin") < closestTransition || closestTransition == -1))
|
|
|
|
closestTransition = segment->Get("begin");
|
|
|
|
|
|
|
|
if (segment->Get("end") > begin && (segment->Get("end") < closestTransition || closestTransition == -1))
|
|
|
|
closestTransition = segment->Get("end");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closestTransition;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimePeriod::UpdateTimerHandler(void)
|
|
|
|
{
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
|
2013-03-17 20:19:29 +01:00
|
|
|
double valid_end;
|
|
|
|
|
2013-03-14 12:21:10 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(tp);
|
|
|
|
tp->PurgeSegments(now - 3600);
|
2013-03-17 20:19:29 +01:00
|
|
|
|
|
|
|
valid_end = tp->m_ValidEnd;
|
2013-03-14 12:21:10 +01:00
|
|
|
}
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-04-16 10:12:53 +02:00
|
|
|
tp->UpdateRegion(valid_end, now + 24 * 3600, false);
|
|
|
|
tp->Dump();
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 10:12:53 +02:00
|
|
|
Array::Ptr TimePeriod::EmptyTimePeriodUpdate(const TimePeriod::Ptr&, double, double)
|
2013-03-13 16:04:53 +01:00
|
|
|
{
|
2013-03-15 09:54:06 +01:00
|
|
|
Array::Ptr segments = boost::make_shared<Array>();
|
2013-03-25 18:36:15 +01:00
|
|
|
return segments;
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
|
|
|
|
2013-04-16 10:12:53 +02:00
|
|
|
Array::Ptr TimePeriod::EvenMinutesTimePeriodUpdate(const TimePeriod::Ptr&, double begin, double end)
|
2013-03-13 16:04:53 +01:00
|
|
|
{
|
2013-03-15 09:54:06 +01:00
|
|
|
Array::Ptr segments = boost::make_shared<Array>();
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
for (long t = begin / 60 - 1; t * 60 < end; t++) {
|
|
|
|
if ((t % 2) == 0) {
|
2013-03-15 09:54:06 +01:00
|
|
|
Dictionary::Ptr segment = boost::make_shared<Dictionary>();
|
2013-03-18 11:02:18 +01:00
|
|
|
segment->Set("begin", t * 60);
|
2013-03-18 11:59:26 +01:00
|
|
|
segment->Set("end", (t + 1) * 60);
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-03-15 09:54:06 +01:00
|
|
|
segments->Add(segment);
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
return segments;
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
2013-04-16 10:12:53 +02:00
|
|
|
|
|
|
|
void TimePeriod::Dump(void)
|
|
|
|
{
|
|
|
|
Array::Ptr segments = m_Segments;
|
|
|
|
|
|
|
|
Log(LogDebug, "icinga", "Dumping TimePeriod '" + GetName() + "'");
|
2013-09-19 00:01:18 +02:00
|
|
|
Log(LogDebug, "icinga", "Valid from '" + Utility::FormatDateTime("%c", m_ValidBegin) + "' until '" + Utility::FormatDateTime("%c", m_ValidEnd));
|
2013-04-16 10:12:53 +02:00
|
|
|
|
|
|
|
if (segments) {
|
|
|
|
ObjectLock dlock(segments);
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& segment, segments) {
|
|
|
|
Log(LogDebug, "icinga", "Segment: " +
|
|
|
|
Utility::FormatDateTime("%c", segment->Get("begin")) + " <-> " +
|
|
|
|
Utility::FormatDateTime("%c", segment->Get("end")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(LogDebug, "icinga", "---");
|
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
|
|
|
|
void TimePeriod::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
|
|
|
|
{
|
|
|
|
DynamicObject::InternalSerialize(bag, attributeTypes);
|
|
|
|
|
|
|
|
if (attributeTypes & Attribute_Config) {
|
|
|
|
bag->Set("display_name", m_DisplayName);
|
|
|
|
bag->Set("ranges", m_Ranges);
|
|
|
|
}
|
|
|
|
|
2013-08-29 10:40:43 +02:00
|
|
|
if (attributeTypes & Attribute_State) {
|
|
|
|
bag->Set("valid_begin", m_ValidBegin);
|
|
|
|
bag->Set("valid_end", m_ValidEnd);
|
|
|
|
bag->Set("segments", m_Segments);
|
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TimePeriod::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
|
|
|
|
{
|
|
|
|
DynamicObject::InternalDeserialize(bag, attributeTypes);
|
|
|
|
|
|
|
|
if (attributeTypes & Attribute_Config) {
|
|
|
|
m_DisplayName = bag->Get("display_name");
|
|
|
|
m_Ranges = bag->Get("ranges");
|
|
|
|
}
|
|
|
|
|
2013-08-29 10:40:43 +02:00
|
|
|
if (attributeTypes & Attribute_State) {
|
|
|
|
m_ValidBegin = bag->Get("valid_begin");
|
|
|
|
m_ValidEnd = bag->Get("valid_end");
|
|
|
|
m_Segments = bag->Get("segments");
|
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|