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/utility.h"
30 #include "base/scriptfunction.h"
31 #include "base/debug.h"
32 #include "config/configitembuilder.h"
33 #include "config/configcompilercontext.h"
34 #include <boost/tuple/tuple.hpp>
35 #include <boost/smart_ptr/make_shared.hpp>
36 #include <boost/foreach.hpp>
38 using namespace icinga;
42 void Host::Start(void)
44 DynamicObject::Start();
48 Array::Ptr groups = GetGroups();
51 ObjectLock olock(groups);
53 BOOST_FOREACH(const String& name, groups) {
54 HostGroup::Ptr hg = HostGroup::GetByName(name);
57 hg->AddMember(GetSelf());
62 void Host::OnConfigLoaded(void)
64 UpdateSlaveServices();
69 DynamicObject::Stop();
71 Array::Ptr groups = GetGroups();
74 ObjectLock olock(groups);
76 BOOST_FOREACH(const String& name, groups) {
77 HostGroup::Ptr hg = HostGroup::GetByName(name);
80 hg->RemoveMember(GetSelf());
84 // TODO: unregister slave services/notifications?
87 bool Host::IsReachable(void) const
91 std::set<Service::Ptr> parentServices = GetParentServices();
93 BOOST_FOREACH(const Service::Ptr& service, parentServices) {
94 ObjectLock olock(service);
96 /* ignore pending services */
97 if (!service->GetLastCheckResult())
100 /* ignore soft states */
101 if (service->GetStateType() == StateTypeSoft)
104 /* ignore services states OK and Warning */
105 if (service->GetState() == StateOK ||
106 service->GetState() == StateWarning)
112 std::set<Host::Ptr> parentHosts = GetParentHosts();
114 BOOST_FOREACH(const Host::Ptr& host, parentHosts) {
115 Service::Ptr hc = host->GetCheckService();
117 /* ignore hosts that don't have a check */
121 ObjectLock olock(hc);
123 /* ignore soft states */
124 if (hc->GetStateType() == StateTypeSoft)
127 /* ignore hosts that are up */
128 if (hc->GetState() == StateOK)
137 void Host::UpdateSlaveServices(void)
141 ConfigItem::Ptr item = ConfigItem::GetObject("Host", GetName());
143 /* Don't create slave services unless we own this object */
147 Dictionary::Ptr service_descriptions = GetServiceDescriptions();
149 if (!service_descriptions)
152 ObjectLock olock(service_descriptions);
155 BOOST_FOREACH(boost::tie(svcname, svcdesc), service_descriptions) {
156 if (svcdesc.IsScalar())
159 std::ostringstream namebuf;
160 namebuf << GetName() << ":" << svcname;
161 String name = namebuf.str();
163 std::vector<String> path;
164 path.push_back("services");
165 path.push_back(svcname);
168 item->GetLinkedExpressionList()->FindDebugInfoPath(path, di);
170 if (di.Path.IsEmpty())
171 di = item->GetDebugInfo();
173 ConfigItemBuilder::Ptr builder = boost::make_shared<ConfigItemBuilder>(di);
174 builder->SetType("Service");
175 builder->SetName(name);
176 builder->AddExpression("host", OperatorSet, GetName());
177 builder->AddExpression("display_name", OperatorSet, svcname);
178 builder->AddExpression("short_name", OperatorSet, svcname);
180 if (!svcdesc.IsObjectType<Dictionary>())
181 BOOST_THROW_EXCEPTION(std::invalid_argument("Service description must be either a string or a dictionary."));
183 Dictionary::Ptr service = svcdesc;
185 Array::Ptr templates = service->Get("templates");
188 ObjectLock olock(templates);
190 BOOST_FOREACH(const Value& tmpl, templates) {
191 builder->AddParent(tmpl);
195 /* Clone attributes from the service expression list. */
196 ExpressionList::Ptr svc_exprl = boost::make_shared<ExpressionList>();
197 item->GetLinkedExpressionList()->ExtractPath(path, svc_exprl);
199 std::vector<String> dpath;
200 dpath.push_back("templates");
201 svc_exprl->ErasePath(dpath);
203 builder->AddExpressionList(svc_exprl);
205 ConfigItem::Ptr serviceItem = builder->Compile();
206 serviceItem->Register();
207 DynamicObject::Ptr dobj = serviceItem->Commit();
208 dobj->OnConfigLoaded();
212 std::set<Service::Ptr> Host::GetServices(void) const
214 boost::mutex::scoped_lock lock(m_ServicesMutex);
216 std::set<Service::Ptr> services;
217 Service::WeakPtr wservice;
218 BOOST_FOREACH(boost::tie(boost::tuples::ignore, wservice), m_Services) {
219 Service::Ptr service = wservice.lock();
224 services.insert(service);
230 void Host::AddService(const Service::Ptr& service)
232 boost::mutex::scoped_lock lock(m_ServicesMutex);
234 m_Services[service->GetShortName()] = service;
237 void Host::RemoveService(const Service::Ptr& service)
239 boost::mutex::scoped_lock lock(m_ServicesMutex);
241 m_Services.erase(service->GetShortName());
244 int Host::GetTotalServices(void) const
246 return GetServices().size();
249 Service::Ptr Host::GetServiceByShortName(const Value& name) const
251 if (name.IsScalar()) {
253 boost::mutex::scoped_lock lock(m_ServicesMutex);
255 std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
257 if (it != m_Services.end())
261 return Service::Ptr();
262 } else if (name.IsObjectType<Dictionary>()) {
263 Dictionary::Ptr dict = name;
266 return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
268 BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + name.Serialize()));
272 std::set<Host::Ptr> Host::GetParentHosts(void) const
274 std::set<Host::Ptr> parents;
276 Array::Ptr dependencies = GetHostDependencies();
279 ObjectLock olock(dependencies);
281 BOOST_FOREACH(const Value& value, dependencies) {
282 if (value == GetName())
285 Host::Ptr host = GetByName(value);
290 parents.insert(host);
297 std::set<Host::Ptr> Host::GetChildHosts(void) const
299 std::set<Host::Ptr> children;
301 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
302 Array::Ptr dependencies = host->GetHostDependencies();
305 ObjectLock olock(dependencies);
307 BOOST_FOREACH(const Value& value, dependencies) {
308 if (value == GetName())
309 children.insert(host);
317 Service::Ptr Host::GetCheckService(void) const
319 String host_check = GetCheck();
321 if (host_check.IsEmpty())
322 return Service::Ptr();
324 return GetServiceByShortName(host_check);
327 std::set<Service::Ptr> Host::GetParentServices(void) const
329 std::set<Service::Ptr> parents;
331 Array::Ptr dependencies = GetServiceDependencies();
334 ObjectLock olock(dependencies);
336 BOOST_FOREACH(const Value& value, dependencies) {
337 parents.insert(GetServiceByShortName(value));
344 HostState Host::CalculateState(ServiceState state, bool reachable)
347 return HostUnreachable;
358 HostState Host::GetState(void) const
363 return HostUnreachable;
365 Service::Ptr hc = GetCheckService();
370 switch (hc->GetState()) {
380 HostState Host::GetLastState(void) const
385 return HostUnreachable;
387 Service::Ptr hc = GetCheckService();
392 switch (hc->GetLastState()) {
401 HostState Host::GetLastHardState(void) const
406 return HostUnreachable;
408 Service::Ptr hc = GetCheckService();
413 switch (hc->GetLastHardState()) {
422 double Host::GetLastStateUp(void) const
426 Service::Ptr hc = GetCheckService();
431 if (hc->GetLastStateOK() > hc->GetLastStateWarning())
432 return hc->GetLastStateOK();
434 return hc->GetLastStateWarning();
437 double Host::GetLastStateDown(void) const
441 Service::Ptr hc = GetCheckService();
446 return hc->GetLastStateCritical();
449 double Host::GetLastStateUnreachable(void) const
453 Service::Ptr hc = GetCheckService();
458 return hc->GetLastStateUnreachable();
461 double Host::GetLastStateChange(void) const
463 Service::Ptr hc = GetCheckService();
466 return Application::GetStartTime();
468 return hc->GetLastStateChange();
472 double Host::GetLastHardStateChange(void) const
474 Service::Ptr hc = GetCheckService();
477 return Application::GetStartTime();
479 return hc->GetLastHardStateChange();
482 StateType Host::GetLastStateType(void) const
484 Service::Ptr hc = GetCheckService();
487 return StateTypeHard;
489 return hc->GetLastStateType();
492 StateType Host::GetStateType(void) const
494 Service::Ptr hc = GetCheckService();
497 return StateTypeHard;
499 return hc->GetStateType();
502 HostState Host::StateFromString(const String& state)
506 else if (state == "DOWN")
508 else if (state == "UNREACHABLE")
509 return HostUnreachable;
511 return HostUnreachable;
514 String Host::StateToString(HostState state)
521 case HostUnreachable:
522 return "UNREACHABLE";
528 StateType Host::StateTypeFromString(const String& type)
531 return StateTypeSoft;
533 return StateTypeHard;
536 String Host::StateTypeToString(StateType type)
538 if (type == StateTypeSoft)
544 bool Host::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
546 if (macro == "HOSTNAME") {
550 else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") {
551 *result = GetDisplayName();
555 Service::Ptr hc = GetCheckService();
556 Dictionary::Ptr hccr;
559 ServiceState state = hc->GetState();
560 bool reachable = IsReachable();
562 if (macro == "HOSTSTATE") {
563 HostState hstate = CalculateState(state, reachable);
566 case HostUnreachable:
567 *result = "UNREACHABLE";
580 } else if (macro == "HOSTSTATEID") {
581 *result = Convert::ToString(state);
583 } else if (macro == "HOSTSTATETYPE") {
584 *result = Service::StateTypeToString(hc->GetStateType());
586 } else if (macro == "HOSTATTEMPT") {
587 *result = Convert::ToString(hc->GetCheckAttempt());
589 } else if (macro == "MAXHOSTATTEMPT") {
590 *result = Convert::ToString(hc->GetMaxCheckAttempts());
592 } else if (macro == "LASTHOSTSTATE") {
593 *result = StateToString(GetLastState());
595 } else if (macro == "LASTHOSTSTATEID") {
596 *result = Convert::ToString(GetLastState());
598 } else if (macro == "LASTHOSTSTATETYPE") {
599 *result = Service::StateTypeToString(GetLastStateType());
601 } else if (macro == "LASTHOSTSTATECHANGE") {
602 *result = Convert::ToString((long)hc->GetLastStateChange());
604 } else if (macro == "HOSTDURATIONSEC") {
605 *result = Convert::ToString((long)(Utility::GetTime() - hc->GetLastStateChange()));
609 hccr = hc->GetLastCheckResult();
613 if (macro == "HOSTLATENCY") {
614 *result = Convert::ToString(Service::CalculateLatency(hccr));
616 } else if (macro == "HOSTEXECUTIONTIME") {
617 *result = Convert::ToString(Service::CalculateExecutionTime(hccr));
619 } else if (macro == "HOSTOUTPUT") {
620 *result = hccr->Get("output");
622 } else if (macro == "HOSTPERFDATA") {
623 *result = hccr->Get("performance_data_raw");
625 } else if (macro == "LASTHOSTCHECK") {
626 *result = Convert::ToString((long)hccr->Get("schedule_start"));
631 Dictionary::Ptr macros = GetMacros();
635 if (name == "HOSTADDRESS")
637 else if (macro == "HOSTADDRESS6")
640 if (macros && macros->Contains(name)) {
641 *result = macros->Get(name);
645 if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {