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 DynamicObject::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->AddMember(GetSelf());
64 DynamicObject::Stop();
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->RemoveMember(GetSelf());
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
152 return CalculateState(GetStateRaw());
155 HostState Host::GetLastState(void) const
159 return CalculateState(GetLastStateRaw());
162 HostState Host::GetLastHardState(void) const
166 return CalculateState(GetLastHardStateRaw());
169 double Host::GetLastStateUp(void) const
173 if (GetLastStateOK() > GetLastStateWarning())
174 return GetLastStateOK();
176 return GetLastStateWarning();
179 double Host::GetLastStateDown(void) const
183 return GetLastStateCritical();
186 HostState Host::StateFromString(const String& state)
194 String Host::StateToString(HostState state)
206 StateType Host::StateTypeFromString(const String& type)
209 return StateTypeSoft;
211 return StateTypeHard;
214 String Host::StateTypeToString(StateType type)
216 if (type == StateTypeSoft)
222 bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
225 Dictionary::Ptr vars;
227 /* special treatment for address macros providing name fallback */
228 if (macro == "address" || macro == "address6") {
232 if (vars && vars->Contains(macro))
233 value = vars->Get(macro);
235 if (value.IsEmpty()) {
243 else if (macro == "host.vars.address" || macro == "host.vars.address6") {
244 key = macro.SubStr(10);
248 if (vars && vars->Contains(key))
249 value = vars->Get(key);
251 if (value.IsEmpty()) {
260 /* require prefix for object macros */
261 if (macro.SubStr(0, 5) == "host.") {
262 key = macro.SubStr(5);
264 if (key.SubStr(0, 5) == "vars.") {
266 String vars_key = key.SubStr(5);
268 if (vars && vars->Contains(vars_key)) {
269 *result = vars->Get(vars_key);
273 else if (key == "name") {
277 else if (key == "displaymane") {
278 *result = GetDisplayName();
282 CheckResult::Ptr cr = GetLastCheckResult();
284 if (key == "state") {
285 *result = StateToString(GetState());
287 } else if (key == "stateid") {
288 *result = Convert::ToString(GetState());
290 } else if (key == "statetype") {
291 *result = StateTypeToString(GetStateType());
293 } else if (key == "attempt") {
294 *result = Convert::ToString(GetCheckAttempt());
296 } else if (key == "maxattempt") {
297 *result = Convert::ToString(GetMaxCheckAttempts());
299 } else if (key == "laststate") {
300 *result = StateToString(GetLastState());
302 } else if (key == "laststateid") {
303 *result = Convert::ToString(GetLastState());
305 } else if (key == "laststatetype") {
306 *result = StateTypeToString(GetLastStateType());
308 } else if (key == "laststatechange") {
309 *result = Convert::ToString((long)GetLastStateChange());
311 } else if (key == "durationsec") {
312 *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
314 } else if (key == "checkcommand") {
315 CheckCommand::Ptr commandObj = GetCheckCommand();
318 *result = commandObj->GetName();
323 } else if (key == "totalservices" || key == "totalservicesok" || key == "totalserviceswarning"
324 || key == "totalservicesunknown" || key == "totalservicescritical") {
328 if (key == "totalservicesok")
330 else if (key == "totalserviceswarning")
331 filter = StateWarning;
332 else if (key == "totalservicesunknown")
333 filter = StateUnknown;
334 else if (key == "totalservicescritical")
335 filter = StateCritical;
337 BOOST_FOREACH(const Service::Ptr& service, GetServices()) {
338 if (filter != -1 && service->GetState() != filter)
344 *result = Convert::ToString(count);
350 if (key == "latency") {
351 *result = Convert::ToString(Service::CalculateLatency(cr));
353 } else if (key == "executiontime") {
354 *result = Convert::ToString(Service::CalculateExecutionTime(cr));
356 } else if (key == "output") {
357 *result = cr->GetOutput();
359 } else if (key == "perfdata") {
360 *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
362 } else if (key == "lastcheck") {
363 *result = Convert::ToString((long)cr->GetScheduleStart());
370 if (vars && vars->Contains(macro)) {
371 *result = vars->Get(macro);