1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
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. *
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. *
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 ******************************************************************************/
20 #include "icinga/service.hpp"
21 #include "icinga/dependency.hpp"
22 #include "base/logger.hpp"
24 using namespace icinga;
26 void Checkable::AddDependency(const Dependency::Ptr& dep)
28 boost::mutex::scoped_lock lock(m_DependencyMutex);
29 m_Dependencies.insert(dep);
32 void Checkable::RemoveDependency(const Dependency::Ptr& dep)
34 boost::mutex::scoped_lock lock(m_DependencyMutex);
35 m_Dependencies.erase(dep);
38 std::vector<Dependency::Ptr> Checkable::GetDependencies() const
40 boost::mutex::scoped_lock lock(m_DependencyMutex);
41 return std::vector<Dependency::Ptr>(m_Dependencies.begin(), m_Dependencies.end());
44 void Checkable::AddReverseDependency(const Dependency::Ptr& dep)
46 boost::mutex::scoped_lock lock(m_DependencyMutex);
47 m_ReverseDependencies.insert(dep);
50 void Checkable::RemoveReverseDependency(const Dependency::Ptr& dep)
52 boost::mutex::scoped_lock lock(m_DependencyMutex);
53 m_ReverseDependencies.erase(dep);
56 std::vector<Dependency::Ptr> Checkable::GetReverseDependencies() const
58 boost::mutex::scoped_lock lock(m_DependencyMutex);
59 return std::vector<Dependency::Ptr>(m_ReverseDependencies.begin(), m_ReverseDependencies.end());
62 bool Checkable::IsReachable(DependencyType dt, Dependency::Ptr *failedDependency, int rstack) const
64 /* Anything greater than 256 causes recursion bus errors. */
68 Log(LogWarning, "Checkable")
69 << "Too many nested dependencies (>" << limit << ") for checkable '" << GetName() << "': Dependency failed.";
74 for (const Checkable::Ptr& checkable : GetParents()) {
75 if (!checkable->IsReachable(dt, failedDependency, rstack + 1))
79 /* implicit dependency on host if this is a service */
80 const auto *service = dynamic_cast<const Service *>(this);
81 if (service && (dt == DependencyState || dt == DependencyNotification)) {
82 Host::Ptr host = service->GetHost();
84 if (host && host->GetState() != HostUp && host->GetStateType() == StateTypeHard) {
86 *failedDependency = nullptr;
92 for (const Dependency::Ptr& dep : GetDependencies()) {
93 if (!dep->IsAvailable(dt)) {
95 *failedDependency = dep;
101 if (failedDependency)
102 *failedDependency = nullptr;
107 std::set<Checkable::Ptr> Checkable::GetParents() const
109 std::set<Checkable::Ptr> parents;
111 for (const Dependency::Ptr& dep : GetDependencies()) {
112 Checkable::Ptr parent = dep->GetParent();
114 if (parent && parent.get() != this)
115 parents.insert(parent);
121 std::set<Checkable::Ptr> Checkable::GetChildren() const
123 std::set<Checkable::Ptr> parents;
125 for (const Dependency::Ptr& dep : GetReverseDependencies()) {
126 Checkable::Ptr service = dep->GetChild();
128 if (service && service.get() != this)
129 parents.insert(service);
135 std::set<Checkable::Ptr> Checkable::GetAllChildren() const
137 std::set<Checkable::Ptr> children = GetChildren();
139 GetAllChildrenInternal(children, 0);
144 void Checkable::GetAllChildrenInternal(std::set<Checkable::Ptr>& children, int level) const
149 std::set<Checkable::Ptr> localChildren;
151 for (const Checkable::Ptr& checkable : children) {
152 std::set<Checkable::Ptr> cChildren = checkable->GetChildren();
154 if (!cChildren.empty()) {
155 GetAllChildrenInternal(cChildren, level + 1);
156 localChildren.insert(cChildren.begin(), cChildren.end());
159 localChildren.insert(checkable);
162 children.insert(localChildren.begin(), localChildren.end());