2013-07-31 09:14:58 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* 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 "ido/timeperioddbobject.h"
|
|
|
|
#include "ido/dbtype.h"
|
|
|
|
#include "ido/dbvalue.h"
|
|
|
|
#include "icinga/timeperiod.h"
|
2013-08-09 10:39:03 +02:00
|
|
|
#include "icinga/legacytimeperiod.h"
|
|
|
|
#include "base/exception.h"
|
2013-07-31 09:14:58 +02:00
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include <boost/foreach.hpp>
|
2013-08-09 10:39:03 +02:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2013-07-31 09:14:58 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-08-02 17:12:07 +02:00
|
|
|
REGISTER_DBTYPE(TimePeriod, "timeperiod", DbObjectTypeTimePeriod, "timeperiod_object_id", TimePeriodDbObject);
|
2013-07-31 09:14:58 +02:00
|
|
|
|
2013-08-01 13:20:30 +02:00
|
|
|
TimePeriodDbObject::TimePeriodDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
|
|
|
|
: DbObject(type, name1, name2)
|
2013-07-31 09:14:58 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
Dictionary::Ptr TimePeriodDbObject::GetConfigFields(void) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr fields = boost::make_shared<Dictionary>();
|
2013-08-05 10:06:19 +02:00
|
|
|
TimePeriod::Ptr tp = static_pointer_cast<TimePeriod>(GetObject());
|
2013-07-31 09:14:58 +02:00
|
|
|
|
2013-08-05 10:06:19 +02:00
|
|
|
fields->Set("alias", tp->GetDisplayName());
|
2013-07-31 09:14:58 +02:00
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr TimePeriodDbObject::GetStatusFields(void) const
|
|
|
|
{
|
|
|
|
return Empty;
|
|
|
|
}
|
2013-08-09 10:39:03 +02:00
|
|
|
|
|
|
|
void TimePeriodDbObject::OnConfigUpdate(void)
|
|
|
|
{
|
|
|
|
TimePeriod::Ptr tp = static_pointer_cast<TimePeriod>(GetObject());
|
|
|
|
|
|
|
|
DbQuery query_del1;
|
|
|
|
query_del1.Table = GetType()->GetTable() + "_timeranges";
|
|
|
|
query_del1.Type = DbQueryDelete;
|
|
|
|
query_del1.WhereCriteria = boost::make_shared<Dictionary>();
|
|
|
|
query_del1.WhereCriteria->Set("timeperiod_id", DbValue::FromObjectInsertID(tp));
|
|
|
|
OnQuery(query_del1);
|
|
|
|
|
|
|
|
Dictionary::Ptr ranges = tp->Get("ranges");
|
|
|
|
|
|
|
|
if (!ranges)
|
|
|
|
return;
|
|
|
|
|
|
|
|
time_t refts = Utility::GetTime();
|
|
|
|
ObjectLock olock(ranges);
|
|
|
|
String key;
|
|
|
|
Value value;
|
|
|
|
BOOST_FOREACH(boost::tie(key, value), ranges) {
|
|
|
|
int wday = LegacyTimePeriod::WeekdayFromString(key);
|
|
|
|
|
|
|
|
if (wday == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tm reference;
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
tm *temp = localtime(&refts);
|
|
|
|
|
|
|
|
if (temp == NULL) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("localtime")
|
|
|
|
<< boost::errinfo_errno(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
reference = *temp;
|
|
|
|
#else /* _MSC_VER */
|
|
|
|
if (localtime_r(&refts, &reference) == NULL) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("localtime_r")
|
|
|
|
<< boost::errinfo_errno(errno));
|
|
|
|
}
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
|
|
|
tm begin, end;
|
2013-08-09 11:07:55 +02:00
|
|
|
LegacyTimePeriod::ProcessTimeRangeRaw(value, &reference, &begin, &end);
|
2013-08-09 10:39:03 +02:00
|
|
|
|
|
|
|
DbQuery query;
|
|
|
|
query.Table = GetType()->GetTable() + "_timeranges";
|
|
|
|
query.Type = DbQueryInsert;
|
|
|
|
query.Fields = boost::make_shared<Dictionary>();
|
|
|
|
query.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
query.Fields->Set("timeperiod_id", DbValue::FromObjectInsertID(tp));
|
|
|
|
query.Fields->Set("day", wday);
|
2013-08-09 11:07:55 +02:00
|
|
|
query.Fields->Set("start_sec", begin.tm_hour * 3600 + begin.tm_min * 60 + begin.tm_sec);
|
|
|
|
query.Fields->Set("end_sec", end.tm_hour * 3600 + end.tm_min * 60 + end.tm_sec);
|
2013-08-09 10:39:03 +02:00
|
|
|
OnQuery(query);
|
|
|
|
}
|
|
|
|
}
|