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;
43 void Host::Start(void)
45 DynamicObject::Start();
49 Array::Ptr groups = GetGroups();
52 ObjectLock olock(groups);
54 BOOST_FOREACH(const String& name, groups) {
55 HostGroup::Ptr hg = HostGroup::GetByName(name);
58 hg->AddMember(GetSelf());
63 void Host::OnConfigLoaded(void)
65 UpdateSlaveServices();
70 DynamicObject::Stop();
72 Array::Ptr groups = GetGroups();
75 ObjectLock olock(groups);
77 BOOST_FOREACH(const String& name, groups) {
78 HostGroup::Ptr hg = HostGroup::GetByName(name);
81 hg->RemoveMember(GetSelf());
85 // TODO: unregister slave services/notifications?
88 bool Host::IsReachable(void) const
92 std::set<Service::Ptr> parentServices = GetParentServices();
94 BOOST_FOREACH(const Service::Ptr& service, parentServices) {
95 ObjectLock olock(service);
97 /* ignore pending services */
98 if (!service->GetLastCheckResult())
101 /* ignore soft states */
102 if (service->GetStateType() == StateTypeSoft)
105 /* ignore services states OK and Warning */
106 if (service->GetState() == StateOK ||
107 service->GetState() == StateWarning)
113 std::set<Host::Ptr> parentHosts = GetParentHosts();
115 BOOST_FOREACH(const Host::Ptr& host, parentHosts) {
116 Service::Ptr hc = host->GetCheckService();
118 /* ignore hosts that don't have a check */
122 ObjectLock olock(hc);
124 /* ignore soft states */
125 if (hc->GetStateType() == StateTypeSoft)
128 /* ignore hosts that are up */
129 if (hc->GetState() == StateOK)
138 void Host::UpdateSlaveServices(void)
142 ConfigItem::Ptr item = ConfigItem::GetObject("Host", GetName());
144 /* Don't create slave services unless we own this object */
148 Dictionary::Ptr service_descriptions = GetServiceDescriptions();
150 if (!service_descriptions)
153 ObjectLock olock(service_descriptions);
156 BOOST_FOREACH(boost::tie(svcname, svcdesc), service_descriptions) {
157 if (svcdesc.IsScalar())
160 std::ostringstream namebuf;
161 namebuf << GetName() << ":" << svcname;
162 String name = namebuf.str();
164 std::vector<String> path;
165 path.push_back("services");
166 path.push_back(svcname);
169 item->GetLinkedExpressionList()->FindDebugInfoPath(path, di);
171 if (di.Path.IsEmpty())
172 di = item->GetDebugInfo();
174 ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
175 builder->SetType("Service");
176 builder->SetName(name);
177 builder->AddExpression("host", OperatorSet, GetName());
178 builder->AddExpression("display_name", OperatorSet, svcname);
179 builder->AddExpression("short_name", OperatorSet, svcname);
181 if (!svcdesc.IsObjectType<Dictionary>())
182 BOOST_THROW_EXCEPTION(std::invalid_argument("Service description must be either a string or a dictionary."));
184 Dictionary::Ptr service = svcdesc;
186 Array::Ptr templates = service->Get("templates");
189 ObjectLock olock(templates);
191 BOOST_FOREACH(const Value& tmpl, templates) {
192 builder->AddParent(tmpl);
196 /* Clone attributes from the service expression list. */
197 ExpressionList::Ptr svc_exprl = make_shared<ExpressionList>();
198 item->GetLinkedExpressionList()->ExtractPath(path, svc_exprl);
200 builder->AddExpressionList(svc_exprl);
202 ConfigItem::Ptr serviceItem = builder->Compile();
203 serviceItem->Register();
204 DynamicObject::Ptr dobj = serviceItem->Commit();
205 dobj->OnConfigLoaded();
209 std::set<Service::Ptr> Host::GetServices(void) const
211 boost::mutex::scoped_lock lock(m_ServicesMutex);
213 std::set<Service::Ptr> services;
214 Service::WeakPtr wservice;
215 BOOST_FOREACH(boost::tie(boost::tuples::ignore, wservice), m_Services) {
216 Service::Ptr service = wservice.lock();
221 services.insert(service);
227 void Host::AddService(const Service::Ptr& service)
229 boost::mutex::scoped_lock lock(m_ServicesMutex);
231 m_Services[service->GetShortName()] = service;
234 void Host::RemoveService(const Service::Ptr& service)
236 boost::mutex::scoped_lock lock(m_ServicesMutex);
238 m_Services.erase(service->GetShortName());
241 int Host::GetTotalServices(void) const
243 return GetServices().size();
246 Service::Ptr Host::GetServiceByShortName(const Value& name) const
248 if (name.IsScalar()) {
250 boost::mutex::scoped_lock lock(m_ServicesMutex);
252 std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
254 if (it != m_Services.end())
258 return Service::Ptr();
259 } else if (name.IsObjectType<Dictionary>()) {
260 Dictionary::Ptr dict = name;
263 return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
265 BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + JsonSerialize(name)));
269 std::set<Host::Ptr> Host::GetParentHosts(void) const
271 std::set<Host::Ptr> parents;
273 Array::Ptr dependencies = GetHostDependencies();
276 ObjectLock olock(dependencies);
278 BOOST_FOREACH(const Value& value, dependencies) {
279 if (value == GetName())
282 Host::Ptr host = GetByName(value);
284 parents.insert(host);
291 std::set<Host::Ptr> Host::GetChildHosts(void) const
293 std::set<Host::Ptr> children;
295 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
296 Array::Ptr dependencies = host->GetHostDependencies();
299 ObjectLock olock(dependencies);
301 BOOST_FOREACH(const Value& value, dependencies) {
302 if (value == GetName())
303 children.insert(host);
311 Service::Ptr Host::GetCheckService(void) const
313 String host_check = GetCheck();
315 if (host_check.IsEmpty())
316 return Service::Ptr();
318 return GetServiceByShortName(host_check);
321 std::set<Service::Ptr> Host::GetParentServices(void) const
323 std::set<Service::Ptr> parents;
325 Array::Ptr dependencies = GetServiceDependencies();
328 ObjectLock olock(dependencies);
330 BOOST_FOREACH(const Value& value, dependencies) {
331 parents.insert(GetServiceByShortName(value));
338 HostState Host::CalculateState(ServiceState state, bool reachable)
341 return HostUnreachable;
352 HostState Host::GetState(void) const
357 return HostUnreachable;
359 Service::Ptr hc = GetCheckService();
364 switch (hc->GetState()) {
374 HostState Host::GetLastState(void) const
379 return HostUnreachable;
381 Service::Ptr hc = GetCheckService();
386 switch (hc->GetLastState()) {
395 HostState Host::GetLastHardState(void) const
400 return HostUnreachable;
402 Service::Ptr hc = GetCheckService();
407 switch (hc->GetLastHardState()) {
416 double Host::GetLastStateUp(void) const
420 Service::Ptr hc = GetCheckService();
425 if (hc->GetLastStateOK() > hc->GetLastStateWarning())
426 return hc->GetLastStateOK();
428 return hc->GetLastStateWarning();
431 double Host::GetLastStateDown(void) const
435 Service::Ptr hc = GetCheckService();
440 return hc->GetLastStateCritical();
443 double Host::GetLastStateUnreachable(void) const
447 Service::Ptr hc = GetCheckService();
452 return hc->GetLastStateUnreachable();
455 double Host::GetLastStateChange(void) const
457 Service::Ptr hc = GetCheckService();
460 return Application::GetStartTime();
462 return hc->GetLastStateChange();
466 double Host::GetLastHardStateChange(void) const
468 Service::Ptr hc = GetCheckService();
471 return Application::GetStartTime();
473 return hc->GetLastHardStateChange();
476 StateType Host::GetLastStateType(void) const
478 Service::Ptr hc = GetCheckService();
481 return StateTypeHard;
483 return hc->GetLastStateType();
486 StateType Host::GetStateType(void) const
488 Service::Ptr hc = GetCheckService();
491 return StateTypeHard;
493 return hc->GetStateType();
496 HostState Host::StateFromString(const String& state)
500 else if (state == "DOWN")
502 else if (state == "UNREACHABLE")
503 return HostUnreachable;
505 return HostUnreachable;
508 String Host::StateToString(HostState state)
515 case HostUnreachable:
516 return "UNREACHABLE";
522 StateType Host::StateTypeFromString(const String& type)
525 return StateTypeSoft;
527 return StateTypeHard;
530 String Host::StateTypeToString(StateType type)
532 if (type == StateTypeSoft)
538 bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
540 if (macro == "HOSTNAME") {
544 else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") {
545 *result = GetDisplayName();
549 Service::Ptr hc = GetCheckService();
550 CheckResult::Ptr hccr;
553 ServiceState state = hc->GetState();
554 bool reachable = IsReachable();
556 if (macro == "HOSTSTATE") {
557 HostState hstate = CalculateState(state, reachable);
560 case HostUnreachable:
561 *result = "UNREACHABLE";
574 } else if (macro == "HOSTSTATEID") {
575 *result = Convert::ToString(state);
577 } else if (macro == "HOSTSTATETYPE") {
578 *result = Service::StateTypeToString(hc->GetStateType());
580 } else if (macro == "HOSTATTEMPT") {
581 *result = Convert::ToString(hc->GetCheckAttempt());
583 } else if (macro == "MAXHOSTATTEMPT") {
584 *result = Convert::ToString(hc->GetMaxCheckAttempts());
586 } else if (macro == "LASTHOSTSTATE") {
587 *result = StateToString(GetLastState());
589 } else if (macro == "LASTHOSTSTATEID") {
590 *result = Convert::ToString(GetLastState());
592 } else if (macro == "LASTHOSTSTATETYPE") {
593 *result = Service::StateTypeToString(GetLastStateType());
595 } else if (macro == "LASTHOSTSTATECHANGE") {
596 *result = Convert::ToString((long)hc->GetLastStateChange());
598 } else if (macro == "HOSTDURATIONSEC") {
599 *result = Convert::ToString((long)(Utility::GetTime() - hc->GetLastStateChange()));
603 hccr = hc->GetLastCheckResult();
607 if (macro == "HOSTLATENCY") {
608 *result = Convert::ToString(Service::CalculateLatency(hccr));
610 } else if (macro == "HOSTEXECUTIONTIME") {
611 *result = Convert::ToString(Service::CalculateExecutionTime(hccr));
613 } else if (macro == "HOSTOUTPUT") {
614 *result = hccr->GetOutput();
616 } else if (macro == "HOSTPERFDATA") {
617 *result = PluginUtility::FormatPerfdata(hccr->GetPerformanceData());
619 } else if (macro == "LASTHOSTCHECK") {
620 *result = Convert::ToString((long)hccr->GetScheduleStart());
625 Dictionary::Ptr macros = GetMacros();
629 if (name == "HOSTADDRESS")
631 else if (macro == "HOSTADDRESS6")
634 if (macros && macros->Contains(name)) {
635 *result = macros->Get(name);
639 if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {