icinga2/lib/icinga/dependency.cpp

198 lines
6.6 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 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 "icinga/dependency.h"
2014-04-03 15:36:13 +02:00
#include "icinga/service.h"
#include "config/configcompilercontext.h"
#include "base/logger_fwd.h"
#include "base/scriptfunction.h"
#include <boost/foreach.hpp>
using namespace icinga;
REGISTER_TYPE(Dependency);
REGISTER_SCRIPTFUNCTION(ValidateDependencyFilters, &Dependency::ValidateFilters);
String DependencyNameComposer::MakeName(const String& shortName, const Dictionary::Ptr props) const
{
if (!props)
return "";
String name = props->Get("child_host_name");
if (props->Contains("child_service_name"))
name += "!" + props->Get("child_service_name");
name += "!" + shortName;
return name;
}
void Dependency::OnConfigLoaded(void)
{
Value defaultFilter;
if (GetParentServiceName().IsEmpty())
defaultFilter = StateFilterUp;
else
defaultFilter = StateFilterOK | StateFilterWarning;
SetStateFilter(FilterArrayToInt(GetStates(), defaultFilter));
}
void Dependency::OnStateLoaded(void)
{
DynamicObject::Start();
ASSERT(!OwnsLock());
2014-04-03 15:36:13 +02:00
if (!GetChild())
Log(LogWarning, "icinga", "Dependency '" + GetName() + "' references an invalid child object and will be ignored.");
else
2014-04-03 15:36:13 +02:00
GetChild()->AddDependency(GetSelf());
2014-04-03 15:36:13 +02:00
if (!GetParent())
Log(LogWarning, "icinga", "Dependency '" + GetName() + "' references an invalid parent object and will always fail.");
else
2014-04-03 15:36:13 +02:00
GetParent()->AddReverseDependency(GetSelf());
}
void Dependency::Stop(void)
{
DynamicObject::Stop();
2014-04-03 15:36:13 +02:00
if (GetChild())
GetChild()->RemoveDependency(GetSelf());
2014-04-03 15:36:13 +02:00
if (GetParent())
GetParent()->RemoveReverseDependency(GetSelf());
}
bool Dependency::IsAvailable(DependencyType dt) const
{
2014-04-03 15:36:13 +02:00
Checkable::Ptr parent = GetParent();
2014-04-03 15:36:13 +02:00
if (!parent)
return false;
Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(parent);
/* ignore if it's the same checkable object */
2014-04-03 15:36:13 +02:00
if (parent == GetChild()) {
Log(LogNotice, "icinga", "Dependency '" + GetName() + "' passed: Parent and child " + (service ? "service" : "host") + " are identical.");
return true;
}
/* ignore pending */
2014-04-03 15:36:13 +02:00
if (!parent->GetLastCheckResult()) {
Log(LogNotice, "icinga", "Dependency '" + GetName() + "' passed: " + (service ? "Service" : "Host") + " '" + parent->GetName() + "' hasn't been checked yet.");
return true;
}
/* ignore soft states */
2014-04-03 15:36:13 +02:00
if (parent->GetStateType() == StateTypeSoft) {
Log(LogNotice, "icinga", "Dependency '" + GetName() + "' passed: " + (service ? "Service" : "Host") + " '" + parent->GetName() + "' is in a soft state.");
return true;
}
2014-04-03 15:36:13 +02:00
int state;
if (service)
state = ServiceStateToFilter(service->GetState());
2014-04-03 15:36:13 +02:00
else
state = HostStateToFilter(host->GetState());
2014-04-03 15:36:13 +02:00
/* check state */
if (state & GetStateFilter()) {
Log(LogNotice, "icinga", "Dependency '" + GetName() + "' passed: " + (service ? "Service" : "Host") + " '" + parent->GetName() + "' matches state filter.");
return true;
}
/* ignore if not in time period */
TimePeriod::Ptr tp = GetPeriod();
if (tp && !tp->IsInside(Utility::GetTime())) {
Log(LogNotice, "icinga", "Dependency '" + GetName() + "' passed: Outside time period.");
return true;
}
if (dt == DependencyCheckExecution && !GetDisableChecks()) {
Log(LogNotice, "icinga", "Dependency '" + GetName() + "' passed: Checks are not disabled.");
return true;
} else if (dt == DependencyNotification && !GetDisableNotifications()) {
Log(LogNotice, "icinga", "Dependency '" + GetName() + "' passed: Notifications are not disabled");
return true;
}
Log(LogNotice, "icinga", "Dependency '" + GetName() + "' failed. Parent " +
(service ? "service" : "host") + " '" + parent->GetName() + "' is " +
(service ? Service::StateToString(service->GetState()) : Host::StateToString(host->GetState())));
return false;
}
2014-04-03 15:36:13 +02:00
Checkable::Ptr Dependency::GetChild(void) const
{
Host::Ptr host = Host::GetByName(GetChildHostName());
if (!host)
return Service::Ptr();
if (GetChildServiceName().IsEmpty())
2014-04-03 15:36:13 +02:00
return host;
else
return host->GetServiceByShortName(GetChildServiceName());
}
2014-04-03 15:36:13 +02:00
Checkable::Ptr Dependency::GetParent(void) const
{
Host::Ptr host = Host::GetByName(GetParentHostName());
if (!host)
return Service::Ptr();
if (GetParentServiceName().IsEmpty()) {
Log(LogDebug, "icinga", "Dependency '" + GetName() + "' parent host '" + GetParentHostName() + ".");
2014-04-03 15:36:13 +02:00
return host;
} else {
Log(LogDebug, "icinga", "Dependency '" + GetName() + "' parent host '" + GetParentHostName() + "' service '" + GetParentServiceName() + "' .");
return host->GetServiceByShortName(GetParentServiceName());
}
}
TimePeriod::Ptr Dependency::GetPeriod(void) const
{
return TimePeriod::GetByName(GetPeriodRaw());
}
void Dependency::ValidateFilters(const String& location, const Dictionary::Ptr& attrs)
{
int sfilter = FilterArrayToInt(attrs->Get("state_filter"), 0);
if (!attrs->Contains("parent_service_name") && (sfilter & ~(StateFilterUp | StateFilterDown)) != 0) {
ConfigCompilerContext::GetInstance()->AddMessage(true, "Validation failed for " +
location + ": State filter is invalid for host dependency.");
}
if (attrs->Contains("parent_service_name") && (sfilter & ~(StateFilterOK | StateFilterWarning | StateFilterCritical | StateFilterUnknown)) != 0) {
ConfigCompilerContext::GetInstance()->AddMessage(true, "Validation failed for " +
location + ": State filter is invalid for service dependency.");
}
}