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);
287 parents.insert(host);
294 std::set<Host::Ptr> Host::GetChildHosts(void) const
296 std::set<Host::Ptr> children;
298 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
299 Array::Ptr dependencies = host->GetHostDependencies();
302 ObjectLock olock(dependencies);
304 BOOST_FOREACH(const Value& value, dependencies) {
305 if (value == GetName())
306 children.insert(host);
314 Service::Ptr Host::GetCheckService(void) const
316 String host_check = GetCheck();
318 if (host_check.IsEmpty())
319 return Service::Ptr();
321 return GetServiceByShortName(host_check);
324 std::set<Service::Ptr> Host::GetParentServices(void) const
326 std::set<Service::Ptr> parents;
328 Array::Ptr dependencies = GetServiceDependencies();
331 ObjectLock olock(dependencies);
333 BOOST_FOREACH(const Value& value, dependencies) {
334 parents.insert(GetServiceByShortName(value));
341 HostState Host::CalculateState(ServiceState state, bool reachable)
344 return HostUnreachable;
355 HostState Host::GetState(void) const
360 return HostUnreachable;
362 Service::Ptr hc = GetCheckService();
367 switch (hc->GetState()) {
377 HostState Host::GetLastState(void) const
382 return HostUnreachable;
384 Service::Ptr hc = GetCheckService();
389 switch (hc->GetLastState()) {
398 HostState Host::GetLastHardState(void) const
403 return HostUnreachable;
405 Service::Ptr hc = GetCheckService();
410 switch (hc->GetLastHardState()) {
419 double Host::GetLastStateUp(void) const
423 Service::Ptr hc = GetCheckService();
428 if (hc->GetLastStateOK() > hc->GetLastStateWarning())
429 return hc->GetLastStateOK();
431 return hc->GetLastStateWarning();
434 double Host::GetLastStateDown(void) const
438 Service::Ptr hc = GetCheckService();
443 return hc->GetLastStateCritical();
446 double Host::GetLastStateUnreachable(void) const
450 Service::Ptr hc = GetCheckService();
455 return hc->GetLastStateUnreachable();
458 double Host::GetLastStateChange(void) const
460 Service::Ptr hc = GetCheckService();
463 return Application::GetStartTime();
465 return hc->GetLastStateChange();
469 double Host::GetLastHardStateChange(void) const
471 Service::Ptr hc = GetCheckService();
474 return Application::GetStartTime();
476 return hc->GetLastHardStateChange();
479 StateType Host::GetLastStateType(void) const
481 Service::Ptr hc = GetCheckService();
484 return StateTypeHard;
486 return hc->GetLastStateType();
489 StateType Host::GetStateType(void) const
491 Service::Ptr hc = GetCheckService();
494 return StateTypeHard;
496 return hc->GetStateType();
499 HostState Host::StateFromString(const String& state)
503 else if (state == "DOWN")
505 else if (state == "UNREACHABLE")
506 return HostUnreachable;
508 return HostUnreachable;
511 String Host::StateToString(HostState state)
518 case HostUnreachable:
519 return "UNREACHABLE";
525 StateType Host::StateTypeFromString(const String& type)
528 return StateTypeSoft;
530 return StateTypeHard;
533 String Host::StateTypeToString(StateType type)
535 if (type == StateTypeSoft)
541 bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
543 if (macro == "HOSTNAME") {
547 else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") {
548 *result = GetDisplayName();
552 Service::Ptr hc = GetCheckService();
553 CheckResult::Ptr hccr;
556 ServiceState state = hc->GetState();
557 bool reachable = IsReachable();
559 if (macro == "HOSTSTATE") {
560 HostState hstate = CalculateState(state, reachable);
563 case HostUnreachable:
564 *result = "UNREACHABLE";
577 } else if (macro == "HOSTSTATEID") {
578 *result = Convert::ToString(state);
580 } else if (macro == "HOSTSTATETYPE") {
581 *result = Service::StateTypeToString(hc->GetStateType());
583 } else if (macro == "HOSTATTEMPT") {
584 *result = Convert::ToString(hc->GetCheckAttempt());
586 } else if (macro == "MAXHOSTATTEMPT") {
587 *result = Convert::ToString(hc->GetMaxCheckAttempts());
589 } else if (macro == "LASTHOSTSTATE") {
590 *result = StateToString(GetLastState());
592 } else if (macro == "LASTHOSTSTATEID") {
593 *result = Convert::ToString(GetLastState());
595 } else if (macro == "LASTHOSTSTATETYPE") {
596 *result = Service::StateTypeToString(GetLastStateType());
598 } else if (macro == "LASTHOSTSTATECHANGE") {
599 *result = Convert::ToString((long)hc->GetLastStateChange());
601 } else if (macro == "HOSTDURATIONSEC") {
602 *result = Convert::ToString((long)(Utility::GetTime() - hc->GetLastStateChange()));
606 hccr = hc->GetLastCheckResult();
610 if (macro == "HOSTLATENCY") {
611 *result = Convert::ToString(Service::CalculateLatency(hccr));
613 } else if (macro == "HOSTEXECUTIONTIME") {
614 *result = Convert::ToString(Service::CalculateExecutionTime(hccr));
616 } else if (macro == "HOSTOUTPUT") {
617 *result = hccr->GetOutput();
619 } else if (macro == "HOSTPERFDATA") {
620 *result = PluginUtility::FormatPerfdata(hccr->GetPerformanceData());
622 } else if (macro == "LASTHOSTCHECK") {
623 *result = Convert::ToString((long)hccr->GetScheduleStart());
628 Dictionary::Ptr macros = GetMacros();
632 if (name == "HOSTADDRESS")
634 else if (macro == "HOSTADDRESS6")
637 if (macros && macros->Contains(name)) {
638 *result = macros->Get(name);
642 if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {