mirror of https://github.com/Icinga/icinga2.git
parent
eadb309dab
commit
e047e06fc8
|
@ -352,29 +352,34 @@ Example:
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
type Pizza {
|
type Pizza {
|
||||||
number radius,
|
%require "radius",
|
||||||
|
%attribute number "radius",
|
||||||
|
|
||||||
dictionary ingredients {
|
%attribute dictionary "ingredients" {
|
||||||
string *,
|
%validator "native::ValidateIngredients",
|
||||||
|
|
||||||
dictionary * {
|
%attribute string "*",
|
||||||
number quantity,
|
|
||||||
string name
|
%attribute dictionary "*" {
|
||||||
|
%attribute number "quantity",
|
||||||
|
%attribute string "name"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
any custom::*
|
%attribute any "custom::*"
|
||||||
}
|
}
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
The Pizza definition provides the following validation rules:
|
The Pizza definition provides the following validation rules:
|
||||||
|
|
||||||
* Pizza objects may contain an attribute "radius" which has to be a number.
|
* Pizza objects must contain an attribute "radius" which has to be a number.
|
||||||
* Pizza objects may contain an attribute "ingredients" which has to be a
|
* Pizza objects may contain an attribute "ingredients" which has to be a
|
||||||
dictionary.
|
dictionary.
|
||||||
* Elements in the ingredients dictionary can be either a string or a dictionary.
|
* Elements in the ingredients dictionary can be either a string or a dictionary.
|
||||||
* If they're a dictionary they may contain attributes "quantity" (of type
|
* If they're a dictionary they may contain attributes "quantity" (of type
|
||||||
number) and "name" (of type string).
|
number) and "name" (of type string).
|
||||||
|
* The script function "native::ValidateIngredients" is run to perform further
|
||||||
|
validation of the ingredients dictionary.
|
||||||
* Pizza objects may contain attribute matching the pattern "custom::*" of any
|
* Pizza objects may contain attribute matching the pattern "custom::*" of any
|
||||||
type.
|
type.
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,29 @@ type Host {
|
||||||
%attribute string "*"
|
%attribute string "*"
|
||||||
},
|
},
|
||||||
%attribute dictionary "services" {
|
%attribute dictionary "services" {
|
||||||
%attribute any "*" /* TODO: more specific validation rules */
|
%validator "native::ValidateServiceDictionary",
|
||||||
|
|
||||||
|
%attribute string "*",
|
||||||
|
%attribute dictionary "*" {
|
||||||
|
%attribute string "service",
|
||||||
|
|
||||||
|
%attribute dictionary "macros" {
|
||||||
|
%attribute string "*"
|
||||||
|
},
|
||||||
|
|
||||||
|
%attribute number "check_interval",
|
||||||
|
%attribute number "retry_interval",
|
||||||
|
|
||||||
|
%attribute dictionary "servicegroups" {
|
||||||
|
%attribute string "*"
|
||||||
|
},
|
||||||
|
%attribute dictionary "checkers" {
|
||||||
|
%attribute string "*"
|
||||||
|
},
|
||||||
|
%attribute dictionary "dependencies" {
|
||||||
|
%attribute string "*"
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/* service attributes */
|
/* service attributes */
|
||||||
|
|
|
@ -507,7 +507,17 @@ bool Utility::Glob(const String& pathSpec, const function<void (const String&)>&
|
||||||
*/
|
*/
|
||||||
void Utility::WaitUntil(const function<bool (void)>& predicate)
|
void Utility::WaitUntil(const function<bool (void)>& predicate)
|
||||||
{
|
{
|
||||||
while (!predicate())
|
while (!predicate()) {
|
||||||
Application::GetInstance()->ProcessEvents();
|
Application::Ptr instance = Application::GetInstance();
|
||||||
|
|
||||||
|
/* Waiting for a predicate requires an application instance.
|
||||||
|
* This means we cannot do certain asynchronous things
|
||||||
|
* (like spawning a process) until the application instance
|
||||||
|
* has been initialized. */
|
||||||
|
if (!instance)
|
||||||
|
throw_exception(runtime_error("Waiting for predicate failed: Application instance is not initialized."));
|
||||||
|
|
||||||
|
instance->ProcessEvents();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,148 +1,168 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* Icinga 2 *
|
||||||
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
||||||
* *
|
* *
|
||||||
* This program is free software; you can redistribute it and/or *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
* as published by the Free Software Foundation; either version 2 *
|
* as published by the Free Software Foundation; either version 2 *
|
||||||
* of the License, or (at your option) any later version. *
|
* of the License, or (at your option) any later version. *
|
||||||
* *
|
* *
|
||||||
* This program is distributed in the hope that it will be useful, *
|
* This program is distributed in the hope that it will be useful, *
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
* GNU General Public License for more details. *
|
* GNU General Public License for more details. *
|
||||||
* *
|
* *
|
||||||
* You should have received a copy of the GNU General Public License *
|
* You should have received a copy of the GNU General Public License *
|
||||||
* along with this program; if not, write to the Free Software Foundation *
|
* along with this program; if not, write to the Free Software Foundation *
|
||||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
#include "i2-config.h"
|
#include "i2-config.h"
|
||||||
|
|
||||||
using namespace icinga;
|
using namespace icinga;
|
||||||
|
|
||||||
ConfigType::ConfigType(const String& name, const DebugInfo& debuginfo)
|
ConfigType::ConfigType(const String& name, const DebugInfo& debuginfo)
|
||||||
: m_Name(name), m_RuleList(boost::make_shared<TypeRuleList>()), m_DebugInfo(debuginfo)
|
: m_Name(name), m_RuleList(boost::make_shared<TypeRuleList>()), m_DebugInfo(debuginfo)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
String ConfigType::GetName(void) const
|
String ConfigType::GetName(void) const
|
||||||
{
|
{
|
||||||
return m_Name;
|
return m_Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
String ConfigType::GetParent(void) const
|
String ConfigType::GetParent(void) const
|
||||||
{
|
{
|
||||||
return m_Parent;
|
return m_Parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigType::SetParent(const String& parent)
|
void ConfigType::SetParent(const String& parent)
|
||||||
{
|
{
|
||||||
m_Parent = parent;
|
m_Parent = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
TypeRuleList::Ptr ConfigType::GetRuleList(void) const
|
TypeRuleList::Ptr ConfigType::GetRuleList(void) const
|
||||||
{
|
{
|
||||||
return m_RuleList;
|
return m_RuleList;
|
||||||
}
|
}
|
||||||
|
|
||||||
DebugInfo ConfigType::GetDebugInfo(void) const
|
DebugInfo ConfigType::GetDebugInfo(void) const
|
||||||
{
|
{
|
||||||
return m_DebugInfo;
|
return m_DebugInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigType::ValidateItem(const ConfigItem::Ptr& item) const
|
void ConfigType::ValidateItem(const ConfigItem::Ptr& item) const
|
||||||
{
|
{
|
||||||
Dictionary::Ptr attrs = item->Link();
|
Dictionary::Ptr attrs = item->Link();
|
||||||
|
|
||||||
/* Don't validate abstract items. */
|
/* Don't validate abstract items. */
|
||||||
if (attrs->Get("__abstract"))
|
if (attrs->Get("__abstract"))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
vector<String> locations;
|
vector<String> locations;
|
||||||
locations.push_back("Object '" + item->GetName() + "' (Type: '" + item->GetType() + "')");
|
locations.push_back("Object '" + item->GetName() + "' (Type: '" + item->GetType() + "')");
|
||||||
|
|
||||||
ConfigType::Ptr parent;
|
ConfigType::Ptr parent;
|
||||||
if (m_Parent.IsEmpty()) {
|
if (m_Parent.IsEmpty()) {
|
||||||
if (GetName() != "DynamicObject")
|
if (GetName() != "DynamicObject")
|
||||||
parent = ConfigCompilerContext::GetContext()->GetType("DynamicObject");
|
parent = ConfigCompilerContext::GetContext()->GetType("DynamicObject");
|
||||||
} else {
|
} else {
|
||||||
parent = ConfigCompilerContext::GetContext()->GetType(m_Parent);
|
parent = ConfigCompilerContext::GetContext()->GetType(m_Parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<TypeRuleList::Ptr> ruleLists;
|
vector<TypeRuleList::Ptr> ruleLists;
|
||||||
if (parent)
|
if (parent)
|
||||||
ruleLists.push_back(parent->m_RuleList);
|
ruleLists.push_back(parent->m_RuleList);
|
||||||
|
|
||||||
ruleLists.push_back(m_RuleList);
|
ruleLists.push_back(m_RuleList);
|
||||||
|
|
||||||
ValidateDictionary(attrs, ruleLists, locations);
|
ValidateDictionary(attrs, ruleLists, locations);
|
||||||
}
|
}
|
||||||
|
|
||||||
String ConfigType::LocationToString(const vector<String>& locations)
|
String ConfigType::LocationToString(const vector<String>& locations)
|
||||||
{
|
{
|
||||||
bool first = true;
|
bool first = true;
|
||||||
String stack;
|
String stack;
|
||||||
BOOST_FOREACH(const String& location, locations) {
|
BOOST_FOREACH(const String& location, locations) {
|
||||||
if (!first)
|
if (!first)
|
||||||
stack += " -> ";
|
stack += " -> ";
|
||||||
else
|
else
|
||||||
first = false;
|
first = false;
|
||||||
|
|
||||||
stack += location;
|
stack += location;
|
||||||
}
|
}
|
||||||
|
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigType::ValidateDictionary(const Dictionary::Ptr& dictionary,
|
void ConfigType::ValidateDictionary(const Dictionary::Ptr& dictionary,
|
||||||
const vector<TypeRuleList::Ptr>& ruleLists, vector<String>& locations)
|
const vector<TypeRuleList::Ptr>& ruleLists, vector<String>& locations)
|
||||||
{
|
{
|
||||||
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
|
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
|
||||||
BOOST_FOREACH(const String& require, ruleList->GetRequires()) {
|
BOOST_FOREACH(const String& require, ruleList->GetRequires()) {
|
||||||
locations.push_back("Attribute '" + require + "'");
|
locations.push_back("Attribute '" + require + "'");
|
||||||
|
|
||||||
Value value = dictionary->Get(require);
|
Value value = dictionary->Get(require);
|
||||||
|
|
||||||
if (value.IsEmpty())
|
if (value.IsEmpty()) {
|
||||||
ConfigCompilerContext::GetContext()->AddError(false, "Required attribute is missing: " + LocationToString(locations));
|
ConfigCompilerContext::GetContext()->AddError(false,
|
||||||
|
"Required attribute is missing: " + LocationToString(locations));
|
||||||
locations.pop_back();
|
}
|
||||||
}
|
|
||||||
}
|
locations.pop_back();
|
||||||
|
}
|
||||||
String key;
|
|
||||||
Value value;
|
String validator = ruleList->GetValidator();
|
||||||
BOOST_FOREACH(tie(key, value), dictionary) {
|
|
||||||
TypeValidationResult overallResult = ValidationUnknownField;
|
if (!validator.IsEmpty()) {
|
||||||
vector<TypeRuleList::Ptr> subRuleLists;
|
ScriptFunction::Ptr func = ScriptFunction::GetByName(validator);
|
||||||
|
|
||||||
locations.push_back("Attribute '" + key + "'");
|
if (!func)
|
||||||
|
throw_exception(invalid_argument("Validator function '" + validator + "' does not exist."));
|
||||||
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
|
|
||||||
TypeRuleList::Ptr subRuleList;
|
vector<Value> arguments;
|
||||||
TypeValidationResult result = ruleList->ValidateAttribute(key, value, &subRuleList);
|
arguments.push_back(LocationToString(locations));
|
||||||
|
arguments.push_back(dictionary);
|
||||||
if (subRuleList)
|
|
||||||
subRuleLists.push_back(subRuleList);
|
ScriptTask::Ptr task = boost::make_shared<ScriptTask>(func, arguments);
|
||||||
|
task->Start();
|
||||||
if (result == ValidationOK) {
|
task->Wait();
|
||||||
overallResult = result;
|
task->GetResult();
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == ValidationInvalidType)
|
String key;
|
||||||
overallResult = result;
|
Value value;
|
||||||
}
|
BOOST_FOREACH(tie(key, value), dictionary) {
|
||||||
|
TypeValidationResult overallResult = ValidationUnknownField;
|
||||||
if (overallResult == ValidationUnknownField)
|
vector<TypeRuleList::Ptr> subRuleLists;
|
||||||
ConfigCompilerContext::GetContext()->AddError(true, "Unknown attribute: " + LocationToString(locations));
|
|
||||||
else if (overallResult == ValidationInvalidType)
|
locations.push_back("Attribute '" + key + "'");
|
||||||
ConfigCompilerContext::GetContext()->AddError(false, "Invalid type for attribute: " + LocationToString(locations));
|
|
||||||
|
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
|
||||||
if (subRuleLists.size() > 0 && value.IsObjectType<Dictionary>())
|
TypeRuleList::Ptr subRuleList;
|
||||||
ValidateDictionary(value, subRuleLists, locations);
|
TypeValidationResult result = ruleList->ValidateAttribute(key, value, &subRuleList);
|
||||||
|
|
||||||
locations.pop_back();
|
if (subRuleList)
|
||||||
}
|
subRuleLists.push_back(subRuleList);
|
||||||
}
|
|
||||||
|
if (result == ValidationOK) {
|
||||||
|
overallResult = result;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == ValidationInvalidType)
|
||||||
|
overallResult = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (overallResult == ValidationUnknownField)
|
||||||
|
ConfigCompilerContext::GetContext()->AddError(true, "Unknown attribute: " + LocationToString(locations));
|
||||||
|
else if (overallResult == ValidationInvalidType)
|
||||||
|
ConfigCompilerContext::GetContext()->AddError(false, "Invalid type for attribute: " + LocationToString(locations));
|
||||||
|
|
||||||
|
if (subRuleLists.size() > 0 && value.IsObjectType<Dictionary>())
|
||||||
|
ValidateDictionary(value, subRuleLists, locations);
|
||||||
|
|
||||||
|
locations.pop_back();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ using namespace icinga;
|
||||||
map<String, vector<Service::WeakPtr> > Host::m_ServicesCache;
|
map<String, vector<Service::WeakPtr> > Host::m_ServicesCache;
|
||||||
bool Host::m_ServicesCacheValid = true;
|
bool Host::m_ServicesCacheValid = true;
|
||||||
|
|
||||||
REGISTER_SCRIPTFUNCTION("native::ValidateHostItem", &Host::ValidateHostItem);
|
REGISTER_SCRIPTFUNCTION("native::ValidateServiceDictionary", &Host::ValidateServiceDictionary);
|
||||||
|
|
||||||
static AttributeDescription hostAttributes[] = {
|
static AttributeDescription hostAttributes[] = {
|
||||||
{ "acknowledgement", Attribute_Replicated },
|
{ "acknowledgement", Attribute_Replicated },
|
||||||
|
@ -407,18 +407,40 @@ void Host::ValidateServicesCache(void)
|
||||||
m_ServicesCacheValid = true;
|
m_ServicesCacheValid = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Host::ValidateHostItem(const ScriptTask::Ptr& task, const vector<Value>& arguments)
|
void Host::ValidateServiceDictionary(const ScriptTask::Ptr& task, const vector<Value>& arguments)
|
||||||
{
|
{
|
||||||
if (arguments.size() < 1)
|
if (arguments.size() < 1)
|
||||||
throw_exception(invalid_argument("Missing argument: Host config item must be specified."));
|
throw_exception(invalid_argument("Missing argument: Location must be specified."));
|
||||||
|
|
||||||
if (arguments.size() < 2)
|
if (arguments.size() < 2)
|
||||||
throw_exception(invalid_argument("Missing argument: Attribute dictionary must be specified."));
|
throw_exception(invalid_argument("Missing argument: Attribute dictionary must be specified."));
|
||||||
|
|
||||||
ConfigItem::Ptr item = arguments[0];
|
String location = arguments[0];
|
||||||
Dictionary::Ptr attrs = arguments[1];
|
Dictionary::Ptr attrs = arguments[1];
|
||||||
|
|
||||||
// TODO: validate item
|
String key;
|
||||||
|
Value value;
|
||||||
|
BOOST_FOREACH(tie(key, value), attrs) {
|
||||||
|
String name;
|
||||||
|
|
||||||
ConfigCompilerContext::GetContext()->AddError(false, "Hello World!");
|
if (value.IsScalar()) {
|
||||||
|
name = value;
|
||||||
|
} else if (value.IsObjectType<Dictionary>()) {
|
||||||
|
Dictionary::Ptr serviceDesc = value;
|
||||||
|
|
||||||
|
if (serviceDesc->Contains("service"))
|
||||||
|
name = serviceDesc->Get("service");
|
||||||
|
else
|
||||||
|
name = key;
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ConfigItem::GetObject("Service", name)) {
|
||||||
|
ConfigCompilerContext::GetContext()->AddError(false, "Validation failed for " +
|
||||||
|
location + ": Service '" + name + "' not found.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
task->FinishResult(Empty);
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ public:
|
||||||
set<shared_ptr<Service> > GetServices(void) const;
|
set<shared_ptr<Service> > GetServices(void) const;
|
||||||
static void InvalidateServicesCache(void);
|
static void InvalidateServicesCache(void);
|
||||||
|
|
||||||
static void ValidateHostItem(const ScriptTask::Ptr& task,
|
static void ValidateServiceDictionary(const ScriptTask::Ptr& task,
|
||||||
const std::vector<icinga::Value>& arguments);
|
const std::vector<icinga::Value>& arguments);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
Loading…
Reference in New Issue