2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-09-10 14:07:32 +02:00
|
|
|
#include "i2-icinga.h"
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-01-25 12:46:49 +01:00
|
|
|
map<String, vector<Service::WeakPtr> > Host::m_ServicesCache;
|
2013-01-24 15:10:17 +01:00
|
|
|
bool Host::m_ServicesCacheValid = true;
|
|
|
|
|
2012-12-04 08:42:24 +01:00
|
|
|
static AttributeDescription hostAttributes[] = {
|
|
|
|
{ "alias", Attribute_Config },
|
2013-01-27 11:35:47 +01:00
|
|
|
{ "hostgroups", Attribute_Config },
|
2013-01-27 11:45:51 +01:00
|
|
|
{ "dependencies", Attribute_Config },
|
|
|
|
{ "hostchecks", Attribute_Config },
|
2013-01-27 11:35:47 +01:00
|
|
|
{ "acknowledgement", Attribute_Replicated },
|
2013-01-29 14:19:54 +01:00
|
|
|
{ "acknowledgement_expiry", Attribute_Replicated },
|
2013-01-30 09:59:22 +01:00
|
|
|
{ "downtimes", Attribute_Replicated },
|
|
|
|
{ "comments", Attribute_Replicated }
|
2012-12-04 08:42:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TYPE(Host, hostAttributes);
|
2012-07-09 17:07:20 +02:00
|
|
|
|
2012-09-21 13:49:14 +02:00
|
|
|
bool Host::m_InitializerDone = false;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Host::Host(const Dictionary::Ptr& properties)
|
|
|
|
: DynamicObject(properties)
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
2012-09-21 13:49:14 +02:00
|
|
|
if (!m_InitializerDone) {
|
2012-10-15 08:45:14 +02:00
|
|
|
ConfigItem::OnCommitted.connect(boost::bind(&Host::ObjectCommittedHandler, _1));
|
|
|
|
ConfigItem::OnRemoved.connect(boost::bind(&Host::ObjectRemovedHandler, _1));
|
2012-09-21 13:49:14 +02:00
|
|
|
|
|
|
|
m_InitializerDone = true;
|
|
|
|
}
|
2013-01-24 13:21:35 +01:00
|
|
|
|
|
|
|
HostGroup::InvalidateMembersCache();
|
2013-01-29 14:19:54 +01:00
|
|
|
DowntimeProcessor::InvalidateDowntimeCache();
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
2012-06-13 13:42:55 +02:00
|
|
|
|
2013-01-24 15:01:06 +01:00
|
|
|
Host::~Host(void)
|
|
|
|
{
|
|
|
|
HostGroup::InvalidateMembersCache();
|
2013-01-29 14:19:54 +01:00
|
|
|
DowntimeProcessor::InvalidateDowntimeCache();
|
2013-01-24 15:01:06 +01:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String Host::GetAlias(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
String value = Get("alias");
|
|
|
|
if (!value.IsEmpty())
|
2012-06-13 13:42:55 +02:00
|
|
|
return value;
|
2012-08-02 09:38:08 +02:00
|
|
|
else
|
|
|
|
return GetName();
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
bool Host::Exists(const String& name)
|
2012-06-30 13:39:55 +02:00
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
return (DynamicObject::GetObject("Host", name));
|
2012-06-30 13:39:55 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Host::Ptr Host::GetByName(const String& name)
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr configObject = DynamicObject::GetObject("Host", name);
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
if (!configObject)
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(invalid_argument("Host '" + name + "' does not exist."));
|
2012-06-13 13:42:55 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
return dynamic_pointer_cast<Host>(configObject);
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
2012-06-30 13:39:55 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr Host::GetGroups(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("hostgroups");
|
2012-06-30 13:39:55 +02:00
|
|
|
}
|
2012-07-09 10:09:53 +02:00
|
|
|
|
2013-01-24 13:21:35 +01:00
|
|
|
set<Host::Ptr> Host::GetParents(void)
|
2012-07-09 12:44:31 +02:00
|
|
|
{
|
2013-01-24 13:21:35 +01:00
|
|
|
set<Host::Ptr> parents;
|
2012-07-09 12:44:31 +02:00
|
|
|
|
2012-08-03 13:19:55 +02:00
|
|
|
Dictionary::Ptr dependencies = Get("dependencies");
|
2012-08-02 09:38:08 +02:00
|
|
|
if (dependencies) {
|
2012-07-27 16:05:02 +02:00
|
|
|
dependencies = Service::ResolveDependencies(GetSelf(), dependencies);
|
2012-07-09 12:44:31 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Value dependency;
|
2012-07-17 19:19:03 +02:00
|
|
|
BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) {
|
2012-07-27 16:05:02 +02:00
|
|
|
Service::Ptr service = Service::GetByName(dependency);
|
2012-07-09 12:44:31 +02:00
|
|
|
|
2013-01-24 13:21:35 +01:00
|
|
|
Host::Ptr parent = service->GetHost();
|
2012-07-09 12:44:31 +02:00
|
|
|
|
|
|
|
/* ignore ourselves */
|
2013-01-24 13:21:35 +01:00
|
|
|
if (parent->GetName() == GetName())
|
2012-07-09 12:44:31 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
parents.insert(parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parents;
|
|
|
|
}
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2012-07-13 11:29:23 +02:00
|
|
|
Dictionary::Ptr Host::GetMacros(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("macros");
|
2012-07-13 11:29:23 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 14:19:54 +01:00
|
|
|
Dictionary::Ptr Host::GetDowntimes(void) const
|
|
|
|
{
|
|
|
|
return Get("downtimes");
|
|
|
|
}
|
|
|
|
|
2013-01-29 16:29:09 +01:00
|
|
|
Dictionary::Ptr Host::GetComments(void) const
|
|
|
|
{
|
|
|
|
return Get("comments");
|
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
bool Host::IsReachable(void)
|
2012-07-09 13:27:02 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Dictionary::Ptr dependencies = Get("dependencies");
|
2012-08-02 09:38:08 +02:00
|
|
|
if (dependencies) {
|
2012-07-27 16:05:02 +02:00
|
|
|
dependencies = Service::ResolveDependencies(GetSelf(), dependencies);
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Value dependency;
|
2012-07-17 19:19:03 +02:00
|
|
|
BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) {
|
2012-07-27 16:05:02 +02:00
|
|
|
Service::Ptr service = Service::GetByName(dependency);
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (!service->IsReachable() ||
|
|
|
|
(service->GetState() != StateOK && service->GetState() != StateWarning)) {
|
2012-07-09 13:27:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-29 14:19:54 +01:00
|
|
|
bool Host::IsInDowntime(void) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr downtimes = GetDowntimes();
|
|
|
|
|
|
|
|
if (!downtimes)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Dictionary::Ptr downtime;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, downtime), downtimes) {
|
|
|
|
if (DowntimeProcessor::IsDowntimeActive(downtime))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
bool Host::IsUp(void)
|
2012-07-09 13:27:02 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Dictionary::Ptr hostchecks = Get("hostchecks");
|
2012-08-02 09:38:08 +02:00
|
|
|
if (hostchecks) {
|
2012-07-27 16:05:02 +02:00
|
|
|
hostchecks = Service::ResolveDependencies(GetSelf(), hostchecks);
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Value hostcheck;
|
2012-07-17 19:19:03 +02:00
|
|
|
BOOST_FOREACH(tie(tuples::ignore, hostcheck), hostchecks) {
|
2012-07-27 16:05:02 +02:00
|
|
|
Service::Ptr service = Service::GetByName(hostcheck);
|
2012-07-16 22:00:50 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (service->GetState() != StateOK && service->GetState() != StateWarning) {
|
2012-07-09 13:27:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-09-21 13:49:14 +02:00
|
|
|
|
|
|
|
template<typename TDict>
|
|
|
|
static void CopyServiceAttributes(const Host::Ptr& host, TDict serviceDesc,
|
|
|
|
const ConfigItemBuilder::Ptr& builder)
|
|
|
|
{
|
|
|
|
/* TODO: we only need to copy macros if this is an inline definition,
|
|
|
|
* i.e. host->GetProperties() != service, however for now we just
|
|
|
|
* copy them anyway. */
|
|
|
|
Value macros = serviceDesc->Get("macros");
|
|
|
|
if (!macros.IsEmpty())
|
|
|
|
builder->AddExpression("macros", OperatorPlus, macros);
|
|
|
|
|
|
|
|
Value checkInterval = serviceDesc->Get("check_interval");
|
|
|
|
if (!checkInterval.IsEmpty())
|
|
|
|
builder->AddExpression("check_interval", OperatorSet, checkInterval);
|
|
|
|
|
|
|
|
Value retryInterval = serviceDesc->Get("retry_interval");
|
|
|
|
if (!retryInterval.IsEmpty())
|
|
|
|
builder->AddExpression("retry_interval", OperatorSet, retryInterval);
|
|
|
|
|
|
|
|
Value sgroups = serviceDesc->Get("servicegroups");
|
|
|
|
if (!sgroups.IsEmpty())
|
|
|
|
builder->AddExpression("servicegroups", OperatorPlus, sgroups);
|
|
|
|
|
|
|
|
Value checkers = serviceDesc->Get("checkers");
|
|
|
|
if (!checkers.IsEmpty())
|
|
|
|
builder->AddExpression("checkers", OperatorSet, checkers);
|
|
|
|
|
|
|
|
Value dependencies = serviceDesc->Get("dependencies");
|
|
|
|
if (!dependencies.IsEmpty())
|
|
|
|
builder->AddExpression("dependencies", OperatorPlus,
|
|
|
|
Service::ResolveDependencies(host, dependencies));
|
|
|
|
|
|
|
|
Value hostchecks = serviceDesc->Get("hostchecks");
|
|
|
|
if (!hostchecks.IsEmpty())
|
|
|
|
builder->AddExpression("dependencies", OperatorPlus,
|
|
|
|
Service::ResolveDependencies(host, hostchecks));
|
2013-01-22 16:01:08 +01:00
|
|
|
|
|
|
|
Value enableChecks = serviceDesc->Get("enable_checks");
|
|
|
|
if (!enableChecks.IsEmpty())
|
|
|
|
builder->AddExpression("enable_checks", OperatorSet, enableChecks);
|
2012-09-21 13:49:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Host::ObjectCommittedHandler(const ConfigItem::Ptr& item)
|
|
|
|
{
|
|
|
|
if (item->GetType() != "Host")
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* ignore abstract host objects */
|
|
|
|
if (!Host::Exists(item->GetName()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Host::Ptr host = Host::GetByName(item->GetName());
|
|
|
|
|
|
|
|
Dictionary::Ptr oldServices = host->Get("convenience_services");
|
|
|
|
|
|
|
|
Dictionary::Ptr newServices;
|
|
|
|
newServices = boost::make_shared<Dictionary>();
|
|
|
|
|
|
|
|
Dictionary::Ptr serviceDescs = host->Get("services");
|
|
|
|
|
|
|
|
if (serviceDescs) {
|
|
|
|
String svcname;
|
|
|
|
Value svcdesc;
|
|
|
|
BOOST_FOREACH(tie(svcname, svcdesc), serviceDescs) {
|
|
|
|
stringstream namebuf;
|
|
|
|
namebuf << item->GetName() << "-" << svcname;
|
|
|
|
String name = namebuf.str();
|
|
|
|
|
|
|
|
ConfigItemBuilder::Ptr builder = boost::make_shared<ConfigItemBuilder>(item->GetDebugInfo());
|
|
|
|
builder->SetType("Service");
|
|
|
|
builder->SetName(name);
|
|
|
|
builder->AddExpression("host_name", OperatorSet, item->GetName());
|
|
|
|
builder->AddExpression("alias", OperatorSet, svcname);
|
|
|
|
|
|
|
|
CopyServiceAttributes(host, host, builder);
|
|
|
|
|
|
|
|
if (svcdesc.IsScalar()) {
|
|
|
|
builder->AddParent(svcdesc);
|
|
|
|
} else if (svcdesc.IsObjectType<Dictionary>()) {
|
|
|
|
Dictionary::Ptr service = svcdesc;
|
|
|
|
|
|
|
|
String parent = service->Get("service");
|
|
|
|
if (parent.IsEmpty())
|
|
|
|
parent = svcname;
|
|
|
|
|
|
|
|
builder->AddParent(parent);
|
|
|
|
|
|
|
|
CopyServiceAttributes(host, service, builder);
|
|
|
|
} else {
|
|
|
|
throw_exception(invalid_argument("Service description must be either a string or a dictionary."));
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigItem::Ptr serviceItem = builder->Compile();
|
|
|
|
serviceItem->Commit();
|
|
|
|
|
|
|
|
newServices->Set(name, serviceItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldServices) {
|
|
|
|
ConfigItem::Ptr service;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, service), oldServices) {
|
|
|
|
if (!service)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!newServices->Contains(service->GetName()))
|
|
|
|
service->Unregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
host->Set("convenience_services", newServices);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Host::ObjectRemovedHandler(const ConfigItem::Ptr& item)
|
|
|
|
{
|
|
|
|
if (item->GetType() != "Host")
|
|
|
|
return;
|
|
|
|
|
|
|
|
DynamicObject::Ptr host = item->GetDynamicObject();
|
|
|
|
|
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Dictionary::Ptr services = host->Get("convenience_services");
|
|
|
|
|
|
|
|
if (!services)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ConfigItem::Ptr service;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, service), services) {
|
|
|
|
service->Unregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-24 13:21:35 +01:00
|
|
|
void Host::OnAttributeChanged(const String& name, const Value& oldValue)
|
|
|
|
{
|
|
|
|
if (name == "hostgroups")
|
|
|
|
HostGroup::InvalidateMembersCache();
|
2013-01-29 14:19:54 +01:00
|
|
|
else if (name == "downtimes")
|
|
|
|
DowntimeProcessor::InvalidateDowntimeCache();
|
2013-01-24 13:21:35 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 15:10:17 +01:00
|
|
|
set<Service::Ptr> Host::GetServices(void) const
|
|
|
|
{
|
|
|
|
set<Service::Ptr> services;
|
|
|
|
|
|
|
|
ValidateServicesCache();
|
|
|
|
|
2013-01-25 12:46:49 +01:00
|
|
|
BOOST_FOREACH(const Service::WeakPtr& svc, m_ServicesCache[GetName()]) {
|
|
|
|
Service::Ptr service = svc.lock();
|
|
|
|
|
|
|
|
if (!service)
|
2013-01-24 15:10:17 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
services.insert(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
return services;
|
|
|
|
}
|
|
|
|
|
2013-01-27 11:35:47 +01:00
|
|
|
AcknowledgementType Host::GetAcknowledgement(void)
|
|
|
|
{
|
|
|
|
Value value = Get("acknowledgement");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return AcknowledgementNone;
|
|
|
|
|
|
|
|
int ivalue = static_cast<int>(value);
|
|
|
|
AcknowledgementType avalue = static_cast<AcknowledgementType>(ivalue);
|
|
|
|
|
|
|
|
if (avalue != AcknowledgementNone) {
|
|
|
|
double expiry = GetAcknowledgementExpiry();
|
|
|
|
|
|
|
|
if (expiry != 0 && expiry < Utility::GetTime()) {
|
|
|
|
avalue = AcknowledgementNone;
|
|
|
|
SetAcknowledgement(avalue);
|
|
|
|
SetAcknowledgementExpiry(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return avalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Host::SetAcknowledgement(AcknowledgementType acknowledgement)
|
|
|
|
{
|
|
|
|
Set("acknowledgement", static_cast<long>(acknowledgement));
|
|
|
|
}
|
|
|
|
|
|
|
|
double Host::GetAcknowledgementExpiry(void) const
|
|
|
|
{
|
|
|
|
Value value = Get("acknowledgement_expiry");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return static_cast<double>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Host::SetAcknowledgementExpiry(double timestamp)
|
|
|
|
{
|
|
|
|
Set("acknowledgement_expiry", timestamp);
|
|
|
|
}
|
|
|
|
|
2013-01-24 15:10:17 +01:00
|
|
|
void Host::InvalidateServicesCache(void)
|
|
|
|
{
|
|
|
|
m_ServicesCacheValid = false;
|
|
|
|
m_ServicesCache.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Host::ValidateServicesCache(void)
|
|
|
|
{
|
|
|
|
if (m_ServicesCacheValid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_ServicesCache.clear();
|
|
|
|
|
|
|
|
DynamicObject::Ptr object;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) {
|
|
|
|
const Service::Ptr& service = static_pointer_cast<Service>(object);
|
|
|
|
|
2013-01-25 12:46:49 +01:00
|
|
|
m_ServicesCache[service->GetHost()->GetName()].push_back(service);
|
2013-01-24 15:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_ServicesCacheValid = true;
|
|
|
|
}
|
|
|
|
|