Move internal script functions into the 'Internal' namespace

fixes #12338
This commit is contained in:
Gunnar Beutner 2016-08-09 10:40:29 +02:00
parent 43c2ec31ef
commit dc06209e4a
26 changed files with 134 additions and 86 deletions

View File

@ -18,6 +18,6 @@
add_subdirectory(plugins-contrib.d)
install(
FILES itl command.conf command-icinga.conf hangman timeperiod.conf plugins command-plugins.conf manubulon command-plugins-manubulon.conf windows-plugins command-plugins-windows.conf nscp command-nscp-local.conf plugins-contrib
FILES itl command.conf command-icinga.conf hangman plugins command-plugins.conf manubulon command-plugins-manubulon.conf windows-plugins command-plugins-windows.conf nscp command-nscp-local.conf plugins-contrib
DESTINATION ${CMAKE_INSTALL_DATADIR}/icinga2/include
)

View File

@ -24,4 +24,3 @@
include "command.conf"
include "command-icinga.conf"
include "timeperiod.conf"

View File

@ -548,7 +548,6 @@ void ConfigObject::RestoreObject(const String& message, int attributeTypes)
if (!object)
return;
ASSERT(!object->IsActive());
#ifdef I2_DEBUG
Log(LogDebug, "ConfigObject")
<< "Restoring object '" << name << "' of type '" << type << "'.";

View File

@ -63,7 +63,16 @@ private:
Function::Ptr sf = new icinga::Function(WrapFunction(callback)); \
ScriptGlobal::Set(#name, sf); \
} \
INITIALIZE_ONCE(RegisterFunction); \
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
} } }
#define REGISTER_SCRIPTFUNCTION_NS(ns, name, callback) \
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \
void RegisterFunction(void) { \
Function::Ptr sf = new icinga::Function(WrapFunction(callback)); \
ScriptGlobal::Set(#ns "." #name, sf); \
} \
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
} } }
#define REGISTER_SAFE_SCRIPTFUNCTION(name, callback) \
@ -72,9 +81,17 @@ private:
Function::Ptr sf = new icinga::Function(WrapFunction(callback), true); \
ScriptGlobal::Set(#name, sf); \
} \
INITIALIZE_ONCE(RegisterFunction); \
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
} } }
#define REGISTER_SAFE_SCRIPTFUNCTION_NS(ns, name, callback) \
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \
void RegisterFunction(void) { \
Function::Ptr sf = new icinga::Function(WrapFunction(callback), true); \
ScriptGlobal::Set(#ns "." #name, sf); \
} \
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
} } }
}
#endif /* SCRIPTFUNCTION_H */

View File

@ -68,7 +68,6 @@ private:
Loader(void);
static boost::thread_specific_ptr<std::priority_queue<DeferredInitializer> >& GetDeferredInitializers(void);
};
}

View File

@ -27,6 +27,7 @@
#include "base/objectlock.hpp"
#include "base/exception.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/split.hpp>
#include <fstream>
using namespace icinga;
@ -47,7 +48,33 @@ Value ScriptGlobal::Get(const String& name, const Value *defaultValue)
void ScriptGlobal::Set(const String& name, const Value& value)
{
m_Globals->Set(name, value);
std::vector<String> tokens;
boost::algorithm::split(tokens, name, boost::is_any_of("."));
if (tokens.empty())
BOOST_THROW_EXCEPTION(std::invalid_argument("Name must not be empty"));
{
ObjectLock olock(m_Globals);
Dictionary::Ptr parent = m_Globals;
for (std::vector<String>::size_type i = 0; i < tokens.size(); i++) {
const String& token = tokens[i];
if (i + 1 != tokens.size()) {
if (!parent->Contains(token)) {
Dictionary::Ptr dict = new Dictionary();
parent->Set(token, dict);
parent = dict;
} else {
parent = parent->Get(token);
}
}
}
parent->Set(tokens[tokens.size() - 1], value);
}
}
bool ScriptGlobal::Exists(const String& name)

View File

