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 "base/dynamictype.h"
25 #include "base/objectlock.h"
26 #include "base/logger_fwd.h"
27 #include "base/timer.h"
28 #include "base/convert.h"
29 #include "base/scriptfunction.h"
30 #include "base/debug.h"
31 #include "config/configitembuilder.h"
32 #include "config/configcompilercontext.h"
33 #include <boost/tuple/tuple.hpp>
34 #include <boost/smart_ptr/make_shared.hpp>
35 #include <boost/foreach.hpp>
37 using namespace icinga;
41 void Host::Start(void)
43 DynamicObject::Start();
47 Array::Ptr groups = GetGroups();
50 ObjectLock olock(groups);
52 BOOST_FOREACH(const String& name, groups) {
53 HostGroup::Ptr hg = HostGroup::GetByName(name);
56 hg->AddMember(GetSelf());
61 void Host::OnConfigLoaded(void)
63 UpdateSlaveServices();
68 DynamicObject::Stop();
70 Array::Ptr groups = GetGroups();
73 ObjectLock olock(groups);
75 BOOST_FOREACH(const String& name, groups) {
76 HostGroup::Ptr hg = HostGroup::GetByName(name);
79 hg->RemoveMember(GetSelf());
83 // TODO: unregister slave services/notifications?
86 String Host::GetDisplayName(void) const
88 if (!m_DisplayName.IsEmpty())
94 Array::Ptr Host::GetGroups(void) const
99 Dictionary::Ptr Host::GetMacros(void) const
104 Array::Ptr Host::GetHostDependencies(void) const
106 return m_HostDependencies;;
109 Array::Ptr Host::GetServiceDependencies(void) const
111 return m_ServiceDependencies;
114 String Host::GetCheck(void) const
119 Dictionary::Ptr Host::GetNotificationDescriptions(void) const
121 return m_NotificationDescriptions;
124 bool Host::IsReachable(void) const
128 std::set<Service::Ptr> parentServices = GetParentServices();
130 BOOST_FOREACH(const Service::Ptr& service, parentServices) {
131 ObjectLock olock(service);
133 /* ignore pending services */
134 if (!service->GetLastCheckResult())
137 /* ignore soft states */
138 if (service->GetStateType() == StateTypeSoft)
141 /* ignore services states OK and Warning */
142 if (service->GetState() == StateOK ||
143 service->GetState() == StateWarning)
149 std::set<Host::Ptr> parentHosts = GetParentHosts();
151 BOOST_FOREACH(const Host::Ptr& host, parentHosts) {
152 Service::Ptr hc = host->GetCheckService();
154 /* ignore hosts that don't have a check */
158 ObjectLock olock(hc);
160 /* ignore soft states */
161 if (hc->GetStateType() == StateTypeSoft)
164 /* ignore hosts that are up */
165 if (hc->GetState() == StateOK)
174 void Host::UpdateSlaveServices(void)
178 ConfigItem::Ptr item = ConfigItem::GetObject("Host", GetName());
180 /* Don't create slave services unless we own this object */
184 if (!m_ServiceDescriptions)
187 ObjectLock olock(m_ServiceDescriptions);
190 BOOST_FOREACH(boost::tie(svcname, svcdesc), m_ServiceDescriptions) {
191 if (svcdesc.IsScalar())
194 std::ostringstream namebuf;
195 namebuf << GetName() << ":" << svcname;
196 String name = namebuf.str();
198 std::vector<String> path;
199 path.push_back("services");
200 path.push_back(svcname);
203 item->GetLinkedExpressionList()->FindDebugInfoPath(path, di);
205 if (di.Path.IsEmpty())
206 di = item->GetDebugInfo();
208 ConfigItemBuilder::Ptr builder = boost::make_shared<ConfigItemBuilder>(di);
209 builder->SetType("Service");
210 builder->SetName(name);
211 builder->AddExpression("host_name", OperatorSet, GetName());
212 builder->AddExpression("display_name", OperatorSet, svcname);
213 builder->AddExpression("short_name", OperatorSet, svcname);
215 if (!svcdesc.IsObjectType<Dictionary>())
216 BOOST_THROW_EXCEPTION(std::invalid_argument("Service description must be either a string or a dictionary."));
218 Dictionary::Ptr service = svcdesc;
220 Array::Ptr templates = service->Get("templates");
223 ObjectLock olock(templates);
225 BOOST_FOREACH(const Value& tmpl, templates) {
226 builder->AddParent(tmpl);
230 /* Clone attributes from the service expression list. */
231 ExpressionList::Ptr svc_exprl = boost::make_shared<ExpressionList>();
232 item->GetLinkedExpressionList()->ExtractPath(path, svc_exprl);
234 std::vector<String> dpath;
235 dpath.push_back("templates");
236 svc_exprl->ErasePath(dpath);
238 builder->AddExpressionList(svc_exprl);
240 ConfigItem::Ptr serviceItem = builder->Compile();
241 serviceItem->Register();
242 DynamicObject::Ptr dobj = serviceItem->Commit();
243 dobj->OnConfigLoaded();
247 std::set<Service::Ptr> Host::GetServices(void) const
249 boost::mutex::scoped_lock lock(m_ServicesMutex);
251 std::set<Service::Ptr> services;
252 Service::WeakPtr wservice;
253 BOOST_FOREACH(boost::tie(boost::tuples::ignore, wservice), m_Services) {
254 Service::Ptr service = wservice.lock();
259 services.insert(service);
265 void Host::AddService(const Service::Ptr& service)
267 boost::mutex::scoped_lock lock(m_ServicesMutex);
269 m_Services[service->GetShortName()] = service;
272 void Host::RemoveService(const Service::Ptr& service)
274 boost::mutex::scoped_lock lock(m_ServicesMutex);
276 m_Services.erase(service->GetShortName());
279 int Host::GetTotalServices(void) const
281 return GetServices().size();
284 Service::Ptr Host::GetServiceByShortName(const Value& name) const
286 if (name.IsScalar()) {
288 boost::mutex::scoped_lock lock(m_ServicesMutex);
290 std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
292 if (it != m_Services.end())
296 return Service::Ptr();
297 } else if (name.IsObjectType<Dictionary>()) {
298 Dictionary::Ptr dict = name;
301 return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
303 BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + name.Serialize()));
307 std::set<Host::Ptr> Host::GetParentHosts(void) const
309 std::set<Host::Ptr> parents;
311 Array::Ptr dependencies = GetHostDependencies();
314 ObjectLock olock(dependencies);
316 BOOST_FOREACH(const Value& value, dependencies) {
317 if (value == GetName())
320 Host::Ptr host = GetByName(value);
325 parents.insert(host);
332 std::set<Host::Ptr> Host::GetChildHosts(void) const
334 std::set<Host::Ptr> childs;
336 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
337 Array::Ptr dependencies = host->GetHostDependencies();
340 ObjectLock olock(dependencies);
342 BOOST_FOREACH(const Value& value, dependencies) {
343 if (value == GetName())
353 Service::Ptr Host::GetCheckService(void) const
355 String host_check = GetCheck();
357 if (host_check.IsEmpty())
358 return Service::Ptr();
360 return GetServiceByShortName(host_check);
363 std::set<Service::Ptr> Host::GetParentServices(void) const
365 std::set<Service::Ptr> parents;
367 Array::Ptr dependencies = GetServiceDependencies();
370 ObjectLock olock(dependencies);
372 BOOST_FOREACH(const Value& value, dependencies) {
373 parents.insert(GetServiceByShortName(value));
380 HostState Host::CalculateState(ServiceState state, bool reachable)
383 return HostUnreachable;
394 HostState Host::GetState(void) const
399 return HostUnreachable;
401 Service::Ptr hc = GetCheckService();
406 switch (hc->GetState()) {
416 HostState Host::GetLastState(void) const
421 return HostUnreachable;
423 Service::Ptr hc = GetCheckService();
428 switch (hc->GetLastState()) {
437 HostState Host::GetLastHardState(void) const
442 return HostUnreachable;
444 Service::Ptr hc = GetCheckService();
449 switch (hc->GetLastHardState()) {
458 double Host::GetLastStateUp(void) const
462 Service::Ptr hc = GetCheckService();
467 if (hc->GetLastStateOK() > hc->GetLastStateWarning())
468 return hc->GetLastStateOK();
470 return hc->GetLastStateWarning();
473 double Host::GetLastStateDown(void) const
477 Service::Ptr hc = GetCheckService();
482 return hc->GetLastStateCritical();
485 double Host::GetLastStateUnreachable(void) const
489 Service::Ptr hc = GetCheckService();
494 return hc->GetLastStateUnreachable();
497 double Host::GetLastStateChange(void) const
499 Service::Ptr hc = GetCheckService();
502 return IcingaApplication::GetInstance()->GetStartTime();
504 return hc->GetLastStateChange();
508 double Host::GetLastHardStateChange(void) const
510 Service::Ptr hc = GetCheckService();
513 return IcingaApplication::GetInstance()->GetStartTime();
515 return hc->GetLastHardStateChange();
518 StateType Host::GetLastStateType(void) const
520 Service::Ptr hc = GetCheckService();
523 return StateTypeHard;
525 return hc->GetLastStateType();
528 StateType Host::GetStateType(void) const
530 Service::Ptr hc = GetCheckService();
533 return StateTypeHard;
535 return hc->GetStateType();
538 String Host::StateToString(HostState state)
545 case HostUnreachable:
546 return "UNREACHABLE";
552 bool Host::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
554 if (macro == "HOSTNAME") {
558 else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") {
559 *result = GetDisplayName();
563 Service::Ptr hc = GetCheckService();
564 Dictionary::Ptr hccr;
567 ServiceState state = hc->GetState();
568 bool reachable = IsReachable();
570 if (macro == "HOSTSTATE") {
571 HostState hstate = CalculateState(state, reachable);
574 case HostUnreachable:
575 *result = "UNREACHABLE";
588 } else if (macro == "HOSTSTATEID") {
589 *result = Convert::ToString(state);
591 } else if (macro == "HOSTSTATETYPE") {
592 *result = Service::StateTypeToString(hc->GetStateType());
594 } else if (macro == "HOSTATTEMPT") {
595 *result = Convert::ToString(hc->GetCurrentCheckAttempt());
597 } else if (macro == "MAXHOSTATTEMPT") {
598 *result = Convert::ToString(hc->GetMaxCheckAttempts());
600 } else if (macro == "LASTHOSTSTATE") {
601 *result = StateToString(GetLastState());
603 } else if (macro == "LASTHOSTSTATEID") {
604 *result = Convert::ToString(GetLastState());
606 } else if (macro == "LASTHOSTSTATETYPE") {
607 *result = Service::StateTypeToString(GetLastStateType());
609 } else if (macro == "LASTHOSTSTATECHANGE") {
610 *result = Convert::ToString((long)hc->GetLastStateChange());
614 hccr = hc->GetLastCheckResult();
618 if (macro == "HOSTLATENCY") {
619 *result = Convert::ToString(Service::CalculateLatency(hccr));
621 } else if (macro == "HOSTEXECUTIONTIME") {
622 *result = Convert::ToString(Service::CalculateExecutionTime(hccr));
624 } else if (macro == "HOSTOUTPUT") {
625 *result = hccr->Get("output");
627 } else if (macro == "HOSTPERFDATA") {
628 *result = hccr->Get("performance_data_raw");
630 } else if (macro == "LASTHOSTCHECK") {
631 *result = Convert::ToString((long)hccr->Get("schedule_start"));
636 Dictionary::Ptr macros = GetMacros();
640 if (name == "HOSTADDRESS")
642 else if (macro == "HOSTADDRESS6")
645 if (macros && macros->Contains(name)) {
646 *result = macros->Get(name);
650 if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {
658 void Host::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
660 DynamicObject::InternalSerialize(bag, attributeTypes);
662 if (attributeTypes & Attribute_Config) {
663 bag->Set("display_name", m_DisplayName);
664 bag->Set("groups", m_Groups);
665 bag->Set("macros", m_Macros);
666 bag->Set("host_dependencies", m_HostDependencies);
667 bag->Set("service_dependencies", m_ServiceDependencies);
668 bag->Set("check", m_Check);
669 bag->Set("services", m_ServiceDescriptions);
670 bag->Set("notifications", m_NotificationDescriptions);
674 void Host::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
676 DynamicObject::InternalDeserialize(bag, attributeTypes);
678 if (attributeTypes & Attribute_Config) {
679 m_DisplayName = bag->Get("display_name");
680 m_Groups = bag->Get("groups");
681 m_Macros = bag->Get("macros");
682 m_HostDependencies = bag->Get("host_dependencies");
683 m_ServiceDependencies = bag->Get("service_dependencies");
684 m_Check = bag->Get("check");
685 m_ServiceDescriptions = bag->Get("services");
686 m_NotificationDescriptions = bag->Get("notifications");