]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable.cpp
Fix spelling errors.
[icinga2] / lib / icinga / checkable.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "icinga/checkable.hpp"
4 #include "icinga/checkable-ti.cpp"
5 #include "icinga/host.hpp"
6 #include "icinga/service.hpp"
7 #include "base/objectlock.hpp"
8 #include "base/utility.hpp"
9 #include "base/exception.hpp"
10 #include "base/timer.hpp"
11 #include <boost/thread/once.hpp>
12
13 using namespace icinga;
14
15 REGISTER_TYPE_WITH_PROTOTYPE(Checkable, Checkable::GetPrototype());
16 INITIALIZE_ONCE(&Checkable::StaticInitialize);
17
18 boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, bool, bool, double, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementSet;
19 boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementCleared;
20
21 static Timer::Ptr l_CheckablesFireSuppressedNotifications;
22
23 void Checkable::StaticInitialize()
24 {
25         /* fixed downtime start */
26         Downtime::OnDowntimeStarted.connect(std::bind(&Checkable::NotifyFixedDowntimeStart, _1));
27         /* flexible downtime start */
28         Downtime::OnDowntimeTriggered.connect(std::bind(&Checkable::NotifyFlexibleDowntimeStart, _1));
29         /* fixed/flexible downtime end */
30         Downtime::OnDowntimeRemoved.connect(std::bind(&Checkable::NotifyDowntimeEnd, _1));
31 }
32
33 Checkable::Checkable()
34 {
35         SetSchedulingOffset(Utility::Random());
36 }
37
38 void Checkable::OnAllConfigLoaded()
39 {
40         ObjectImpl<Checkable>::OnAllConfigLoaded();
41
42         Endpoint::Ptr endpoint = GetCommandEndpoint();
43
44         if (endpoint) {
45                 Zone::Ptr checkableZone = static_pointer_cast<Zone>(GetZone());
46
47                 if (checkableZone) {
48                         Zone::Ptr cmdZone = endpoint->GetZone();
49
50                         if (cmdZone != checkableZone && cmdZone->GetParent() != checkableZone) {
51                                 BOOST_THROW_EXCEPTION(ValidationError(this, { "command_endpoint" },
52                                         "Command endpoint must be in zone '" + checkableZone->GetName() + "' or in a direct child zone thereof."));
53                         }
54                 } else {
55                         BOOST_THROW_EXCEPTION(ValidationError(this, { "command_endpoint" },
56                                 "Command endpoint must not be set."));
57                 }
58         }
59 }
60
61 void Checkable::Start(bool runtimeCreated)
62 {
63         double now = Utility::GetTime();
64
65         if (GetNextCheck() < now + 60) {
66                 double delta = std::min(GetCheckInterval(), 60.0);
67                 delta *= (double)std::rand() / RAND_MAX;
68                 SetNextCheck(now + delta);
69         }
70
71         ObjectImpl<Checkable>::Start(runtimeCreated);
72
73         static boost::once_flag once = BOOST_ONCE_INIT;
74
75         boost::call_once(once, []() {
76                 l_CheckablesFireSuppressedNotifications = new Timer();
77                 l_CheckablesFireSuppressedNotifications->SetInterval(5);
78                 l_CheckablesFireSuppressedNotifications->OnTimerExpired.connect(&Checkable::FireSuppressedNotifications);
79                 l_CheckablesFireSuppressedNotifications->Start();
80         });
81 }
82
83 void Checkable::AddGroup(const String& name)
84 {
85         boost::mutex::scoped_lock lock(m_CheckableMutex);
86
87         Array::Ptr groups;
88         auto *host = dynamic_cast<Host *>(this);
89
90         if (host)
91                 groups = host->GetGroups();
92         else
93                 groups = static_cast<Service *>(this)->GetGroups();
94
95         if (groups && groups->Contains(name))
96                 return;
97
98         if (!groups)
99                 groups = new Array();
100
101         groups->Add(name);
102 }
103
104 AcknowledgementType Checkable::GetAcknowledgement()
105 {
106         auto avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
107
108         if (avalue != AcknowledgementNone) {
109                 double expiry = GetAcknowledgementExpiry();
110
111                 if (expiry != 0 && expiry < Utility::GetTime()) {
112                         avalue = AcknowledgementNone;
113                         ClearAcknowledgement();
114                 }
115         }
116
117         return avalue;
118 }
119
120 bool Checkable::IsAcknowledged() const
121 {
122         return const_cast<Checkable *>(this)->GetAcknowledgement() != AcknowledgementNone;
123 }
124
125 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, bool persistent, double expiry, const MessageOrigin::Ptr& origin)
126 {
127         SetAcknowledgementRaw(type);
128         SetAcknowledgementExpiry(expiry);
129
130         if (notify && !IsPaused())
131                 OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment, nullptr);
132
133         OnAcknowledgementSet(this, author, comment, type, notify, persistent, expiry, origin);
134 }
135
136 void Checkable::ClearAcknowledgement(const MessageOrigin::Ptr& origin)
137 {
138         SetAcknowledgementRaw(AcknowledgementNone);
139         SetAcknowledgementExpiry(0);
140
141         OnAcknowledgementCleared(this, origin);
142 }
143
144 Endpoint::Ptr Checkable::GetCommandEndpoint() const
145 {
146         return Endpoint::GetByName(GetCommandEndpointRaw());
147 }
148
149 int Checkable::GetSeverity() const
150 {
151         /* overridden in Host/Service class. */
152         return 0;
153 }
154
155 bool Checkable::GetProblem() const
156 {
157         return !IsStateOK(GetStateRaw());
158 }
159
160 bool Checkable::GetHandled() const
161 {
162         return GetProblem() && (IsInDowntime() || IsAcknowledged());
163 }
164
165 void Checkable::NotifyFixedDowntimeStart(const Downtime::Ptr& downtime)
166 {
167         if (!downtime->GetFixed())
168                 return;
169
170         NotifyDowntimeInternal(downtime);
171 }
172
173 void Checkable::NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime)
174 {
175         if (downtime->GetFixed())
176                 return;
177
178         NotifyDowntimeInternal(downtime);
179 }
180
181 void Checkable::NotifyDowntimeInternal(const Downtime::Ptr& downtime)
182 {
183         Checkable::Ptr checkable = downtime->GetCheckable();
184
185         if (!checkable->IsPaused())
186                 OnNotificationsRequested(checkable, NotificationDowntimeStart, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
187 }
188
189 void Checkable::NotifyDowntimeEnd(const Downtime::Ptr& downtime)
190 {
191         /* don't send notifications for flexible downtimes which never triggered */
192         if (!downtime->GetFixed() && !downtime->IsTriggered())
193                 return;
194
195         Checkable::Ptr checkable = downtime->GetCheckable();
196
197         if (!checkable->IsPaused())
198                 OnNotificationsRequested(checkable, NotificationDowntimeEnd, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
199 }
200
201 void Checkable::ValidateCheckInterval(const Lazy<double>& lvalue, const ValidationUtils& utils)
202 {
203         ObjectImpl<Checkable>::ValidateCheckInterval(lvalue, utils);
204
205         if (lvalue() <= 0)
206                 BOOST_THROW_EXCEPTION(ValidationError(this, { "check_interval" }, "Interval must be greater than 0."));
207 }
208
209 void Checkable::ValidateRetryInterval(const Lazy<double>& lvalue, const ValidationUtils& utils)
210 {
211         ObjectImpl<Checkable>::ValidateRetryInterval(lvalue, utils);
212
213         if (lvalue() <= 0)
214                 BOOST_THROW_EXCEPTION(ValidationError(this, { "retry_interval" }, "Interval must be greater than 0."));
215 }
216
217 void Checkable::ValidateMaxCheckAttempts(const Lazy<int>& lvalue, const ValidationUtils& utils)
218 {
219         ObjectImpl<Checkable>::ValidateMaxCheckAttempts(lvalue, utils);
220
221         if (lvalue() <= 0)
222                 BOOST_THROW_EXCEPTION(ValidationError(this, { "max_check_attempts" }, "Value must be greater than 0."));
223 }