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, bool reachable)
140 return HostUnreachable;
151 HostState Host::GetState(void) const
156 return HostUnreachable;
158 switch (GetStateRaw()) {
168 HostState Host::GetLastState(void) const
173 return HostUnreachable;
175 switch (GetLastStateRaw()) {
184 HostState Host::GetLastHardState(void) const
189 return HostUnreachable;
191 switch (GetLastHardStateRaw()) {
200 double Host::GetLastStateUp(void) const
204 if (GetLastStateOK() > GetLastStateWarning())
205 return GetLastStateOK();
207 return GetLastStateWarning();
210 double Host::GetLastStateDown(void) const
214 return GetLastStateCritical();
217 HostState Host::StateFromString(const String& state)
221 else if (state == "DOWN")
223 else if (state == "UNREACHABLE")
224 return HostUnreachable;
226 return HostUnreachable;
229 String Host::StateToString(HostState state)
236 case HostUnreachable:
237 return "UNREACHABLE";
243 StateType Host::StateTypeFromString(const String& type)
246 return StateTypeSoft;
248 return StateTypeHard;
251 String Host::StateTypeToString(StateType type)
253 if (type == StateTypeSoft)
259 bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
262 Dictionary::Ptr vars;
264 /* require prefix for object macros */
265 if (macro.SubStr(0, 5) == "host.") {
266 key = macro.SubStr(5);
268 if (key.SubStr(0, 5) == "vars.") {
270 String vars_key = key.SubStr(5);
272 if (vars && vars->Contains(vars_key)) {
273 *result = vars->Get(vars_key);
277 else if (key == "name") {
281 else if (key == "displaymane") {
282 *result = GetDisplayName();
286 CheckResult::Ptr cr = GetLastCheckResult();
288 if (key == "state") {
289 switch (GetState()) {
290 case HostUnreachable:
291 *result = "UNREACHABLE";
304 } else if (key == "stateid") {
305 *result = Convert::ToString(GetState());
307 } else if (key == "statetype") {
308 *result = StateTypeToString(GetStateType());
310 } else if (key == "attempt") {
311 *result = Convert::ToString(GetCheckAttempt());
313 } else if (key == "maxattempt") {
314 *result = Convert::ToString(GetMaxCheckAttempts());
316 } else if (key == "laststate") {
317 *result = StateToString(GetLastState());
319 } else if (key == "laststateid") {
320 *result = Convert::ToString(GetLastState());
322 } else if (key == "laststatetype") {
323 *result = StateTypeToString(GetLastStateType());
325 } else if (key == "laststatechange") {
326 *result = Convert::ToString((long)GetLastStateChange());
328 } else if (key == "durationsec") {
329 *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
331 } else if (key == "checkcommand") {
332 CheckCommand::Ptr commandObj = GetCheckCommand();
335 *result = commandObj->GetName();
340 } else if (key == "totalservices" || key == "totalservicesok" || key == "totalserviceswarning"
341 || key == "totalservicesunknown" || key == "totalservicescritical") {
345 if (key == "totalservicesok")
347 else if (key == "totalserviceswarning")
348 filter = StateWarning;
349 else if (key == "totalservicesunknown")
350 filter = StateUnknown;
351 else if (key == "totalservicescritical")
352 filter = StateCritical;
354 BOOST_FOREACH(const Service::Ptr& service, GetServices()) {
355 if (filter != -1 && service->GetState() != filter)
361 *result = Convert::ToString(count);
367 if (key == "latency") {
368 *result = Convert::ToString(Service::CalculateLatency(cr));
370 } else if (key == "executiontime") {
371 *result = Convert::ToString(Service::CalculateExecutionTime(cr));
373 } else if (key == "output") {
374 *result = cr->GetOutput();
376 } else if (key == "perfdata") {
377 *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
379 } else if (key == "lastcheck") {
380 *result = Convert::ToString((long)cr->GetScheduleStart());
387 if (vars && vars->Contains(macro)) {
388 *result = vars->Get(macro);