icinga2/lib/icinga/host.cpp

625 lines
15 KiB
C++
Raw Normal View History

/******************************************************************************
* 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"
using namespace icinga;
boost::mutex Host::m_ServiceMutex;
2013-02-27 21:49:03 +01:00
map<String, map<String, Service::WeakPtr> > Host::m_ServicesCache;
2013-02-27 16:04:49 +01:00
bool Host::m_ServicesCacheValid = true;
REGISTER_SCRIPTFUNCTION("ValidateServiceDictionary", &Host::ValidateServiceDictionary);
2013-03-01 12:07:52 +01:00
REGISTER_TYPE(Host);
Host::Host(const Dictionary::Ptr& properties)
: DynamicObject(properties)
2013-02-26 10:13:54 +01:00
{
RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
RegisterAttribute("hostgroups", Attribute_Config, &m_HostGroups);
RegisterAttribute("macros", Attribute_Config, &m_Macros);
RegisterAttribute("hostdependencies", Attribute_Config, &m_HostDependencies);
RegisterAttribute("servicedependencies", Attribute_Config, &m_ServiceDependencies);
RegisterAttribute("hostcheck", Attribute_Config, &m_HostCheck);
}
2013-02-20 19:52:25 +01:00
Host::~Host(void)
{
2013-02-27 15:23:25 +01:00
HostGroup::InvalidateMembersCache();
2013-02-26 10:13:54 +01:00
if (m_SlaveServices) {
ConfigItem::Ptr service;
2013-02-26 10:13:54 +01:00
BOOST_FOREACH(tie(tuples::ignore, service), m_SlaveServices) {
service->Unregister();
}
}
}
void Host::OnRegistrationCompleted(void)
{
DynamicObject::OnRegistrationCompleted();
2013-02-27 16:04:49 +01:00
Host::InvalidateServicesCache();
Host::UpdateSlaveServices(GetSelf());
}
String Host::GetDisplayName(void) const
{
2013-02-26 10:13:54 +01:00
if (!m_DisplayName.IsEmpty())
return m_DisplayName;
else
return GetName();
}
2013-02-17 19:14:34 +01:00
/**
* @threadsafety Always.
*/
Host::Ptr Host::GetByName(const String& name)
{
2012-07-30 10:17:29 +02:00
DynamicObject::Ptr configObject = DynamicObject::GetObject("Host", name);
return dynamic_pointer_cast<Host>(configObject);
}
2012-06-30 13:39:55 +02:00
Dictionary::Ptr Host::GetGroups(void) const
{
2013-02-26 10:13:54 +01:00
return m_HostGroups;;
2012-06-30 13:39:55 +02:00
}
2012-07-09 10:09:53 +02:00
2012-07-13 11:29:23 +02:00
Dictionary::Ptr Host::GetMacros(void) const
{
2013-02-26 10:13:54 +01:00
return m_Macros;
2012-07-13 11:29:23 +02:00
}
2013-02-07 20:29:35 +01:00
Dictionary::Ptr Host::GetHostDependencies(void) const
{
2013-02-26 10:13:54 +01:00
return m_HostDependencies;;
2013-02-07 20:29:35 +01:00
}
Dictionary::Ptr Host::GetServiceDependencies(void) const
{
2013-02-26 10:13:54 +01:00
return m_ServiceDependencies;
2013-02-07 20:29:35 +01:00
}
String Host::GetHostCheck(void) const
{
2013-02-26 10:13:54 +01:00
return m_HostCheck;
2013-02-07 20:29:35 +01:00
}
2013-02-19 23:02:08 +01:00
bool Host::IsReachable(const Host::Ptr& self)
{
set<Service::Ptr> parentServices = Host::GetParentServices(self);
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;
2013-02-07 20:29:35 +01:00
/* ignore soft states */
if (service->GetStateType() == StateTypeSoft)
continue;
2013-02-07 20:29:35 +01:00
/* ignore services states OK and Warning */
if (service->GetState() == StateOK ||
service->GetState() == StateWarning)
continue;
return false;
}
set<Host::Ptr> parentHosts = Host::GetParentHosts(self);
2013-02-19 23:02:08 +01:00
BOOST_FOREACH(const Host::Ptr& host, parentHosts) {
Service::Ptr hc = Host::GetHostCheckService(host);
ObjectLock olock(hc);
2013-02-19 23:02:08 +01:00
/* ignore hosts that are up */
if (hc && hc->GetState() == StateOK)
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;
}
template<bool copyServiceAttrs, typename TDict>
static void CopyServiceAttributes(TDict serviceDesc, const ConfigItemBuilder::Ptr& builder)
{
2013-03-01 12:07:52 +01:00
ObjectLock olock(serviceDesc);
/* TODO: we only need to copy macros if this is an inline definition,
* i.e. "typeid(serviceDesc)" != 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 short_name = serviceDesc->Get("short_name");
if (!short_name.IsEmpty())
builder->AddExpression("short_name", OperatorSet, short_name);
Value notification_interval = serviceDesc->Get("notification_interval");
if (!notification_interval.IsEmpty())
builder->AddExpression("notification_interval", OperatorSet, notification_interval);
if (copyServiceAttrs) {
Value servicedependencies = serviceDesc->Get("servicedependencies");
if (!servicedependencies.IsEmpty())
builder->AddExpression("servicedependencies", OperatorPlus, servicedependencies);
Value hostdependencies = serviceDesc->Get("hostdependencies");
if (!hostdependencies.IsEmpty())
builder->AddExpression("hostdependencies", OperatorPlus, hostdependencies);
}
}
2013-02-26 10:13:54 +01:00
void Host::UpdateSlaveServices(const Host::Ptr& self)
{
2013-02-26 10:13:54 +01:00
ConfigItem::Ptr item;
Dictionary::Ptr oldServices, newServices, serviceDescs;
String host_name;
2013-02-26 10:13:54 +01:00
{
ObjectLock olock(self);
host_name = self->GetName();
item = ConfigItem::GetObject("Host", host_name);
2013-02-26 10:13:54 +01:00
/* Don't create slave services unless we own this object
* and it's not a template. */
if (!item || self->IsAbstract())
return;
oldServices = self->m_SlaveServices;
serviceDescs = self->Get("services");
}
newServices = boost::make_shared<Dictionary>();
2013-03-01 12:07:52 +01:00
ObjectLock nlock(newServices);
2013-02-26 10:13:54 +01:00
DebugInfo debug_info;
{
ObjectLock olock(item);
debug_info = item->GetDebugInfo();
}
if (serviceDescs) {
2013-03-01 12:07:52 +01:00
ObjectLock olock(serviceDescs);
String svcname;
Value svcdesc;
BOOST_FOREACH(tie(svcname, svcdesc), serviceDescs) {
if (svcdesc.IsScalar())
svcname = svcdesc;
stringstream namebuf;
2013-02-26 10:13:54 +01:00
namebuf << host_name << "-" << svcname;
String name = namebuf.str();
2013-02-26 10:13:54 +01:00
ConfigItemBuilder::Ptr builder = boost::make_shared<ConfigItemBuilder>(debug_info);
builder->SetType("Service");
builder->SetName(name);
2013-02-26 10:13:54 +01:00
builder->AddExpression("host_name", OperatorSet, host_name);
builder->AddExpression("display_name", OperatorSet, svcname);
builder->AddExpression("short_name", OperatorSet, svcname);
2013-02-26 10:13:54 +01:00
CopyServiceAttributes<false>(self, builder);
if (svcdesc.IsScalar()) {
builder->AddParent(svcdesc);
} else if (svcdesc.IsObjectType<Dictionary>()) {
Dictionary::Ptr service = svcdesc;
2013-03-01 12:07:52 +01:00
Dictionary::Ptr templates;
{
ObjectLock olock(service);
templates = service->Get("templates");
}
if (templates) {
2013-03-01 12:07:52 +01:00
ObjectLock olock(templates);
String tmpl;
BOOST_FOREACH(tie(tuples::ignore, tmpl), templates) {
builder->AddParent(tmpl);
}
} else {
builder->AddParent(svcname);
}
CopyServiceAttributes<true>(service, builder);
} else {
BOOST_THROW_EXCEPTION(invalid_argument("Service description must be either a string or a dictionary."));
}
ConfigItem::Ptr serviceItem = builder->Compile();
2013-02-26 10:13:54 +01:00
DynamicObject::Ptr dobj = ConfigItem::Commit(serviceItem);
newServices->Set(name, serviceItem);
}
}
if (oldServices) {
2013-03-01 12:07:52 +01:00
ObjectLock olock(oldServices);
ConfigItem::Ptr service;
BOOST_FOREACH(tie(tuples::ignore, service), oldServices) {
if (!service)
continue;
if (!newServices->Contains(service->GetName()))
service->Unregister();
}
}
2013-02-24 01:10:34 +01:00
newServices->Seal();
2013-03-01 12:07:52 +01:00
ObjectLock olock(self);
2013-02-26 10:13:54 +01:00
self->Set("slave_services", newServices);
}
2013-02-02 22:13:11 +01:00
void Host::OnAttributeChanged(const String& name, const Value&)
{
if (name == "hostgroups")
2013-02-27 15:23:25 +01:00
HostGroup::InvalidateMembersCache();
2013-02-19 23:02:08 +01:00
else if (name == "services") {
2013-03-01 12:07:52 +01:00
Host::Ptr self;
{
ObjectLock olock(this);
self = GetSelf();
}
UpdateSlaveServices(self);
2013-02-19 23:02:08 +01:00
} else if (name == "notifications") {
set<Service::Ptr> services;
{
ObjectLock olock(this);
services = GetServices();
}
BOOST_FOREACH(const Service::Ptr& service, services) {
2013-02-26 10:13:54 +01:00
Service::UpdateSlaveNotifications(service);
}
}
}
set<Service::Ptr> Host::GetServices(void) const
{
set<Service::Ptr> services;
boost::mutex::scoped_lock lock(m_ServiceMutex);
Service::WeakPtr wservice;
BOOST_FOREACH(tie(tuples::ignore, wservice), m_ServicesCache[GetName()]) {
Service::Ptr service = wservice.lock();
if (!service)
continue;
services.insert(service);
}
return services;
}
2013-02-27 15:23:25 +01:00
void Host::InvalidateServicesCache(void)
{
{
boost::mutex::scoped_lock lock(m_ServiceMutex);
2013-02-27 16:04:49 +01:00
if (m_ServicesCacheValid)
Utility::QueueAsyncCallback(boost::bind(&Host::RefreshServicesCache));
2013-02-27 15:23:25 +01:00
m_ServicesCacheValid = false;
}
}
void Host::RefreshServicesCache(void)
{
2013-02-27 15:23:25 +01:00
{
boost::mutex::scoped_lock lock(m_ServiceMutex);
if (m_ServicesCacheValid)
return;
m_ServicesCacheValid = true;
}
2013-02-27 21:49:03 +01:00
map<String, map<String, Service::WeakPtr> > newServicesCache;
2013-02-18 23:44:24 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
const Service::Ptr& service = static_pointer_cast<Service>(object);
2013-02-26 10:13:54 +01:00
Host::Ptr host;
String short_name;
{
ObjectLock olock(service);
host = service->GetHost();
short_name = service->GetShortName();
}
2013-02-27 16:04:49 +01:00
if (!host)
continue;
2013-02-26 10:13:54 +01:00
String host_name;
{
ObjectLock olock(host);
host_name = host->GetName();
}
// TODO: assert for duplicate short_names
newServicesCache[host_name][short_name] = service;
}
boost::mutex::scoped_lock lock(m_ServiceMutex);
m_ServicesCache.swap(newServicesCache);
}
void Host::ValidateServiceDictionary(const ScriptTask::Ptr& task, const vector<Value>& arguments)
{
if (arguments.size() < 1)
BOOST_THROW_EXCEPTION(invalid_argument("Missing argument: Location must be specified."));
if (arguments.size() < 2)
BOOST_THROW_EXCEPTION(invalid_argument("Missing argument: Attribute dictionary must be specified."));
String location = arguments[0];
Dictionary::Ptr attrs = arguments[1];
2013-03-01 12:07:52 +01:00
ObjectLock olock(attrs);
String key;
Value value;
BOOST_FOREACH(tie(key, value), attrs) {
String name;
if (value.IsScalar()) {
name = value;
} else if (value.IsObjectType<Dictionary>()) {
Dictionary::Ptr serviceDesc = value;
2013-03-01 12:07:52 +01:00
name = serviceDesc->Get("service");
if (name.IsEmpty())
name = key;
} else {
continue;
}
2013-02-18 14:40:24 +01:00
ConfigItem::Ptr item;
ConfigCompilerContext *context = ConfigCompilerContext::GetContext();
if (context)
item = context->GetItem("Service", name);
/* ignore already active objects while we're in the compiler
* context and linking to existing items is disabled. */
if (!item && (!context || (context->GetFlags() & CompilerLinkExisting)))
item = ConfigItem::GetObject("Service", name);
if (!item) {
ConfigCompilerContext::GetContext()->AddError(false, "Validation failed for " +
location + ": Service '" + name + "' not found.");
}
}
task->FinishResult(Empty);
}
2013-02-07 20:29:35 +01:00
Service::Ptr Host::GetServiceByShortName(const Host::Ptr& self, const Value& name)
2013-02-07 20:29:35 +01:00
{
String host_name;
2013-02-07 20:29:35 +01:00
{
ObjectLock olock(self);
host_name = self->GetName();
}
2013-02-26 10:13:54 +01:00
if (name.IsScalar()) {
{
boost::mutex::scoped_lock lock(m_ServiceMutex);
2013-02-26 10:13:54 +01:00
2013-02-27 21:49:03 +01:00
map<String, Service::WeakPtr>& services = m_ServicesCache[host_name];
map<String, Service::WeakPtr>::iterator it = services.find(name);
2013-02-26 10:13:54 +01:00
if (it != services.end()) {
Service::Ptr service = it->second.lock();
assert(service);
return service;
}
}
2013-02-27 15:23:25 +01:00
return Service::Ptr();
} else if (name.IsObjectType<Dictionary>()) {
Dictionary::Ptr dict = name;
2013-02-27 15:23:25 +01:00
String short_name;
{
ObjectLock olock(dict);
host_name = dict->Get("host");
short_name = dict->Get("service");
}
return Service::GetByNamePair(host_name, short_name);
} else {
BOOST_THROW_EXCEPTION(invalid_argument("Host/Service name pair is invalid."));
}
2013-02-07 20:29:35 +01:00
}
set<Host::Ptr> Host::GetParentHosts(const Host::Ptr& self)
2013-02-07 20:29:35 +01:00
{
set<Host::Ptr> parents;
Dictionary::Ptr dependencies;
String host_name;
{
ObjectLock olock(self);
dependencies = self->GetHostDependencies();
host_name = self->GetName();
}
2013-02-07 20:29:35 +01:00
if (dependencies) {
Value value;
BOOST_FOREACH(tie(tuples::ignore, value), dependencies) {
if (value == host_name)
2013-02-07 20:29:35 +01:00
continue;
2013-02-27 15:23:25 +01:00
Host::Ptr host = GetByName(value);
if (!host)
continue;
parents.insert(host);
2013-02-07 20:29:35 +01:00
}
}
return parents;
}
Service::Ptr Host::GetHostCheckService(const Host::Ptr& self)
2013-02-07 20:29:35 +01:00
{
String host_check;
{
ObjectLock olock(self);
host_check = self->GetHostCheck();
}
2013-02-27 15:23:25 +01:00
if (host_check.IsEmpty())
return Service::Ptr();
return GetServiceByShortName(self, host_check);
2013-02-07 20:29:35 +01:00
}
set<Service::Ptr> Host::GetParentServices(const Host::Ptr& self)
2013-02-07 20:29:35 +01:00
{
set<Service::Ptr> parents;
Dictionary::Ptr dependencies;
{
ObjectLock olock(self);
dependencies = self->GetServiceDependencies();
}
2013-02-07 20:29:35 +01:00
if (dependencies) {
Value value;
BOOST_FOREACH(tie(tuples::ignore, value), dependencies) {
parents.insert(GetServiceByShortName(self, value));
2013-02-07 20:29:35 +01:00
}
}
return parents;
}
2013-02-24 01:10:34 +01:00
Dictionary::Ptr Host::CalculateDynamicMacros(const Host::Ptr& self)
{
Dictionary::Ptr macros = boost::make_shared<Dictionary>();
2013-03-01 12:07:52 +01:00
ObjectLock mlock(macros);
2013-02-24 01:10:34 +01:00
{
ObjectLock olock(self);
macros->Set("HOSTNAME", self->GetName());
macros->Set("HOSTDISPLAYNAME", self->GetDisplayName());
macros->Set("HOSTALIAS", self->GetName());
}
bool reachable = Host::IsReachable(self);
Dictionary::Ptr cr;
Service::Ptr hc = Host::GetHostCheckService(self);
2013-02-24 01:10:34 +01:00
if (hc) {
ObjectLock olock(hc);
String state;
int stateid;
switch (hc->GetState()) {
case StateOK:
case StateWarning:
state = "UP";
stateid = 0;
break;
default:
state = "DOWN";
stateid = 1;
break;
}
if (!reachable) {
state = "UNREACHABLE";
stateid = 2;
}
2013-02-24 01:10:34 +01:00
macros->Set("HOSTSTATE", state);
macros->Set("HOSTSTATEID", stateid);
macros->Set("HOSTSTATETYPE", Service::StateTypeToString(hc->GetStateType()));
macros->Set("HOSTATTEMPT", hc->GetCurrentCheckAttempt());
macros->Set("MAXHOSTATTEMPT", hc->GetMaxCheckAttempts());
2013-02-24 01:10:34 +01:00
cr = hc->GetLastCheckResult();
}
2013-02-24 01:10:34 +01:00
if (cr) {
macros->Set("HOSTLATENCY", Service::CalculateLatency(cr));
macros->Set("HOSTEXECUTIONTIME", Service::CalculateExecutionTime(cr));
ObjectLock olock(cr);
macros->Set("HOSTOUTPUT", cr->Get("output"));
macros->Set("HOSTPERFDATA", cr->Get("performance_data_raw"));
}
macros->Seal();
return macros;
}