]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable.cpp
Convert Comment/Downtime to config objects
[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(bool runtimeCreated)
44 {
45         double now = Utility::GetTime();
46
47         if (GetNextCheck() < now + 300)
48                 UpdateNextCheck();
49
50         ObjectImpl<Checkable>::Start(runtimeCreated);
51 }
52
53 void Checkable::AddGroup(const String& name)
54 {
55         boost::mutex::scoped_lock lock(m_CheckableMutex);
56
57         Array::Ptr groups;
58         Host *host = dynamic_cast<Host *>(this);
59
60         if (host)
61                 groups = host->GetGroups();
62         else
63                 groups = static_cast<Service *>(this)->GetGroups();
64
65         if (groups && groups->Contains(name))
66                 return;
67
68         if (!groups)
69                 groups = new Array();
70
71         groups->Add(name);
72 }
73
74 AcknowledgementType Checkable::GetAcknowledgement(void)
75 {
76         AcknowledgementType avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
77
78         if (avalue != AcknowledgementNone) {
79                 double expiry = GetAcknowledgementExpiry();
80
81                 if (expiry != 0 && expiry < Utility::GetTime()) {
82                         avalue = AcknowledgementNone;
83                         ClearAcknowledgement();
84                 }
85         }
86
87         return avalue;
88 }
89
90 bool Checkable::IsAcknowledged(void)
91 {
92         return GetAcknowledgement() != AcknowledgementNone;
93 }
94
95 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, double expiry, const MessageOrigin::Ptr& origin)
96 {
97         SetAcknowledgementRaw(type);
98         SetAcknowledgementExpiry(expiry);
99
100         if (notify)
101                 OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment);
102
103         OnAcknowledgementSet(this, author, comment, type, notify, expiry, origin);
104 }
105
106 void Checkable::ClearAcknowledgement(const MessageOrigin::Ptr& origin)
107 {
108         SetAcknowledgementRaw(AcknowledgementNone);
109         SetAcknowledgementExpiry(0);
110
111         OnAcknowledgementCleared(this, origin);
112 }
113
114 Endpoint::Ptr Checkable::GetCommandEndpoint(void) const
115 {
116         return Endpoint::GetByName(GetCommandEndpointRaw());
117 }
118
119 void Checkable::ValidateCheckInterval(double value, const ValidationUtils& utils)
120 {
121         ObjectImpl<Checkable>::ValidateCheckInterval(value, utils);
122
123         if (value <= 0)
124                 BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("check_interval"), "Interval must be greater than 0."));
125 }