2012-07-06 11:22:38 +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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "i2-convenience.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the component.
|
|
|
|
*
|
|
|
|
* @returns The name.
|
|
|
|
*/
|
|
|
|
string ConvenienceComponent::GetName(void) const
|
|
|
|
{
|
|
|
|
return "convenience";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the component.
|
|
|
|
*/
|
|
|
|
void ConvenienceComponent::Start(void)
|
|
|
|
{
|
|
|
|
ConfigItem::Set::Ptr itemSet = ConfigItem::GetAllObjects();
|
|
|
|
itemSet->OnObjectAdded.connect(boost::bind(&ConvenienceComponent::HostAddedHandler, this, _2));
|
|
|
|
itemSet->OnObjectCommitted.connect(boost::bind(&ConvenienceComponent::HostCommittedHandler, this, _2));
|
|
|
|
itemSet->OnObjectRemoved.connect(boost::bind(&ConvenienceComponent::HostRemovedHandler, this, _2));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the component.
|
|
|
|
*/
|
|
|
|
void ConvenienceComponent::Stop(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvenienceComponent::HostAddedHandler(const ConfigItem::Ptr& item)
|
|
|
|
{
|
|
|
|
HostCommittedHandler(item);
|
|
|
|
}
|
|
|
|
|
2012-07-09 10:09:53 +02:00
|
|
|
void ConvenienceComponent::CopyServiceAttributes(const ConfigObject::Ptr& host, const Dictionary::Ptr& service, const ConfigItemBuilder::Ptr& builder)
|
2012-07-08 10:20:54 +02:00
|
|
|
{
|
2012-07-13 11:40:57 +02:00
|
|
|
/* 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. */
|
|
|
|
Dictionary::Ptr macros;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (service->Get("macros", ¯os))
|
2012-07-08 10:20:54 +02:00
|
|
|
builder->AddExpression("macros", OperatorPlus, macros);
|
|
|
|
|
|
|
|
long checkInterval;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (service->Get("check_interval", &checkInterval))
|
2012-07-08 10:20:54 +02:00
|
|
|
builder->AddExpression("check_interval", OperatorSet, checkInterval);
|
|
|
|
|
|
|
|
long retryInterval;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (service->Get("retry_interval", &retryInterval))
|
2012-07-08 10:20:54 +02:00
|
|
|
builder->AddExpression("retry_interval", OperatorSet, retryInterval);
|
|
|
|
|
|
|
|
Dictionary::Ptr sgroups;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (service->Get("servicegroups", &sgroups))
|
2012-07-08 10:20:54 +02:00
|
|
|
builder->AddExpression("servicegroups", OperatorPlus, sgroups);
|
|
|
|
|
|
|
|
Dictionary::Ptr checkers;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (service->Get("checkers", &checkers))
|
2012-07-08 10:20:54 +02:00
|
|
|
builder->AddExpression("checkers", OperatorSet, checkers);
|
2012-07-09 10:28:49 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr dependencies;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (service->Get("dependencies", &dependencies))
|
2012-07-09 10:28:49 +02:00
|
|
|
builder->AddExpression("dependencies", OperatorPlus,
|
|
|
|
Service::ResolveDependencies(host, dependencies));
|
2012-07-09 12:44:31 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr hostchecks;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (service->Get("hostchecks", &hostchecks))
|
2012-07-09 12:44:31 +02:00
|
|
|
builder->AddExpression("dependencies", OperatorPlus,
|
|
|
|
Service::ResolveDependencies(host, hostchecks));
|
2012-07-08 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
void ConvenienceComponent::HostCommittedHandler(const ConfigItem::Ptr& item)
|
|
|
|
{
|
|
|
|
if (item->GetType() != "host")
|
|
|
|
return;
|
|
|
|
|
|
|
|
ConfigObject::Ptr host = ConfigObject::GetObject("host", item->GetName());
|
2012-07-06 15:52:13 +02:00
|
|
|
|
|
|
|
/* ignore abstract host objects */
|
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
Dictionary::Ptr oldServices;
|
|
|
|
host->GetTag("convenience-services", &oldServices);
|
|
|
|
|
|
|
|
Dictionary::Ptr newServices;
|
|
|
|
newServices = boost::make_shared<Dictionary>();
|
|
|
|
|
|
|
|
Dictionary::Ptr serviceDescs;
|
|
|
|
host->GetProperty("services", &serviceDescs);
|
|
|
|
|
|
|
|
if (serviceDescs) {
|
|
|
|
Dictionary::Iterator it;
|
|
|
|
for (it = serviceDescs->Begin(); it != serviceDescs->End(); it++) {
|
2012-07-08 10:20:54 +02:00
|
|
|
string svcname = it->first;
|
|
|
|
Variant svcdesc = it->second;
|
2012-07-06 14:33:10 +02:00
|
|
|
|
2012-07-08 10:20:54 +02:00
|
|
|
stringstream namebuf;
|
|
|
|
namebuf << item->GetName() << "-" << svcname;
|
|
|
|
string name = namebuf.str();
|
2012-07-06 11:22:38 +02:00
|
|
|
|
2012-07-08 10:20:54 +02:00
|
|
|
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);
|
2012-07-06 11:22:38 +02:00
|
|
|
|
2012-07-09 10:09:53 +02:00
|
|
|
CopyServiceAttributes(host, host->GetProperties(), builder);
|
2012-07-06 11:22:38 +02:00
|
|
|
|
2012-07-11 20:55:46 +02:00
|
|
|
if (svcdesc.IsScalar()) {
|
2012-07-08 10:20:54 +02:00
|
|
|
builder->AddParent(svcdesc);
|
2012-07-11 20:55:46 +02:00
|
|
|
} else if (svcdesc.IsObjectType<Dictionary>()) {
|
|
|
|
Dictionary::Ptr service = svcdesc;
|
2012-07-06 11:22:38 +02:00
|
|
|
|
2012-07-08 10:20:54 +02:00
|
|
|
string parent;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (!service->Get("service", &parent))
|
2012-07-08 11:37:15 +02:00
|
|
|
parent = svcname;
|
2012-07-06 11:22:38 +02:00
|
|
|
|
2012-07-08 10:20:54 +02:00
|
|
|
builder->AddParent(parent);
|
2012-07-06 11:22:38 +02:00
|
|
|
|
2012-07-09 10:09:53 +02:00
|
|
|
CopyServiceAttributes(host, service, builder);
|
2012-07-06 11:22:38 +02:00
|
|
|
} else {
|
2012-07-08 10:20:54 +02:00
|
|
|
throw invalid_argument("Service description must be either a string or a dictionary.");
|
2012-07-06 11:22:38 +02:00
|
|
|
}
|
2012-07-08 10:20:54 +02:00
|
|
|
|
|
|
|
ConfigItem::Ptr serviceItem = builder->Compile();
|
|
|
|
serviceItem->Commit();
|
|
|
|
|
2012-07-09 16:19:56 +02:00
|
|
|
newServices->Set(name, serviceItem);
|
2012-07-06 11:22:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldServices) {
|
|
|
|
Dictionary::Iterator it;
|
|
|
|
for (it = oldServices->Begin(); it != oldServices->End(); it++) {
|
2012-07-11 20:55:46 +02:00
|
|
|
ConfigItem::Ptr service = it->second;
|
2012-07-06 11:22:38 +02:00
|
|
|
|
|
|
|
if (!newServices->Contains(service->GetName()))
|
|
|
|
service->Unregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-06 14:36:58 +02:00
|
|
|
host->SetTag("convenience-services", newServices);
|
2012-07-06 11:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvenienceComponent::HostRemovedHandler(const ConfigItem::Ptr& item)
|
|
|
|
{
|
|
|
|
if (item->GetType() != "host")
|
|
|
|
return;
|
|
|
|
|
|
|
|
ConfigObject::Ptr host = item->GetConfigObject();
|
|
|
|
|
2012-07-08 11:37:15 +02:00
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
Dictionary::Ptr services;
|
|
|
|
host->GetTag("convenience-services", &services);
|
|
|
|
|
|
|
|
if (!services)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Dictionary::Iterator it;
|
|
|
|
for (it = services->Begin(); it != services->End(); it++) {
|
2012-07-11 20:55:46 +02:00
|
|
|
ConfigItem::Ptr service = it->second;
|
2012-07-06 11:22:38 +02:00
|
|
|
service->Unregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_COMPONENT(convenience, ConvenienceComponent);
|