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-02-27 12:44:51 +01:00
|
|
|
boost::mutex Host::m_ServiceMutex;
|
2013-02-27 21:49:03 +01:00
|
|
|
map<String, map<String, Service::WeakPtr> > Host::m_ServicesCache;
|
2013-03-06 11:03:50 +01:00
|
|
|
bool Host::m_ServicesCacheNeedsUpdate = false;
|
|
|
|
Timer::Ptr Host::m_ServicesCacheTimer;
|
2013-01-24 15:10:17 +01:00
|
|
|
|
2013-03-13 16:04:53 +01:00
|
|
|
REGISTER_SCRIPTFUNCTION(ValidateServiceDictionary, &Host::ValidateServiceDictionary);
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
REGISTER_TYPE(Host);
|
2012-07-09 17:07:20 +02:00
|
|
|
|
2013-03-14 23:52:52 +01:00
|
|
|
Host::Host(const Dictionary::Ptr& serializedUpdate)
|
|
|
|
: DynamicObject(serializedUpdate)
|
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
|
|
|
|
2013-01-24 15:01:06 +01:00
|
|
|
Host::~Host(void)
|
|
|
|
{
|
2013-02-27 15:23:25 +01:00
|
|
|
HostGroup::InvalidateMembersCache();
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
if (m_SlaveServices) {
|
2013-02-08 21:05:08 +01:00
|
|
|
ConfigItem::Ptr service;
|
2013-02-26 10:13:54 +01:00
|
|
|
BOOST_FOREACH(tie(tuples::ignore, service), m_SlaveServices) {
|
2013-02-08 21:05:08 +01:00
|
|
|
service->Unregister();
|
|
|
|
}
|
|
|
|
}
|
2013-01-24 15:01:06 +01:00
|
|
|
}
|
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
void Host::OnRegistrationCompleted(void)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
DynamicObject::OnRegistrationCompleted();
|
|
|
|
|
2013-02-27 16:04:49 +01:00
|
|
|
Host::InvalidateServicesCache();
|
2013-03-02 09:07:47 +01:00
|
|
|
UpdateSlaveServices();
|
2013-02-27 12:44:51 +01:00
|
|
|
}
|
|
|
|
|
2013-02-09 17:50:47 +01:00
|
|
|
String Host::GetDisplayName(void) const
|
2012-08-02 09:38:08 +02:00
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
if (!m_DisplayName.IsEmpty())
|
|
|
|
return m_DisplayName;
|
2012-08-02 09:38:08 +02:00
|
|
|
else
|
|
|
|
return GetName();
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
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
|
|
|
|
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
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
Array::Ptr Host::GetGroups(void) const
|
2012-06-30 13:39:55 +02:00
|
|
|
{
|
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-03-14 12:17:46 +01:00
|
|
|
Array::Ptr Host::GetHostDependencies(void) const
|
2013-02-07 20:29:35 +01:00
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_HostDependencies;;
|
2013-02-07 20:29:35 +01:00
|
|
|
}
|
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
Array::Ptr Host::GetServiceDependencies(void) const
|
2013-02-07 20:29:35 +01:00
|
|
|
{
|
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-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
|
|
|
|
|
|
|
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-02 09:07:47 +01:00
|
|
|
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-03-06 11:03:50 +01:00
|
|
|
Service::Ptr hc = host->GetHostCheckService();
|
2013-03-02 09:07:47 +01:00
|
|
|
|
|
|
|
/* ignore hosts that don't have a hostcheck */
|
|
|
|
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-02-08 21:38:34 +01:00
|
|
|
template<bool copyServiceAttrs, typename TDict>
|
2013-02-08 15:38:22 +01:00
|
|
|
static void CopyServiceAttributes(TDict serviceDesc, const ConfigItemBuilder::Ptr& builder)
|
2012-09-21 13:49:14 +02:00
|
|
|
{
|
|
|
|
/* TODO: we only need to copy macros if this is an inline definition,
|
2013-02-09 15:20:10 +01:00
|
|
|
* i.e. "typeid(serviceDesc)" != Service, however for now we just
|
2012-09-21 13:49:14 +02:00
|
|
|
* 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);
|
2013-02-08 21:38:34 +01:00
|
|
|
|
2013-02-13 18:43:56 +01:00
|
|
|
Value short_name = serviceDesc->Get("short_name");
|
|
|
|
if (!short_name.IsEmpty())
|
|
|
|
builder->AddExpression("short_name", OperatorSet, short_name);
|
|
|
|
|
2013-02-27 18:15:01 +01:00
|
|
|
Value notification_interval = serviceDesc->Get("notification_interval");
|
|
|
|
if (!notification_interval.IsEmpty())
|
|
|
|
builder->AddExpression("notification_interval", OperatorSet, notification_interval);
|
|
|
|
|
2013-03-13 16:04:53 +01:00
|
|
|
Value check_period = serviceDesc->Get("check_period");
|
|
|
|
if (!check_period.IsEmpty())
|
|
|
|
builder->AddExpression("check_period", OperatorSet, check_period);
|
|
|
|
|
2013-02-08 21:38:34 +01:00
|
|
|
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);
|
|
|
|
}
|
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-03-02 09:07:47 +01:00
|
|
|
ConfigItem::Ptr item = ConfigItem::GetObject("Host", GetName());
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-03-11 12:04:10 +01:00
|
|
|
/* Don't create slave services unless we own this object */
|
|
|
|
if (!item)
|
2013-03-02 09:07:47 +01:00
|
|
|
return;
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Dictionary::Ptr oldServices = m_SlaveServices;
|
|
|
|
Dictionary::Ptr serviceDescs = Get("services");
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Dictionary::Ptr newServices = boost::make_shared<Dictionary>();
|
2012-09-21 13:49:14 +02:00
|
|
|
|
|
|
|
if (serviceDescs) {
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(serviceDescs);
|
2012-09-21 13:49:14 +02:00
|
|
|
String svcname;
|
|
|
|
Value svcdesc;
|
|
|
|
BOOST_FOREACH(tie(svcname, svcdesc), serviceDescs) {
|
2013-02-13 18:43:56 +01:00
|
|
|
if (svcdesc.IsScalar())
|
|
|
|
svcname = svcdesc;
|
|
|
|
|
2012-09-21 13:49:14 +02:00
|
|
|
stringstream namebuf;
|
2013-03-02 09:07:47 +01:00
|
|
|
namebuf << GetName() << "-" << svcname;
|
2012-09-21 13:49:14 +02:00
|
|
|
String name = namebuf.str();
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
ConfigItemBuilder::Ptr builder = boost::make_shared<ConfigItemBuilder>(item->GetDebugInfo());
|
2012-09-21 13:49:14 +02:00
|
|
|
builder->SetType("Service");
|
|
|
|
builder->SetName(name);
|
2013-03-02 09:07:47 +01:00
|
|
|
builder->AddExpression("host_name", OperatorSet, GetName());
|
2013-02-13 19:32:44 +01:00
|
|
|
builder->AddExpression("display_name", OperatorSet, svcname);
|
2013-02-08 15:38:22 +01:00
|
|
|
builder->AddExpression("short_name", OperatorSet, svcname);
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
CopyServiceAttributes<false>(this, builder);
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
if (!svcdesc.IsObjectType<Dictionary>())
|
|
|
|
BOOST_THROW_EXCEPTION(invalid_argument("Service description must be either a string or a dictionary."));
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
Dictionary::Ptr service = svcdesc;
|
2013-02-16 09:28:05 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
Array::Ptr templates = service->Get("templates");
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
if (templates) {
|
|
|
|
ObjectLock olock(templates);
|
2012-09-21 13:49:14 +02:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
BOOST_FOREACH(const Value& tmpl, templates) {
|
|
|
|
builder->AddParent(tmpl);
|
|
|
|
}
|
2012-09-21 13:49:14 +02:00
|
|
|
}
|
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
CopyServiceAttributes<true>(service, builder);
|
|
|
|
|
2012-09-21 13:49:14 +02:00
|
|
|
ConfigItem::Ptr serviceItem = builder->Compile();
|
2013-03-02 09:07:47 +01:00
|
|
|
DynamicObject::Ptr dobj = serviceItem->Commit();
|
2012-09-21 13:49:14 +02:00
|
|
|
|
|
|
|
newServices->Set(name, serviceItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldServices) {
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(oldServices);
|
|
|
|
|
2012-09-21 13:49:14 +02:00
|
|
|
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-02 09:07:47 +01:00
|
|
|
Set("slave_services", newServices);
|
2012-09-21 13:49:14 +02:00
|
|
|
}
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
void Host::OnAttributeChanged(const String& name)
|
2013-01-24 13:21:35 +01:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-01-24 13:21:35 +01:00
|
|
|
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-02 09:07:47 +01:00
|
|
|
UpdateSlaveServices();
|
2013-02-19 23:02:08 +01:00
|
|
|
} else if (name == "notifications") {
|
2013-03-04 15:52:42 +01:00
|
|
|
BOOST_FOREACH(const Service::Ptr& service, GetServices()) {
|
|
|
|
service->UpdateSlaveNotifications();
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_ServiceMutex);
|
2013-01-24 15:10:17 +01:00
|
|
|
|
2013-02-08 15:38:22 +01:00
|
|
|
Service::WeakPtr wservice;
|
2013-02-13 18:43:56 +01:00
|
|
|
BOOST_FOREACH(tie(tuples::ignore, wservice), m_ServicesCache[GetName()]) {
|
2013-02-08 15:38:22 +01:00
|
|
|
Service::Ptr service = wservice.lock();
|
2013-01-25 12:46:49 +01:00
|
|
|
|
|
|
|
if (!service)
|
2013-01-24 15:10:17 +01:00
|
|
|
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
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
if (m_ServicesCacheNeedsUpdate)
|
|
|
|
return; /* Someone else has already requested a refresh. */
|
2013-02-27 16:04:49 +01:00
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
if (!m_ServicesCacheTimer) {
|
|
|
|
m_ServicesCacheTimer = boost::make_shared<Timer>();
|
|
|
|
m_ServicesCacheTimer->SetInterval(0.5);
|
|
|
|
m_ServicesCacheTimer->OnTimerExpired.connect(boost::bind(&Host::RefreshServicesCache));
|
2013-03-06 13:01:51 +01:00
|
|
|
m_ServicesCacheTimer->Start();
|
2013-03-06 11:03:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_ServicesCacheNeedsUpdate = true;
|
2013-02-27 15:23:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
void Host::RefreshServicesCache(void)
|
2013-01-24 15:10:17 +01:00
|
|
|
{
|
2013-02-27 15:23:25 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_ServiceMutex);
|
|
|
|
|
2013-03-06 13:01:51 +01:00
|
|
|
if (!m_ServicesCacheNeedsUpdate)
|
|
|
|
return;
|
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
m_ServicesCacheNeedsUpdate = false;
|
2013-02-27 15:23:25 +01:00
|
|
|
}
|
|
|
|
|
2013-03-15 11:18:56 +01:00
|
|
|
Logger::Write(LogDebug, "icinga", "Updating Host services cache.");
|
2013-03-06 11:03:50 +01:00
|
|
|
|
2013-02-27 21:49:03 +01:00
|
|
|
map<String, map<String, Service::WeakPtr> > newServicesCache;
|
2013-01-24 15:10:17 +01:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
|
2013-01-24 15:10:17 +01:00
|
|
|
const Service::Ptr& service = static_pointer_cast<Service>(object);
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
Host::Ptr host = service->GetHost();
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-02-27 16:04:49 +01:00
|
|
|
if (!host)
|
|
|
|
continue;
|
|
|
|
|
2013-02-08 15:38:22 +01:00
|
|
|
// TODO: assert for duplicate short_names
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
newServicesCache[host->GetName()][service->GetShortName()] = service;
|
2013-01-24 15:10:17 +01:00
|
|
|
}
|
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_ServiceMutex);
|
|
|
|
m_ServicesCache.swap(newServicesCache);
|
2013-01-24 15:10:17 +01:00
|
|
|
}
|
|
|
|
|
2013-02-06 12:09:50 +01:00
|
|
|
void Host::ValidateServiceDictionary(const ScriptTask::Ptr& task, const vector<Value>& arguments)
|
2013-02-06 00:32:05 +01:00
|
|
|
{
|
|
|
|
if (arguments.size() < 1)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(invalid_argument("Missing argument: Location must be specified."));
|
2013-02-06 00:32:05 +01:00
|
|
|
|
|
|
|
if (arguments.size() < 2)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(invalid_argument("Missing argument: Attribute dictionary must be specified."));
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-02-06 12:09:50 +01:00
|
|
|
String location = arguments[0];
|
2013-02-06 00:32:05 +01:00
|
|
|
Dictionary::Ptr attrs = arguments[1];
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(attrs);
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-02-06 12:09:50 +01:00
|
|
|
String key;
|
|
|
|
Value value;
|
|
|
|
BOOST_FOREACH(tie(key, value), attrs) {
|
2013-03-08 14:44:03 +01:00
|
|
|
vector<String> templates;
|
2013-02-06 12:09:50 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
if (!value.IsObjectType<Dictionary>())
|
|
|
|
BOOST_THROW_EXCEPTION(invalid_argument("Service description must be a dictionary."));
|
|
|
|
|
|
|
|
Dictionary::Ptr serviceDesc = value;
|
2013-02-06 12:09:50 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
Array::Ptr templatesArray = serviceDesc->Get("templates");
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
if (templatesArray) {
|
|
|
|
ObjectLock tlock(templatesArray);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Value& tmpl, templatesArray) {
|
2013-03-08 14:44:03 +01:00
|
|
|
templates.push_back(tmpl);
|
|
|
|
}
|
2013-02-06 12:09:50 +01:00
|
|
|
}
|
|
|
|
|
2013-03-08 14:44:03 +01:00
|
|
|
BOOST_FOREACH(const String& name, templates) {
|
|
|
|
ConfigItem::Ptr item;
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-03-08 14:44:03 +01:00
|
|
|
ConfigCompilerContext *context = ConfigCompilerContext::GetContext();
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-03-08 14:44:03 +01:00
|
|
|
if (context)
|
|
|
|
item = context->GetItem("Service", name);
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-03-08 14:44:03 +01:00
|
|
|
/* 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);
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-03-08 14:44:03 +01:00
|
|
|
if (!item) {
|
|
|
|
ConfigCompilerContext::GetContext()->AddError(false, "Validation failed for " +
|
|
|
|
location + ": Template '" + name + "' not found.");
|
|
|
|
}
|
2013-02-06 12:09:50 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-02-06 12:09:50 +01:00
|
|
|
task->FinishResult(Empty);
|
2013-02-06 00:32:05 +01:00
|
|
|
}
|
2013-02-07 20:29:35 +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()) {
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_ServiceMutex);
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
map<String, Service::WeakPtr>& services = m_ServicesCache[GetName()];
|
2013-02-27 21:49:03 +01:00
|
|
|
map<String, Service::WeakPtr>::iterator it = services.find(name);
|
2013-02-08 15:38:22 +01:00
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
if (it != services.end()) {
|
|
|
|
Service::Ptr service = it->second.lock();
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(service);
|
2013-02-26 10:13:54 +01:00
|
|
|
return service;
|
|
|
|
}
|
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-07 16:00:10 +01:00
|
|
|
ASSERT(dict->IsSealed());
|
2013-02-27 15:23:25 +01:00
|
|
|
|
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 {
|
|
|
|
BOOST_THROW_EXCEPTION(invalid_argument("Host/Service name pair is invalid."));
|
|
|
|
}
|
2013-02-07 20:29:35 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
set<Host::Ptr> Host::GetParentHosts(void) const
|
2013-02-07 20:29:35 +01:00
|
|
|
{
|
|
|
|
set<Host::Ptr> parents;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (!host)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
parents.insert(host);
|
2013-02-07 20:29:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parents;
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Service::Ptr Host::GetHostCheckService(void) const
|
2013-02-07 20:29:35 +01:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
String host_check = GetHostCheck();
|
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-02 09:07:47 +01:00
|
|
|
set<Service::Ptr> Host::GetParentServices(void) const
|
2013-02-07 20:29:35 +01:00
|
|
|
{
|
|
|
|
set<Service::Ptr> parents;
|
|
|
|
|
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-07 12:04:20 +01:00
|
|
|
HostState Host::GetState(void) const
|
|
|
|
{
|
|
|
|
if (!IsReachable())
|
|
|
|
return HostUnreachable;
|
|
|
|
|
|
|
|
Service::Ptr hc = GetHostCheckService();
|
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return HostUp;
|
|
|
|
|
|
|
|
switch (hc->GetState()) {
|
|
|
|
case StateOK:
|
|
|
|
case StateWarning:
|
|
|
|
return HostUp;
|
|
|
|
default:
|
|
|
|
return HostDown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StateType Host::GetStateType(void) const
|
|
|
|
{
|
|
|
|
Service::Ptr hc = GetHostCheckService();
|
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return StateTypeHard;
|
|
|
|
|
|
|
|
return hc->GetStateType();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
Service::Ptr hc = GetHostCheckService();
|
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return HostUp;
|
|
|
|
|
|
|
|
switch (hc->GetLastState()) {
|
|
|
|
case StateOK:
|
|
|
|
case StateWarning:
|
|
|
|
return HostUp;
|
|
|
|
default:
|
|
|
|
return HostDown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StateType Host::GetLastStateType(void) const
|
|
|
|
{
|
|
|
|
Service::Ptr hc = GetHostCheckService();
|
|
|
|
|
|
|
|
if (!hc)
|
|
|
|
return StateTypeHard;
|
|
|
|
|
|
|
|
return hc->GetLastStateType();
|
|
|
|
}
|
|
|
|
|
|
|
|
String Host::HostStateToString(HostState state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case HostUp:
|
|
|
|
return "UP";
|
|
|
|
case HostDown:
|
|
|
|
return "DOWN";
|
|
|
|
case HostUnreachable:
|
|
|
|
return "UNREACHABLE";
|
|
|
|
default:
|
|
|
|
return "INVALID";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Dictionary::Ptr Host::CalculateDynamicMacros(void) const
|
2013-02-09 18:39:43 +01:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-02-09 18:39:43 +01:00
|
|
|
Dictionary::Ptr macros = boost::make_shared<Dictionary>();
|
|
|
|
|
2013-03-07 12:04:20 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
macros->Set("HOSTNAME", GetName());
|
|
|
|
macros->Set("HOSTDISPLAYNAME", GetDisplayName());
|
|
|
|
macros->Set("HOSTALIAS", GetName());
|
2013-03-07 15:00:26 +01:00
|
|
|
}
|
2013-02-24 01:10:34 +01:00
|
|
|
|
|
|
|
Dictionary::Ptr cr;
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Service::Ptr hc = GetHostCheckService();
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
if (hc) {
|
|
|
|
ObjectLock olock(hc);
|
|
|
|
|
2013-03-07 15:00:26 +01:00
|
|
|
macros->Set("HOSTSTATE", HostStateToString(GetState()));
|
|
|
|
macros->Set("HOSTSTATEID", GetState());
|
2013-02-24 01:10:34 +01:00
|
|
|
macros->Set("HOSTSTATETYPE", Service::StateTypeToString(hc->GetStateType()));
|
|
|
|
macros->Set("HOSTATTEMPT", hc->GetCurrentCheckAttempt());
|
|
|
|
macros->Set("MAXHOSTATTEMPT", hc->GetMaxCheckAttempts());
|
2013-02-09 18:39:43 +01:00
|
|
|
|
2013-03-07 15:00:26 +01:00
|
|
|
macros->Set("LASTHOSTSTATE", HostStateToString(GetLastState()));
|
|
|
|
macros->Set("LASTHOSTSTATEID", GetLastState());
|
|
|
|
macros->Set("LASTHOSTSTATETYPE", Service::StateTypeToString(GetLastStateType()));
|
|
|
|
macros->Set("LASTHOSTSTATECHANGE", (long)hc->GetLastStateChange());
|
2013-03-07 12:04:20 +01:00
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
cr = hc->GetLastCheckResult();
|
2013-02-09 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
if (cr) {
|
|
|
|
macros->Set("HOSTLATENCY", Service::CalculateLatency(cr));
|
|
|
|
macros->Set("HOSTEXECUTIONTIME", Service::CalculateExecutionTime(cr));
|
|
|
|
|
|
|
|
macros->Set("HOSTOUTPUT", cr->Get("output"));
|
|
|
|
macros->Set("HOSTPERFDATA", cr->Get("performance_data_raw"));
|
2013-03-07 12:04:20 +01:00
|
|
|
|
2013-03-07 15:00:26 +01:00
|
|
|
macros->Set("LASTHOSTCHECK", (long)cr->Get("schedule_start"));
|
2013-02-24 01:10:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
macros->Seal();
|
|
|
|
|
2013-02-09 18:39:43 +01:00
|
|
|
return macros;
|
|
|
|
}
|