icinga2/lib/icinga/service-downtime.cpp

343 lines
8.6 KiB
C++
Raw Normal View History

2013-01-29 14:50:51 +01: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 "i2-icinga.h"
using namespace icinga;
int Service::m_NextDowntimeID = 1;
boost::mutex Service::m_DowntimeMutex;
map<int, String> Service::m_LegacyDowntimesCache;
map<String, Service::WeakPtr> Service::m_DowntimesCache;
2013-02-27 15:23:25 +01:00
bool Service::m_DowntimesCacheValid = true;
Timer::Ptr Service::m_DowntimesExpireTimer;
2013-01-29 14:50:51 +01:00
int Service::GetNextDowntimeID(void)
2013-01-29 14:50:51 +01:00
{
return m_NextDowntimeID;
}
Dictionary::Ptr Service::GetDowntimes(void) const
{
2013-02-26 10:13:54 +01:00
return m_Downtimes;
}
String Service::AddDowntime(const String& author, const String& comment,
double startTime, double endTime, bool fixed,
const String& triggeredBy, double duration)
2013-01-29 14:50:51 +01:00
{
Dictionary::Ptr downtime = boost::make_shared<Dictionary>();
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("triggers", boost::make_shared<Dictionary>());
2013-01-29 14:50:51 +01:00
downtime->Set("trigger_time", 0);
int legacy_id;
{
boost::mutex::scoped_lock lock(m_DowntimeMutex);
legacy_id = m_NextDowntimeID++;
}
downtime->Set("legacy_id", legacy_id);
2013-01-29 14:50:51 +01:00
if (!triggeredBy.IsEmpty()) {
Service::Ptr otherOwner = GetOwnerByDowntimeID(triggeredBy);
Dictionary::Ptr otherDowntimes = otherOwner->Get("downtimes");
Dictionary::Ptr otherDowntime = otherDowntimes->Get(triggeredBy);
Dictionary::Ptr triggers = otherDowntime->Get("triggers");
triggers->Set(triggeredBy, triggeredBy);
otherOwner->Touch("downtimes");
}
2013-02-26 10:13:54 +01:00
Dictionary::Ptr downtimes = m_Downtimes;
2013-01-29 14:50:51 +01:00
if (!downtimes)
downtimes = boost::make_shared<Dictionary>();
String id = Utility::NewUUID();
downtimes->Set(id, downtime);
2013-02-26 10:13:54 +01:00
m_Downtimes = downtimes;
Touch("downtimes");
return id;
2013-01-29 14:50:51 +01:00
}
void Service::RemoveDowntime(const String& id)
2013-01-29 14:50:51 +01:00
{
Service::Ptr owner = GetOwnerByDowntimeID(id);
2013-01-29 14:50:51 +01:00
if (!owner)
return;
2013-02-26 10:13:54 +01:00
Dictionary::Ptr downtimes = owner->m_Downtimes;
2013-01-29 14:50:51 +01:00
if (!downtimes)
return;
{
ObjectLock olock(downtimes);
downtimes->Remove(id);
}
owner->Touch("downtimes");
}
void Service::TriggerDowntimes(void)
{
2013-02-26 10:13:54 +01:00
Dictionary::Ptr downtimes = m_Downtimes;
if (!downtimes)
return;
ObjectLock olock(downtimes);
String id;
BOOST_FOREACH(tie(id, tuples::ignore), downtimes) {
TriggerDowntime(id);
}
}
void Service::TriggerDowntime(const String& id)
{
Service::Ptr owner = GetOwnerByDowntimeID(id);
Dictionary::Ptr downtime = GetDowntimeByID(id);
if (!downtime)
return;
double now = Utility::GetTime();
ObjectLock olock(downtime);
if (now < downtime->Get("start_time") ||
now > downtime->Get("end_time"))
return;
if (downtime->Get("trigger_time") == 0)
downtime->Set("trigger_time", now);
Dictionary::Ptr triggers = downtime->Get("triggers");
ObjectLock tlock(triggers);
String tid;
BOOST_FOREACH(tie(tid, tuples::ignore), triggers) {
TriggerDowntime(tid);
2013-01-29 14:50:51 +01:00
}
owner->Touch("downtimes");
2013-01-29 14:50:51 +01:00
}
String Service::GetDowntimeIDFromLegacyID(int id)
{
boost::mutex::scoped_lock lock(m_DowntimeMutex);
map<int, String>::iterator it = m_LegacyDowntimesCache.find(id);
if (it == m_LegacyDowntimesCache.end())
return Empty;
return it->second;
}
Service::Ptr Service::GetOwnerByDowntimeID(const String& id)
2013-01-29 14:50:51 +01:00
{
boost::mutex::scoped_lock lock(m_DowntimeMutex);
return m_DowntimesCache[id].lock();
2013-01-29 14:50:51 +01:00
}
Dictionary::Ptr Service::GetDowntimeByID(const String& id)
2013-01-29 14:50:51 +01:00
{
Service::Ptr owner = GetOwnerByDowntimeID(id);
2013-01-29 16:29:09 +01:00
if (!owner)
return Dictionary::Ptr();
2013-01-29 14:50:51 +01:00
2013-02-26 10:13:54 +01:00
Dictionary::Ptr downtimes = owner->m_Downtimes;
2013-01-29 14:50:51 +01:00
if (downtimes) {
ObjectLock olock(downtimes);
Dictionary::Ptr downtime = downtimes->Get(id);
2013-01-29 14:50:51 +01:00
return downtime;
}
return Dictionary::Ptr();
}
bool Service::IsDowntimeActive(const Dictionary::Ptr& downtime)
2013-01-29 14:50:51 +01:00
{
double now = Utility::GetTime();
ObjectLock olock(downtime);
2013-01-30 14:28:13 +01:00
if (now < downtime->Get("start_time") ||
now > downtime->Get("end_time"))
2013-01-29 14:50:51 +01:00
return false;
if (static_cast<bool>(downtime->Get("fixed")))
return true;
2013-01-30 14:28:13 +01:00
double triggerTime = downtime->Get("trigger_time");
2013-01-29 14:50:51 +01:00
if (triggerTime == 0)
return false;
2013-01-30 14:28:13 +01:00
return (triggerTime + downtime->Get("duration") < now);
}
bool Service::IsDowntimeExpired(const Dictionary::Ptr& downtime)
2013-01-30 14:28:13 +01:00
{
ObjectLock olock(downtime);
2013-01-30 14:28:13 +01:00
return (downtime->Get("end_time") < Utility::GetTime());
2013-01-29 14:50:51 +01:00
}
2013-02-27 15:23:25 +01:00
void Service::InvalidateDowntimesCache(void)
{
{
boost::mutex::scoped_lock lock(m_DowntimeMutex);
m_DowntimesCacheValid = false;
}
Utility::QueueAsyncCallback(boost::bind(&Service::RefreshDowntimesCache));
}
void Service::RefreshDowntimesCache(void)
2013-01-29 14:50:51 +01:00
{
2013-02-27 15:23:25 +01:00
{
boost::mutex::scoped_lock lock(m_DowntimeMutex);
if (m_DowntimesCacheValid)
return;
m_DowntimesCacheValid = true;
}
map<int, String> newLegacyDowntimesCache;
map<String, Service::WeakPtr> newDowntimesCache;
2013-01-29 14:50:51 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
Service::Ptr service = dynamic_pointer_cast<Service>(object);
2013-01-29 14:50:51 +01:00
Dictionary::Ptr downtimes;
2013-01-29 14:50:51 +01:00
{
ObjectLock olock(service);
downtimes = service->GetDowntimes();
}
if (!downtimes)
continue;
2013-01-29 14:50:51 +01:00
ObjectLock olock(downtimes);
2013-01-29 14:50:51 +01:00
String id;
Dictionary::Ptr downtime;
BOOST_FOREACH(tie(id, downtime), downtimes) {
ObjectLock dlock(downtime);
int legacy_id = downtime->Get("legacy_id");
2013-01-29 14:50:51 +01:00
if (legacy_id >= m_NextDowntimeID)
m_NextDowntimeID = legacy_id + 1;
2013-01-29 14:50:51 +01:00
if (newLegacyDowntimesCache.find(legacy_id) != newLegacyDowntimesCache.end()) {
/* The legacy_id is already in use by another downtime;
* this shouldn't usually happen - assign it a new ID. */
legacy_id = m_NextDowntimeID++;
downtime->Set("legacy_id", legacy_id);
service->Touch("downtimes");
}
2013-01-29 14:50:51 +01:00
newLegacyDowntimesCache[legacy_id] = id;
newDowntimesCache[id] = service;
}
2013-01-29 14:50:51 +01:00
}
boost::mutex::scoped_lock lock(m_DowntimeMutex);
m_DowntimesCache.swap(newDowntimesCache);
m_LegacyDowntimesCache.swap(newLegacyDowntimesCache);
2013-01-30 14:28:13 +01:00
if (!m_DowntimesExpireTimer) {
m_DowntimesExpireTimer = boost::make_shared<Timer>();
m_DowntimesExpireTimer->SetInterval(300);
m_DowntimesExpireTimer->OnTimerExpired.connect(boost::bind(&Service::DowntimesExpireTimerHandler));
m_DowntimesExpireTimer->Start();
2013-01-30 14:28:13 +01:00
}
}
void Service::RemoveExpiredDowntimes(void)
2013-01-30 14:28:13 +01:00
{
2013-02-26 10:13:54 +01:00
Dictionary::Ptr downtimes = m_Downtimes;
2013-01-30 14:28:13 +01:00
if (!downtimes)
return;
vector<String> expiredDowntimes;
ObjectLock olock(downtimes);
2013-01-30 14:28:13 +01:00
String id;
Dictionary::Ptr downtime;
BOOST_FOREACH(tie(id, downtime), downtimes) {
if (IsDowntimeExpired(downtime))
expiredDowntimes.push_back(id);
}
if (expiredDowntimes.size() > 0) {
BOOST_FOREACH(id, expiredDowntimes) {
downtimes->Remove(id);
}
Touch("downtimes");
2013-01-30 14:28:13 +01:00
}
}
void Service::DowntimesExpireTimerHandler(void)
2013-01-30 14:28:13 +01:00
{
2013-02-18 23:44:24 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
Service::Ptr service = dynamic_pointer_cast<Service>(object);
2013-02-18 14:40:24 +01:00
ObjectLock slock(service);
service->RemoveExpiredDowntimes();
2013-01-30 14:28:13 +01:00
}
2013-01-29 14:50:51 +01:00
}
bool Service::IsInDowntime(void) const
{
Dictionary::Ptr downtimes = GetDowntimes();
if (!downtimes)
return false;
ObjectLock olock(downtimes);
Dictionary::Ptr downtime;
BOOST_FOREACH(tie(tuples::ignore, downtime), downtimes) {
if (Service::IsDowntimeActive(downtime))
return true;
}
return false;
}