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 std::vector<String> dpath;
201 dpath.push_back("templates");
202 svc_exprl->ErasePath(dpath);
204 builder->AddExpressionList(svc_exprl);
206 ConfigItem::Ptr serviceItem = builder->Compile();
207 serviceItem->Register();
208 DynamicObject::Ptr dobj = serviceItem->Commit();
209 dobj->OnConfigLoaded();
213 std::set<Service::Ptr> Host::GetServices(void) const
215 boost::mutex::scoped_lock lock(m_ServicesMutex);
217 std::set<Service::Ptr> services;
218 Service::WeakPtr wservice;
219 BOOST_FOREACH(boost::tie(boost::tuples::ignore, wservice), m_Services) {
220 Service::Ptr service = wservice.lock();
225 services.insert(service);
231 void Host::AddService(const Service::Ptr& service)
233 boost::mutex::scoped_lock lock(m_ServicesMutex);
235 m_Services[service->GetShortName()] = service;
238 void Host::RemoveService(const Service::Ptr& service)
240 boost::mutex::scoped_lock lock(m_ServicesMutex);
242 m_Services.erase(service->GetShortName());
245 int Host::GetTotalServices(void) const
247 return GetServices().size();
250 Service::Ptr Host::GetServiceByShortName(const Value& name) const
252 if (name.IsScalar()) {
254 boost::mutex::scoped_lock lock(m_ServicesMutex);
256 std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
258 if (it != m_Services.end())
262 return Service::Ptr();
263 } else if (name.IsObjectType<Dictionary>()) {
264 Dictionary::Ptr dict = name;
267 return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
269 BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + JsonSerialize(name)));
273 std::set<Host::Ptr> Host::GetParentHosts(void) const
275 std::set<Host::Ptr> parents;
277 Array::Ptr dependencies = GetHostDependencies();
280 ObjectLock olock(dependencies);
282 BOOST_FOREACH(const Value& value, dependencies) {
283 if (value == GetName())
286 Host::Ptr host = GetByName(value);
291 parents.insert(host);
298 std::set<Host::Ptr> Host::GetChildHosts(void) const
300 std::set<Host::Ptr> children;
302 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
303 Array::Ptr dependencies = host->GetHostDependencies();
306 ObjectLock olock(dependencies);
308 BOOST_FOREACH(const Value& value, dependencies) {
309 if (value == GetName())
310 children.insert(host);
318 Service::Ptr Host::GetCheckService(void) const
320 String host_check = GetCheck();
322 if (host_check.IsEmpty())
323 return Service::Ptr();
325 return GetServiceByShortName(host_check);
328 std::set<Service::Ptr> Host::GetParentServices(void) const
330 std::set<Service::Ptr> parents;
332 Array::Ptr dependencies = GetServiceDependencies();
335 ObjectLock olock(dependencies);
337 BOOST_FOREACH(const Value& value, dependencies) {
338 parents.insert(GetServiceByShortName(value));
345 HostState Host::CalculateState(ServiceState state, bool reachable)
348 return HostUnreachable;
359 HostState Host::GetState(void) const
364 return HostUnreachable;
366 Service::Ptr hc = GetCheckService();
371 switch (hc->GetState()) {
381 HostState Host::GetLastState(void) const
386 return HostUnreachable;
388 Service::Ptr hc = GetCheckService();
393 switch (hc->GetLastState()) {
402 HostState Host::GetLastHardState(void) const
407 return HostUnreachable;
409 Service::Ptr hc = GetCheckService();
414 switch (hc->GetLastHardState()) {
423 double Host::GetLastStateUp(void) const
427 Service::Ptr hc = GetCheckService();
432 if (hc->GetLastStateOK() > hc->GetLastStateWarning())
433 return hc->GetLastStateOK();
435 return hc->GetLastStateWarning();
438 double Host::GetLastStateDown(void) const
442 Service::Ptr hc = GetCheckService();
447 return hc->GetLastStateCritical();
450 double Host::GetLastStateUnreachable(void) const
454 Service::Ptr hc = GetCheckService();
459 return hc->GetLastStateUnreachable();
462 double Host::GetLastStateChange(void) const
464 Service::Ptr hc = GetCheckService();
467 return Application::GetStartTime();
469 return hc->GetLastStateChange();
473 double Host::GetLastHardStateChange(void) const
475 Service::Ptr hc = GetCheckService();
478 return Application::GetStartTime();
480 return hc->GetLastHardStateChange();
483 StateType Host::GetLastStateType(void) const
485 Service::Ptr hc = GetCheckService();
488 return StateTypeHard;
490 return hc->GetLastStateType();
493 StateType Host::GetStateType(void) const
495 Service::Ptr hc = GetCheckService();
498 return StateTypeHard;
500 return hc->GetStateType();
503 HostState Host::StateFromString(const String& state)
507 else if (state == "DOWN")
509 else if (state == "UNREACHABLE")
510 return HostUnreachable;
512 return HostUnreachable;
515 String Host::StateToString(HostState state)
522 case HostUnreachable:
523 return "UNREACHABLE";
529 StateType Host::StateTypeFromString(const String& type)
532 return StateTypeSoft;
534 return StateTypeHard;
537 String Host::StateTypeToString(StateType type)
539 if (type == StateTypeSoft)
545 bool Host::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
547 if (macro == "HOSTNAME") {
551 else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") {
552 *result = GetDisplayName();
556 Service::Ptr hc = GetCheckService();
557 Dictionary::Ptr hccr;
560 ServiceState state = hc->GetState();
561 bool reachable = IsReachable();
563 if (macro == "HOSTSTATE") {
564 HostState hstate = CalculateState(state, reachable);
567 case HostUnreachable:
568 *result = "UNREACHABLE";
581 } else if (macro == "HOSTSTATEID") {
582 *result = Convert::ToString(state);
584 } else if (macro == "HOSTSTATETYPE") {
585 *result = Service::StateTypeToString(hc->GetStateType());
587 } else if (macro == "HOSTATTEMPT") {
588 *result = Convert::ToString(hc->GetCheckAttempt());
590 } else if (macro == "MAXHOSTATTEMPT") {
591 *result = Convert::ToString(hc->GetMaxCheckAttempts());
593 } else if (macro == "LASTHOSTSTATE") {
594 *result = StateToString(GetLastState());
596 } else if (macro == "LASTHOSTSTATEID") {
597 *result = Convert::ToString(GetLastState());
599 } else if (macro == "LASTHOSTSTATETYPE") {
600 *result = Service::StateTypeToString(GetLastStateType());
602 } else if (macro == "LASTHOSTSTATECHANGE") {
603 *result = Convert::ToString((long)hc->GetLastStateChange());
605 } else if (macro == "HOSTDURATIONSEC") {
606 *result = Convert::ToString((long)(Utility::GetTime() - hc->GetLastStateChange()));
610 hccr = hc->GetLastCheckResult();
614 if (macro == "HOSTLATENCY") {
615 *result = Convert::ToString(Service::CalculateLatency(hccr));
617 } else if (macro == "HOSTEXECUTIONTIME") {
618 *result = Convert::ToString(Service::CalculateExecutionTime(hccr));
620 } else if (macro == "HOSTOUTPUT") {
621 *result = hccr->Get("output");
623 } else if (macro == "HOSTPERFDATA") {
624 *result = PluginUtility::FormatPerfdata(hccr->Get("performance_data"));
626 } else if (macro == "LASTHOSTCHECK") {
627 *result = Convert::ToString((long)hccr->Get("schedule_start"));
632 Dictionary::Ptr macros = GetMacros();
636 if (name == "HOSTADDRESS")
638 else if (macro == "HOSTADDRESS6")
641 if (macros && macros->Contains(name)) {
642 *result = macros->Get(name);
646 if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {