2013-03-13 16:04:53 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/timeperiod.hpp"
|
2015-02-12 09:12:55 +01:00
|
|
|
#include "icinga/legacytimeperiod.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/dynamictype.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2015-02-12 09:12:55 +01:00
|
|
|
#include "base/exception.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/timer.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2013-03-13 16:04:53 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(TimePeriod);
|
2015-02-12 09:12:55 +01:00
|
|
|
REGISTER_SCRIPTFUNCTION(ValidateTimePeriodRanges, &TimePeriod::ValidateRanges);
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
static Timer::Ptr l_UpdateTimer;
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-12-15 18:36:31 +01:00
|
|
|
INITIALIZE_ONCE(&TimePeriod::StaticInitialize);
|
|
|
|
|
|
|
|
void TimePeriod::StaticInitialize(void)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
l_UpdateTimer = new Timer();
|
2013-12-15 18:36:31 +01:00
|
|
|
l_UpdateTimer->SetInterval(300);
|
|
|
|
l_UpdateTimer->OnTimerExpired.connect(boost::bind(&TimePeriod::UpdateTimerHandler));
|
|
|
|
l_UpdateTimer->Start();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
void TimePeriod::AddSegment(double begin, double end)
|
|
|
|
{
|
|
|
|
ASSERT(OwnsLock());
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "TimePeriod")
|
|
|
|
<< "Adding segment '" << Utility::FormatDateTime("%c", begin) << "' <-> '"
|
|
|
|
<< Utility::FormatDateTime("%c", end) << "' to TimePeriod '" << GetName() << "'";
|
2013-09-19 00:01:18 +02:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
if (GetValidBegin().IsEmpty() || begin < GetValidBegin())
|
|
|
|
SetValidBegin(begin);
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
if (GetValidEnd().IsEmpty() || end > GetValidEnd())
|
|
|
|
SetValidEnd(end);
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-03-13 16:04:53 +01:00
|
|
|
|
|
|
|
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. */
|
|
|
|
|
2013-12-05 12:30:02 +01:00
|
|
|
if (segment->Get("begin") <= begin && segment->Get("end") >= begin) {
|
2013-03-13 16:04:53 +01:00
|
|
|
segment->Set("end", end); /* Extend an existing segment. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-05 12:30:02 +01:00
|
|
|
if (segment->Get("begin") >= begin && segment->Get("begin") <= end) {
|
2013-03-13 16:04:53 +01:00
|
|
|
segment->Set("begin", begin); /* Extend an existing segment. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create new segment if we weren't able to merge this into an existing segment. */
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr segment = new Dictionary();
|
2013-03-13 16:04:53 +01:00
|
|
|
segment->Set("begin", begin);
|
|
|
|
segment->Set("end", end);
|
|
|
|
|
|
|
|
if (!segments) {
|
2014-11-08 21:17:16 +01:00
|
|
|
segments = new Array();
|
2013-10-26 09:41:45 +02:00
|
|
|
SetSegments(segments);
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "TimePeriod")
|
|
|
|
<< "Removing segment '" << Utility::FormatDateTime("%c", begin) << "' <-> '"
|
|
|
|
<< Utility::FormatDateTime("%c", end) << "' from TimePeriod '" << GetName() << "'";
|
2013-09-19 00:01:18 +02:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
if (GetValidBegin().IsEmpty() || begin < GetValidBegin())
|
|
|
|
SetValidBegin(begin);
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
if (GetValidEnd().IsEmpty() || end > GetValidEnd())
|
|
|
|
SetValidEnd(end);
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-03-13 16:04:53 +01:00
|
|
|
|
|
|
|
if (!segments)
|
|
|
|
return;
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr newSegments = new Array();
|
2013-03-14 07:29:53 +01:00
|
|
|
|
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-10-26 09:41:45 +02:00
|
|
|
SetSegments(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());
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "TimePeriod")
|
|
|
|
<< "Purging segments older than '" << Utility::FormatDateTime("%c", end)
|
|
|
|
<< "' from TimePeriod '" << GetName() << "'";
|
2013-09-19 00:01:18 +02:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
if (GetValidBegin().IsEmpty() || end < GetValidBegin())
|
2013-03-13 16:04:53 +01:00
|
|
|
return;
|
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
SetValidBegin(end);
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-03-13 16:04:53 +01:00
|
|
|
|
|
|
|
if (!segments)
|
|
|
|
return;
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr newSegments = new Array();
|
2013-03-13 16:04:53 +01:00
|
|
|
|
|
|
|
/* Remove old segments. */
|
|
|
|
ObjectLock dlock(segments);
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& segment, segments) {
|
|
|
|
if (segment->Get("end") >= end)
|
|
|
|
newSegments->Add(segment);
|
|
|
|
}
|
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
SetSegments(newSegments);
|
2013-03-13 16:04:53 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2013-10-26 09:41:45 +02:00
|
|
|
if (begin < GetValidEnd())
|
|
|
|
begin = GetValidEnd();
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
if (end < GetValidEnd())
|
2013-04-16 10:12:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-03-13 16:04:53 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<Value> arguments;
|
2014-11-08 21:17:16 +01:00
|
|
|
arguments.push_back(this);
|
2013-03-13 16:04:53 +01:00
|
|
|
arguments.push_back(begin);
|
|
|
|
arguments.push_back(end);
|
|
|
|
|
2014-12-12 20:40:24 +01:00
|
|
|
Array::Ptr segments = GetUpdate()->Invoke(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
|
|
|
}
|
|
|
|
|
2015-02-23 14:06:32 +01:00
|
|
|
bool TimePeriod::GetIsInside(void) const
|
|
|
|
{
|
|
|
|
return IsInside(Utility::GetTime());
|
|
|
|
}
|
|
|
|
|
2013-03-13 16:04:53 +01:00
|
|
|
bool TimePeriod::IsInside(double ts) const
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
if (GetValidBegin().IsEmpty() || ts < GetValidBegin() || GetValidEnd().IsEmpty() || ts > GetValidEnd())
|
2013-03-13 16:04:53 +01:00
|
|
|
return true; /* Assume that all invalid regions are "inside". */
|
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-03-13 16:04:53 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-03-13 16:04:53 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2014-09-02 13:02:22 +02:00
|
|
|
BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjectsByType<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
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
valid_end = tp->GetValidEnd();
|
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
|
|
|
void TimePeriod::Dump(void)
|
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-04-16 10:12:53 +02:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "TimePeriod")
|
|
|
|
<< "Dumping TimePeriod '" << GetName() << "'";
|
|
|
|
|
|
|
|
Log(LogDebug, "TimePeriod")
|
|
|
|
<< "Valid from '" << Utility::FormatDateTime("%c", GetValidBegin())
|
|
|
|
<< "' until '" << Utility::FormatDateTime("%c", GetValidEnd());
|
2013-04-16 10:12:53 +02:00
|
|
|
|
|
|
|
if (segments) {
|
|
|
|
ObjectLock dlock(segments);
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& segment, segments) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "TimePeriod")
|
|
|
|
<< "Segment: " << Utility::FormatDateTime("%c", segment->Get("begin")) << " <-> "
|
|
|
|
<< Utility::FormatDateTime("%c", segment->Get("end"));
|
2013-04-16 10:12:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 13:45:45 +02:00
|
|
|
Log(LogDebug, "TimePeriod", "---");
|
2013-04-16 10:12:53 +02:00
|
|
|
}
|
2015-02-12 09:12:55 +01:00
|
|
|
|
|
|
|
void TimePeriod::ValidateRanges(const String& location, const TimePeriod::Ptr& object)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr ranges = object->GetRanges();
|
|
|
|
|
|
|
|
if (!ranges)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* create a fake time environment to validate the definitions */
|
2015-03-03 09:56:38 +01:00
|
|
|
time_t refts = Utility::GetTime();
|
|
|
|
tm reference = Utility::LocalTime(refts);
|
2015-02-12 09:12:55 +01:00
|
|
|
Array::Ptr segments = new Array();
|
|
|
|
|
|
|
|
ObjectLock olock(ranges);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, ranges) {
|
|
|
|
try {
|
2015-03-03 09:56:38 +01:00
|
|
|
tm begin_tm, end_tm;
|
|
|
|
int stride;
|
|
|
|
LegacyTimePeriod::ParseTimeRange(kv.first, &begin_tm, &end_tm, &stride, &reference);
|
2015-03-29 08:13:11 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2015-02-12 09:12:55 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
|
2015-03-29 08:13:11 +02:00
|
|
|
location + ": Invalid time specification '" + kv.first + "': " + ex.what(), object->GetDebugInfo()));
|
2015-02-12 09:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
LegacyTimePeriod::ProcessTimeRanges(kv.second, &reference, segments);
|
2015-03-29 08:13:11 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2015-02-12 09:12:55 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
|
2015-03-29 08:13:11 +02:00
|
|
|
location + ": Invalid time range definition '" + kv.second + "': " + ex.what(), object->GetDebugInfo()));
|
2015-02-12 09:12:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|