2014-02-27 11:05:55 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2014-02-27 11:05:55 +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/dependency.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "config/configcompilercontext.hpp"
|
|
|
|
#include "base/logger_fwd.hpp"
|
|
|
|
#include "base/scriptfunction.hpp"
|
2014-02-27 11:05:55 +01:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(Dependency);
|
2014-04-07 13:59:41 +02:00
|
|
|
REGISTER_SCRIPTFUNCTION(ValidateDependencyFilters, &Dependency::ValidateFilters);
|
2014-02-27 11:05:55 +01:00
|
|
|
|
2014-04-06 08:09:49 +02:00
|
|
|
String DependencyNameComposer::MakeName(const String& shortName, const Dictionary::Ptr props) const
|
2014-04-05 22:17:20 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-07 13:59:41 +02:00
|
|
|
void Dependency::OnConfigLoaded(void)
|
|
|
|
{
|
|
|
|
Value defaultFilter;
|
|
|
|
|
|
|
|
if (GetParentServiceName().IsEmpty())
|
|
|
|
defaultFilter = StateFilterUp;
|
|
|
|
else
|
|
|
|
defaultFilter = StateFilterOK | StateFilterWarning;
|
|
|
|
|
2014-04-09 10:25:23 +02:00
|
|
|
SetStateFilter(FilterArrayToInt(GetStates(), defaultFilter));
|
2014-04-07 13:59:41 +02:00
|
|
|
}
|
|
|
|
|
2014-02-27 11:05:55 +01:00
|
|
|
void Dependency::OnStateLoaded(void)
|
|
|
|
{
|
|
|
|
DynamicObject::Start();
|
|
|
|
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
2014-06-24 11:46:53 +02:00
|
|
|
Host::Ptr childHost = Host::GetByName(GetChildHostName());
|
|
|
|
|
|
|
|
if (childHost) {
|
|
|
|
if (GetChildServiceName().IsEmpty()) {
|
|
|
|
Log(LogDebug, "Dependency", "Dependency '" + GetName() + "' child host '" + GetChildHostName() + ".");
|
|
|
|
m_Child = childHost;
|
|
|
|
} else {
|
|
|
|
Log(LogDebug, "Dependency", "Dependency '" + GetName() + "' child host '" + GetChildHostName() + "' service '" + GetChildServiceName() + "' .");
|
|
|
|
m_Child = childHost->GetServiceByShortName(GetChildServiceName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_Child)
|
2014-05-28 13:45:45 +02:00
|
|
|
Log(LogWarning, "Dependency", "Dependency '" + GetName() + "' references an invalid child object and will be ignored.");
|
2014-02-27 11:05:55 +01:00
|
|
|
else
|
2014-06-24 11:46:53 +02:00
|
|
|
m_Child->AddDependency(GetSelf());
|
|
|
|
|
|
|
|
Host::Ptr parentHost = Host::GetByName(GetParentHostName());
|
|
|
|
|
|
|
|
if (parentHost) {
|
|
|
|
if (GetParentServiceName().IsEmpty()) {
|
|
|
|
Log(LogDebug, "Dependency", "Dependency '" + GetName() + "' parent host '" + GetParentHostName() + ".");
|
|
|
|
m_Parent = parentHost;
|
|
|
|
} else {
|
|
|
|
Log(LogDebug, "Dependency", "Dependency '" + GetName() + "' parent host '" + GetParentHostName() + "' service '" + GetParentServiceName() + "' .");
|
|
|
|
m_Parent = parentHost->GetServiceByShortName(GetParentServiceName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_Parent)
|
2014-05-28 13:45:45 +02:00
|
|
|
Log(LogWarning, "Dependency", "Dependency '" + GetName() + "' references an invalid parent object and will always fail.");
|
2014-02-27 11:05:55 +01:00
|
|
|
else
|
2014-06-24 11:46:53 +02:00
|
|
|
m_Parent->AddReverseDependency(GetSelf());
|
2014-02-27 11:05:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dependency::Stop(void)
|
|
|
|
{
|
|
|
|
DynamicObject::Stop();
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (GetChild())
|
|
|
|
GetChild()->RemoveDependency(GetSelf());
|
2014-02-27 11:05:55 +01:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (GetParent())
|
|
|
|
GetParent()->RemoveReverseDependency(GetSelf());
|
2014-02-27 11:05:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Dependency::IsAvailable(DependencyType dt) const
|
|
|
|
{
|
2014-04-03 15:36:13 +02:00
|
|
|
Checkable::Ptr parent = GetParent();
|
2014-02-27 11:05:55 +01:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (!parent)
|
2014-02-27 11:05:55 +01:00
|
|
|
return false;
|
|
|
|
|
2014-05-01 22:07:35 +02:00
|
|
|
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()) {
|
2014-05-28 13:45:45 +02:00
|
|
|
Log(LogNotice, "Dependency", "Dependency '" + GetName() + "' passed: Parent and child " + (service ? "service" : "host") + " are identical.");
|
2014-02-27 11:05:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-01 22:07:35 +02:00
|
|
|
/* ignore pending */
|
2014-04-03 15:36:13 +02:00
|
|
|
if (!parent->GetLastCheckResult()) {
|
2014-05-28 13:45:45 +02:00
|
|
|
Log(LogNotice, "Dependency", "Dependency '" + GetName() + "' passed: " + (service ? "Service" : "Host") + " '" + parent->GetName() + "' hasn't been checked yet.");
|
2014-02-27 11:05:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ignore soft states */
|
2014-04-03 15:36:13 +02:00
|
|
|
if (parent->GetStateType() == StateTypeSoft) {
|
2014-05-28 13:45:45 +02:00
|
|
|
Log(LogNotice, "Dependency", "Dependency '" + GetName() + "' passed: " + (service ? "Service" : "Host") + " '" + parent->GetName() + "' is in a soft state.");
|
2014-02-27 11:05:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
int state;
|
|
|
|
|
2014-04-07 22:06:05 +02:00
|
|
|
if (service)
|
|
|
|
state = ServiceStateToFilter(service->GetState());
|
2014-04-03 15:36:13 +02:00
|
|
|
else
|
2014-04-07 22:06:05 +02:00
|
|
|
state = HostStateToFilter(host->GetState());
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2014-02-27 11:05:55 +01:00
|
|
|
/* check state */
|
2014-04-07 13:59:41 +02:00
|
|
|
if (state & GetStateFilter()) {
|
2014-05-28 13:45:45 +02:00
|
|
|
Log(LogNotice, "Dependency", "Dependency '" + GetName() + "' passed: " + (service ? "Service" : "Host") + " '" + parent->GetName() + "' matches state filter.");
|
2014-02-27 11:05:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ignore if not in time period */
|
|
|
|
TimePeriod::Ptr tp = GetPeriod();
|
|
|
|
if (tp && !tp->IsInside(Utility::GetTime())) {
|
2014-05-28 13:45:45 +02:00
|
|
|
Log(LogNotice, "Dependency", "Dependency '" + GetName() + "' passed: Outside time period.");
|
2014-02-27 11:05:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dt == DependencyCheckExecution && !GetDisableChecks()) {
|
2014-05-28 13:45:45 +02:00
|
|
|
Log(LogNotice, "Dependency", "Dependency '" + GetName() + "' passed: Checks are not disabled.");
|
2014-02-27 11:05:55 +01:00
|
|
|
return true;
|
|
|
|
} else if (dt == DependencyNotification && !GetDisableNotifications()) {
|
2014-05-28 13:45:45 +02:00
|
|
|
Log(LogNotice, "Dependency", "Dependency '" + GetName() + "' passed: Notifications are not disabled");
|
2014-02-27 11:05:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-28 13:45:45 +02:00
|
|
|
Log(LogNotice, "Dependency", "Dependency '" + GetName() + "' failed. Parent " +
|
2014-05-22 23:47:03 +02:00
|
|
|
(service ? "service" : "host") + " '" + parent->GetName() + "' is " +
|
|
|
|
(service ? Service::StateToString(service->GetState()) : Host::StateToString(host->GetState())));
|
2014-02-27 11:05:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
Checkable::Ptr Dependency::GetChild(void) const
|
2014-02-27 11:05:55 +01:00
|
|
|
{
|
2014-06-24 11:46:53 +02:00
|
|
|
return m_Child;
|
2014-02-27 11:05:55 +01:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
Checkable::Ptr Dependency::GetParent(void) const
|
2014-02-27 11:05:55 +01:00
|
|
|
{
|
2014-06-24 11:46:53 +02:00
|
|
|
return m_Parent;
|
2014-02-27 11:05:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TimePeriod::Ptr Dependency::GetPeriod(void) const
|
|
|
|
{
|
|
|
|
return TimePeriod::GetByName(GetPeriodRaw());
|
|
|
|
}
|
2014-04-07 13:59:41 +02:00
|
|
|
|
|
|
|
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 " +
|
2014-05-02 19:37:55 +02:00
|
|
|
location + ": State filter is invalid for host dependency.");
|
2014-04-07 13:59:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (attrs->Contains("parent_service_name") && (sfilter & ~(StateFilterOK | StateFilterWarning | StateFilterCritical | StateFilterUnknown)) != 0) {
|
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, "Validation failed for " +
|
2014-05-02 19:37:55 +02:00
|
|
|
location + ": State filter is invalid for service dependency.");
|
2014-04-07 13:59:41 +02:00
|
|
|
}
|
|
|
|
}
|