2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-01-09 00:32:11 +01:00
|
|
|
* Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) *
|
2012-07-09 20:32:02 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "icinga/host.h"
|
|
|
|
#include "icinga/service.h"
|
|
|
|
#include "icinga/hostgroup.h"
|
2013-07-05 09:35:49 +02:00
|
|
|
#include "icinga/icingaapplication.h"
|
2013-11-07 14:38:37 +01:00
|
|
|
#include "icinga/pluginutility.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/logger_fwd.h"
|
2013-03-18 11:02:18 +01:00
|
|
|
#include "base/timer.h"
|
2013-03-22 14:40:55 +01:00
|
|
|
#include "base/convert.h"
|
2013-10-18 11:30:55 +02:00
|
|
|
#include "base/utility.h"
|
2013-03-25 18:36:15 +01:00
|
|
|
#include "base/scriptfunction.h"
|
2013-08-28 08:18:58 +02:00
|
|
|
#include "base/debug.h"
|
2013-11-08 11:17:46 +01:00
|
|
|
#include "base/serializer.h"
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "config/configitembuilder.h"
|
|
|
|
#include "config/configcompilercontext.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
REGISTER_TYPE(Host);
|
2012-07-09 17:07:20 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Host::Start(void)
|
2013-02-26 10:13:54 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Start();
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
ASSERT(!OwnsLock());
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Array::Ptr groups = GetGroups();
|
|
|
|
|
|
|
|
if (groups) {
|
2013-09-09 13:52:37 +02:00
|
|
|
ObjectLock olock(groups);
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const String& name, groups) {
|
|
|
|
HostGroup::Ptr hg = HostGroup::GetByName(name);
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (hg)
|
|
|
|
hg->AddMember(GetSelf());
|
2013-02-08 21:05:08 +01:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 16:53:57 +02:00
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2013-08-29 16:53:57 +02:00
|
|
|
void Host::OnConfigLoaded(void)
|
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
UpdateSlaveServices();
|
2013-01-24 15:01:06 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Host::Stop(void)
|
2013-02-27 12:44:51 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Stop();
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Array::Ptr groups = GetGroups();
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (groups) {
|
2013-09-09 13:52:37 +02:00
|
|
|
ObjectLock olock(groups);
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const String& name, groups) {
|
|
|
|
HostGroup::Ptr hg = HostGroup::GetByName(name);
|
|
|
|
|
|
|
|
if (hg)
|
|
|
|
hg->RemoveMember(GetSelf());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: unregister slave services/notifications?
|
2013-02-27 12:44:51 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
bool Host::IsReachable(void) const
|
2012-07-09 13:27:02 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<Service::Ptr> parentServices = GetParentServices();
|
2013-02-19 23:02:08 +01:00
|
|
|
|
|
|
|
BOOST_FOREACH(const Service::Ptr& service, parentServices) {
|
|
|
|
ObjectLock olock(service);
|
|
|
|
|
2013-02-07 20:29:35 +01:00
|
|
|
/* ignore pending services */
|
|
|
|
if (!service->GetLastCheckResult())
|
|
|
|
continue;
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2013-02-07 20:29:35 +01:00
|
|
|
/* ignore soft states */
|
|
|
|
if (service->GetStateType() == StateTypeSoft)
|
|
|
|
continue;
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2013-02-07 20:29:35 +01:00
|
|
|
/* ignore services states OK and Warning */
|
|
|
|
if (service->GetState() == StateOK ||
|
|
|
|
service->GetState() == StateWarning)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<Host::Ptr> parentHosts = GetParentHosts();
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2013-02-19 23:02:08 +01:00
|
|
|
BOOST_FOREACH(const Host::Ptr& host, parentHosts) {
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = host->GetCheckService();
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-09-25 09:12:15 +02:00
|
|
|
/* ignore hosts that don't have a check */
|
2013-03-02 09:07:47 +01:00
|
|
|
if (!hc)
|
|
|
|
continue;
|
|
|
|
|
2013-02-22 08:12:43 +01:00
|
|
|
ObjectLock olock(hc);
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
/* ignore soft states */
|
|
|
|
if (hc->GetStateType() == StateTypeSoft)
|
|
|
|
continue;
|
|
|
|
|
2013-02-19 23:02:08 +01:00
|
|
|
/* ignore hosts that are up */
|
2013-03-02 09:07:47 +01:00
|
|
|
if (hc->GetState() == StateOK)
|
2013-02-19 23:02:08 +01:00
|
|
|
continue;
|
2013-02-19 12:17:31 +01:00
|
|
|
|
2013-02-19 23:02:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-02-19 12:17:31 +01:00
|
|
|
|
2013-02-19 23:02:08 +01:00
|
|
|
return true;
|
2012-07-09 13:27:02 +02:00
|
|
|
}
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
void Host::UpdateSlaveServices(void)
|
2012-09-21 13:49:14 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
Dictionary::Ptr service_descriptions = GetServiceDescriptions();
|
|
|
|
|
2013-12-12 06:30:11 +01:00
|
|
|
if (!service_descriptions ||service_descriptions->GetLength() == 0)
|
2013-08-20 11:06:04 +02:00
|
|
|
return;
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-12-12 06:30:11 +01:00
|
|
|
ConfigItem::Ptr item = ConfigItem::GetObject("Host", GetName());
|
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
ObjectLock olock(service_descriptions);
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, service_descriptions) {
|
2013-08-20 11:06:04 +02:00
|
|
|
std::ostringstream namebuf;
|
2013-12-12 11:48:43 +01:00
|
|
|
namebuf << GetName() << "!" << kv.first;
|
2013-08-20 11:06:04 +02:00
|
|
|
String name = namebuf.str();
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
std::vector<String> path;
|
|
|
|
path.push_back("services");
|
2013-11-30 17:42:50 +01:00
|
|
|
path.push_back(kv.first);
|
2013-09-24 13:13:14 +02:00
|
|
|
|
2013-12-14 23:10:37 +01:00
|
|
|
ExpressionList::Ptr exprl;
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock ilock(item);
|
|
|
|
|
|
|
|
exprl = item->GetLinkedExpressionList();
|
|
|
|
}
|
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
DebugInfo di;
|
2013-12-14 23:10:37 +01:00
|
|
|
exprl->FindDebugInfoPath(path, di);
|
2013-09-24 13:13:14 +02:00
|
|
|
|
|
|
|
if (di.Path.IsEmpty())
|
|
|
|
di = item->GetDebugInfo();
|
|
|
|
|
2013-11-06 08:51:56 +01:00
|
|
|
ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
|
2013-08-20 11:06:04 +02:00
|
|
|
builder->SetType("Service");
|
|
|
|
builder->SetName(name);
|
2013-09-25 09:19:25 +02:00
|
|
|
builder->AddExpression("host", OperatorSet, GetName());
|
2013-11-30 17:42:50 +01:00
|
|
|
builder->AddExpression("display_name", OperatorSet, kv.first);
|
|
|
|
builder->AddExpression("short_name", OperatorSet, kv.first);
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
if (!kv.second.IsObjectType<Dictionary>())
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Service description must be either a string or a dictionary."));
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
Dictionary::Ptr service = kv.second;
|
2013-02-16 09:28:05 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Array::Ptr templates = service->Get("templates");
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (templates) {
|
|
|
|
ObjectLock olock(templates);
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const Value& tmpl, templates) {
|
|
|
|
builder->AddParent(tmpl);
|
2012-09-21 13:49:14 +02:00
|
|
|
}
|
|
|
|
}
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
/* Clone attributes from the service expression list. */
|
2013-11-06 08:51:56 +01:00
|
|
|
ExpressionList::Ptr svc_exprl = make_shared<ExpressionList>();
|
2013-12-14 23:10:37 +01:00
|
|
|
exprl->ExtractPath(path, svc_exprl);
|
2013-09-24 13:13:14 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
builder->AddExpressionList(svc_exprl);
|
|
|
|
|
|
|
|
ConfigItem::Ptr serviceItem = builder->Compile();
|
2013-08-30 15:09:42 +02:00
|
|
|
serviceItem->Register();
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Ptr dobj = serviceItem->Commit();
|
2013-08-29 19:25:34 +02:00
|
|
|
dobj->OnConfigLoaded();
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|
2013-01-24 13:21:35 +01:00
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<Service::Ptr> Host::GetServices(void) const
|
2013-01-24 15:10:17 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ServicesMutex);
|
2013-01-24 15:10:17 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
std::set<Service::Ptr> services;
|
2013-11-30 17:42:50 +01:00
|
|
|
typedef std::pair<String, Service::Ptr> ServicePair;
|
|
|
|
BOOST_FOREACH(const ServicePair& kv, m_Services) {
|
|
|
|
services.insert(kv.second);
|
2013-01-24 15:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return services;
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Host::AddService(const Service::Ptr& service)
|
2013-07-05 09:35:49 +02:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ServicesMutex);
|
|
|
|
|
|
|
|
m_Services[service->GetShortName()] = service;
|
2013-07-05 09:35:49 +02:00
|
|
|
}
|
2013-07-09 16:59:31 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Host::RemoveService(const Service::Ptr& service)
|
2013-02-27 15:23:25 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ServicesMutex);
|
2013-02-27 16:04:49 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
m_Services.erase(service->GetShortName());
|
2013-02-27 15:23:25 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
int Host::GetTotalServices(void) const
|
2013-01-24 15:10:17 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
return GetServices().size();
|
2013-01-24 15:10:17 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Service::Ptr Host::GetServiceByShortName(const Value& name) const
|
2013-02-07 20:29:35 +01:00
|
|
|
{
|
2013-02-27 12:44:51 +01:00
|
|
|
if (name.IsScalar()) {
|
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ServicesMutex);
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
|
2013-02-08 15:38:22 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (it != m_Services.end())
|
|
|
|
return it->second;
|
2013-02-08 21:30:14 +01:00
|
|
|
}
|
2013-02-08 15:38:22 +01:00
|
|
|
|
2013-02-27 15:23:25 +01:00
|
|
|
return Service::Ptr();
|
2013-02-08 21:30:14 +01:00
|
|
|
} else if (name.IsObjectType<Dictionary>()) {
|
|
|
|
Dictionary::Ptr dict = name;
|
2013-02-27 15:23:25 +01:00
|
|
|
String short_name;
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
|
2013-02-08 21:30:14 +01:00
|
|
|
} else {
|
2013-11-08 11:17:46 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + JsonSerialize(name)));
|
2013-02-08 21:30:14 +01:00
|
|
|
}
|
2013-02-07 20:29:35 +01:00
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<Host::Ptr> Host::GetParentHosts(void) const
|
2013-02-07 20:29:35 +01:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<Host::Ptr> parents;
|
2013-02-07 20:29:35 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
Array::Ptr dependencies = GetHostDependencies();
|
2013-02-07 20:29:35 +01:00
|
|
|
|
|
|
|
if (dependencies) {
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(dependencies);
|
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
BOOST_FOREACH(const Value& value, dependencies) {
|
2013-03-02 09:07:47 +01:00
|
|
|
if (value == GetName())
|
2013-02-07 20:29:35 +01:00
|
|
|
continue;
|
|
|
|
|
2013-02-27 15:23:25 +01:00
|
|
|
Host::Ptr host = GetByName(value);
|
|
|
|
|
|
|
|
parents.insert(host);
|
2013-02-07 20:29:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parents;
|
|
|
|
}
|
|
|
|
|
2013-07-19 16:11:17 +02:00
|
|
|
std::set<Host::Ptr> Host::GetChildHosts(void) const
|
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
std::set<Host::Ptr> children;
|
2013-07-19 16:11:17 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
|
2013-07-19 16:11:17 +02:00
|
|
|
Array::Ptr dependencies = host->GetHostDependencies();
|
|
|
|
|
|
|
|
if (dependencies) {
|
|
|
|
ObjectLock olock(dependencies);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Value& value, dependencies) {
|
|
|
|
if (value == GetName())
|
2013-10-26 09:41:45 +02:00
|
|
|
children.insert(host);
|
2013-07-19 16:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
return children;
|
2013-07-19 16:11:17 +02:00
|
|
|
}
|
|
|
|
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr Host::GetCheckService(void) const
|
2013-02-07 20:29:35 +01:00
|
|
|
{
|
2013-09-25 09:12:15 +02:00
|
|
|
String host_check = GetCheck();
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-02-27 15:23:25 +01:00
|
|
|
if (host_check.IsEmpty())
|
|
|
|
return Service::Ptr();
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
return GetServiceByShortName(host_check);
|
2013-02-07 20:29:35 +01:00
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<Service::Ptr> Host::GetParentServices(void) const
|
2013-02-07 20:29:35 +01:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<Service::Ptr> parents;
|
2013-02-07 20:29:35 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
Array::Ptr dependencies = GetServiceDependencies();
|
2013-02-07 20:29:35 +01:00
|
|
|
|
|
|
|
if (dependencies) {
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(dependencies);
|
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
BOOST_FOREACH(const Value& value, dependencies) {
|
2013-03-02 09:07:47 +01:00
|
|
|
parents.insert(GetServiceByShortName(value));
|
2013-02-07 20:29:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parents;
|
|
|
|
}
|
2013-02-09 18:39:43 +01:00
|
|
|
|
2013-03-19 13:04:30 +01:00
|
|
|
HostState Host::CalculateState(ServiceState state, bool reachable)
|
2013-03-07 12:04:20 +01:00
|
|
|
{
|
2013-03-19 13:04:30 +01:00
|
|
|
if (!reachable)
|
2013-03-07 12:04:20 +01:00
|
|
|
return HostUnreachable;
|
|
|
|
|
2013-03-19 13:04:30 +01:00
|
|
|
switch (state) {
|
2013-03-07 12:04:20 +01:00
|
|
|
case StateOK:
|
|
|
|
case StateWarning:
|
|
|
|
return HostUp;
|
|
|
|
default:
|
|
|
|
return HostDown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:35:49 +02:00
|
|
|
HostState Host::GetState(void) const
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
|
|
|
if (!IsReachable())
|
|
|
|
return HostUnreachable;
|
|
|
|
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = GetCheckService();
|
2013-07-05 09:35:49 +02:00
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return HostUp;
|
|
|
|
|
|
|
|
switch (hc->GetState()) {
|
|
|
|
case StateOK:
|
|
|
|
case StateWarning:
|
|
|
|
return HostUp;
|
|
|
|
default:
|
|
|
|
return HostDown;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-07 12:04:20 +01:00
|
|
|
HostState Host::GetLastState(void) const
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-07 12:04:20 +01:00
|
|
|
|
|
|
|
if (!IsReachable())
|
|
|
|
return HostUnreachable;
|
|
|
|
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = GetCheckService();
|
2013-03-07 12:04:20 +01:00
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return HostUp;
|
|
|
|
|
|
|
|
switch (hc->GetLastState()) {
|
|
|
|
case StateOK:
|
|
|
|
case StateWarning:
|
|
|
|
return HostUp;
|
|
|
|
default:
|
|
|
|
return HostDown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:35:49 +02:00
|
|
|
HostState Host::GetLastHardState(void) const
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
|
|
|
if (!IsReachable())
|
|
|
|
return HostUnreachable;
|
|
|
|
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = GetCheckService();
|
2013-07-05 09:35:49 +02:00
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return HostUp;
|
|
|
|
|
|
|
|
switch (hc->GetLastHardState()) {
|
|
|
|
case StateOK:
|
|
|
|
case StateWarning:
|
|
|
|
return HostUp;
|
|
|
|
default:
|
|
|
|
return HostDown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 18:16:39 +02:00
|
|
|
double Host::GetLastStateUp(void) const
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = GetCheckService();
|
2013-07-18 18:16:39 +02:00
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (hc->GetLastStateOK() > hc->GetLastStateWarning())
|
|
|
|
return hc->GetLastStateOK();
|
|
|
|
else
|
|
|
|
return hc->GetLastStateWarning();
|
|
|
|
}
|
|
|
|
|
|
|
|
double Host::GetLastStateDown(void) const
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = GetCheckService();
|
2013-07-18 18:16:39 +02:00
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return hc->GetLastStateCritical();
|
|
|
|
}
|
|
|
|
|
|
|
|
double Host::GetLastStateUnreachable(void) const
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = GetCheckService();
|
2013-07-18 18:16:39 +02:00
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return hc->GetLastStateUnreachable();
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:35:49 +02:00
|
|
|
double Host::GetLastStateChange(void) const
|
|
|
|
{
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = GetCheckService();
|
2013-07-05 09:35:49 +02:00
|
|
|
|
|
|
|
if (!hc)
|
2013-10-26 09:41:45 +02:00
|
|
|
return Application::GetStartTime();
|
2013-07-05 09:35:49 +02:00
|
|
|
|
|
|
|
return hc->GetLastStateChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double Host::GetLastHardStateChange(void) const
|
|
|
|
{
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = GetCheckService();
|
2013-07-05 09:35:49 +02:00
|
|
|
|
|
|
|
if (!hc)
|
2013-10-26 09:41:45 +02:00
|
|
|
return Application::GetStartTime();
|
2013-07-05 09:35:49 +02:00
|
|
|
|
|
|
|
return hc->GetLastHardStateChange();
|
|
|
|
}
|
|
|
|
|
2013-03-07 12:04:20 +01:00
|
|
|
StateType Host::GetLastStateType(void) const
|
|
|
|
{
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = GetCheckService();
|
2013-03-07 12:04:20 +01:00
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return StateTypeHard;
|
|
|
|
|
|
|
|
return hc->GetLastStateType();
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:35:49 +02:00
|
|
|
StateType Host::GetStateType(void) const
|
|
|
|
{
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = GetCheckService();
|
2013-07-05 09:35:49 +02:00
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return StateTypeHard;
|
|
|
|
|
|
|
|
return hc->GetStateType();
|
|
|
|
}
|
|
|
|
|
2013-10-29 13:44:43 +01:00
|
|
|
HostState Host::StateFromString(const String& state)
|
|
|
|
{
|
|
|
|
if (state == "UP")
|
|
|
|
return HostUp;
|
|
|
|
else if (state == "DOWN")
|
|
|
|
return HostDown;
|
|
|
|
else if (state == "UNREACHABLE")
|
|
|
|
return HostUnreachable;
|
|
|
|
else
|
|
|
|
return HostUnreachable;
|
|
|
|
}
|
|
|
|
|
2013-03-19 13:04:30 +01:00
|
|
|
String Host::StateToString(HostState state)
|
2013-03-07 12:04:20 +01:00
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case HostUp:
|
|
|
|
return "UP";
|
|
|
|
case HostDown:
|
|
|
|
return "DOWN";
|
|
|
|
case HostUnreachable:
|
|
|
|
return "UNREACHABLE";
|
|
|
|
default:
|
|
|
|
return "INVALID";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 13:44:43 +01:00
|
|
|
StateType Host::StateTypeFromString(const String& type)
|
|
|
|
{
|
|
|
|
if (type == "SOFT")
|
|
|
|
return StateTypeSoft;
|
|
|
|
else
|
|
|
|
return StateTypeHard;
|
|
|
|
}
|
|
|
|
|
|
|
|
String Host::StateTypeToString(StateType type)
|
|
|
|
{
|
|
|
|
if (type == StateTypeSoft)
|
|
|
|
return "SOFT";
|
|
|
|
else
|
|
|
|
return "HARD";
|
|
|
|
}
|
|
|
|
|
2013-11-09 14:22:38 +01:00
|
|
|
bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
|
2013-02-09 18:39:43 +01:00
|
|
|
{
|
2013-07-02 09:54:24 +02:00
|
|
|
if (macro == "HOSTNAME") {
|
2013-03-22 14:40:55 +01:00
|
|
|
*result = GetName();
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-02 09:54:24 +02:00
|
|
|
else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") {
|
2013-03-22 14:40:55 +01:00
|
|
|
*result = GetDisplayName();
|
|
|
|
return true;
|
2013-03-07 15:00:26 +01:00
|
|
|
}
|
2013-02-24 01:10:34 +01:00
|
|
|
|
2013-09-25 09:12:15 +02:00
|
|
|
Service::Ptr hc = GetCheckService();
|
2013-11-09 14:22:38 +01:00
|
|
|
CheckResult::Ptr hccr;
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
if (hc) {
|
2013-03-19 13:04:30 +01:00
|
|
|
ServiceState state = hc->GetState();
|
|
|
|
bool reachable = IsReachable();
|
|
|
|
|
2013-03-22 14:40:55 +01:00
|
|
|
if (macro == "HOSTSTATE") {
|
2013-08-30 17:02:21 +02:00
|
|
|
HostState hstate = CalculateState(state, reachable);
|
|
|
|
|
|
|
|
switch (hstate) {
|
|
|
|
case HostUnreachable:
|
|
|
|
*result = "UNREACHABLE";
|
|
|
|
break;
|
|
|
|
case HostUp:
|
|
|
|
*result = "UP";
|
|
|
|
break;
|
|
|
|
case HostDown:
|
|
|
|
*result = "DOWN";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
}
|
|
|
|
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
|
|
|
} else if (macro == "HOSTSTATEID") {
|
|
|
|
*result = Convert::ToString(state);
|
|
|
|
return true;
|
|
|
|
} else if (macro == "HOSTSTATETYPE") {
|
|
|
|
*result = Service::StateTypeToString(hc->GetStateType());
|
|
|
|
return true;
|
|
|
|
} else if (macro == "HOSTATTEMPT") {
|
2013-10-26 09:41:45 +02:00
|
|
|
*result = Convert::ToString(hc->GetCheckAttempt());
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
|
|
|
} else if (macro == "MAXHOSTATTEMPT") {
|
|
|
|
*result = Convert::ToString(hc->GetMaxCheckAttempts());
|
|
|
|
return true;
|
|
|
|
} else if (macro == "LASTHOSTSTATE") {
|
|
|
|
*result = StateToString(GetLastState());
|
|
|
|
return true;
|
|
|
|
} else if (macro == "LASTHOSTSTATEID") {
|
|
|
|
*result = Convert::ToString(GetLastState());
|
|
|
|
return true;
|
|
|
|
} else if (macro == "LASTHOSTSTATETYPE") {
|
|
|
|
*result = Service::StateTypeToString(GetLastStateType());
|
|
|
|
return true;
|
|
|
|
} else if (macro == "LASTHOSTSTATECHANGE") {
|
|
|
|
*result = Convert::ToString((long)hc->GetLastStateChange());
|
|
|
|
return true;
|
2013-10-18 11:29:31 +02:00
|
|
|
} else if (macro == "HOSTDURATIONSEC") {
|
|
|
|
*result = Convert::ToString((long)(Utility::GetTime() - hc->GetLastStateChange()));
|
|
|
|
return true;
|
2013-03-22 14:40:55 +01:00
|
|
|
}
|
2013-03-07 12:04:20 +01:00
|
|
|
|
2013-03-22 14:40:55 +01:00
|
|
|
hccr = hc->GetLastCheckResult();
|
2013-02-09 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
2013-03-22 14:40:55 +01:00
|
|
|
if (hccr) {
|
|
|
|
if (macro == "HOSTLATENCY") {
|
|
|
|
*result = Convert::ToString(Service::CalculateLatency(hccr));
|
|
|
|
return true;
|
|
|
|
} else if (macro == "HOSTEXECUTIONTIME") {
|
|
|
|
*result = Convert::ToString(Service::CalculateExecutionTime(hccr));
|
|
|
|
return true;
|
|
|
|
} else if (macro == "HOSTOUTPUT") {
|
2013-11-09 14:22:38 +01:00
|
|
|
*result = hccr->GetOutput();
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
|
|
|
} else if (macro == "HOSTPERFDATA") {
|
2013-11-09 14:22:38 +01:00
|
|
|
*result = PluginUtility::FormatPerfdata(hccr->GetPerformanceData());
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
|
|
|
} else if (macro == "LASTHOSTCHECK") {
|
2013-11-09 14:22:38 +01:00
|
|
|
*result = Convert::ToString((long)hccr->GetScheduleStart());
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-02-24 01:10:34 +01:00
|
|
|
|
2013-12-18 10:53:26 +01:00
|
|
|
if (macro.SubStr(0, 5) == "_HOST") {
|
|
|
|
Dictionary::Ptr custom = GetCustom();
|
|
|
|
*result = custom ? custom->Get(macro.SubStr(5)) : "";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-22 14:40:55 +01:00
|
|
|
Dictionary::Ptr macros = GetMacros();
|
2013-03-07 12:04:20 +01:00
|
|
|
|
2013-07-02 10:06:08 +02:00
|
|
|
String name = macro;
|
2013-07-02 09:47:31 +02:00
|
|
|
|
2013-07-02 10:06:08 +02:00
|
|
|
if (name == "HOSTADDRESS")
|
|
|
|
name = "address";
|
|
|
|
else if (macro == "HOSTADDRESS6")
|
|
|
|
name = "address6";
|
2013-07-02 09:47:31 +02:00
|
|
|
|
2013-07-02 10:06:08 +02:00
|
|
|
if (macros && macros->Contains(name)) {
|
|
|
|
*result = macros->Get(name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {
|
|
|
|
*result = GetName();
|
|
|
|
return true;
|
2013-02-24 01:10:34 +01:00
|
|
|
}
|
|
|
|
|
2013-03-22 14:40:55 +01:00
|
|
|
return false;
|
2013-02-09 18:39:43 +01:00
|
|
|
}
|