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/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 Dictionary::Ptr service_descriptions = GetServiceDescriptions();
143 if (!service_descriptions ||service_descriptions->GetLength() == 0)
146 ConfigItem::Ptr item = ConfigItem::GetObject("Host", GetName());
148 ObjectLock olock(service_descriptions);
149 BOOST_FOREACH(const Dictionary::Pair& kv, service_descriptions) {
150 std::ostringstream namebuf;
151 namebuf << GetName() << "!" << kv.first;
152 String name = namebuf.str();
154 std::vector<String> path;
155 path.push_back("services");
156 path.push_back(kv.first);
159 item->GetLinkedExpressionList()->FindDebugInfoPath(path, di);
161 if (di.Path.IsEmpty())
162 di = item->GetDebugInfo();
164 ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
165 builder->SetType("Service");
166 builder->SetName(name);
167 builder->AddExpression("host", OperatorSet, GetName());
168 builder->AddExpression("display_name", OperatorSet, kv.first);
169 builder->AddExpression("short_name", OperatorSet, kv.first);
171 if (!kv.second.IsObjectType<Dictionary>())
172 BOOST_THROW_EXCEPTION(std::invalid_argument("Service description must be either a string or a dictionary."));
174 Dictionary::Ptr service = kv.second;
176 Array::Ptr templates = service->Get("templates");
179 ObjectLock olock(templates);
181 BOOST_FOREACH(const Value& tmpl, templates) {
182 builder->AddParent(tmpl);
186 /* Clone attributes from the service expression list. */
187 ExpressionList::Ptr svc_exprl = make_shared<ExpressionList>();
188 item->GetLinkedExpressionList()->ExtractPath(path, svc_exprl);
190 builder->AddExpressionList(svc_exprl);
192 ConfigItem::Ptr serviceItem = builder->Compile();
193 serviceItem->Register();
194 DynamicObject::Ptr dobj = serviceItem->Commit();
195 dobj->OnConfigLoaded();
199 std::set<Service::Ptr> Host::GetServices(void) const
201 boost::mutex::scoped_lock lock(m_ServicesMutex);
203 std::set<Service::Ptr> services;
204 typedef std::pair<String, Service::Ptr> ServicePair;
205 BOOST_FOREACH(const ServicePair& kv, m_Services) {
206 services.insert(kv.second);
212 void Host::AddService(const Service::Ptr& service)
214 boost::mutex::scoped_lock lock(m_ServicesMutex);
216 m_Services[service->GetShortName()] = service;
219 void Host::RemoveService(const Service::Ptr& service)
221 boost::mutex::scoped_lock lock(m_ServicesMutex);
223 m_Services.erase(service->GetShortName());
226 int Host::GetTotalServices(void) const
228 return GetServices().size();
231 Service::Ptr Host::GetServiceByShortName(const Value& name) const
233 if (name.IsScalar()) {
235 boost::mutex::scoped_lock lock(m_ServicesMutex);
237 std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
239 if (it != m_Services.end())
243 return Service::Ptr();
244 } else if (name.IsObjectType<Dictionary>()) {
245 Dictionary::Ptr dict = name;
248 return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
250 BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + JsonSerialize(name)));
254 std::set<Host::Ptr> Host::GetParentHosts(void) const
256 std::set<Host::Ptr> parents;
258 Array::Ptr dependencies = GetHostDependencies();
261 ObjectLock olock(dependencies);
263 BOOST_FOREACH(const Value& value, dependencies) {
264 if (value == GetName())
267 Host::Ptr host = GetByName(value);
269 parents.insert(host);
276 std::set<Host::Ptr> Host::GetChildHosts(void) const
278 std::set<Host::Ptr> children;
280 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
281 Array::Ptr dependencies = host->GetHostDependencies();
284 ObjectLock olock(dependencies);
286 BOOST_FOREACH(const Value& value, dependencies) {
287 if (value == GetName())
288 children.insert(host);
296 Service::Ptr Host::GetCheckService(void) const
298 String host_check = GetCheck();
300 if (host_check.IsEmpty())
301 return Service::Ptr();
303 return GetServiceByShortName(host_check);
306 std::set<Service::Ptr> Host::GetParentServices(void) const
308 std::set<Service::Ptr> parents;
310 Array::Ptr dependencies = GetServiceDependencies();
313 ObjectLock olock(dependencies);
315 BOOST_FOREACH(const Value& value, dependencies) {
316 parents.insert(GetServiceByShortName(value));
323 HostState Host::CalculateState(ServiceState state, bool reachable)
326 return HostUnreachable;
337 HostState Host::GetState(void) const
342 return HostUnreachable;
344 Service::Ptr hc = GetCheckService();
349 switch (hc->GetState()) {
359 HostState Host::GetLastState(void) const
364 return HostUnreachable;
366 Service::Ptr hc = GetCheckService();
371 switch (hc->GetLastState()) {
380 HostState Host::GetLastHardState(void) const
385 return HostUnreachable;
387 Service::Ptr hc = GetCheckService();
392 switch (hc->GetLastHardState()) {
401 double Host::GetLastStateUp(void) const
405 Service::Ptr hc = GetCheckService();
410 if (hc->GetLastStateOK() > hc->GetLastStateWarning())
411 return hc->GetLastStateOK();
413 return hc->GetLastStateWarning();
416 double Host::GetLastStateDown(void) const
420 Service::Ptr hc = GetCheckService();
425 return hc->GetLastStateCritical();
428 double Host::GetLastStateUnreachable(void) const
432 Service::Ptr hc = GetCheckService();
437 return hc->GetLastStateUnreachable();
440 double Host::GetLastStateChange(void) const
442 Service::Ptr hc = GetCheckService();
445 return Application::GetStartTime();
447 return hc->GetLastStateChange();
451 double Host::GetLastHardStateChange(void) const
453 Service::Ptr hc = GetCheckService();
456 return Application::GetStartTime();
458 return hc->GetLastHardStateChange();
461 StateType Host::GetLastStateType(void) const
463 Service::Ptr hc = GetCheckService();
466 return StateTypeHard;
468 return hc->GetLastStateType();
471 StateType Host::GetStateType(void) const
473 Service::Ptr hc = GetCheckService();
476 return StateTypeHard;
478 return hc->GetStateType();
481 HostState Host::StateFromString(const String& state)
485 else if (state == "DOWN")
487 else if (state == "UNREACHABLE")
488 return HostUnreachable;
490 return HostUnreachable;
493 String Host::StateToString(HostState state)
500 case HostUnreachable:
501 return "UNREACHABLE";
507 StateType Host::StateTypeFromString(const String& type)
510 return StateTypeSoft;
512 return StateTypeHard;
515 String Host::StateTypeToString(StateType type)
517 if (type == StateTypeSoft)
523 bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
525 if (macro == "HOSTNAME") {
529 else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") {
530 *result = GetDisplayName();
534 Service::Ptr hc = GetCheckService();
535 CheckResult::Ptr hccr;
538 ServiceState state = hc->GetState();
539 bool reachable = IsReachable();
541 if (macro == "HOSTSTATE") {
542 HostState hstate = CalculateState(state, reachable);
545 case HostUnreachable:
546 *result = "UNREACHABLE";
559 } else if (macro == "HOSTSTATEID") {
560 *result = Convert::ToString(state);
562 } else if (macro == "HOSTSTATETYPE") {
563 *result = Service::StateTypeToString(hc->GetStateType());
565 } else if (macro == "HOSTATTEMPT") {
566 *result = Convert::ToString(hc->GetCheckAttempt());
568 } else if (macro == "MAXHOSTATTEMPT") {
569 *result = Convert::ToString(hc->GetMaxCheckAttempts());
571 } else if (macro == "LASTHOSTSTATE") {
572 *result = StateToString(GetLastState());
574 } else if (macro == "LASTHOSTSTATEID") {
575 *result = Convert::ToString(GetLastState());
577 } else if (macro == "LASTHOSTSTATETYPE") {
578 *result = Service::StateTypeToString(GetLastStateType());
580 } else if (macro == "LASTHOSTSTATECHANGE") {
581 *result = Convert::ToString((long)hc->GetLastStateChange());
583 } else if (macro == "HOSTDURATIONSEC") {
584 *result = Convert::ToString((long)(Utility::GetTime() - hc->GetLastStateChange()));
588 hccr = hc->GetLastCheckResult();
592 if (macro == "HOSTLATENCY") {
593 *result = Convert::ToString(Service::CalculateLatency(hccr));
595 } else if (macro == "HOSTEXECUTIONTIME") {
596 *result = Convert::ToString(Service::CalculateExecutionTime(hccr));
598 } else if (macro == "HOSTOUTPUT") {
599 *result = hccr->GetOutput();
601 } else if (macro == "HOSTPERFDATA") {
602 *result = PluginUtility::FormatPerfdata(hccr->GetPerformanceData());
604 } else if (macro == "LASTHOSTCHECK") {
605 *result = Convert::ToString((long)hccr->GetScheduleStart());
610 Dictionary::Ptr macros = GetMacros();
614 if (name == "HOSTADDRESS")
616 else if (macro == "HOSTADDRESS6")
619 if (macros && macros->Contains(name)) {
620 *result = macros->Get(name);
624 if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {