Implement triggered downtime for services.

Fixes #3583
This commit is contained in:
Gunnar Beutner 2013-01-31 13:57:14 +01:00
parent 24e2d1d3f2
commit 9862ab5324
4 changed files with 64 additions and 11 deletions

View File

@ -231,13 +231,10 @@ void CompatComponent::DumpDowntimes(ofstream& fp, const DynamicObject::Ptr& owne
fp << "servicedowntime {" << "\n"
<< "\t" << "service_description=" << service->GetAlias() << "\n";
String triggeredBy = downtime->Get("triggered_by");
Dictionary::Ptr triggeredByObj = DowntimeProcessor::GetDowntimeByID(downtime->Get("triggered_by"));
int triggeredByLegacy = 0;
if (!triggeredBy.IsEmpty()) {
Dictionary::Ptr triggeredByObj = DowntimeProcessor::GetDowntimeByID(triggeredBy);
if (triggeredByObj->Contains("legacy_id"))
if (triggeredByObj)
triggeredByLegacy = triggeredByObj->Get("legacy_id");
}
fp << "\t" << "host_name=" << host->GetName() << "\n"
<< "\t" << "downtime_id=" << static_cast<String>(downtime->Get("legacy_id")) << "\n"

View File

@ -46,9 +46,19 @@ String DowntimeProcessor::AddDowntime(const DynamicObject::Ptr& owner,
downtime->Set("fixed", fixed);
downtime->Set("duration", duration);
downtime->Set("triggered_by", triggeredBy);
downtime->Set("triggers", boost::make_shared<Dictionary>());
downtime->Set("trigger_time", 0);
downtime->Set("legacy_id", m_NextDowntimeID++);
if (!triggeredBy.IsEmpty()) {
DynamicObject::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");
}
Dictionary::Ptr downtimes = owner->Get("downtimes");
if (!downtimes)
@ -65,20 +75,60 @@ void DowntimeProcessor::RemoveDowntime(const String& id)
{
DynamicObject::Ptr owner = GetOwnerByDowntimeID(id);
if (!owner)
return;
Dictionary::Ptr downtimes = owner->Get("downtimes");
if (downtimes) {
if (!downtimes)
return;
downtimes->Remove(id);
owner->Touch("downtimes");
}
void DowntimeProcessor::TriggerDowntimes(const DynamicObject::Ptr& owner)
{
Dictionary::Ptr downtimes = owner->Get("downtimes");
if (!downtimes)
return;
String id;
BOOST_FOREACH(tie(id, tuples::ignore), downtimes) {
TriggerDowntime(id);
}
}
void DowntimeProcessor::TriggerDowntime(const String& id)
{
DynamicObject::Ptr owner = GetOwnerByDowntimeID(id);
Dictionary::Ptr downtime = GetDowntimeByID(id);
double now = Utility::GetTime();
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");
String tid;
BOOST_FOREACH(tie(tid, tuples::ignore), triggers) {
TriggerDowntime(tid);
}
owner->Touch("downtimes");
}
String DowntimeProcessor::GetIDFromLegacyID(int id)
{
map<int, String>::iterator it = m_LegacyDowntimeCache.find(id);
if (it == m_LegacyDowntimeCache.end())
throw_exception(invalid_argument("Invalid legacy downtime ID specified."));
return Empty;
return it->second;
}
@ -95,7 +145,7 @@ Dictionary::Ptr DowntimeProcessor::GetDowntimeByID(const String& id)
DynamicObject::Ptr owner = GetOwnerByDowntimeID(id);
if (!owner)
throw_exception(invalid_argument("Downtime ID does not exist."));
return Dictionary::Ptr();
Dictionary::Ptr downtimes = owner->Get("downtimes");

View File

@ -40,6 +40,9 @@ public:
static void RemoveDowntime(const String& id);
static void TriggerDowntimes(const DynamicObject::Ptr& owner);
static void TriggerDowntime(const String& id);
static String GetIDFromLegacyID(int id);
static DynamicObject::Ptr GetOwnerByDowntimeID(const String& id);
static Dictionary::Ptr GetDowntimeByID(const String& id);

View File

@ -564,6 +564,9 @@ void Service::ApplyCheckResult(const Dictionary::Ptr& cr)
}
}
}
if (GetState() != StateOK)
DowntimeProcessor::TriggerDowntimes(GetSelf());
}
ServiceState Service::StateFromString(const String& state)