]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable.cpp
Remove dead code from old modified attributes
[icinga2] / lib / icinga / checkable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 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/checkable.hpp"
21 #include "icinga/checkable.tcpp"
22 #include "icinga/host.hpp"
23 #include "icinga/service.hpp"
24 #include "base/objectlock.hpp"
25 #include "base/utility.hpp"
26 #include "base/exception.hpp"
27 #include <boost/foreach.hpp>
28 #include <boost/bind/apply.hpp>
29
30 using namespace icinga;
31
32 REGISTER_TYPE(Checkable);
33
34 boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, bool, double, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementSet;
35 boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementCleared;
36
37 Checkable::Checkable(void)
38         : m_CheckRunning(false)
39 {
40         SetSchedulingOffset(Utility::Random());
41 }
42
43 void Checkable::Start(void)
44 {
45         double now = Utility::GetTime();
46
47         if (GetNextCheck() < now + 300)
48                 UpdateNextCheck();
49
50         ObjectImpl<Checkable>::Start();
51 }
52
53 void Checkable::OnStateLoaded(void)
54 {
55         AddDowntimesToCache();
56         AddCommentsToCache();
57
58         std::vector<String> ids;
59         Dictionary::Ptr downtimes = GetDowntimes();
60
61         {
62                 ObjectLock dlock(downtimes);
63                 BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
64                         Downtime::Ptr downtime = kv.second;
65
66                         if (downtime->GetScheduledBy().IsEmpty())
67                                 continue;
68
69                         ids.push_back(kv.first);
70                 }
71         }
72
73         BOOST_FOREACH(const String& id, ids) {
74                 /* override config owner to clear downtimes once */
75                 Downtime::Ptr downtime = GetDowntimeByID(id);
76                 downtime->SetConfigOwner(Empty);
77                 RemoveDowntime(id, true);
78         }
79 }
80
81 void Checkable::AddGroup(const String& name)
82 {
83         boost::mutex::scoped_lock lock(m_CheckableMutex);
84
85         Array::Ptr groups;
86         Host *host = dynamic_cast<Host *>(this);
87
88         if (host)
89                 groups = host->GetGroups();
90         else
91                 groups = static_cast<Service *>(this)->GetGroups();
92
93         if (groups && groups->Contains(name))
94                 return;
95
96         if (!groups)
97                 groups = new Array();
98
99         groups->Add(name);
100 }
101
102 AcknowledgementType Checkable::GetAcknowledgement(void)
103 {
104         AcknowledgementType avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
105
106         if (avalue != AcknowledgementNone) {
107                 double expiry = GetAcknowledgementExpiry();
108
109                 if (expiry != 0 && expiry < Utility::GetTime()) {
110                         avalue = AcknowledgementNone;
111                         ClearAcknowledgement();
112                 }
113         }
114
115         return avalue;
116 }
117
118 bool Checkable::IsAcknowledged(void)
119 {
120         return GetAcknowledgement() != AcknowledgementNone;
121 }
122
123 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, double expiry, const MessageOrigin::Ptr& origin)
124 {
125         SetAcknowledgementRaw(type);
126         SetAcknowledgementExpiry(expiry);
127
128         if (notify)
129                 OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment);
130
131         OnAcknowledgementSet(this, author, comment, type, notify, expiry, origin);
132 }
133
134 void Checkable::ClearAcknowledgement(const MessageOrigin::Ptr& origin)
135 {
136         SetAcknowledgementRaw(AcknowledgementNone);
137         SetAcknowledgementExpiry(0);
138
139         OnAcknowledgementCleared(this, origin);
140 }
141
142 Endpoint::Ptr Checkable::GetCommandEndpoint(void) const
143 {
144         return Endpoint::GetByName(GetCommandEndpointRaw());
145 }
146
147 void Checkable::ValidateCheckInterval(double value, const ValidationUtils& utils)
148 {
149         ObjectImpl<Checkable>::ValidateCheckInterval(value, utils);
150
151         if (value <= 0)
152                 BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("check_interval"), "Interval must be greater than 0."));
153 }