1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
20 #include "icinga/host.h"
21 #include "icinga/service.h"
22 #include "icinga/hostgroup.h"
23 #include "icinga/icingaapplication.h"
24 #include "icinga/pluginutility.h"
25 #include "base/dynamictype.h"
26 #include "base/objectlock.h"
27 #include "base/logger_fwd.h"
28 #include "base/timer.h"
29 #include "base/convert.h"
30 #include "base/utility.h"
31 #include "base/scriptfunction.h"
32 #include "base/debug.h"
33 #include "base/serializer.h"
34 #include "config/configitembuilder.h"
35 #include "config/configcompilercontext.h"
36 #include <boost/foreach.hpp>
38 using namespace icinga;
42 void Host::OnConfigLoaded(void)
44 Checkable::OnConfigLoaded();
48 Array::Ptr groups = GetGroups();
51 ObjectLock olock(groups);
53 BOOST_FOREACH(const String& name, groups) {
54 HostGroup::Ptr hg = HostGroup::GetByName(name);
57 hg->ResolveGroupMembership(GetSelf(), true);
66 Array::Ptr groups = GetGroups();
69 ObjectLock olock(groups);
71 BOOST_FOREACH(const String& name, groups) {
72 HostGroup::Ptr hg = HostGroup::GetByName(name);
75 hg->ResolveGroupMembership(GetSelf(), false);
79 // TODO: unregister slave services/notifications?
82 std::set<Service::Ptr> Host::GetServices(void) const
84 boost::mutex::scoped_lock lock(m_ServicesMutex);
86 std::set<Service::Ptr> services;
87 typedef std::pair<String, Service::Ptr> ServicePair;
88 BOOST_FOREACH(const ServicePair& kv, m_Services) {
89 services.insert(kv.second);
95 void Host::AddService(const Service::Ptr& service)
97 boost::mutex::scoped_lock lock(m_ServicesMutex);
99 m_Services[service->GetShortName()] = service;
102 void Host::RemoveService(const Service::Ptr& service)
104 boost::mutex::scoped_lock lock(m_ServicesMutex);
106 m_Services.erase(service->GetShortName());
109 int Host::GetTotalServices(void) const
111 return GetServices().size();
114 Service::Ptr Host::GetServiceByShortName(const Value& name)
116 if (name.IsScalar()) {
118 boost::mutex::scoped_lock lock(m_ServicesMutex);
120 std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
122 if (it != m_Services.end())
126 return Service::Ptr();
127 } else if (name.IsObjectType<Dictionary>()) {
128 Dictionary::Ptr dict = name;
131 return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
133 BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + JsonSerialize(name)));
137 HostState Host::CalculateState(ServiceState state)
148 HostState Host::GetState(void) const
150 return CalculateState(GetStateRaw());
153 HostState Host::GetLastState(void) const
155 return CalculateState(GetLastStateRaw());
158 HostState Host::GetLastHardState(void) const
160 return CalculateState(GetLastHardStateRaw());
163 double Host::GetLastStateUp(void) const
165 if (GetLastStateOK() > GetLastStateWarning())
166 return GetLastStateOK();
168 return GetLastStateWarning();
171 double Host::GetLastStateDown(void) const
173 return GetLastStateCritical();
176 HostState Host::StateFromString(const String& state)
184 String Host::StateToString(HostState state)
196 StateType Host::StateTypeFromString(const String& type)
199 return StateTypeSoft;
201 return StateTypeHard;
204 String Host::StateTypeToString(StateType type)
206 if (type == StateTypeSoft)
212 bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
214 if (macro == "state") {
215 *result = StateToString(GetState());
217 } else if (macro == "state_id") {
218 *result = Convert::ToString(GetState());
220 } else if (macro == "state_type") {
221 *result = StateTypeToString(GetStateType());
223 } else if (macro == "last_state") {
224 *result = StateToString(GetLastState());
226 } else if (macro == "last_state_id") {
227 *result = Convert::ToString(GetLastState());
229 } else if (macro == "last_state_type") {
230 *result = StateTypeToString(GetLastStateType());
232 } else if (macro == "last_state_change") {
233 *result = Convert::ToString((long)GetLastStateChange());
235 } else if (macro == "duration_sec") {
236 *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
238 } else if (macro == "total_services" || macro == "total_services_ok" || macro == "total_services_warning"
239 || macro == "total_services_unknown" || macro == "total_services_critical") {
243 if (macro == "total_services_ok")
245 else if (macro == "total_services_warning")
246 filter = ServiceWarning;
247 else if (macro == "total_services_unknown")
248 filter = ServiceUnknown;
249 else if (macro == "total_services_critical")
250 filter = ServiceCritical;
252 BOOST_FOREACH(const Service::Ptr& service, GetServices()) {
253 if (filter != -1 && service->GetState() != filter)
259 *result = Convert::ToString(count);
263 CheckResult::Ptr cr = GetLastCheckResult();
266 if (macro == "latency") {
267 *result = Convert::ToString(Service::CalculateLatency(cr));
269 } else if (macro == "execution_time") {
270 *result = Convert::ToString(Service::CalculateExecutionTime(cr));
272 } else if (macro == "output") {
273 *result = cr->GetOutput();
275 } else if (macro == "perfdata") {
276 *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
278 } else if (macro == "last_check") {
279 *result = Convert::ToString((long)cr->GetScheduleStart());