2013-11-13 14:56:31 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2013-11-13 14:56:31 +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/scheduleddowntime.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "icinga/scheduleddowntime.tcpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/legacytimeperiod.hpp"
|
|
|
|
#include "icinga/downtime.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "base/timer.hpp"
|
|
|
|
#include "base/dynamictype.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/exception.hpp"
|
2013-11-13 14:56:31 +01:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(ScheduledDowntime);
|
|
|
|
|
|
|
|
INITIALIZE_ONCE(&ScheduledDowntime::StaticInitialize);
|
|
|
|
|
|
|
|
static Timer::Ptr l_Timer;
|
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
String ScheduledDowntimeNameComposer::MakeName(const String& shortName, const Object::Ptr& context) const
|
2014-04-05 22:17:20 +02:00
|
|
|
{
|
2014-11-06 19:35:47 +01:00
|
|
|
ScheduledDowntime::Ptr downtime = dynamic_pointer_cast<ScheduledDowntime>(context);
|
|
|
|
|
|
|
|
if (!downtime)
|
2014-04-05 22:17:20 +02:00
|
|
|
return "";
|
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
String name = downtime->GetHostName();
|
2014-04-05 22:17:20 +02:00
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
if (!downtime->GetServiceName().IsEmpty())
|
|
|
|
name += "!" + downtime->GetServiceName();
|
2014-04-05 22:17:20 +02:00
|
|
|
|
|
|
|
name += "!" + shortName;
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2013-11-13 14:56:31 +01:00
|
|
|
void ScheduledDowntime::StaticInitialize(void)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
l_Timer = new Timer();
|
2013-11-13 14:56:31 +01:00
|
|
|
l_Timer->SetInterval(60);
|
|
|
|
l_Timer->OnTimerExpired.connect(boost::bind(&ScheduledDowntime::TimerProc));
|
|
|
|
l_Timer->Start();
|
|
|
|
}
|
|
|
|
|
2014-12-19 10:56:34 +01:00
|
|
|
void ScheduledDowntime::OnAllConfigLoaded(void)
|
|
|
|
{
|
|
|
|
CustomVarObject::OnAllConfigLoaded();
|
|
|
|
|
|
|
|
if (!GetCheckable())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("ScheduledDowntime '" + GetName() + "' references a host/service which doesn't exist.", GetDebugInfo()));
|
|
|
|
}
|
|
|
|
|
2013-11-13 14:56:31 +01:00
|
|
|
void ScheduledDowntime::Start(void)
|
|
|
|
{
|
|
|
|
DynamicObject::Start();
|
|
|
|
|
|
|
|
CreateNextDowntime();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduledDowntime::TimerProc(void)
|
|
|
|
{
|
2014-09-02 13:02:22 +02:00
|
|
|
BOOST_FOREACH(const ScheduledDowntime::Ptr& sd, DynamicType::GetObjectsByType<ScheduledDowntime>()) {
|
2013-11-13 14:56:31 +01:00
|
|
|
sd->CreateNextDowntime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
Checkable::Ptr ScheduledDowntime::GetCheckable(void) const
|
2013-11-13 14:56:31 +01:00
|
|
|
{
|
2014-04-05 09:15:40 +02:00
|
|
|
Host::Ptr host = Host::GetByName(GetHostName());
|
2013-11-13 14:56:31 +01:00
|
|
|
|
2014-04-05 09:15:40 +02:00
|
|
|
if (GetServiceName().IsEmpty())
|
2014-04-03 15:36:13 +02:00
|
|
|
return host;
|
2013-11-13 14:56:31 +01:00
|
|
|
else
|
2014-04-05 09:15:40 +02:00
|
|
|
return host->GetServiceByShortName(GetServiceName());
|
2013-11-13 14:56:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<double, double> ScheduledDowntime::FindNextSegment(void)
|
|
|
|
{
|
|
|
|
time_t refts = Utility::GetTime();
|
|
|
|
tm reference = Utility::LocalTime(refts);
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "ScheduledDowntime")
|
|
|
|
<< "Finding next scheduled downtime segment for time " << refts;
|
2013-11-13 14:56:31 +01:00
|
|
|
|
|
|
|
Dictionary::Ptr ranges = GetRanges();
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr segments = new Array();
|
2013-11-13 14:56:31 +01:00
|
|
|
|
|
|
|
Dictionary::Ptr bestSegment;
|
|
|
|
double bestBegin;
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
|
|
|
ObjectLock olock(ranges);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, ranges) {
|
2014-12-20 22:04:24 +01:00
|
|
|
Log(LogDebug, "ScheduledDowntime")
|
|
|
|
<< "Evaluating segment: " << kv.first << ": " << kv.second << " at ";
|
|
|
|
|
2013-11-13 14:56:31 +01:00
|
|
|
Dictionary::Ptr segment = LegacyTimePeriod::FindNextSegment(kv.first, kv.second, &reference);
|
|
|
|
|
|
|
|
if (!segment)
|
|
|
|
continue;
|
|
|
|
|
2014-12-20 22:04:24 +01:00
|
|
|
Log(LogDebug, "ScheduledDowntime")
|
|
|
|
<< "Considering segment: " << Utility::FormatDateTime("%c", segment->Get("begin")) << " -> " << Utility::FormatDateTime("%c", segment->Get("end"));
|
|
|
|
|
2013-11-13 14:56:31 +01:00
|
|
|
double begin = segment->Get("begin");
|
|
|
|
|
|
|
|
if (begin < now)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!bestSegment || begin < bestBegin) {
|
|
|
|
bestSegment = segment;
|
|
|
|
bestBegin = begin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bestSegment)
|
|
|
|
return std::make_pair(bestSegment->Get("begin"), bestSegment->Get("end"));
|
|
|
|
else
|
|
|
|
return std::make_pair(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduledDowntime::CreateNextDowntime(void)
|
|
|
|
{
|
2014-04-03 15:36:13 +02:00
|
|
|
Dictionary::Ptr downtimes = GetCheckable()->GetDowntimes();
|
2013-11-13 14:56:31 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock dlock(downtimes);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
|
|
|
|
Downtime::Ptr downtime = kv.second;
|
|
|
|
|
|
|
|
if (downtime->GetScheduledBy() != GetName() ||
|
|
|
|
downtime->GetStartTime() < Utility::GetTime())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We've found a downtime that is owned by us and that hasn't started yet - we're done. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<double, double> segment = FindNextSegment();
|
|
|
|
|
|
|
|
if (segment.first == 0 && segment.second == 0) {
|
|
|
|
tm reference = Utility::LocalTime(Utility::GetTime());
|
|
|
|
reference.tm_mday++;
|
|
|
|
reference.tm_hour = 0;
|
|
|
|
reference.tm_min = 0;
|
|
|
|
reference.tm_sec = 0;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-04 16:57:11 +02:00
|
|
|
String uid = GetCheckable()->AddDowntime(GetAuthor(), GetComment(),
|
2013-11-13 14:56:31 +01:00
|
|
|
segment.first, segment.second,
|
|
|
|
GetFixed(), String(), GetDuration(), GetName());
|
2014-05-04 16:57:11 +02:00
|
|
|
|
|
|
|
Downtime::Ptr downtime = Checkable::GetDowntimeByID(uid);
|
|
|
|
downtime->SetConfigOwner(GetName());
|
2013-11-13 14:56:31 +01:00
|
|
|
}
|
2015-03-09 07:51:55 +01:00
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
void ScheduledDowntime::ValidateRanges(const Dictionary::Ptr& value, const ValidationUtils& utils)
|
2015-03-09 07:51:55 +01:00
|
|
|
{
|
2014-11-30 23:32:13 +01:00
|
|
|
if (!value)
|
2015-03-09 07:51:55 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* create a fake time environment to validate the definitions */
|
|
|
|
time_t refts = Utility::GetTime();
|
|
|
|
tm reference = Utility::LocalTime(refts);
|
|
|
|
Array::Ptr segments = new Array();
|
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
ObjectLock olock(value);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, value) {
|
2015-03-09 07:51:55 +01:00
|
|
|
try {
|
|
|
|
tm begin_tm, end_tm;
|
|
|
|
int stride;
|
|
|
|
LegacyTimePeriod::ParseTimeRange(kv.first, &begin_tm, &end_tm, &stride, &reference);
|
|
|
|
} catch (std::exception&) {
|
2014-11-30 23:32:13 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("ranges"), "Invalid time specification: " + kv.first));
|
2015-03-09 07:51:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
LegacyTimePeriod::ProcessTimeRanges(kv.second, &reference, segments);
|
|
|
|
} catch (std::exception&) {
|
2014-11-30 23:32:13 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("ranges"), "Invalid time range definition: " + kv.first));
|
2015-03-09 07:51:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|