1 /******************************************************************************
3 * Copyright (C) 2012-2013 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/tuple/tuple.hpp>
37 #include <boost/foreach.hpp>
39 using namespace icinga;
44 void Host::Start(void)
46 DynamicObject::Start();
50 Array::Ptr groups = GetGroups();
53 ObjectLock olock(groups);
55 BOOST_FOREACH(const String& name, groups) {
56 HostGroup::Ptr hg = HostGroup::GetByName(name);
59 hg->AddMember(GetSelf());
64 void Host::OnConfigLoaded(void)
66 UpdateSlaveServices();
71 DynamicObject::Stop();
73 Array::Ptr groups = GetGroups();
76 ObjectLock olock(groups);
78 BOOST_FOREACH(const String& name, groups) {
79 HostGroup::Ptr hg = HostGroup::GetByName(name);
82 hg->RemoveMember(GetSelf());
86 // TODO: unregister slave services/notifications?
89 bool Host::IsReachable(void) const
93 std::set<Service::Ptr> parentServices = GetParentServices();
95 BOOST_FOREACH(const Service::Ptr& service, parentServices) {
96 ObjectLock olock(service);
98 /* ignore pending services */
99 if (!service->GetLastCheckResult())
102 /* ignore soft states */
103 if (service->GetStateType() == StateTypeSoft)
106 /* ignore services states OK and Warning */
107 if (service->GetState() == StateOK ||
108 service->GetState() == StateWarning)
114 std::set<Host::Ptr> parentHosts = GetParentHosts();
116 BOOST_FOREACH(const Host::Ptr& host, parentHosts) {
117 Service::Ptr hc = host->GetCheckService();
119 /* ignore hosts that don't have a check */
123 ObjectLock olock(hc);
125 /* ignore soft states */
126 if (hc->GetStateType() == StateTypeSoft)
129 /* ignore hosts that are up */
130 if (hc->GetState() == StateOK)
139 void Host::UpdateSlaveServices(void)
143 ConfigItem::Ptr item = ConfigItem::GetObject("Host", GetName());
145 /* Don't create slave services unless we own this object */
149 Dictionary::Ptr service_descriptions = GetServiceDescriptions();
151 if (!service_descriptions)
154 ObjectLock olock(service_descriptions);
157 BOOST_FOREACH(boost::tie(svcname, svcdesc), service_descriptions) {
158 if (svcdesc.IsScalar())
161 std::ostringstream namebuf;
162 namebuf << GetName() << ":" << svcname;
163 String name = namebuf.str();
165 std::vector<String> path;
166 path.push_back("services");
167 path.push_back(svcname);
170 item->GetLinkedExpressionList()->FindDebugInfoPath(path, di);
172 if (di.Path.IsEmpty())
173 di = item->GetDebugInfo();
175 ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
176 builder->SetType("Service");
177 builder->SetName(name);
178 builder->AddExpression("host", OperatorSet, GetName());
179 builder->AddExpression("display_name", OperatorSet, svcname);
180 builder->AddExpression("short_name", OperatorSet, svcname);
182 if (!svcdesc.IsObjectType<Dictionary>())
183 BOOST_THROW_EXCEPTION(std::invalid_argument("Service description must be either a string or a dictionary."));
185 Dictionary::Ptr service = svcdesc;
187 Array::Ptr templates = service->Get("templates");
190 ObjectLock olock(templates);
192 BOOST_FOREACH(const Value& tmpl, templates) {
193 builder->AddParent(tmpl);
197 /* Clone attributes from the service expression list. */
198 ExpressionList::Ptr svc_exprl = make_shared<ExpressionList>();
199 item->GetLinkedExpressionList()->ExtractPath(path, svc_exprl);
201 std::vector<String> dpath;
202 dpath.push_back("templates");
203 svc_exprl->ErasePath(dpath);
205 builder->AddExpressionList(svc_exprl);
207 ConfigItem::Ptr serviceItem = builder->Compile();
208 serviceItem->Register();
209 DynamicObject::Ptr dobj = serviceItem->Commit();
210 dobj->OnConfigLoaded();
214 std::set<Service::Ptr> Host::GetServices(void) const
216 boost::mutex::scoped_lock lock(m_ServicesMutex);
218 std::set<Service::Ptr> services;
219 Service::WeakPtr wservice;
220 BOOST_FOREACH(boost::tie(boost::tuples::ignore, wservice), m_Services) {
221 Service::Ptr service = wservice.lock();
226 services.insert(service);
232 void Host::AddService(const Service::Ptr& service)
234 boost::mutex::scoped_lock lock(m_ServicesMutex);
236 m_Services[service->GetShortName()] = service;
239 void Host::RemoveService(const Service::Ptr& service)
241 boost::mutex::scoped_lock lock(m_ServicesMutex);
243 m_Services.erase(service->GetShortName());
246 int Host::GetTotalServices(void) const
248 return GetServices().size();
251 Service::Ptr Host::GetServiceByShortName(const Value& name) const
253 if (name.IsScalar()) {
255 boost::mutex::scoped_lock lock(m_ServicesMutex);
257 std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
259 if (it != m_Services.end())
263 return Service::Ptr();
264 } else if (name.IsObjectType<Dictionary>()) {
265 Dictionary::Ptr dict = name;
268 return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
270 BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + JsonSerialize(name)));
274 std::set<Host::Ptr> Host::GetParentHosts(void) const
276 std::set<Host::Ptr> parents;
278 Array::Ptr dependencies = GetHostDependencies();
281 ObjectLock olock(dependencies);
283 BOOST_FOREACH(const Value& value, dependencies) {
284 if (value == GetName())
287 Host::Ptr host = GetByName(value);
292 parents.insert(host);
299 std::set<Host::Ptr> Host::GetChildHosts(void) const
301 std::set<Host::Ptr> children;
303 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
304 Array::Ptr dependencies = host->GetHostDependencies();
307 ObjectLock olock(dependencies);
309 BOOST_FOREACH(const Value& value, dependencies) {
310 if (value == GetName())
311 children.insert(host);
319 Service::Ptr Host::GetCheckService(void) const
321 String host_check = GetCheck();
323 if (host_check.IsEmpty())
324 return Service::Ptr();
326 return GetServiceByShortName(host_check);
329 std::set<Service::Ptr> Host::GetParentServices(void) const
331 std::set<Service::Ptr> parents;
333 Array::Ptr dependencies = GetServiceDependencies();
336 ObjectLock olock(dependencies);
338 BOOST_FOREACH(const Value& value, dependencies) {
339 parents.insert(GetServiceByShortName(value));
346 HostState Host::CalculateState(ServiceState state, bool reachable)
349 return HostUnreachable;
360 HostState Host::GetState(void) const
365 return HostUnreachable;
367 Service::Ptr hc = GetCheckService();
372 switch (hc->GetState()) {
382 HostState Host::GetLastState(void) const
387 return HostUnreachable;
389 Service::Ptr hc = GetCheckService();
394 switch (hc->GetLastState()) {
403 HostState Host::GetLastHardState(void) const
408 return HostUnreachable;
410 Service::Ptr hc = GetCheckService();
415 switch (hc->GetLastHardState()) {
424 double Host::GetLastStateUp(void) const
428 Service::Ptr hc = GetCheckService();
433 if (hc->GetLastStateOK() > hc->GetLastStateWarning())
434 return hc->GetLastStateOK();
436 return hc->GetLastStateWarning();
439 double Host::GetLastStateDown(void) const
443 Service::Ptr hc = GetCheckService();
448 return hc->GetLastStateCritical();
451 double Host::GetLastStateUnreachable(void) const
455 Service::Ptr hc = GetCheckService();
460 return hc->GetLastStateUnreachable();
463 double Host::GetLastStateChange(void) const
465 Service::Ptr hc = GetCheckService();
468 return Application::GetStartTime();
470 return hc->GetLastStateChange();
474 double Host::GetLastHardStateChange(void) const
476 Service::Ptr hc = GetCheckService();
479 return Application::GetStartTime();
481 return hc->GetLastHardStateChange();
484 StateType Host::GetLastStateType(void) const
486 Service::Ptr hc = GetCheckService();
489 return StateTypeHard;
491 return hc->GetLastStateType();
494 StateType Host::GetStateType(void) const
496 Service::Ptr hc = GetCheckService();
499 return StateTypeHard;
501 return hc->GetStateType();
504 HostState Host::StateFromString(const String& state)
508 else if (state == "DOWN")
510 else if (state == "UNREACHABLE")
511 return HostUnreachable;
513 return HostUnreachable;
516 String Host::StateToString(HostState state)
523 case HostUnreachable:
524 return "UNREACHABLE";
530 StateType Host::StateTypeFromString(const String& type)
533 return StateTypeSoft;
535 return StateTypeHard;
538 String Host::StateTypeToString(StateType type)
540 if (type == StateTypeSoft)
546 bool Host::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
548 if (macro == "HOSTNAME") {
552 else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") {
553 *result = GetDisplayName();
557 Service::Ptr hc = GetCheckService();
558 Dictionary::Ptr hccr;
561 ServiceState state = hc->GetState();
562 bool reachable = IsReachable();
564 if (macro == "HOSTSTATE") {
565 HostState hstate = CalculateState(state, reachable);
568 case HostUnreachable:
569 *result = "UNREACHABLE";
582 } else if (macro == "HOSTSTATEID") {
583 *result = Convert::ToString(state);
585 } else if (macro == "HOSTSTATETYPE") {
586 *result = Service::StateTypeToString(hc->GetStateType());
588 } else if (macro == "HOSTATTEMPT") {
589 *result = Convert::ToString(hc->GetCheckAttempt());
591 } else if (macro == "MAXHOSTATTEMPT") {
592 *result = Convert::ToString(hc->GetMaxCheckAttempts());
594 } else if (macro == "LASTHOSTSTATE") {
595 *result = StateToString(GetLastState());
597 } else if (macro == "LASTHOSTSTATEID") {
598 *result = Convert::ToString(GetLastState());
600 } else if (macro == "LASTHOSTSTATETYPE") {
601 *result = Service::StateTypeToString(GetLastStateType());
603 } else if (macro == "LASTHOSTSTATECHANGE") {
604 *result = Convert::ToString((long)hc->GetLastStateChange());
606 } else if (macro == "HOSTDURATIONSEC") {
607 *result = Convert::ToString((long)(Utility::GetTime() - hc->GetLastStateChange()));
611 hccr = hc->GetLastCheckResult();
615 if (macro == "HOSTLATENCY") {
616 *result = Convert::ToString(Service::CalculateLatency(hccr));
618 } else if (macro == "HOSTEXECUTIONTIME") {
619 *result = Convert::ToString(Service::CalculateExecutionTime(hccr));
621 } else if (macro == "HOSTOUTPUT") {
622 *result = hccr->Get("output");
624 } else if (macro == "HOSTPERFDATA") {
625 *result = PluginUtility::FormatPerfdata(hccr->Get("performance_data"));
627 } else if (macro == "LASTHOSTCHECK") {
628 *result = Convert::ToString((long)hccr->Get("schedule_start"));
633 Dictionary::Ptr macros = GetMacros();
637 if (name == "HOSTADDRESS")
639 else if (macro == "HOSTADDRESS6")
642 if (macros && macros->Contains(name)) {
643 *result = macros->Get(name);
647 if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {