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());
61 UpdateSlaveServices();
66 DynamicObject::Stop();
68 Array::Ptr groups = GetGroups();
71 ObjectLock olock(groups);
73 BOOST_FOREACH(const String& name, groups) {
74 HostGroup::Ptr hg = HostGroup::GetByName(name);
77 hg->RemoveMember(GetSelf());
81 // TODO: unregister slave services/notifications?
84 bool Host::IsReachable(DependencyType dt, shared_ptr<Dependency> *failedDependency) const
88 Service::Ptr hc = GetCheckService();
92 return hc->IsReachable(dt, failedDependency);
95 void Host::UpdateSlaveServices(void)
99 Dictionary::Ptr service_descriptions = GetServiceDescriptions();
101 if (!service_descriptions ||service_descriptions->GetLength() == 0)
104 ConfigItem::Ptr item = ConfigItem::GetObject("Host", GetName());
106 ObjectLock olock(service_descriptions);
107 BOOST_FOREACH(const Dictionary::Pair& kv, service_descriptions) {
108 std::ostringstream namebuf;
109 namebuf << GetName() << "!" << kv.first;
110 String name = namebuf.str();
112 std::vector<String> path;
113 path.push_back("services");
114 path.push_back(kv.first);
116 AExpression::Ptr exprl;
119 ObjectLock ilock(item);
121 exprl = item->GetLinkedExpressionList();
125 exprl->FindDebugInfoPath(path, di);
127 if (di.Path.IsEmpty())
128 di = item->GetDebugInfo();
130 ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
131 builder->SetType("Service");
132 builder->SetName(name);
133 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet, "host", make_shared<AExpression>(&AExpression::OpLiteral, GetName(), di), di));
134 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet, "display_name", make_shared<AExpression>(&AExpression::OpLiteral, kv.first, di), di));
135 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet, "short_name", make_shared<AExpression>(&AExpression::OpLiteral, kv.first, di), di));
137 if (!kv.second.IsObjectType<Dictionary>())
138 BOOST_THROW_EXCEPTION(std::invalid_argument("Service description must be either a string or a dictionary."));
140 Dictionary::Ptr service = kv.second;
142 Array::Ptr templates = service->Get("templates");
145 ObjectLock olock(templates);
147 BOOST_FOREACH(const Value& tmpl, templates) {
148 builder->AddParent(tmpl);
152 /* Clone attributes from the service expression list. */
153 Array::Ptr svc_exprl = make_shared<Array>();
154 exprl->ExtractPath(path, svc_exprl);
156 builder->AddExpression(make_shared<AExpression>(&AExpression::OpDict, svc_exprl, true, di));
158 ConfigItem::Ptr serviceItem = builder->Compile();
159 serviceItem->Register();
160 DynamicObject::Ptr dobj = serviceItem->Commit();
161 dobj->OnConfigLoaded();
165 std::set<Service::Ptr> Host::GetServices(void) const
167 boost::mutex::scoped_lock lock(m_ServicesMutex);
169 std::set<Service::Ptr> services;
170 typedef std::pair<String, Service::Ptr> ServicePair;
171 BOOST_FOREACH(const ServicePair& kv, m_Services) {
172 services.insert(kv.second);
178 void Host::AddService(const Service::Ptr& service)
180 boost::mutex::scoped_lock lock(m_ServicesMutex);
182 m_Services[service->GetShortName()] = service;
185 void Host::RemoveService(const Service::Ptr& service)
187 boost::mutex::scoped_lock lock(m_ServicesMutex);
189 m_Services.erase(service->GetShortName());
192 int Host::GetTotalServices(void) const
194 return GetServices().size();
197 Service::Ptr Host::GetServiceByShortName(const Value& name) const
199 if (name.IsEmpty()) {
200 Service::Ptr hc = GetCheckService();
203 BOOST_THROW_EXCEPTION(std::invalid_argument("Host does not have a host check service: " + GetName()));
206 } else if (name.IsScalar()) {
208 boost::mutex::scoped_lock lock(m_ServicesMutex);
210 std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
212 if (it != m_Services.end())
216 return Service::Ptr();
217 } else if (name.IsObjectType<Dictionary>()) {
218 Dictionary::Ptr dict = name;
221 return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
223 BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + JsonSerialize(name)));
227 Service::Ptr Host::GetCheckService(void) const
229 String host_check = GetCheck();
231 if (host_check.IsEmpty())
232 return Service::Ptr();
234 return GetServiceByShortName(host_check);
237 std::set<Host::Ptr> Host::GetParentHosts(void) const
239 std::set<Host::Ptr> result;
240 Service::Ptr hc = GetCheckService();
243 result = hc->GetParentHosts();
248 std::set<Host::Ptr> Host::GetChildHosts(void) const
250 std::set<Host::Ptr> result;
251 Service::Ptr hc = GetCheckService();
254 result = hc->GetChildHosts();
259 std::set<Service::Ptr> Host::GetParentServices(void) const
261 std::set<Service::Ptr> result;
262 Service::Ptr hc = GetCheckService();
265 result = hc->GetParentServices();
270 std::set<Service::Ptr> Host::GetChildServices(void) const
272 std::set<Service::Ptr> result;
273 Service::Ptr hc = GetCheckService();
276 result = hc->GetChildServices();
281 HostState Host::CalculateState(ServiceState state, bool reachable)
284 return HostUnreachable;
295 HostState Host::GetState(void) const
300 return HostUnreachable;
302 Service::Ptr hc = GetCheckService();
307 switch (hc->GetState()) {
317 HostState Host::GetLastState(void) const
322 return HostUnreachable;
324 Service::Ptr hc = GetCheckService();
329 switch (hc->GetLastState()) {
338 HostState Host::GetLastHardState(void) const
343 return HostUnreachable;
345 Service::Ptr hc = GetCheckService();
350 switch (hc->GetLastHardState()) {
359 double Host::GetLastStateUp(void) const
363 Service::Ptr hc = GetCheckService();
368 if (hc->GetLastStateOK() > hc->GetLastStateWarning())
369 return hc->GetLastStateOK();
371 return hc->GetLastStateWarning();
374 double Host::GetLastStateDown(void) const
378 Service::Ptr hc = GetCheckService();
383 return hc->GetLastStateCritical();
386 double Host::GetLastStateUnreachable(void) const
390 Service::Ptr hc = GetCheckService();
395 return hc->GetLastStateUnreachable();
398 double Host::GetLastStateChange(void) const
400 Service::Ptr hc = GetCheckService();
403 return Application::GetStartTime();
405 return hc->GetLastStateChange();
409 double Host::GetLastHardStateChange(void) const
411 Service::Ptr hc = GetCheckService();
414 return Application::GetStartTime();
416 return hc->GetLastHardStateChange();
419 StateType Host::GetLastStateType(void) const
421 Service::Ptr hc = GetCheckService();
424 return StateTypeHard;
426 return hc->GetLastStateType();
429 StateType Host::GetStateType(void) const
431 Service::Ptr hc = GetCheckService();
434 return StateTypeHard;
436 return hc->GetStateType();
439 HostState Host::StateFromString(const String& state)
443 else if (state == "DOWN")
445 else if (state == "UNREACHABLE")
446 return HostUnreachable;
448 return HostUnreachable;
451 String Host::StateToString(HostState state)
458 case HostUnreachable:
459 return "UNREACHABLE";
465 StateType Host::StateTypeFromString(const String& type)
468 return StateTypeSoft;
470 return StateTypeHard;
473 String Host::StateTypeToString(StateType type)
475 if (type == StateTypeSoft)
481 bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
483 if (macro == "HOSTNAME") {
487 else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") {
488 *result = GetDisplayName();
492 Service::Ptr hc = GetCheckService();
493 CheckResult::Ptr hccr;
496 ServiceState state = hc->GetState();
497 bool reachable = IsReachable();
499 if (macro == "HOSTSTATE") {
500 HostState hstate = CalculateState(state, reachable);
503 case HostUnreachable:
504 *result = "UNREACHABLE";
517 } else if (macro == "HOSTSTATEID") {
518 *result = Convert::ToString(state);
520 } else if (macro == "HOSTSTATETYPE") {
521 *result = Service::StateTypeToString(hc->GetStateType());
523 } else if (macro == "HOSTATTEMPT") {
524 *result = Convert::ToString(hc->GetCheckAttempt());
526 } else if (macro == "MAXHOSTATTEMPT") {
527 *result = Convert::ToString(hc->GetMaxCheckAttempts());
529 } else if (macro == "LASTHOSTSTATE") {
530 *result = StateToString(GetLastState());
532 } else if (macro == "LASTHOSTSTATEID") {
533 *result = Convert::ToString(GetLastState());
535 } else if (macro == "LASTHOSTSTATETYPE") {
536 *result = Service::StateTypeToString(GetLastStateType());
538 } else if (macro == "LASTHOSTSTATECHANGE") {
539 *result = Convert::ToString((long)hc->GetLastStateChange());
541 } else if (macro == "HOSTDURATIONSEC") {
542 *result = Convert::ToString((long)(Utility::GetTime() - hc->GetLastStateChange()));
546 hccr = hc->GetLastCheckResult();
550 if (macro == "HOSTLATENCY") {
551 *result = Convert::ToString(Service::CalculateLatency(hccr));
553 } else if (macro == "HOSTEXECUTIONTIME") {
554 *result = Convert::ToString(Service::CalculateExecutionTime(hccr));
556 } else if (macro == "HOSTOUTPUT") {
557 *result = hccr->GetOutput();
559 } else if (macro == "HOSTPERFDATA") {
560 *result = PluginUtility::FormatPerfdata(hccr->GetPerformanceData());
562 } else if (macro == "LASTHOSTCHECK") {
563 *result = Convert::ToString((long)hccr->GetScheduleStart());
568 if (macro.SubStr(0, 5) == "_HOST") {
569 Dictionary::Ptr custom = GetCustom();
570 *result = custom ? custom->Get(macro.SubStr(5)) : "";
574 Dictionary::Ptr macros = GetMacros();
578 if (name == "HOSTADDRESS")
580 else if (macro == "HOSTADDRESS6")
583 if (macros && macros->Contains(name)) {
584 *result = macros->Get(name);
588 if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {