]> granicus.if.org Git - icinga2/blobdiff - lib/icinga/checkable-dependency.cpp
Service: be handled while host is down
[icinga2] / lib / icinga / checkable-dependency.cpp
index f3769faec556647039bd7f8b19ba8dcee99efac8..10e39ab582066ddb7cfaecd988c6f652eaea9935 100644 (file)
@@ -1,26 +1,8 @@
-/******************************************************************************
- * Icinga 2                                                                   *
- * Copyright (C) 2012-2015 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.             *
- ******************************************************************************/
+/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
 
 #include "icinga/service.hpp"
 #include "icinga/dependency.hpp"
 #include "base/logger.hpp"
-#include <boost/foreach.hpp>
 
 using namespace icinga;
 
@@ -36,10 +18,10 @@ void Checkable::RemoveDependency(const Dependency::Ptr& dep)
        m_Dependencies.erase(dep);
 }
 
-std::set<Dependency::Ptr> Checkable::GetDependencies(void) const
+std::vector<Dependency::Ptr> Checkable::GetDependencies() const
 {
        boost::mutex::scoped_lock lock(m_DependencyMutex);
-       return m_Dependencies;
+       return std::vector<Dependency::Ptr>(m_Dependencies.begin(), m_Dependencies.end());
 }
 
 void Checkable::AddReverseDependency(const Dependency::Ptr& dep)
@@ -54,40 +36,43 @@ void Checkable::RemoveReverseDependency(const Dependency::Ptr& dep)
        m_ReverseDependencies.erase(dep);
 }
 
-std::set<Dependency::Ptr> Checkable::GetReverseDependencies(void) const
+std::vector<Dependency::Ptr> Checkable::GetReverseDependencies() const
 {
        boost::mutex::scoped_lock lock(m_DependencyMutex);
-       return m_ReverseDependencies;
+       return std::vector<Dependency::Ptr>(m_ReverseDependencies.begin(), m_ReverseDependencies.end());
 }
 
 bool Checkable::IsReachable(DependencyType dt, Dependency::Ptr *failedDependency, int rstack) const
 {
-       if (rstack > 20) {
+       /* Anything greater than 256 causes recursion bus errors. */
+       int limit = 256;
+
+       if (rstack > limit) {
                Log(LogWarning, "Checkable")
-                   << "Too many nested dependencies for service '" << GetName() << "': Dependency failed.";
+                       << "Too many nested dependencies (>" << limit << ") for checkable '" << GetName() << "': Dependency failed.";
 
                return false;
        }
 
-       BOOST_FOREACH(const Checkable::Ptr& checkable, GetParents()) {
+       for (const Checkable::Ptr& checkable : GetParents()) {
                if (!checkable->IsReachable(dt, failedDependency, rstack + 1))
                        return false;
        }
 
        /* implicit dependency on host if this is a service */
-       const Service *service = dynamic_cast<const Service *>(this);
+       const auto *service = dynamic_cast<const Service *>(this);
        if (service && (dt == DependencyState || dt == DependencyNotification)) {
                Host::Ptr host = service->GetHost();
 
                if (host && host->GetState() != HostUp && host->GetStateType() == StateTypeHard) {
                        if (failedDependency)
-                               *failedDependency = Dependency::Ptr();
+                               *failedDependency = nullptr;
 
                        return false;
                }
        }
 
-       BOOST_FOREACH(const Dependency::Ptr& dep, GetDependencies()) {
+       for (const Dependency::Ptr& dep : GetDependencies()) {
                if (!dep->IsAvailable(dt)) {
                        if (failedDependency)
                                *failedDependency = dep;
@@ -97,16 +82,16 @@ bool Checkable::IsReachable(DependencyType dt, Dependency::Ptr *failedDependency
        }
 
        if (failedDependency)
-               *failedDependency = Dependency::Ptr();
+               *failedDependency = nullptr;
 
        return true;
 }
 
-std::set<Checkable::Ptr> Checkable::GetParents(void) const
+std::set<Checkable::Ptr> Checkable::GetParents() const
 {
        std::set<Checkable::Ptr> parents;
 
-       BOOST_FOREACH(const Dependency::Ptr& dep, GetDependencies()) {
+       for (const Dependency::Ptr& dep : GetDependencies()) {
                Checkable::Ptr parent = dep->GetParent();
 
                if (parent && parent.get() != this)
@@ -116,11 +101,11 @@ std::set<Checkable::Ptr> Checkable::GetParents(void) const
        return parents;
 }
 
-std::set<Checkable::Ptr> Checkable::GetChildren(void) const
+std::set<Checkable::Ptr> Checkable::GetChildren() const
 {
        std::set<Checkable::Ptr> parents;
 
-       BOOST_FOREACH(const Dependency::Ptr& dep, GetReverseDependencies()) {
+       for (const Dependency::Ptr& dep : GetReverseDependencies()) {
                Checkable::Ptr service = dep->GetChild();
 
                if (service && service.get() != this)
@@ -129,3 +114,33 @@ std::set<Checkable::Ptr> Checkable::GetChildren(void) const
 
        return parents;
 }
+
+std::set<Checkable::Ptr> Checkable::GetAllChildren() 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 (!cChildren.empty()) {
+                       GetAllChildrenInternal(cChildren, level + 1);
+                       localChildren.insert(cChildren.begin(), cChildren.end());
+               }
+
+               localChildren.insert(checkable);
+       }
+
+       children.insert(localChildren.begin(), localChildren.end());
+}