1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
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"
23 #include <boost/foreach.hpp>
25 using namespace icinga;
27 void Checkable::AddDependency(const Dependency::Ptr& dep)
29 boost::mutex::scoped_lock lock(m_DependencyMutex);
30 m_Dependencies.insert(dep);
33 void Checkable::RemoveDependency(const Dependency::Ptr& dep)
35 boost::mutex::scoped_lock lock(m_DependencyMutex);
36 m_Dependencies.erase(dep);
39 std::set<Dependency::Ptr> Checkable::GetDependencies(void) const
41 boost::mutex::scoped_lock lock(m_DependencyMutex);
42 return m_Dependencies;
45 void Checkable::AddReverseDependency(const Dependency::Ptr& dep)
47 boost::mutex::scoped_lock lock(m_DependencyMutex);
48 m_ReverseDependencies.insert(dep);
51 void Checkable::RemoveReverseDependency(const Dependency::Ptr& dep)
53 boost::mutex::scoped_lock lock(m_DependencyMutex);
54 m_ReverseDependencies.erase(dep);
57 std::set<Dependency::Ptr> Checkable::GetReverseDependencies(void) const
59 boost::mutex::scoped_lock lock(m_DependencyMutex);
60 return m_ReverseDependencies;
63 bool Checkable::IsReachable(DependencyType dt, Dependency::Ptr *failedDependency, int rstack) const
66 Log(LogWarning, "Checkable")
67 << "Too many nested dependencies for service '" << GetName() << "': Dependency failed.";
72 BOOST_FOREACH(const Checkable::Ptr& checkable, GetParents()) {
73 if (!checkable->IsReachable(dt, failedDependency, rstack + 1))
77 /* implicit dependency on host if this is a service */
78 const Service *service = dynamic_cast<const Service *>(this);
79 if (service && (dt == DependencyState || dt == DependencyNotification)) {
80 Host::Ptr host = service->GetHost();
82 if (host && host->GetState() != HostUp && host->GetStateType() == StateTypeHard) {
84 *failedDependency = Dependency::Ptr();
90 BOOST_FOREACH(const Dependency::Ptr& dep, GetDependencies()) {
91 if (!dep->IsAvailable(dt)) {
93 *failedDependency = dep;
100 *failedDependency = Dependency::Ptr();
105 std::set<Checkable::Ptr> Checkable::GetParents(void) const
107 std::set<Checkable::Ptr> parents;
109 BOOST_FOREACH(const Dependency::Ptr& dep, GetDependencies()) {
110 Checkable::Ptr parent = dep->GetParent();
112 if (parent && parent.get() != this)
113 parents.insert(parent);
119 std::set<Checkable::Ptr> Checkable::GetChildren(void) const
121 std::set<Checkable::Ptr> parents;
123 BOOST_FOREACH(const Dependency::Ptr& dep, GetReverseDependencies()) {
124 Checkable::Ptr service = dep->GetChild();
126 if (service && service.get() != this)
127 parents.insert(service);