From 10d6f70a855237e8697de4b5e77b0d95304eb349 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 7 Aug 2018 13:55:41 +0200 Subject: [PATCH] Move constants and functions into different namespaces --- icinga-app/icinga.cpp | 18 +++++++------- lib/base/logger.cpp | 10 ++++---- lib/base/sysloglogger.cpp | 40 ++++++++++++++++---------------- lib/cli/daemonutility.cpp | 6 ++++- lib/db_ido/dbquery.cpp | 30 ++++++++++++------------ lib/icinga/checkresult.cpp | 12 +++++----- lib/icinga/icingaapplication.cpp | 13 ++++++++--- lib/icinga/notification.cpp | 32 ++++++++++++------------- lib/icinga/objectutils.cpp | 22 +++++++++--------- 9 files changed, 97 insertions(+), 86 deletions(-) diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index 24da88644..5e8cbd8ec 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -300,15 +300,15 @@ static int Main() ScriptGlobal::Set("AttachDebugger", false); - ScriptGlobal::Set("PlatformKernel", Utility::GetPlatformKernel()); - ScriptGlobal::Set("PlatformKernelVersion", Utility::GetPlatformKernelVersion()); - ScriptGlobal::Set("PlatformName", Utility::GetPlatformName()); - ScriptGlobal::Set("PlatformVersion", Utility::GetPlatformVersion()); - ScriptGlobal::Set("PlatformArchitecture", Utility::GetPlatformArchitecture()); - - ScriptGlobal::Set("BuildHostName", ICINGA_BUILD_HOST_NAME); - ScriptGlobal::Set("BuildCompilerName", ICINGA_BUILD_COMPILER_NAME); - ScriptGlobal::Set("BuildCompilerVersion", ICINGA_BUILD_COMPILER_VERSION); + ScriptGlobal::Set("System.PlatformKernel", Utility::GetPlatformKernel(), true); + ScriptGlobal::Set("System.PlatformKernelVersion", Utility::GetPlatformKernelVersion(), true); + ScriptGlobal::Set("System.PlatformName", Utility::GetPlatformName(), true); + ScriptGlobal::Set("System.PlatformVersion", Utility::GetPlatformVersion(), true); + ScriptGlobal::Set("System.PlatformArchitecture", Utility::GetPlatformArchitecture(), true); + + ScriptGlobal::Set("System.BuildHostName", ICINGA_BUILD_HOST_NAME, true); + ScriptGlobal::Set("System.BuildCompilerName", ICINGA_BUILD_COMPILER_NAME, true); + ScriptGlobal::Set("System.BuildCompilerVersion", ICINGA_BUILD_COMPILER_VERSION, true); if (!autocomplete) Application::SetResourceLimits(); diff --git a/lib/base/logger.cpp b/lib/base/logger.cpp index 376296ada..9a59d199f 100644 --- a/lib/base/logger.cpp +++ b/lib/base/logger.cpp @@ -49,11 +49,11 @@ bool Logger::m_TimestampEnabled = true; LogSeverity Logger::m_ConsoleLogSeverity = LogInformation; INITIALIZE_ONCE([]() { - ScriptGlobal::Set("LogDebug", LogDebug); - ScriptGlobal::Set("LogNotice", LogNotice); - ScriptGlobal::Set("LogInformation", LogInformation); - ScriptGlobal::Set("LogWarning", LogWarning); - ScriptGlobal::Set("LogCritical", LogCritical); + ScriptGlobal::Set("System.LogDebug", LogDebug, true); + ScriptGlobal::Set("System.LogNotice", LogNotice, true); + ScriptGlobal::Set("System.LogInformation", LogInformation, true); + ScriptGlobal::Set("System.LogWarning", LogWarning, true); + ScriptGlobal::Set("System.LogCritical", LogCritical, true); }); /** diff --git a/lib/base/sysloglogger.cpp b/lib/base/sysloglogger.cpp index b5d735a80..673a27930 100644 --- a/lib/base/sysloglogger.cpp +++ b/lib/base/sysloglogger.cpp @@ -35,26 +35,26 @@ std::map SyslogLogger::m_FacilityMap; void SyslogLogger::StaticInitialize() { - ScriptGlobal::Set("FacilityAuth", "LOG_AUTH"); - ScriptGlobal::Set("FacilityAuthPriv", "LOG_AUTHPRIV"); - ScriptGlobal::Set("FacilityCron", "LOG_CRON"); - ScriptGlobal::Set("FacilityDaemon", "LOG_DAEMON"); - ScriptGlobal::Set("FacilityFtp", "LOG_FTP"); - ScriptGlobal::Set("FacilityKern", "LOG_KERN"); - ScriptGlobal::Set("FacilityLocal0", "LOG_LOCAL0"); - ScriptGlobal::Set("FacilityLocal1", "LOG_LOCAL1"); - ScriptGlobal::Set("FacilityLocal2", "LOG_LOCAL2"); - ScriptGlobal::Set("FacilityLocal3", "LOG_LOCAL3"); - ScriptGlobal::Set("FacilityLocal4", "LOG_LOCAL4"); - ScriptGlobal::Set("FacilityLocal5", "LOG_LOCAL5"); - ScriptGlobal::Set("FacilityLocal6", "LOG_LOCAL6"); - ScriptGlobal::Set("FacilityLocal7", "LOG_LOCAL7"); - ScriptGlobal::Set("FacilityLpr", "LOG_LPR"); - ScriptGlobal::Set("FacilityMail", "LOG_MAIL"); - ScriptGlobal::Set("FacilityNews", "LOG_NEWS"); - ScriptGlobal::Set("FacilitySyslog", "LOG_SYSLOG"); - ScriptGlobal::Set("FacilityUser", "LOG_USER"); - ScriptGlobal::Set("FacilityUucp", "LOG_UUCP"); + ScriptGlobal::Set("System.FacilityAuth", "LOG_AUTH", true); + ScriptGlobal::Set("System.FacilityAuthPriv", "LOG_AUTHPRIV", true); + ScriptGlobal::Set("System.FacilityCron", "LOG_CRON", true); + ScriptGlobal::Set("System.FacilityDaemon", "LOG_DAEMON", true); + ScriptGlobal::Set("System.FacilityFtp", "LOG_FTP", true); + ScriptGlobal::Set("System.FacilityKern", "LOG_KERN", true); + ScriptGlobal::Set("System.FacilityLocal0", "LOG_LOCAL0", true); + ScriptGlobal::Set("System.FacilityLocal1", "LOG_LOCAL1", true); + ScriptGlobal::Set("System.FacilityLocal2", "LOG_LOCAL2", true); + ScriptGlobal::Set("System.FacilityLocal3", "LOG_LOCAL3", true); + ScriptGlobal::Set("System.FacilityLocal4", "LOG_LOCAL4", true); + ScriptGlobal::Set("System.FacilityLocal5", "LOG_LOCAL5", true); + ScriptGlobal::Set("System.FacilityLocal6", "LOG_LOCAL6", true); + ScriptGlobal::Set("System.FacilityLocal7", "LOG_LOCAL7", true); + ScriptGlobal::Set("System.FacilityLpr", "LOG_LPR", true); + ScriptGlobal::Set("System.FacilityMail", "LOG_MAIL", true); + ScriptGlobal::Set("System.FacilityNews", "LOG_NEWS", true); + ScriptGlobal::Set("System.FacilitySyslog", "LOG_SYSLOG", true); + ScriptGlobal::Set("System.FacilityUser", "LOG_USER", true); + ScriptGlobal::Set("System.FacilityUucp", "LOG_UUCP", true); m_FacilityMap["LOG_AUTH"] = LOG_AUTH; m_FacilityMap["LOG_AUTHPRIV"] = LOG_AUTHPRIV; diff --git a/lib/cli/daemonutility.cpp b/lib/cli/daemonutility.cpp index 62d39c2f4..64d8b17c8 100644 --- a/lib/cli/daemonutility.cpp +++ b/lib/cli/daemonutility.cpp @@ -145,7 +145,11 @@ bool DaemonUtility::ValidateConfigFiles(const std::vector& configs, if (!success) return false; - Type::Ptr appType = Type::GetByName(ScriptGlobal::Get("ApplicationType", &Empty)); + Namespace::Ptr systemNS = ScriptGlobal::Get("System"); + Value vAppType; + VERIFY(systemNS->Get("ApplicationType", &vAppType)); + + Type::Ptr appType = Type::GetByName(vAppType); if (ConfigItem::GetItems(appType).empty()) { ConfigItemBuilder builder; diff --git a/lib/db_ido/dbquery.cpp b/lib/db_ido/dbquery.cpp index cd3656c1f..d64904f53 100644 --- a/lib/db_ido/dbquery.cpp +++ b/lib/db_ido/dbquery.cpp @@ -29,22 +29,22 @@ std::map DbQuery::m_CategoryFilterMap; void DbQuery::StaticInitialize() { - ScriptGlobal::Set("DbCatConfig", DbCatConfig); - ScriptGlobal::Set("DbCatState", DbCatState); - ScriptGlobal::Set("DbCatAcknowledgement", DbCatAcknowledgement); - ScriptGlobal::Set("DbCatComment", DbCatComment); - ScriptGlobal::Set("DbCatDowntime", DbCatDowntime); - ScriptGlobal::Set("DbCatEventHandler", DbCatEventHandler); - ScriptGlobal::Set("DbCatExternalCommand", DbCatExternalCommand); - ScriptGlobal::Set("DbCatFlapping", DbCatFlapping); - ScriptGlobal::Set("DbCatCheck", DbCatCheck); - ScriptGlobal::Set("DbCatLog", DbCatLog); - ScriptGlobal::Set("DbCatNotification", DbCatNotification); - ScriptGlobal::Set("DbCatProgramStatus", DbCatProgramStatus); - ScriptGlobal::Set("DbCatRetention", DbCatRetention); - ScriptGlobal::Set("DbCatStateHistory", DbCatStateHistory); + ScriptGlobal::Set("Icinga.DbCatConfig", DbCatConfig, true); + ScriptGlobal::Set("Icinga.DbCatState", DbCatState, true); + ScriptGlobal::Set("Icinga.DbCatAcknowledgement", DbCatAcknowledgement, true); + ScriptGlobal::Set("Icinga.DbCatComment", DbCatComment, true); + ScriptGlobal::Set("Icinga.DbCatDowntime", DbCatDowntime, true); + ScriptGlobal::Set("Icinga.DbCatEventHandler", DbCatEventHandler, true); + ScriptGlobal::Set("Icinga.DbCatExternalCommand", DbCatExternalCommand, true); + ScriptGlobal::Set("Icinga.DbCatFlapping", DbCatFlapping, true); + ScriptGlobal::Set("Icinga.DbCatCheck", DbCatCheck, true); + ScriptGlobal::Set("Icinga.DbCatLog", DbCatLog, true); + ScriptGlobal::Set("Icinga.DbCatNotification", DbCatNotification, true); + ScriptGlobal::Set("Icinga.DbCatProgramStatus", DbCatProgramStatus, true); + ScriptGlobal::Set("Icinga.DbCatRetention", DbCatRetention, true); + ScriptGlobal::Set("Icinga.DbCatStateHistory", DbCatStateHistory, true); - ScriptGlobal::Set("DbCatEverything", DbCatEverything); + ScriptGlobal::Set("Icinga.DbCatEverything", DbCatEverything, true); m_CategoryFilterMap["DbCatConfig"] = DbCatConfig; m_CategoryFilterMap["DbCatState"] = DbCatState; diff --git a/lib/icinga/checkresult.cpp b/lib/icinga/checkresult.cpp index 52b1bb5e3..e1c8d160c 100644 --- a/lib/icinga/checkresult.cpp +++ b/lib/icinga/checkresult.cpp @@ -26,13 +26,13 @@ using namespace icinga; REGISTER_TYPE(CheckResult); INITIALIZE_ONCE([]() { - ScriptGlobal::Set("ServiceOK", ServiceOK); - ScriptGlobal::Set("ServiceWarning", ServiceWarning); - ScriptGlobal::Set("ServiceCritical", ServiceCritical); - ScriptGlobal::Set("ServiceUnknown", ServiceUnknown); + ScriptGlobal::Set("Icinga.ServiceOK", ServiceOK, true); + ScriptGlobal::Set("Icinga.ServiceWarning", ServiceWarning, true); + ScriptGlobal::Set("Icinga.ServiceCritical", ServiceCritical, true); + ScriptGlobal::Set("Icinga.ServiceUnknown", ServiceUnknown, true); - ScriptGlobal::Set("HostUp", HostUp); - ScriptGlobal::Set("HostDown", HostDown); + ScriptGlobal::Set("Icinga.HostUp", HostUp, true); + ScriptGlobal::Set("Icinga.HostDown", HostDown, true); }) double CheckResult::CalculateExecutionTime() const diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp index 4db75d852..fab5187a6 100644 --- a/lib/icinga/icingaapplication.cpp +++ b/lib/icinga/icingaapplication.cpp @@ -41,7 +41,7 @@ using namespace icinga; static Timer::Ptr l_RetentionTimer; REGISTER_TYPE(IcingaApplication); -INITIALIZE_ONCE(&IcingaApplication::StaticInitialize); +INITIALIZE_ONCE_WITH_PRIORITY(&IcingaApplication::StaticInitialize, 50); void IcingaApplication::StaticInitialize() { @@ -59,9 +59,16 @@ void IcingaApplication::StaticInitialize() ScriptGlobal::Set("NodeName", node_name); - ScriptGlobal::Set("ApplicationType", "IcingaApplication"); + ScriptGlobal::Set("System.ApplicationType", "IcingaApplication", true); - ScriptGlobal::Set("ApplicationVersion", Application::GetAppVersion()); + ScriptGlobal::Set("System.ApplicationVersion", Application::GetAppVersion(), true); + + Namespace::Ptr globalNS = ScriptGlobal::GetGlobals(); + + auto icingaNSBehavior = new ConstNamespaceBehavior(); + icingaNSBehavior->Freeze(); + Namespace::Ptr icingaNS = new Namespace(icingaNSBehavior); + globalNS->SetAttribute("Icinga", std::make_shared(icingaNS)); } REGISTER_STATSFUNCTION(IcingaApplication, &IcingaApplication::StatsFunc); diff --git a/lib/icinga/notification.cpp b/lib/icinga/notification.cpp index ea3fdc90e..a53c17e12 100644 --- a/lib/icinga/notification.cpp +++ b/lib/icinga/notification.cpp @@ -79,22 +79,22 @@ Dictionary::Ptr NotificationNameComposer::ParseName(const String& name) const void Notification::StaticInitialize() { - ScriptGlobal::Set("OK", "OK"); - ScriptGlobal::Set("Warning", "Warning"); - ScriptGlobal::Set("Critical", "Critical"); - ScriptGlobal::Set("Unknown", "Unknown"); - ScriptGlobal::Set("Up", "Up"); - ScriptGlobal::Set("Down", "Down"); - - ScriptGlobal::Set("DowntimeStart", "DowntimeStart"); - ScriptGlobal::Set("DowntimeEnd", "DowntimeEnd"); - ScriptGlobal::Set("DowntimeRemoved", "DowntimeRemoved"); - ScriptGlobal::Set("Custom", "Custom"); - ScriptGlobal::Set("Acknowledgement", "Acknowledgement"); - ScriptGlobal::Set("Problem", "Problem"); - ScriptGlobal::Set("Recovery", "Recovery"); - ScriptGlobal::Set("FlappingStart", "FlappingStart"); - ScriptGlobal::Set("FlappingEnd", "FlappingEnd"); + ScriptGlobal::Set("Icinga.OK", "OK", true); + ScriptGlobal::Set("Icinga.Warning", "Warning", true); + ScriptGlobal::Set("Icinga.Critical", "Critical", true); + ScriptGlobal::Set("Icinga.Unknown", "Unknown", true); + ScriptGlobal::Set("Icinga.Up", "Up", true); + ScriptGlobal::Set("Icinga.Down", "Down", true); + + ScriptGlobal::Set("Icinga.DowntimeStart", "DowntimeStart", true); + ScriptGlobal::Set("Icinga.DowntimeEnd", "DowntimeEnd", true); + ScriptGlobal::Set("Icinga.DowntimeRemoved", "DowntimeRemoved", true); + ScriptGlobal::Set("Icinga.Custom", "Custom", true); + ScriptGlobal::Set("Icinga.Acknowledgement", "Acknowledgement", true); + ScriptGlobal::Set("Icinga.Problem", "Problem", true); + ScriptGlobal::Set("Icinga.Recovery", "Recovery", true); + ScriptGlobal::Set("Icinga.FlappingStart", "FlappingStart", true); + ScriptGlobal::Set("Icinga.FlappingEnd", "FlappingEnd", true); m_StateFilterMap["OK"] = StateFilterOK; m_StateFilterMap["Warning"] = StateFilterWarning; diff --git a/lib/icinga/objectutils.cpp b/lib/icinga/objectutils.cpp index b8057ef18..0f77502b2 100644 --- a/lib/icinga/objectutils.cpp +++ b/lib/icinga/objectutils.cpp @@ -29,17 +29,17 @@ using namespace icinga; -REGISTER_FUNCTION(System, get_host, &Host::GetByName, "name"); -REGISTER_FUNCTION(System, get_service, &ObjectUtils::GetService, "host:name"); -REGISTER_FUNCTION(System, get_services, &ObjectUtils::GetServices, "host"); -REGISTER_FUNCTION(System, get_user, &User::GetByName, "name"); -REGISTER_FUNCTION(System, get_check_command, &CheckCommand::GetByName, "name"); -REGISTER_FUNCTION(System, get_event_command, &EventCommand::GetByName, "name"); -REGISTER_FUNCTION(System, get_notification_command, &NotificationCommand::GetByName, "name"); -REGISTER_FUNCTION(System, get_host_group, &HostGroup::GetByName, "name"); -REGISTER_FUNCTION(System, get_service_group, &ServiceGroup::GetByName, "name"); -REGISTER_FUNCTION(System, get_user_group, &UserGroup::GetByName, "name"); -REGISTER_FUNCTION(System, get_time_period, &TimePeriod::GetByName, "name"); +REGISTER_FUNCTION(Icinga, get_host, &Host::GetByName, "name"); +REGISTER_FUNCTION(Icinga, get_service, &ObjectUtils::GetService, "host:name"); +REGISTER_FUNCTION(Icinga, get_services, &ObjectUtils::GetServices, "host"); +REGISTER_FUNCTION(Icinga, get_user, &User::GetByName, "name"); +REGISTER_FUNCTION(Icinga, get_check_command, &CheckCommand::GetByName, "name"); +REGISTER_FUNCTION(Icinga, get_event_command, &EventCommand::GetByName, "name"); +REGISTER_FUNCTION(Icinga, get_notification_command, &NotificationCommand::GetByName, "name"); +REGISTER_FUNCTION(Icinga, get_host_group, &HostGroup::GetByName, "name"); +REGISTER_FUNCTION(Icinga, get_service_group, &ServiceGroup::GetByName, "name"); +REGISTER_FUNCTION(Icinga, get_user_group, &UserGroup::GetByName, "name"); +REGISTER_FUNCTION(Icinga, get_time_period, &TimePeriod::GetByName, "name"); Service::Ptr ObjectUtils::GetService(const Value& host, const String& name) { -- 2.40.0