]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable-dependency.cpp
Update copyright headers for 2016
[icinga2] / lib / icinga / checkable-dependency.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
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 #include <boost/foreach.hpp>
24
25 using namespace icinga;
26
27 void Checkable::AddDependency(const Dependency::Ptr& dep)
28 {
29         boost::mutex::scoped_lock lock(m_DependencyMutex);
30         m_Dependencies.insert(dep);
31 }
32
33 void Checkable::RemoveDependency(const Dependency::Ptr& dep)
34 {
35         boost::mutex::scoped_lock lock(m_DependencyMutex);
36         m_Dependencies.erase(dep);
37 }
38
39 std::set<Dependency::Ptr> Checkable::GetDependencies(void) const
40 {
41         boost::mutex::scoped_lock lock(m_DependencyMutex);
42         return m_Dependencies;
43 }
44
45 void Checkable::AddReverseDependency(const Dependency::Ptr& dep)
46 {
47         boost::mutex::scoped_lock lock(m_DependencyMutex);
48         m_ReverseDependencies.insert(dep);
49 }
50
51 void Checkable::RemoveReverseDependency(const Dependency::Ptr& dep)
52 {
53         boost::mutex::scoped_lock lock(m_DependencyMutex);
54         m_ReverseDependencies.erase(dep);
55 }
56
57 std::set<Dependency::Ptr> Checkable::GetReverseDependencies(void) const
58 {
59         boost::mutex::scoped_lock lock(m_DependencyMutex);
60         return m_ReverseDependencies;
61 }
62
63 bool Checkable::IsReachable(DependencyType dt, Dependency::Ptr *failedDependency, int rstack) const
64 {
65         if (rstack > 20) {
66                 Log(LogWarning, "Checkable")
67                     << "Too many nested dependencies for service '" << GetName() << "': Dependency failed.";
68
69                 return false;
70         }
71
72         BOOST_FOREACH(const Checkable::Ptr& checkable, GetParents()) {
73                 if (!checkable->IsReachable(dt, failedDependency, rstack + 1))
74                         return false;
75         }
76
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();
81
82                 if (host && host->GetState() != HostUp && host->GetStateType() == StateTypeHard) {
83                         if (failedDependency)
84                                 *failedDependency = Dependency::Ptr();
85
86                         return false;
87                 }
88         }
89
90         BOOST_FOREACH(const Dependency::Ptr& dep, GetDependencies()) {
91                 if (!dep->IsAvailable(dt)) {
92                         if (failedDependency)
93                                 *failedDependency = dep;
94
95                         return false;
96                 }
97         }
98
99         if (failedDependency)
100                 *failedDependency = Dependency::Ptr();
101
102         return true;
103 }
104
105 std::set<Checkable::Ptr> Checkable::GetParents(void) const
106 {
107         std::set<Checkable::Ptr> parents;
108
109         BOOST_FOREACH(const Dependency::Ptr& dep, GetDependencies()) {
110                 Checkable::Ptr parent = dep->GetParent();
111
112                 if (parent && parent.get() != this)
113                         parents.insert(parent);
114         }
115
116         return parents;
117 }
118
119 std::set<Checkable::Ptr> Checkable::GetChildren(void) const
120 {
121         std::set<Checkable::Ptr> parents;
122
123         BOOST_FOREACH(const Dependency::Ptr& dep, GetReverseDependencies()) {
124                 Checkable::Ptr service = dep->GetChild();
125
126                 if (service && service.get() != this)
127                         parents.insert(service);
128         }
129
130         return parents;
131 }