]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable-dependency.cpp
Merge branch 'fix/rename-log-lib-name-6346' into next
[icinga2] / lib / icinga / checkable-dependency.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://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_fwd.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", "Too many nested dependencies for service '" + GetName() + "': Dependency failed.");
67
68                 return false;
69         }
70
71         BOOST_FOREACH(const Checkable::Ptr& service, GetParents()) {
72                 if (!service->IsReachable(dt, failedDependency, rstack + 1))
73                         return false;
74         }
75
76         /* implicit dependency on host if this is a service */
77         const Service *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 = Dependency::Ptr();
84
85                         return false;
86                 }
87         }
88
89         BOOST_FOREACH(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 = Dependency::Ptr();
100
101         return true;
102 }
103
104 std::set<Checkable::Ptr> Checkable::GetParents(void) const
105 {
106         std::set<Checkable::Ptr> parents;
107
108         BOOST_FOREACH(const Dependency::Ptr& dep, GetDependencies()) {
109                 Checkable::Ptr parent = dep->GetParent();
110
111                 if (parent)
112                         parents.insert(parent);
113         }
114
115         return parents;
116 }
117
118 std::set<Checkable::Ptr> Checkable::GetChildren(void) const
119 {
120         std::set<Checkable::Ptr> parents;
121
122         BOOST_FOREACH(const Dependency::Ptr& dep, GetReverseDependencies()) {
123                 Checkable::Ptr service = dep->GetChild();
124
125                 if (service)
126                         parents.insert(service);
127         }
128
129         return parents;
130 }