2014-03-18 11:44:09 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2014-03-18 11:44:09 +01: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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "config/configitembuilder.hpp"
|
|
|
|
#include "config/applyrule.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
|
|
|
#include "base/dynamictype.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/context.hpp"
|
|
|
|
#include "base/workqueue.hpp"
|
2014-12-18 15:11:57 +01:00
|
|
|
#include "base/exception.hpp"
|
2014-03-18 11:44:09 +01:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-03-28 18:26:46 +01:00
|
|
|
INITIALIZE_ONCE(&Service::RegisterApplyRuleHandler);
|
2014-03-18 11:44:09 +01:00
|
|
|
|
2014-03-28 18:26:46 +01:00
|
|
|
void Service::RegisterApplyRuleHandler(void)
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
2014-04-05 12:56:56 +02:00
|
|
|
std::vector<String> targets;
|
|
|
|
targets.push_back("Host");
|
2014-11-16 16:20:39 +01:00
|
|
|
ApplyRule::RegisterType("Service", targets);
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|
|
|
|
|
2015-01-29 12:38:25 +01:00
|
|
|
bool Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& name, ScriptFrame& frame, const ApplyRule& rule)
|
2014-11-04 11:01:00 +01:00
|
|
|
{
|
2015-01-29 12:38:25 +01:00
|
|
|
if (!rule.EvaluateFilter(frame))
|
|
|
|
return false;
|
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
DebugInfo di = rule.GetDebugInfo();
|
|
|
|
|
|
|
|
Log(LogDebug, "Service")
|
|
|
|
<< "Applying service '" << name << "' to host '" << host->GetName() << "' for rule " << di;
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
|
2014-11-04 11:01:00 +01:00
|
|
|
builder->SetType("Service");
|
|
|
|
builder->SetName(name);
|
2014-11-22 12:21:28 +01:00
|
|
|
builder->SetScope(frame.Locals);
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2014-12-14 11:33:45 +01:00
|
|
|
builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2014-12-14 11:33:45 +01:00
|
|
|
builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "name"), OpSetLiteral, MakeLiteral(name), di));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2015-02-09 08:50:17 +01:00
|
|
|
String zone = host->GetZoneName();
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (!zone.IsEmpty())
|
2014-12-14 11:33:45 +01:00
|
|
|
builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "zone"), OpSetLiteral, MakeLiteral(zone), di));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
builder->AddExpression(new OwnedExpression(rule.GetExpression()));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
|
|
|
ConfigItem::Ptr serviceItem = builder->Compile();
|
2015-03-19 15:47:46 +01:00
|
|
|
serviceItem->Register();
|
2015-01-29 12:38:25 +01:00
|
|
|
|
|
|
|
return true;
|
2014-11-04 11:01:00 +01:00
|
|
|
}
|
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule)
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
2014-04-05 12:56:56 +02:00
|
|
|
DebugInfo di = rule.GetDebugInfo();
|
2014-03-18 11:44:09 +01:00
|
|
|
|
2014-04-05 12:56:56 +02:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "Evaluating 'apply' rule (" << di << ")";
|
|
|
|
CONTEXT(msgbuf.str());
|
2014-03-18 15:29:04 +01:00
|
|
|
|
2014-12-12 15:33:02 +01:00
|
|
|
ScriptFrame frame;
|
2014-11-22 12:21:28 +01:00
|
|
|
if (rule.GetScope())
|
|
|
|
rule.GetScope()->CopyTo(frame.Locals);
|
|
|
|
frame.Locals->Set("host", host);
|
2014-03-27 12:30:24 +01:00
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
Value vinstances;
|
2014-03-18 11:44:09 +01:00
|
|
|
|
2014-11-02 07:22:00 +01:00
|
|
|
if (rule.GetFTerm()) {
|
2015-01-29 12:38:25 +01:00
|
|
|
try {
|
|
|
|
vinstances = rule.GetFTerm()->Evaluate(frame);
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
/* Silently ignore errors here and assume there are no instances. */
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-02 07:22:00 +01:00
|
|
|
} else {
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr instances = new Array();
|
2014-11-02 07:22:00 +01:00
|
|
|
instances->Add("");
|
2014-11-04 11:01:00 +01:00
|
|
|
vinstances = instances;
|
2014-11-02 07:22:00 +01:00
|
|
|
}
|
|
|
|
|
2015-01-29 12:38:25 +01:00
|
|
|
bool match = false;
|
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
if (vinstances.IsObjectType<Array>()) {
|
|
|
|
if (!rule.GetFVVar().IsEmpty())
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
|
2014-05-09 10:33:22 +02:00
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
Array::Ptr arr = vinstances;
|
2015-01-29 12:38:25 +01:00
|
|
|
Array::Ptr arrclone = arr->ShallowClone();
|
2014-05-09 10:33:22 +02:00
|
|
|
|
2015-01-29 12:38:25 +01:00
|
|
|
ObjectLock olock(arrclone);
|
2015-02-24 17:51:16 +01:00
|
|
|
BOOST_FOREACH(const Value& instance, arrclone) {
|
2014-11-04 11:01:00 +01:00
|
|
|
String name = rule.GetName();
|
2014-11-02 07:22:00 +01:00
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
if (!rule.GetFKVar().IsEmpty()) {
|
2014-11-22 12:21:28 +01:00
|
|
|
frame.Locals->Set(rule.GetFKVar(), instance);
|
2014-11-04 11:01:00 +01:00
|
|
|
name += instance;
|
|
|
|
}
|
2014-11-02 07:22:00 +01:00
|
|
|
|
2015-01-29 12:38:25 +01:00
|
|
|
if (EvaluateApplyRuleInstance(host, name, frame, rule))
|
|
|
|
match = true;
|
2014-11-04 11:01:00 +01:00
|
|
|
}
|
|
|
|
} else if (vinstances.IsObjectType<Dictionary>()) {
|
|
|
|
if (rule.GetFVVar().IsEmpty())
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", di));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
|
|
|
Dictionary::Ptr dict = vinstances;
|
|
|
|
|
2015-01-29 12:38:25 +01:00
|
|
|
BOOST_FOREACH(const String& key, dict->GetKeys()) {
|
|
|
|
frame.Locals->Set(rule.GetFKVar(), key);
|
|
|
|
frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2015-01-29 12:38:25 +01:00
|
|
|
if (EvaluateApplyRuleInstance(host, rule.GetName() + key, frame, rule))
|
|
|
|
match = true;
|
2014-11-02 07:22:00 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-07 13:23:27 +02:00
|
|
|
|
2015-01-29 12:38:25 +01:00
|
|
|
return match;
|
2014-04-05 12:56:56 +02:00
|
|
|
}
|
2014-03-30 10:00:11 +02:00
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
void Service::EvaluateApplyRules(const Host::Ptr& host)
|
2014-04-05 12:56:56 +02:00
|
|
|
{
|
2014-11-16 16:20:39 +01:00
|
|
|
BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Service")) {
|
2014-05-17 20:13:25 +02:00
|
|
|
CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
|
2014-04-07 13:23:27 +02:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
if (EvaluateApplyRule(host, rule))
|
|
|
|
rule.AddMatch();
|
2014-05-17 20:13:25 +02:00
|
|
|
}
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|