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::GetHostCheck(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->GetHostCheckService();
154 /* ignore hosts that don't have a hostcheck */
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 host object. */
231 std::set<String, string_iless> keys;
232 keys.insert("check_interval");
233 keys.insert("retry_interval");
234 keys.insert("servicegroups");
235 keys.insert("notification_interval");
236 keys.insert("notification_type_filter");
237 keys.insert("notification_state_filter");
238 keys.insert("check_period");
239 keys.insert("servicedependencies");
240 keys.insert("hostdependencies");
241 keys.insert("authorities");
242 keys.insert("domains");
244 ExpressionList::Ptr host_exprl = boost::make_shared<ExpressionList>();
245 item->GetLinkedExpressionList()->ExtractFiltered(keys, host_exprl);
246 builder->AddExpressionList(host_exprl);
248 /* Clone attributes from the service expression list. */
249 ExpressionList::Ptr svc_exprl = boost::make_shared<ExpressionList>();
250 item->GetLinkedExpressionList()->ExtractPath(path, svc_exprl);
252 std::vector<String> dpath;
253 dpath.push_back("templates");
254 svc_exprl->ErasePath(dpath);
256 builder->AddExpressionList(svc_exprl);
258 ConfigItem::Ptr serviceItem = builder->Compile();
259 serviceItem->Register();
260 DynamicObject::Ptr dobj = serviceItem->Commit();
261 dobj->OnConfigLoaded();
265 std::set<Service::Ptr> Host::GetServices(void) const
267 boost::mutex::scoped_lock lock(m_ServicesMutex);
269 std::set<Service::Ptr> services;
270 Service::WeakPtr wservice;
271 BOOST_FOREACH(boost::tie(boost::tuples::ignore, wservice), m_Services) {
272 Service::Ptr service = wservice.lock();
277 services.insert(service);
283 void Host::AddService(const Service::Ptr& service)
285 boost::mutex::scoped_lock lock(m_ServicesMutex);
287 m_Services[service->GetShortName()] = service;
290 void Host::RemoveService(const Service::Ptr& service)
292 boost::mutex::scoped_lock lock(m_ServicesMutex);
294 m_Services.erase(service->GetShortName());
297 int Host::GetTotalServices(void) const
299 return GetServices().size();
302 Service::Ptr Host::GetServiceByShortName(const Value& name) const
304 if (name.IsScalar()) {
306 boost::mutex::scoped_lock lock(m_ServicesMutex);
308 std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
310 if (it != m_Services.end())
314 return Service::Ptr();
315 } else if (name.IsObjectType<Dictionary>()) {
316 Dictionary::Ptr dict = name;
319 return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
321 BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + name.Serialize()));
325 std::set<Host::Ptr> Host::GetParentHosts(void) const
327 std::set<Host::Ptr> parents;
329 Array::Ptr dependencies = GetHostDependencies();
332 ObjectLock olock(dependencies);
334 BOOST_FOREACH(const Value& value, dependencies) {
335 if (value == GetName())
338 Host::Ptr host = GetByName(value);
343 parents.insert(host);
350 std::set<Host::Ptr> Host::GetChildHosts(void) const
352 std::set<Host::Ptr> childs;
354 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
355 Array::Ptr dependencies = host->GetHostDependencies();
358 ObjectLock olock(dependencies);
360 BOOST_FOREACH(const Value& value, dependencies) {
361 if (value == GetName())
371 Service::Ptr Host::GetHostCheckService(void) const
373 String host_check = GetHostCheck();
375 if (host_check.IsEmpty())
376 return Service::Ptr();
378 return GetServiceByShortName(host_check);
381 std::set<Service::Ptr> Host::GetParentServices(void) const
383 std::set<Service::Ptr> parents;
385 Array::Ptr dependencies = GetServiceDependencies();
388 ObjectLock olock(dependencies);
390 BOOST_FOREACH(const Value& value, dependencies) {
391 parents.insert(GetServiceByShortName(value));
398 HostState Host::CalculateState(ServiceState state, bool reachable)
401 return HostUnreachable;
412 HostState Host::GetState(void) const
417 return HostUnreachable;
419 Service::Ptr hc = GetHostCheckService();
424 switch (hc->GetState()) {
434 HostState Host::GetLastState(void) const
439 return HostUnreachable;
441 Service::Ptr hc = GetHostCheckService();
446 switch (hc->GetLastState()) {
455 HostState Host::GetLastHardState(void) const
460 return HostUnreachable;
462 Service::Ptr hc = GetHostCheckService();
467 switch (hc->GetLastHardState()) {
476 double Host::GetLastStateUp(void) const
480 Service::Ptr hc = GetHostCheckService();
485 if (hc->GetLastStateOK() > hc->GetLastStateWarning())
486 return hc->GetLastStateOK();
488 return hc->GetLastStateWarning();
491 double Host::GetLastStateDown(void) const
495 Service::Ptr hc = GetHostCheckService();
500 return hc->GetLastStateCritical();
503 double Host::GetLastStateUnreachable(void) const
507 Service::Ptr hc = GetHostCheckService();
512 return hc->GetLastStateUnreachable();
515 double Host::GetLastStateChange(void) const
517 Service::Ptr hc = GetHostCheckService();
520 return IcingaApplication::GetInstance()->GetStartTime();
522 return hc->GetLastStateChange();
526 double Host::GetLastHardStateChange(void) const
528 Service::Ptr hc = GetHostCheckService();
531 return IcingaApplication::GetInstance()->GetStartTime();
533 return hc->GetLastHardStateChange();
536 StateType Host::GetLastStateType(void) const
538 Service::Ptr hc = GetHostCheckService();
541 return StateTypeHard;
543 return hc->GetLastStateType();
546 StateType Host::GetStateType(void) const
548 Service::Ptr hc = GetHostCheckService();
551 return StateTypeHard;
553 return hc->GetStateType();
556 String Host::StateToString(HostState state)
563 case HostUnreachable:
564 return "UNREACHABLE";
570 bool Host::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
572 if (macro == "HOSTNAME") {
576 else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") {
577 *result = GetDisplayName();
581 Service::Ptr hc = GetHostCheckService();
582 Dictionary::Ptr hccr;
585 ServiceState state = hc->GetState();
586 bool reachable = IsReachable();
588 if (macro == "HOSTSTATE") {
589 HostState hstate = CalculateState(state, reachable);
592 case HostUnreachable:
593 *result = "UNREACHABLE";
606 } else if (macro == "HOSTSTATEID") {
607 *result = Convert::ToString(state);
609 } else if (macro == "HOSTSTATETYPE") {
610 *result = Service::StateTypeToString(hc->GetStateType());
612 } else if (macro == "HOSTATTEMPT") {
613 *result = Convert::ToString(hc->GetCurrentCheckAttempt());
615 } else if (macro == "MAXHOSTATTEMPT") {
616 *result = Convert::ToString(hc->GetMaxCheckAttempts());
618 } else if (macro == "LASTHOSTSTATE") {
619 *result = StateToString(GetLastState());
621 } else if (macro == "LASTHOSTSTATEID") {
622 *result = Convert::ToString(GetLastState());
624 } else if (macro == "LASTHOSTSTATETYPE") {
625 *result = Service::StateTypeToString(GetLastStateType());
627 } else if (macro == "LASTHOSTSTATECHANGE") {
628 *result = Convert::ToString((long)hc->GetLastStateChange());
632 hccr = hc->GetLastCheckResult();
636 if (macro == "HOSTLATENCY") {
637 *result = Convert::ToString(Service::CalculateLatency(hccr));
639 } else if (macro == "HOSTEXECUTIONTIME") {
640 *result = Convert::ToString(Service::CalculateExecutionTime(hccr));
642 } else if (macro == "HOSTOUTPUT") {
643 *result = hccr->Get("output");
645 } else if (macro == "HOSTPERFDATA") {
646 *result = hccr->Get("performance_data_raw");
648 } else if (macro == "LASTHOSTCHECK") {
649 *result = Convert::ToString((long)hccr->Get("schedule_start"));
654 Dictionary::Ptr macros = GetMacros();
658 if (name == "HOSTADDRESS")
660 else if (macro == "HOSTADDRESS6")
663 if (macros && macros->Contains(name)) {
664 *result = macros->Get(name);
668 if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {
676 void Host::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
678 DynamicObject::InternalSerialize(bag, attributeTypes);
680 if (attributeTypes & Attribute_Config) {
681 bag->Set("display_name", m_DisplayName);
682 bag->Set("hostgroups", m_HostGroups);
683 bag->Set("macros", m_Macros);
684 bag->Set("hostdependencies", m_HostDependencies);
685 bag->Set("servicedependencies", m_ServiceDependencies);
686 bag->Set("hostcheck", m_HostCheck);
687 bag->Set("services", m_ServiceDescriptions);
688 bag->Set("notifications", m_NotificationDescriptions);
692 void Host::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
694 DynamicObject::InternalDeserialize(bag, attributeTypes);
696 if (attributeTypes & Attribute_Config) {
697 m_DisplayName = bag->Get("display_name");
698 m_HostGroups = bag->Get("hostgroups");
699 m_Macros = bag->Get("macros");
700 m_HostDependencies = bag->Get("hostdependencies");
701 m_ServiceDependencies = bag->Get("servicedependencies");
702 m_HostCheck = bag->Get("hostcheck");
703 m_ServiceDescriptions = bag->Get("services");
704 m_NotificationDescriptions = bag->Get("notifications");