2014-02-27 11:05:55 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-10-18 09:27:04 +02:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
|
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"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "icinga/dependency-ti.cpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/service.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-12-18 15:43:01 +01:00
|
|
|
#include "base/exception.hpp"
|
2014-02-27 11:05:55 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(Dependency);
|
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
String DependencyNameComposer::MakeName(const String& shortName, const Object::Ptr& context) const
|
2014-04-05 22:17:20 +02:00
|
|
|
{
|
2014-11-06 19:35:47 +01:00
|
|
|
Dependency::Ptr dependency = dynamic_pointer_cast<Dependency>(context);
|
|
|
|
|
|
|
|
if (!dependency)
|
2014-04-05 22:17:20 +02:00
|
|
|
return "";
|
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
String name = dependency->GetChildHostName();
|
2014-04-05 22:17:20 +02:00
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
if (!dependency->GetChildServiceName().IsEmpty())
|
|
|
|
name += "!" + dependency->GetChildServiceName();
|
2014-04-05 22:17:20 +02:00
|
|
|
|
|
|
|
name += "!" + shortName;
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
Dictionary::Ptr DependencyNameComposer::ParseName(const String& name) const
|
|
|
|
{
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> tokens = name.Split("!");
|
2015-08-13 09:02:52 +02:00
|
|
|
|
|
|
|
if (tokens.size() < 2)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid Dependency name."));
|
|
|
|
|
|
|
|
Dictionary::Ptr result = new Dictionary();
|
|
|
|
result->Set("child_host_name", tokens[0]);
|
|
|
|
|
|
|
|
if (tokens.size() > 2) {
|
|
|
|
result->Set("child_service_name", tokens[1]);
|
|
|
|
result->Set("name", tokens[2]);
|
|
|
|
} else {
|
|
|
|
result->Set("name", tokens[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Dependency::OnConfigLoaded()
|
2014-04-07 13:59:41 +02:00
|
|
|
{
|
|
|
|
Value defaultFilter;
|
|
|
|
|
|
|
|
if (GetParentServiceName().IsEmpty())
|
|
|
|
defaultFilter = StateFilterUp;
|
|
|
|
else
|
|
|
|
defaultFilter = StateFilterOK | StateFilterWarning;
|
|
|
|
|
2016-06-21 14:46:01 +02:00
|
|
|
SetStateFilter(FilterArrayToInt(GetStates(), Notification::GetStateFilterMap(), defaultFilter));
|
2014-04-07 13:59:41 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Dependency::OnAllConfigLoaded()
|
2014-02-27 11:05:55 +01:00
|
|
|
{
|
2015-08-25 13:53:43 +02:00
|
|
|
ObjectImpl<Dependency>::OnAllConfigLoaded();
|
2014-02-27 11:05:55 +01:00
|
|
|
|
2014-06-24 11:46:53 +02:00
|
|
|
Host::Ptr childHost = Host::GetByName(GetChildHostName());
|
|
|
|
|
|
|
|
if (childHost) {
|
2016-08-20 23:46:44 +02:00
|
|
|
if (GetChildServiceName().IsEmpty())
|
2014-06-24 11:46:53 +02:00
|
|
|
m_Child = childHost;
|
2016-08-20 23:46:44 +02:00
|
|
|
else
|
2014-06-24 11:46:53 +02:00
|
|
|
m_Child = childHost->GetServiceByShortName(GetChildServiceName());
|
|
|
|
}
|
2017-12-13 12:54:14 +01:00
|
|
|
|
2014-06-24 11:46:53 +02:00
|
|
|
if (!m_Child)
|
2015-03-09 08:04:18 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Dependency '" + GetName() + "' references a child host/service which doesn't exist.", GetDebugInfo()));
|
2014-12-19 10:56:34 +01:00
|
|
|
|
|
|
|
m_Child->AddDependency(this);
|
2014-06-24 11:46:53 +02:00
|
|
|
|
|
|
|
Host::Ptr parentHost = Host::GetByName(GetParentHostName());
|
|
|
|
|
|
|
|
if (parentHost) {
|
2016-08-20 23:46:44 +02:00
|
|
|
if (GetParentServiceName().IsEmpty())
|
2014-06-24 11:46:53 +02:00
|
|
|
m_Parent = parentHost;
|
2016-08-20 23:46:44 +02:00
|
|
|
else
|
2014-06-24 11:46:53 +02:00
|
|
|
m_Parent = parentHost->GetServiceByShortName(GetParentServiceName());
|
|
|
|
}
|
2017-12-13 12:54:14 +01:00
|
|
|
|
2014-06-24 11:46:53 +02:00
|
|
|
if (!m_Parent)
|
2015-03-09 08:04:18 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Dependency '" + GetName() + "' references a parent host/service which doesn't exist.", GetDebugInfo()));
|
2014-12-19 10:56:34 +01:00
|
|
|
|
|
|
|
m_Parent->AddReverseDependency(this);
|
2014-02-27 11:05:55 +01:00
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void Dependency::Stop(bool runtimeRemoved)
|
2014-02-27 11:05:55 +01:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<Dependency>::Stop(runtimeRemoved);
|
2014-02-27 11:05:55 +01:00
|
|
|
|
2014-12-19 10:56:34 +01:00
|
|
|
GetChild()->RemoveDependency(this);
|
|
|
|
GetParent()->RemoveReverseDependency(this);
|
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
|
|
|
|
2015-09-04 13:24:41 +02:00
|
|
|
Host::Ptr parentHost;
|
|
|
|
Service::Ptr parentService;
|
|
|
|
tie(parentHost, parentService) = GetHostService(parent);
|
2014-05-01 22:07:35 +02:00
|
|
|
|
|
|
|
/* ignore if it's the same checkable object */
|
2014-04-03 15:36:13 +02:00
|
|
|
if (parent == GetChild()) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Dependency")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Dependency '" << GetName() << "' passed: Parent and child " << (parentService ? "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-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Dependency")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Dependency '" << GetName() << "' passed: Parent " << (parentService ? "service" : "host") << " '" << parent->GetName() << "' hasn't been checked yet.";
|
2014-02-27 11:05:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-08 00:30:58 +01:00
|
|
|
if (GetIgnoreSoftStates()) {
|
|
|
|
/* ignore soft states */
|
|
|
|
if (parent->GetStateType() == StateTypeSoft) {
|
|
|
|
Log(LogNotice, "Dependency")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Dependency '" << GetName() << "' passed: Parent " << (parentService ? "service" : "host") << " '" << parent->GetName() << "' is in a soft state.";
|
2015-02-08 00:30:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Dependency")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Dependency '" << GetName() << "' failed: Parent " << (parentService ? "service" : "host") << " '" << parent->GetName() << "' is in a soft state.";
|
2014-02-27 11:05:55 +01:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
int state;
|
|
|
|
|
2015-09-04 13:24:41 +02:00
|
|
|
if (parentService)
|
|
|
|
state = ServiceStateToFilter(parentService->GetState());
|
2014-04-03 15:36:13 +02:00
|
|
|
else
|
2015-09-04 13:24:41 +02:00
|
|
|
state = HostStateToFilter(parentHost->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-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Dependency")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Dependency '" << GetName() << "' passed: Parent " << (parentService ? "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-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Dependency")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Dependency '" << GetName() << "' passed: Outside time period.";
|
2014-02-27 11:05:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dt == DependencyCheckExecution && !GetDisableChecks()) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Dependency")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Dependency '" << GetName() << "' passed: Checks are not disabled.";
|
2014-02-27 11:05:55 +01:00
|
|
|
return true;
|
|
|
|
} else if (dt == DependencyNotification && !GetDisableNotifications()) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Dependency")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Dependency '" << GetName() << "' passed: Notifications are not disabled";
|
2014-02-27 11:05:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Dependency")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Dependency '" << GetName() << "' failed. Parent "
|
|
|
|
<< (parentService ? "service" : "host") << " '" << parent->GetName() << "' is "
|
|
|
|
<< (parentService ? Service::StateToString(parentService->GetState()) : Host::StateToString(parentHost->GetState()));
|
2014-10-19 17:52:17 +02:00
|
|
|
|
2014-02-27 11:05:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Checkable::Ptr Dependency::GetChild() 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
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Checkable::Ptr Dependency::GetParent() 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
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
TimePeriod::Ptr Dependency::GetPeriod() const
|
2014-02-27 11:05:55 +01:00
|
|
|
{
|
|
|
|
return TimePeriod::GetByName(GetPeriodRaw());
|
|
|
|
}
|
2014-04-07 13:59:41 +02:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
void Dependency::ValidateStates(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils)
|
2014-04-07 13:59:41 +02:00
|
|
|
{
|
2018-01-11 07:08:09 +01:00
|
|
|
ObjectImpl<Dependency>::ValidateStates(lvalue, utils);
|
2014-04-07 13:59:41 +02:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
int sfilter = FilterArrayToInt(lvalue(), Notification::GetStateFilterMap(), 0);
|
2014-04-07 13:59:41 +02:00
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
if (GetParentServiceName().IsEmpty() && (sfilter & ~(StateFilterUp | StateFilterDown)) != 0)
|
2017-11-30 18:09:38 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "states" }, "State filter is invalid for host dependency."));
|
2014-11-30 23:32:13 +01:00
|
|
|
|
|
|
|
if (!GetParentServiceName().IsEmpty() && (sfilter & ~(StateFilterOK | StateFilterWarning | StateFilterCritical | StateFilterUnknown)) != 0)
|
2017-11-30 18:09:38 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "states" }, "State filter is invalid for service dependency."));
|
2014-04-07 13:59:41 +02:00
|
|
|
}
|
2015-08-25 13:53:43 +02:00
|
|
|
|