@ -46,9 +46,7 @@ ConfigItem::TypeMap ConfigItem::m_Items;
ConfigItem::ItemList ConfigItem::m_UnnamedItems;
ConfigItem::IgnoredItemList ConfigItem::m_IgnoredItems;
#ifdef I2_DEBUG
REGISTER_SCRIPTFUNCTION(__run_with_activation_context, &ConfigItem::RunWithActivationContext);
#endif /* I2_DEBUG */
REGISTER_SCRIPTFUNCTION_NS(Internal, run_with_activation_context, &ConfigItem::RunWithActivationContext);
/**
* Constructor for the ConfigItem class.
@ -534,9 +532,10 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue
return true;
}
bool ConfigItem::CommitItems(const ActivationContext::Ptr& context, WorkQueue& upq, std::vector<ConfigItem::Ptr>& newItems)
bool ConfigItem::CommitItems(const ActivationContext::Ptr& context, WorkQueue& upq, std::vector<ConfigItem::Ptr>& newItems, bool silent)
{
Log(LogInformation, "ConfigItem", "Committing config item(s).");
if (!silent)
Log(LogInformation, "ConfigItem", "Committing config item(s).");
if (!CommitNewItems(context, upq, newItems)) {
upq.ReportExceptions("config");
@ -550,30 +549,33 @@ bool ConfigItem::CommitItems(const ActivationContext::Ptr& context, WorkQueue& u
ApplyRule::CheckMatches();
/* log stats for external parsers */
typedef std::map<Type::Ptr, int> ItemCountMap;
ItemCountMap itemCounts;
BOOST_FOREACH(const ConfigItem::Ptr& item, newItems) {
if (!item->m_Object)
continue;
if (!silent) {
/* log stats for external parsers */
typedef std::map<Type::Ptr, int> ItemCountMap;
ItemCountMap itemCounts;
BOOST_FOREACH(const ConfigItem::Ptr& item, newItems) {
if (!item->m_Object)
continue;
itemCounts[item->m_Object->GetReflectionType()]++;
}
itemCounts[item->m_Object->GetReflectionType()]++;
}
BOOST_FOREACH(const ItemCountMap::value_type& kv, itemCounts) {
Log(LogInformation, "ConfigItem")
<< "Instantiated " << kv.second << " " << (kv.second != 1 ? kv.first->GetPluralName() : kv.first->GetName()) << ".";
BOOST_FOREACH(const ItemCountMap::value_type& kv, itemCounts) {
Log(LogInformation, "ConfigItem")
<< "Instantiated " << kv.second << " " << (kv.second != 1 ? kv.first->GetPluralName() : kv.first->GetName()) << ".";
}
}
return true;
}
bool ConfigItem::ActivateItems(WorkQueue& upq, const std::vector<ConfigItem::Ptr>& newItems, bool runtimeCreated)
bool ConfigItem::ActivateItems(WorkQueue& upq, const std::vector<ConfigItem::Ptr>& newItems, bool runtimeCreated, bool silent)
{
static boost::mutex mtx;
boost::mutex::scoped_lock lock(mtx);
Log(LogInformation, "ConfigItem", "Triggering Start signal for config items");
if (!silent)
Log(LogInformation, "ConfigItem", "Triggering Start signal for config items");
BOOST_FOREACH(const ConfigItem::Ptr& item, newItems) {
if (!item->m_Object)
@ -609,12 +611,12 @@ bool ConfigItem::ActivateItems(WorkQueue& upq, const std::vector<ConfigItem::Ptr
}
#endif /* I2_DEBUG */
Log(LogInformation, "ConfigItem", "Activated all objects.");
if (!silent)
Log(LogInformation, "ConfigItem", "Activated all objects.");
return true;
}
#ifdef I2_DEBUG
bool ConfigItem::RunWithActivationContext(const Function::Ptr& function)
{
ActivationScope scope;
@ -629,15 +631,14 @@ bool ConfigItem::RunWithActivationContext(const Function::Ptr& function)
std::vector<ConfigItem::Ptr> newItems;
if (!CommitItems(scope.GetContext(), upq, newItems))
if (!CommitItems(scope.GetContext(), upq, newItems, true))
return false;
if (!ActivateItems(upq, newItems))
if (!ActivateItems(upq, newItems, false, true))
return false;
return true;
}
#endif /* I2_DEBUG */
std::vector<ConfigItem::Ptr> ConfigItem::GetItems(const String& type)
{

View File

@ -68,12 +68,10 @@ public:
static ConfigItem::Ptr GetByTypeAndName(const String& type,
const String& name);
static bool CommitItems(const ActivationContext::Ptr& context, WorkQueue& upq, std::vector<ConfigItem::Ptr>& newItems);
static bool ActivateItems(WorkQueue& upq, const std::vector<ConfigItem::Ptr>& newItems, bool runtimeCreated = false);
static bool CommitItems(const ActivationContext::Ptr& context, WorkQueue& upq, std::vector<ConfigItem::Ptr>& newItems, bool silent = false);
static bool ActivateItems(WorkQueue& upq, const std::vector<ConfigItem::Ptr>& newItems, bool runtimeCreated = false, bool silent = false);
#ifdef I2_DEBUG
static bool RunWithActivationContext(const Function::Ptr& function);
#endif /* I2_DEBUG */
static std::vector<ConfigItem::Ptr> GetItems(const String& type);

View File

@ -17,10 +17,12 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
template CheckCommand "ido-check-command" {
execute = IdoCheck
}
assert(Internal.run_with_activation_context(function() {
template CheckCommand "ido-check-command" {
execute = Internal.IdoCheck
}
object CheckCommand "ido" {
import "ido-check-command"
}
object CheckCommand "ido" {
import "ido-check-command"
}
}))

View File

@ -33,7 +33,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(IdoCheck, &IdoCheckTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, IdoCheck, &IdoCheckTask::ScriptFunc);
void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)

View File

@ -37,13 +37,15 @@ mkclass_target(timeperiod.ti timeperiod.tcpp timeperiod.thpp)
mkclass_target(usergroup.ti usergroup.tcpp usergroup.thpp)
mkclass_target(user.ti user.tcpp user.thpp)
mkembedconfig_target(icinga-itl.conf icinga-itl.cpp)
set(icinga_SOURCES
apiactions.cpp apievents.cpp checkable.cpp checkable.thpp checkable-dependency.cpp checkable-downtime.cpp checkable-event.cpp
checkable-flapping.cpp checkable-script.cpp checkcommand.cpp checkcommand.thpp checkresult.cpp checkresult.thpp
cib.cpp clusterevents.cpp command.cpp command.thpp comment.cpp comment.thpp compatutility.cpp dependency.cpp dependency.thpp
dependency-apply.cpp downtime.cpp downtime.thpp eventcommand.cpp eventcommand.thpp
externalcommandprocessor.cpp host.cpp host.thpp hostgroup.cpp hostgroup.thpp icingaapplication.cpp icingaapplication.thpp
customvarobject.cpp customvarobject.thpp
icinga-itl.cpp customvarobject.cpp customvarobject.thpp
legacytimeperiod.cpp macroprocessor.cpp notificationcommand.cpp notificationcommand.thpp notification.cpp notification.thpp
notification-apply.cpp objectutils.cpp perfdatavalue.cpp perfdatavalue.thpp pluginutility.cpp scheduleddowntime.cpp scheduleddowntime.thpp
scheduleddowntime-apply.cpp service-apply.cpp checkable-check.cpp checkable-comment.cpp

View File

@ -17,6 +17,8 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
template TimePeriod "legacy-timeperiod" {
update = LegacyTimePeriod
}
assert(Internal.run_with_activation_context(function() {
template TimePeriod "legacy-timeperiod" {
update = Internal.LegacyTimePeriod
}
}))

View File

@ -31,7 +31,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(LegacyTimePeriod, &LegacyTimePeriod::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, LegacyTimePeriod, &LegacyTimePeriod::ScriptFunc);
bool LegacyTimePeriod::IsInTimeRange(tm *begin, tm *end, int stride, tm *reference)
{

View File

@ -39,7 +39,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(ClrCheck, &ClrCheckTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, ClrCheck, &ClrCheckTask::ScriptFunc);
static boost::once_flag l_OnceFlag = BOOST_ONCE_INIT;

View File

@ -34,7 +34,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(ClusterCheck, &ClusterCheckTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterCheck, &ClusterCheckTask::ScriptFunc);
void ClusterCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)

View File

@ -30,7 +30,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(ClusterZoneCheck, &ClusterZoneCheckTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterZoneCheck, &ClusterZoneCheckTask::ScriptFunc);
void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)

View File

@ -29,7 +29,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(ExceptionCheck, &ExceptionCheckTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, ExceptionCheck, &ExceptionCheckTask::ScriptFunc);
void ExceptionCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)

