]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable.cpp
Fix flexible downtimes expiry time and removal
[icinga2] / lib / icinga / checkable.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/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_WITH_PROTOTYPE(Checkable, Checkable::GetPrototype());
33 INITIALIZE_ONCE(&Checkable::StaticInitialize);
34
35 boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, bool, double, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementSet;
36 boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementCleared;
37
38 void Checkable::StaticInitialize(void)
39 {
40         /* fixed downtime start */
41         Downtime::OnDowntimeAdded.connect(boost::bind(&Checkable::NotifyFixedDowntimeStart, _1));
42         /* flexible downtime start */
43         Downtime::OnDowntimeTriggered.connect(boost::bind(&Checkable::NotifyFlexibleDowntimeStart, _1));
44         /* fixed/flexible downtime end */
45         Downtime::OnDowntimeRemoved.connect(boost::bind(&Checkable::NotifyDowntimeEnd, _1));
46 }
47
48 Checkable::Checkable(void)
49         : m_CheckRunning(false)
50 {
51         SetSchedulingOffset(Utility::Random());
52 }
53
54 void Checkable::Start(bool runtimeCreated)
55 {
56         double now = Utility::GetTime();
57
58         if (GetNextCheck() < now + 300)
59                 UpdateNextCheck();
60
61         ObjectImpl<Checkable>::Start(runtimeCreated);
62 }
63
64 void Checkable::AddGroup(const String& name)
65 {
66         boost::mutex::scoped_lock lock(m_CheckableMutex);
67
68         Array::Ptr groups;
69         Host *host = dynamic_cast<Host *>(this);
70
71         if (host)
72                 groups = host->GetGroups();
73         else
74                 groups = static_cast<Service *>(this)->GetGroups();
75
76         if (groups && groups->Contains(name))
77                 return;
78
79         if (!groups)
80                 groups = new Array();
81
82         groups->Add(name);
83 }
84
85 AcknowledgementType Checkable::GetAcknowledgement(void)
86 {
87         AcknowledgementType avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
88
89         if (avalue != AcknowledgementNone) {
90                 double expiry = GetAcknowledgementExpiry();
91
92                 if (expiry != 0 && expiry < Utility::GetTime()) {
93                         avalue = AcknowledgementNone;
94                         ClearAcknowledgement();
95                 }
96         }
97
98         return avalue;
99 }
100
101 bool Checkable::IsAcknowledged(void)
102 {
103         return GetAcknowledgement() != AcknowledgementNone;
104 }
105
106 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, double expiry, const MessageOrigin::Ptr& origin)
107 {
108         SetAcknowledgementRaw(type);
109         SetAcknowledgementExpiry(expiry);
110
111         if (notify && !IsPaused())
112                 OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment, MessageOrigin::Ptr());
113
114         OnAcknowledgementSet(this, author, comment, type, notify, expiry, origin);
115 }
116
117 void Checkable::ClearAcknowledgement(const MessageOrigin::Ptr& origin)
118 {
119         SetAcknowledgementRaw(AcknowledgementNone);
120         SetAcknowledgementExpiry(0);
121
122         OnAcknowledgementCleared(this, origin);
123 }
124
125 Endpoint::Ptr Checkable::GetCommandEndpoint(void) const
126 {
127         return Endpoint::GetByName(GetCommandEndpointRaw());
128 }
129
130 void Checkable::NotifyFixedDowntimeStart(const Downtime::Ptr& downtime)
131 {
132         if (!downtime->GetFixed())
133                 return;
134
135         NotifyDowntimeInternal(downtime);
136 }
137
138 void Checkable::NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime)
139 {
140         if (downtime->GetFixed())
141                 return;
142
143         NotifyDowntimeInternal(downtime);
144 }
145
146 void Checkable::NotifyDowntimeInternal(const Downtime::Ptr& downtime)
147 {
148         Checkable::Ptr checkable = downtime->GetCheckable();
149
150         if (!checkable->IsPaused())
151                 OnNotificationsRequested(checkable, NotificationDowntimeStart, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), MessageOrigin::Ptr());
152 }
153
154 void Checkable::NotifyDowntimeEnd(const Downtime::Ptr& downtime)
155 {
156         /* don't send notifications for flexible downtimes which never triggered */
157         if (!downtime->GetFixed() && !downtime->IsTriggered())
158                 return;
159
160         Checkable::Ptr checkable = downtime->GetCheckable();
161
162         if (!checkable->IsPaused())
163                 OnNotificationsRequested(checkable, NotificationDowntimeEnd, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), MessageOrigin::Ptr());
164 }
165
166 void Checkable::ValidateCheckInterval(double value, const ValidationUtils& utils)
167 {
168         ObjectImpl<Checkable>::ValidateCheckInterval(value, utils);
169
170         if (value <= 0)
171                 BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("check_interval"), "Interval must be greater than 0."));
172 }