"Fossies" - the Fresh Open Source Software Archive

Member "icinga2-2.10.5/lib/icinga/checkable-dependency.cpp" (23 May 2019, 4923 Bytes) of package /linux/misc/icinga2-2.10.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "checkable-dependency.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.10.0_vs_2.10.1.

    1 /******************************************************************************
    2  * Icinga 2                                                                   *
    3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
    4  *                                                                            *
    5  * This program is free software; you can redistribute it and/or              *
    6  * modify it under the terms of the GNU General Public License                *
    7  * as published by the Free Software Foundation; either version 2             *
    8  * of the License, or (at your option) any later version.                     *
    9  *                                                                            *
   10  * This program is distributed in the hope that it will be useful,            *
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
   13  * GNU General Public License for more details.                               *
   14  *                                                                            *
   15  * You should have received a copy of the GNU General Public License          *
   16  * along with this program; if not, write to the Free Software Foundation     *
   17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
   18  ******************************************************************************/
   19 
   20 #include "icinga/service.hpp"
   21 #include "icinga/dependency.hpp"
   22 #include "base/logger.hpp"
   23 
   24 using namespace icinga;
   25 
   26 void Checkable::AddDependency(const Dependency::Ptr& dep)
   27 {
   28     boost::mutex::scoped_lock lock(m_DependencyMutex);
   29     m_Dependencies.insert(dep);
   30 }
   31 
   32 void Checkable::RemoveDependency(const Dependency::Ptr& dep)
   33 {
   34     boost::mutex::scoped_lock lock(m_DependencyMutex);
   35     m_Dependencies.erase(dep);
   36 }
   37 
   38 std::vector<Dependency::Ptr> Checkable::GetDependencies() const
   39 {
   40     boost::mutex::scoped_lock lock(m_DependencyMutex);
   41     return std::vector<Dependency::Ptr>(m_Dependencies.begin(), m_Dependencies.end());
   42 }
   43 
   44 void Checkable::AddReverseDependency(const Dependency::Ptr& dep)
   45 {
   46     boost::mutex::scoped_lock lock(m_DependencyMutex);
   47     m_ReverseDependencies.insert(dep);
   48 }
   49 
   50 void Checkable::RemoveReverseDependency(const Dependency::Ptr& dep)
   51 {
   52     boost::mutex::scoped_lock lock(m_DependencyMutex);
   53     m_ReverseDependencies.erase(dep);
   54 }
   55 
   56 std::vector<Dependency::Ptr> Checkable::GetReverseDependencies() const
   57 {
   58     boost::mutex::scoped_lock lock(m_DependencyMutex);
   59     return std::vector<Dependency::Ptr>(m_ReverseDependencies.begin(), m_ReverseDependencies.end());
   60 }
   61 
   62 bool Checkable::IsReachable(DependencyType dt, Dependency::Ptr *failedDependency, int rstack) const
   63 {
   64     if (rstack > 20) {
   65         Log(LogWarning, "Checkable")
   66             << "Too many nested dependencies for service '" << GetName() << "': Dependency failed.";
   67 
   68         return false;
   69     }
   70 
   71     for (const Checkable::Ptr& checkable : GetParents()) {
   72         if (!checkable->IsReachable(dt, failedDependency, rstack + 1))
   73             return false;
   74     }
   75 
   76     /* implicit dependency on host if this is a service */
   77     const auto *service = dynamic_cast<const Service *>(this);
   78     if (service && (dt == DependencyState || dt == DependencyNotification)) {
   79         Host::Ptr host = service->GetHost();
   80 
   81         if (host && host->GetState() != HostUp && host->GetStateType() == StateTypeHard) {
   82             if (failedDependency)
   83                 *failedDependency = nullptr;
   84 
   85             return false;
   86         }
   87     }
   88 
   89     for (const Dependency::Ptr& dep : GetDependencies()) {
   90         if (!dep->IsAvailable(dt)) {
   91             if (failedDependency)
   92                 *failedDependency = dep;
   93 
   94             return false;
   95         }
   96     }
   97 
   98     if (failedDependency)
   99         *failedDependency = nullptr;
  100 
  101     return true;
  102 }
  103 
  104 std::set<Checkable::Ptr> Checkable::GetParents() const
  105 {
  106     std::set<Checkable::Ptr> parents;
  107 
  108     for (const Dependency::Ptr& dep : GetDependencies()) {
  109         Checkable::Ptr parent = dep->GetParent();
  110 
  111         if (parent && parent.get() != this)
  112             parents.insert(parent);
  113     }
  114 
  115     return parents;
  116 }
  117 
  118 std::set<Checkable::Ptr> Checkable::GetChildren() const
  119 {
  120     std::set<Checkable::Ptr> parents;
  121 
  122     for (const Dependency::Ptr& dep : GetReverseDependencies()) {
  123         Checkable::Ptr service = dep->GetChild();
  124 
  125         if (service && service.get() != this)
  126             parents.insert(service);
  127     }
  128 
  129     return parents;
  130 }
  131 
  132 std::set<Checkable::Ptr> Checkable::GetAllChildren() const
  133 {
  134     std::set<Checkable::Ptr> children = GetChildren();
  135 
  136     GetAllChildrenInternal(children, 0);
  137 
  138     return children;
  139 }
  140 
  141 void Checkable::GetAllChildrenInternal(std::set<Checkable::Ptr>& children, int level) const
  142 {
  143     if (level > 32)
  144         return;
  145 
  146     std::set<Checkable::Ptr> localChildren;
  147 
  148     for (const Checkable::Ptr& checkable : children) {
  149         std::set<Checkable::Ptr> cChildren = checkable->GetChildren();
  150 
  151         if (!cChildren.empty()) {
  152             GetAllChildrenInternal(cChildren, level + 1);
  153             localChildren.insert(cChildren.begin(), cChildren.end());
  154         }
  155 
  156         localChildren.insert(checkable);
  157     }
  158 
  159     children.insert(localChildren.begin(), localChildren.end());
  160 }