View File

@ -30,7 +30,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(IcingaCheck, &IcingaCheckTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, IcingaCheck, &IcingaCheckTask::ScriptFunc);
void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)

View File

@ -17,38 +17,40 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
template CheckCommand "icinga-check-command" {
execute = IcingaCheck
}
assert(Internal.run_with_activation_context(function() {
template CheckCommand "icinga-check-command" {
execute = Internal.IcingaCheck
}
template CheckCommand "cluster-check-command" {
execute = ClusterCheck
}
template CheckCommand "cluster-check-command" {
execute = Internal.ClusterCheck
}
template CheckCommand "cluster-zone-check-command" {
execute = ClusterZoneCheck
}
template CheckCommand "cluster-zone-check-command" {
execute = Internal.ClusterZoneCheck
}
template CheckCommand "plugin-check-command" {
execute = PluginCheck
}
template CheckCommand "plugin-check-command" {
execute = Internal.PluginCheck
}
template CheckCommand "clr-check-command" {
execute = ClrCheck
}
template CheckCommand "clr-check-command" {
execute = Internal.ClrCheck
}
template NotificationCommand "plugin-notification-command" {
execute = PluginNotification
}
template NotificationCommand "plugin-notification-command" {
execute = Internal.PluginNotification
}
template EventCommand "plugin-event-command" {
execute = PluginEvent
}
template EventCommand "plugin-event-command" {
execute = Internal.PluginEvent
}
template CheckCommand "random-check-command" {
execute = RandomCheck
}
template CheckCommand "random-check-command" {
execute = Internal.RandomCheck
}
template CheckCommand "exception-check-command" {
execute = ExceptionCheck
}
template CheckCommand "exception-check-command" {
execute = Internal.ExceptionCheck
}
}))

