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 builder->SetScope(item->GetScope());
160 ConfigItem::Ptr serviceItem = builder->Compile();
161 serviceItem->Register();
162 DynamicObject::Ptr dobj = serviceItem->Commit();
163 dobj->OnConfigLoaded();
167 std::set<Service::Ptr> Host::GetServices(void) const
169 boost::mutex::scoped_lock lock(m_ServicesMutex);
171 std::set<Service::Ptr> services;
172 typedef std::pair<String, Service::Ptr> ServicePair;
173 BOOST_FOREACH(const ServicePair& kv, m_Services) {
174 services.insert(kv.second);
180 void Host::AddService(const Service::Ptr& service)
182 boost::mutex::scoped_lock lock(m_ServicesMutex);
184 m_Services[service->GetShortName()] = service;
187 void Host::RemoveService(const Service::Ptr& service)
189 boost::mutex::scoped_lock lock(m_ServicesMutex);
191 m_Services.erase(service->GetShortName());
194 int Host::GetTotalServices(void) const
196 return GetServices().size();
199 Service::Ptr Host::GetServiceByShortName(const Value& name) const
201 if (name.IsEmpty()) {
202 Service::Ptr hc = GetCheckService();
205 BOOST_THROW_EXCEPTION(std::invalid_argument("Host does not have a host check service: " + GetName()));
208 } else if (name.IsScalar()) {
210 boost::mutex::scoped_lock lock(m_ServicesMutex);
212 std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
214 if (it != m_Services.end())
218 return Service::Ptr();
219 } else if (name.IsObjectType<Dictionary>()) {
220 Dictionary::Ptr dict = name;
223 return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
225 BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + JsonSerialize(name)));
229 Service::Ptr Host::GetCheckService(void) const
231 String host_check = GetCheck();
233 if (host_check.IsEmpty())
234 return Service::Ptr();
236 return GetServiceByShortName(host_check);
239 std::set<Host::Ptr> Host::GetParentHosts(void) const
241 std::set<Host::Ptr> result;
242 Service::Ptr hc = GetCheckService();
245 result = hc->GetParentHosts();
250 std::set<Host::Ptr> Host::GetChildHosts(void) const
252 std::set<Host::Ptr> result;
253 Service::Ptr hc = GetCheckService();
256 result = hc->GetChildHosts();
261 std::set<Service::Ptr> Host::GetParentServices(void) const
263 std::set<Service::Ptr> result;
264 Service::Ptr hc = GetCheckService();
267 result = hc->GetParentServices();
272 std::set<Service::Ptr> Host::GetChildServices(void) const
274 std::set<Service::Ptr> result;
275 Service::Ptr hc = GetCheckService();
278 result = hc->GetChildServices();
283 HostState Host::CalculateState(ServiceState state, bool reachable)
286 return HostUnreachable;
297 HostState Host::GetState(void) const
302 return HostUnreachable;
304 Service::Ptr hc = GetCheckService();
309 switch (hc->GetState()) {
319 HostState Host::GetLastState(void) const
324 return HostUnreachable;
326 Service::Ptr hc = GetCheckService();
331 switch (hc->GetLastState()) {
340 HostState Host::GetLastHardState(void) const
345 return HostUnreachable;
347 Service::Ptr hc = GetCheckService();
352 switch (hc->GetLastHardState()) {
361 double Host::GetLastStateUp(void) const
365 Service::Ptr hc = GetCheckService();
370 if (hc->GetLastStateOK() > hc->GetLastStateWarning())
371 return hc->GetLastStateOK();
373 return hc->GetLastStateWarning();
376 double Host::GetLastStateDown(void) const
380 Service::Ptr hc = GetCheckService();
385 return hc->GetLastStateCritical();
388 double Host::GetLastStateUnreachable(void) const
392 Service::Ptr hc = GetCheckService();
397 return hc->GetLastStateUnreachable();
400 double Host::GetLastStateChange(void) const
402 Service::Ptr hc = GetCheckService();
405 return Application::GetStartTime();
407 return hc->GetLastStateChange();
411 double Host::GetLastHardStateChange(void) const
413 Service::Ptr hc = GetCheckService();
416 return Application::GetStartTime();
418 return hc->GetLastHardStateChange();
421 StateType Host::GetLastStateType(void) const
423 Service::Ptr hc = GetCheckService();
426 return StateTypeHard;
428 return hc->GetLastStateType();
431 StateType Host::GetStateType(void) const
433 Service::Ptr hc = GetCheckService();
436 return StateTypeHard;
438 return hc->GetStateType();
441 HostState Host::StateFromString(const String& state)
445 else if (state == "DOWN")
447 else if (state == "UNREACHABLE")
448 return HostUnreachable;
450 return HostUnreachable;
453 String Host::StateToString(HostState state)
460 case HostUnreachable:
461 return "UNREACHABLE";
467 StateType Host::StateTypeFromString(const String& type)
470 return StateTypeSoft;
472 return StateTypeHard;
475 String Host::StateTypeToString(StateType type)
477 if (type == StateTypeSoft)
483 bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
485 if (macro == "HOSTNAME") {
489 else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") {
490 *result = GetDisplayName();
494 Service::Ptr hc = GetCheckService();
495 CheckResult::Ptr hccr;
498 ServiceState state = hc->GetState();
499 bool reachable = IsReachable();
501 if (macro == "HOSTSTATE") {
502 HostState hstate = CalculateState(state, reachable);
505 case HostUnreachable:
506 *result = "UNREACHABLE";
519 } else if (macro == "HOSTSTATEID") {
520 *result = Convert::ToString(state);
522 } else if (macro == "HOSTSTATETYPE") {
523 *result = Service::StateTypeToString(hc->GetStateType());
525 } else if (macro == "HOSTATTEMPT") {
526 *result = Convert::ToString(hc->GetCheckAttempt());
528 } else if (macro == "MAXHOSTATTEMPT") {
529 *result = Convert::ToString(hc->GetMaxCheckAttempts());
531 } else if (macro == "LASTHOSTSTATE") {
532 *result = StateToString(GetLastState());
534 } else if (macro == "LASTHOSTSTATEID") {
535 *result = Convert::ToString(GetLastState());
537 } else if (macro == "LASTHOSTSTATETYPE") {
538 *result = Service::StateTypeToString(GetLastStateType());
540 } else if (macro == "LASTHOSTSTATECHANGE") {
541 *result = Convert::ToString((long)hc->GetLastStateChange());
543 } else if (macro == "HOSTDURATIONSEC") {
544 *result = Convert::ToString((long)(Utility::GetTime() - hc->GetLastStateChange()));
548 hccr = hc->GetLastCheckResult();
552 if (macro == "HOSTLATENCY") {
553 *result = Convert::ToString(Service::CalculateLatency(hccr));
555 } else if (macro == "HOSTEXECUTIONTIME") {
556 *result = Convert::ToString(Service::CalculateExecutionTime(hccr));
558 } else if (macro == "HOSTOUTPUT") {
559 *result = hccr->GetOutput();
561 } else if (macro == "HOSTPERFDATA") {
562 *result = PluginUtility::FormatPerfdata(hccr->GetPerformanceData());
564 } else if (macro == "LASTHOSTCHECK") {
565 *result = Convert::ToString((long)hccr->GetScheduleStart());
570 if (macro.SubStr(0, 5) == "_HOST") {
571 Dictionary::Ptr custom = GetCustom();
572 *result = custom ? custom->Get(macro.SubStr(5)) : "";
576 Dictionary::Ptr macros = GetMacros();
580 if (name == "HOSTADDRESS")
582 else if (macro == "HOSTADDRESS6")
585 if (macros && macros->Contains(name)) {
586 *result = macros->Get(name);
590 if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {