icinga2/lib/icinga/checkable-downtime.cpp

386 lines
10 KiB
C++
Raw Normal View History

2013-01-29 14:50:51 +01:00
/******************************************************************************
* Icinga 2 *
2015-01-22 12:00:23 +01:00
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
2013-01-29 14:50:51 +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/service.hpp"
#include "base/configtype.hpp"
2014-05-25 16:23:35 +02:00
#include "base/objectlock.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"
#include "base/convert.hpp"
2013-03-16 21:18:53 +01:00
#include <boost/foreach.hpp>
2013-01-29 14:50:51 +01:00
using namespace icinga;
2013-03-18 11:02:18 +01:00
static int l_NextDowntimeID = 1;
static boost::mutex l_DowntimeMutex;
static std::map<int, String> l_LegacyDowntimesCache;
static std::map<String, Checkable::Ptr> l_DowntimesCache;
2013-03-18 11:02:18 +01:00
static Timer::Ptr l_DowntimesExpireTimer;
2013-01-29 14:50:51 +01:00
boost::signals2::signal<void (const Checkable::Ptr&, const Downtime::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnDowntimeAdded;
boost::signals2::signal<void (const Checkable::Ptr&, const Downtime::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnDowntimeRemoved;
2014-04-03 15:36:13 +02:00
boost::signals2::signal<void (const Checkable::Ptr&, const Downtime::Ptr&)> Checkable::OnDowntimeTriggered;
INITIALIZE_ONCE(&Checkable::StartDowntimesExpiredTimer);
2014-04-03 15:36:13 +02:00
int Checkable::GetNextDowntimeID(void)
2013-01-29 14:50:51 +01:00
{
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_DowntimeMutex);
2013-03-02 09:07:47 +01:00
2013-03-18 11:02:18 +01:00
return l_NextDowntimeID;
2013-01-29 14:50:51 +01:00
}
2014-04-03 15:36:13 +02:00
String Checkable::AddDowntime(const String& author, const String& comment,
double startTime, double endTime, bool fixed,
const String& triggeredBy, double duration, const String& scheduledBy,
const String& id, const MessageOrigin::Ptr& origin)
2013-01-29 14:50:51 +01:00
{
String uid;
if (id.IsEmpty())
uid = Utility::NewUniqueID();
else
uid = id;
Downtime::Ptr downtime = new Downtime();
downtime->SetId(uid);
downtime->SetEntryTime(Utility::GetTime());
downtime->SetAuthor(author);
downtime->SetComment(comment);
downtime->SetStartTime(startTime);
downtime->SetEndTime(endTime);
downtime->SetFixed(fixed);
downtime->SetDuration(duration);
downtime->SetTriggeredBy(triggeredBy);
downtime->SetScheduledBy(scheduledBy);
if (!triggeredBy.IsEmpty()) {
Downtime::Ptr triggerDowntime = GetDowntimeByID(triggeredBy);
if (triggerDowntime)
downtime->SetTriggeredByLegacyId(triggerDowntime->GetLegacyId());
}
int legacy_id;
{
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_DowntimeMutex);
legacy_id = l_NextDowntimeID++;
}
downtime->SetLegacyId(legacy_id);
2013-01-29 14:50:51 +01:00
if (!triggeredBy.IsEmpty()) {
2014-04-03 15:36:13 +02:00
Checkable::Ptr otherOwner = GetOwnerByDowntimeID(triggeredBy);
2013-10-26 09:41:45 +02:00
Dictionary::Ptr otherDowntimes = otherOwner->GetDowntimes();
Downtime::Ptr otherDowntime = otherDowntimes->Get(triggeredBy);
Dictionary::Ptr triggers = otherDowntime->GetTriggers();
triggers->Set(triggeredBy, triggeredBy);
}
2013-10-26 09:41:45 +02:00
GetDowntimes()->Set(uid, downtime);
{
boost::mutex::scoped_lock lock(l_DowntimeMutex);
l_LegacyDowntimesCache[legacy_id] = uid;
l_DowntimesCache[uid] = this;
}
2014-10-19 17:52:17 +02:00
Log(LogNotice, "Checkable")
<< "Added downtime with ID '" << downtime->GetLegacyId()
<< "' between '" << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", startTime)
<< "' and '" << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", endTime) << "'.";
OnDowntimeAdded(this, downtime, origin);
/* if this object is already in a NOT-OK state trigger
* this downtime now *after* it has been added (important
* for DB IDO, etc.)
*/
if (GetStateRaw() != ServiceOK) {
Log(LogNotice, "Checkable")
<< "Checkable '" << GetName() << "' already in a NOT-OK state."
<< " Triggering downtime now.";
TriggerDowntime(uid);
}
return uid;
2013-01-29 14:50:51 +01:00
}
void Checkable::RemoveDowntime(const String& id, bool cancelled, const MessageOrigin::Ptr& origin)
2013-01-29 14:50:51 +01:00
{
2014-04-03 15:36:13 +02:00
Checkable::Ptr owner = GetOwnerByDowntimeID(id);
2013-01-29 14:50:51 +01:00
if (!owner)
return;
2013-03-02 09:07:47 +01:00
Dictionary::Ptr downtimes = owner->GetDowntimes();
2013-01-29 14:50:51 +01:00
Downtime::Ptr downtime = downtimes->Get(id);
if (!downtime)
return;
int legacy_id = downtime->GetLegacyId();
String config_owner = downtime->GetConfigOwner();
if (!config_owner.IsEmpty()) {
2014-10-19 17:52:17 +02:00
Log(LogWarning, "Checkable")
<< "Cannot remove downtime with ID '" << legacy_id << "'. It is owned by scheduled downtime object '" << config_owner << "'";
return;
}
downtimes->Remove(id);
{
boost::mutex::scoped_lock lock(l_DowntimeMutex);
l_LegacyDowntimesCache.erase(legacy_id);
l_DowntimesCache.erase(id);
}
downtime->SetWasCancelled(cancelled);
2014-10-19 17:52:17 +02:00
Log(LogNotice, "Checkable")
<< "Removed downtime with ID '" << downtime->GetLegacyId() << "' from service '" << owner->GetName() << "'.";
OnDowntimeRemoved(owner, downtime, origin);
}
void Checkable::RemoveAllDowntimes(void)
{
std::vector<String> ids;
Dictionary::Ptr downtimes = GetDowntimes();
{
ObjectLock olock(downtimes);
BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
ids.push_back(kv.first);
}
}
BOOST_FOREACH(const String& id, ids) {
RemoveDowntime(id, true);
}
}
2014-04-03 15:36:13 +02:00
void Checkable::TriggerDowntimes(void)
{
2013-03-02 09:07:47 +01:00
Dictionary::Ptr downtimes = GetDowntimes();
2013-03-18 12:55:41 +01:00
std::vector<String> ids;
2013-03-18 12:55:41 +01:00
{
ObjectLock olock(downtimes);
BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
ids.push_back(kv.first);
2013-03-18 12:55:41 +01:00
}
}
BOOST_FOREACH(const String& id, ids) {
TriggerDowntime(id);
}
}
2014-04-03 15:36:13 +02:00
void Checkable::TriggerDowntime(const String& id)
{
2014-04-03 15:36:13 +02:00
Checkable::Ptr owner = GetOwnerByDowntimeID(id);
Downtime::Ptr downtime = GetDowntimeByID(id);
if (!downtime)
return;
if (downtime->IsActive() && downtime->IsTriggered()) {
2014-10-19 17:52:17 +02:00
Log(LogDebug, "Checkable")
<< "Not triggering downtime with ID '" << downtime->GetLegacyId() << "': already triggered.";
return;
}
if (downtime->IsExpired()) {
2014-10-19 17:52:17 +02:00
Log(LogDebug, "Checkable")
<< "Not triggering downtime with ID '" << downtime->GetLegacyId() << "': expired.";
return;
}
double now = Utility::GetTime();
if (now < downtime->GetStartTime() || now > downtime->GetEndTime()) {
Log(LogDebug, "Checkable")
<< "Not triggering downtime with ID '" << downtime->GetLegacyId() << "': current time is outside downtime window.";
return;
}
2014-10-19 17:52:17 +02:00
Log(LogNotice, "Checkable")
<< "Triggering downtime with ID '" << downtime->GetLegacyId() << "'.";
if (downtime->GetTriggerTime() == 0)
downtime->SetTriggerTime(Utility::GetTime());
Dictionary::Ptr triggers = downtime->GetTriggers();
{
ObjectLock olock(triggers);
BOOST_FOREACH(const Dictionary::Pair& kv, triggers) {
TriggerDowntime(kv.first);
}
2013-01-29 14:50:51 +01:00
}
2013-11-10 16:53:57 +01:00
OnDowntimeTriggered(owner, downtime);
2013-01-29 14:50:51 +01:00
}
2014-04-03 15:36:13 +02:00
String Checkable::GetDowntimeIDFromLegacyID(int id)
{
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_DowntimeMutex);
2013-03-18 11:02:18 +01:00
std::map<int, String>::iterator it = l_LegacyDowntimesCache.find(id);
2013-03-18 11:02:18 +01:00
if (it == l_LegacyDowntimesCache.end())
return Empty;
return it->second;
}
2014-04-03 15:36:13 +02:00
Checkable::Ptr Checkable::GetOwnerByDowntimeID(const String& id)
2013-01-29 14:50:51 +01:00
{
2013-03-18 11:02:18 +01:00
boost::mutex::scoped_lock lock(l_DowntimeMutex);
return l_DowntimesCache[id];
2013-01-29 14:50:51 +01:00
}
2014-04-03 15:36:13 +02:00
Downtime::Ptr Checkable::GetDowntimeByID(const String& id)
2013-01-29 14:50:51 +01:00
{
2014-04-03 15:36:13 +02:00
Checkable::Ptr owner = GetOwnerByDowntimeID(id);
2013-01-29 16:29:09 +01:00
if (!owner)
return Downtime::Ptr();
2013-01-29 14:50:51 +01:00
2013-03-02 09:07:47 +01:00
Dictionary::Ptr downtimes = owner->GetDowntimes();
2013-01-29 14:50:51 +01:00
2013-03-02 09:07:47 +01:00
if (downtimes)
return downtimes->Get(id);
2013-01-29 14:50:51 +01:00
return Downtime::Ptr();
2013-01-29 14:50:51 +01:00
}
2014-04-03 15:36:13 +02:00
void Checkable::StartDowntimesExpiredTimer(void)
{
l_DowntimesExpireTimer = new Timer();
l_DowntimesExpireTimer->SetInterval(60);
2014-04-03 15:36:13 +02:00
l_DowntimesExpireTimer->OnTimerExpired.connect(boost::bind(&Checkable::DowntimesExpireTimerHandler));
l_DowntimesExpireTimer->Start();
}
2014-04-03 15:36:13 +02:00
void Checkable::AddDowntimesToCache(void)
2013-01-29 14:50:51 +01:00
{
2014-12-19 12:19:28 +01:00
#ifdef I2_DEBUG
Log(LogDebug, "Checkable", "Updating Checkable downtimes cache.");
2014-12-19 12:19:28 +01:00
#endif /* I2_DEBUG */
Dictionary::Ptr downtimes = GetDowntimes();
2013-01-29 14:50:51 +01:00
boost::mutex::scoped_lock lock(l_DowntimeMutex);
2013-01-29 14:50:51 +01:00
ObjectLock olock(downtimes);
2013-01-29 14:50:51 +01:00
BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
Downtime::Ptr downtime = kv.second;
int legacy_id = downtime->GetLegacyId();
if (legacy_id >= l_NextDowntimeID)
l_NextDowntimeID = legacy_id + 1;
2013-01-30 14:28:13 +01:00
l_LegacyDowntimesCache[legacy_id] = kv.first;
l_DowntimesCache[kv.first] = this;
2013-01-30 14:28:13 +01:00
}
}
2014-04-03 15:36:13 +02:00
void Checkable::RemoveExpiredDowntimes(void)
2013-01-30 14:28:13 +01:00
{
2013-03-02 09:07:47 +01:00
Dictionary::Ptr downtimes = GetDowntimes();
2013-01-30 14:28:13 +01:00
2013-03-16 21:18:53 +01:00
std::vector<String> expiredDowntimes;
2013-01-30 14:28:13 +01:00
2013-03-04 15:52:42 +01:00
{
ObjectLock olock(downtimes);
BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
Downtime::Ptr downtime = kv.second;
if (downtime->IsExpired())
expiredDowntimes.push_back(kv.first);
2013-03-04 15:52:42 +01:00
}
2013-01-30 14:28:13 +01:00
}
2013-09-18 10:30:20 +02:00
BOOST_FOREACH(const String& id, expiredDowntimes) {
/* override config owner to clear expired downtimes once */
Downtime::Ptr downtime = GetDowntimeByID(id);
downtime->SetConfigOwner(Empty);
2013-09-18 10:30:20 +02:00
RemoveDowntime(id, false);
2013-01-30 14:28:13 +01:00
}
}
2014-04-03 15:36:13 +02:00
void Checkable::DowntimesExpireTimerHandler(void)
2013-01-30 14:28:13 +01:00
{
BOOST_FOREACH(const Host::Ptr& host, ConfigType::GetObjectsByType<Host>()) {
2014-04-03 15:36:13 +02:00
host->RemoveExpiredDowntimes();
}
BOOST_FOREACH(const Service::Ptr& service, ConfigType::GetObjectsByType<Service>()) {
service->RemoveExpiredDowntimes();
2013-01-30 14:28:13 +01:00
}
2013-01-29 14:50:51 +01:00
}
2014-04-03 15:36:13 +02:00
bool Checkable::IsInDowntime(void) const
{
Dictionary::Ptr downtimes = GetDowntimes();
ObjectLock olock(downtimes);
BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
Downtime::Ptr downtime = kv.second;
if (downtime->IsActive())
return true;
}
return false;
}
2014-04-03 15:36:13 +02:00
int Checkable::GetDowntimeDepth(void) const
{
int downtime_depth = 0;
Dictionary::Ptr downtimes = GetDowntimes();
ObjectLock olock(downtimes);
BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
Downtime::Ptr downtime = kv.second;
if (downtime->IsActive())
downtime_depth++;
}
return downtime_depth;
}