View File

@ -30,7 +30,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(NullCheck, &NullCheckTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, NullCheck, &NullCheckTask::ScriptFunc);
void NullCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)

View File

@ -23,7 +23,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(NullEvent, &NullEventTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, NullEvent, &NullEventTask::ScriptFunc);
void NullEventTask::ScriptFunc(const Checkable::Ptr&, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{ }

View File

@ -34,7 +34,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(PluginCheck, &PluginCheckTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, PluginCheck, &PluginCheckTask::ScriptFunc);
void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)

View File

@ -32,7 +32,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(PluginEvent, &PluginEventTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, PluginEvent, &PluginEventTask::ScriptFunc);
void PluginEventTask::ScriptFunc(const Checkable::Ptr& checkable,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)

View File

@ -33,7 +33,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(PluginNotification, &PluginNotificationTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, PluginNotification, &PluginNotificationTask::ScriptFunc);
void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification,
const User::Ptr& user, const CheckResult::Ptr& cr, int itype,

View File

@ -30,7 +30,7 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(RandomCheck, &RandomCheckTask::ScriptFunc);
REGISTER_SCRIPTFUNCTION_NS(Internal, RandomCheck, &RandomCheckTask::ScriptFunc);
void RandomCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)

View File

@ -22,8 +22,8 @@
using namespace icinga;
REGISTER_SCRIPTFUNCTION(EmptyTimePeriod, &TimePeriodTask::EmptyTimePeriodUpdate);
REGISTER_SCRIPTFUNCTION(EvenMinutesTimePeriod, &TimePeriodTask::EvenMinutesTimePeriodUpdate);
REGISTER_SCRIPTFUNCTION_NS(Internal, EmptyTimePeriod, &TimePeriodTask::EmptyTimePeriodUpdate);
REGISTER_SCRIPTFUNCTION_NS(Internal, EvenMinutesTimePeriod, &TimePeriodTask::EvenMinutesTimePeriodUpdate);
Array::Ptr TimePeriodTask::EmptyTimePeriodUpdate(const TimePeriod::Ptr&, double, double)
{