icinga2/lib/icinga/checkable-dependency.cpp

161 lines
4.7 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2016-01-12 08:29:59 +01:00
* Copyright (C) 2012-2016 Icinga Development Team (https://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. *
******************************************************************************/
2014-05-25 16:23:35 +02:00
#include "icinga/service.hpp"
#include "icinga/dependency.hpp"
2014-10-19 14:21:12 +02:00
#include "base/logger.hpp"
using namespace icinga;
2014-04-03 15:36:13 +02:00
void Checkable::AddDependency(const Dependency::Ptr& dep)
{
boost::mutex::scoped_lock lock(m_DependencyMutex);
m_Dependencies.insert(dep);
}
2014-04-03 15:36:13 +02:00
void Checkable::RemoveDependency(const Dependency::Ptr& dep)
{
boost::mutex::scoped_lock lock(m_DependencyMutex);
m_Dependencies.erase(dep);
}
2014-04-03 15:36:13 +02:00
std::set<Dependency::Ptr> Checkable::GetDependencies(void) const
{
boost::mutex::scoped_lock lock(m_DependencyMutex);
return m_Dependencies;
}
2014-04-03 15:36:13 +02:00
void Checkable::AddReverseDependency(const Dependency::Ptr& dep)
{
boost::mutex::scoped_lock lock(m_DependencyMutex);
m_ReverseDependencies.insert(dep);
}
2014-04-03 15:36:13 +02:00
void Checkable::RemoveReverseDependency(const Dependency::Ptr& dep)
{
boost::mutex::scoped_lock lock(m_DependencyMutex);
m_ReverseDependencies.erase(dep);
}
2014-04-03 15:36:13 +02:00
std::set<Dependency::Ptr> Checkable::GetReverseDependencies(void) const
{
boost::mutex::scoped_lock lock(m_DependencyMutex);
return m_ReverseDependencies;
}
2014-04-03 15:36:13 +02:00
bool Checkable::IsReachable(DependencyType dt, Dependency::Ptr *failedDependency, int rstack) const
{
if (rstack > 20) {
2014-10-19 17:52:17 +02:00
Log(LogWarning, "Checkable")
<< "Too many nested dependencies for service '" << GetName() << "': Dependency failed.";
return false;
}
for (const Checkable::Ptr& checkable : GetParents()) {
if (!checkable->IsReachable(dt, failedDependency, rstack + 1))
return false;
}
2014-04-03 15:36:13 +02:00
/* implicit dependency on host if this is a service */
const Service *service = dynamic_cast<const Service *>(this);
if (service && (dt == DependencyState || dt == DependencyNotification)) {
Host::Ptr host = service->GetHost();
2014-04-03 15:36:13 +02:00
if (host && host->GetState() != HostUp && host->GetStateType() == StateTypeHard) {
if (failedDependency)
*failedDependency = Dependency::Ptr();
return false;
}
}
for (const Dependency::Ptr& dep : GetDependencies()) {
if (!dep->IsAvailable(dt)) {
if (failedDependency)
*failedDependency = dep;
return false;
}
}
if (failedDependency)
*failedDependency = Dependency::Ptr();
return true;
}
2014-04-03 15:36:13 +02:00
std::set<Checkable::Ptr> Checkable::GetParents(void) const
{
2014-04-03 15:36:13 +02:00
std::set<Checkable::Ptr> parents;
for (const Dependency::Ptr& dep : GetDependencies()) {
2014-04-03 15:36:13 +02:00
Checkable::Ptr parent = dep->GetParent();
if (parent && parent.get() != this)
2014-04-03 15:36:13 +02:00
parents.insert(parent);
}
return parents;
}
2014-04-03 15:36:13 +02:00
std::set<Checkable::Ptr> Checkable::GetChildren(void) const
{
2014-04-03 15:36:13 +02:00
std::set<Checkable::Ptr> parents;
for (const Dependency::Ptr& dep : GetReverseDependencies()) {
2014-04-03 15:36:13 +02:00
Checkable::Ptr service = dep->GetChild();
if (service && service.get() != this)
parents.insert(service);
}
return parents;
}
std::set<Checkable::Ptr> Checkable::GetAllChildren(void) const
{
std::set<Checkable::Ptr> children = GetChildren();
GetAllChildrenInternal(children, 0);
return children;
}
void Checkable::GetAllChildrenInternal(std::set<Checkable::Ptr>& children, int level) const
{
if (level > 32)
return;
std::set<Checkable::Ptr> localChildren;
for (const Checkable::Ptr& checkable : children) {
std::set<Checkable::Ptr> cChildren = checkable->GetChildren();
if (!checkable->GetChildren().empty()) {
GetAllChildrenInternal(cChildren, level + 1);
localChildren.insert(cChildren.begin(), cChildren.end());
}
localChildren.insert(checkable);
}
children.insert(localChildren.begin(), localChildren.end());
}