icinga2 daemon -C: write icinga2.debug only if --dump-objects given

to save config (re)load time.
This commit is contained in:
Alexander A. Klimov 2022-11-21 12:37:07 +01:00
parent a6b05059ab
commit 0767c6ef87
7 changed files with 48 additions and 25 deletions

View File

@ -430,6 +430,7 @@ Command options:
-c [ --config ] arg parse a configuration file
-z [ --no-config ] start without a configuration file
-C [ --validate ] exit after validating the configuration
--dump-objects write icinga2.debug cache file for icinga2 object list
-e [ --errorlog ] arg log fatal errors to the specified log file (only
works in combination with --daemonize or
--close-stdio)
@ -520,10 +521,9 @@ attributes. The command also shows where each of the attributes was modified and
provides debug information for further configuration problem analysis.
That way you can also identify which objects have been created from your [apply rules](17-language-reference.md#apply).
Runtime modifications via the [REST API](12-icinga2-api.md#icinga2-api-config-objects)
are not immediately updated. Furthermore there is a known issue with
Configuration modifications are not immediately updated. Furthermore there is a known issue with
[group assign expressions](17-language-reference.md#group-assign) which are not reflected in the host object output.
You need to restart Icinga 2 in order to update the `icinga2.debug` cache file.
You need to run `icinga2 daemon -C --dump-objects` in order to update the `icinga2.debug` cache file.
More information can be found in the [troubleshooting](15-troubleshooting.md#troubleshooting-list-configuration-objects) section.

View File

@ -256,10 +256,9 @@ Found 1 Service objects.
[2014-10-15 14:27:19 +0200] information/cli: Parsed 175 objects.
```
Runtime modifications via the [REST API](12-icinga2-api.md#icinga2-api-config-objects)
are not immediately updated. Furthermore there is a known issue with
Configuration modifications are not immediately updated. Furthermore there is a known issue with
[group assign expressions](17-language-reference.md#group-assign) which are not reflected in the host object output.
You need to restart Icinga 2 in order to update the `icinga2.debug` cache file.
You need to `icinga2 daemon -C --dump-objects` in order to update the `icinga2.debug` cache file.
### Apply rules do not match <a id="apply-rules-do-not-match"></a>

View File

@ -176,6 +176,7 @@ void DaemonCommand::InitParameters(boost::program_options::options_description&
("config,c", po::value<std::vector<std::string> >(), "parse a configuration file")
("no-config,z", "start without a configuration file")
("validate,C", "exit after validating the configuration")
("dump-objects", "write icinga2.debug cache file for icinga2 object list")
("errorlog,e", po::value<std::string>(), "log fatal errors to the specified log file (only works in combination with --daemonize or --close-stdio)")
#ifndef _WIN32
("daemonize,d", "detach from the controlling terminal")
@ -218,6 +219,8 @@ static double GetDebugWorkerDelay()
}
#endif /* I2_DEBUG */
static String l_ObjectsPath;
/**
* Do the actual work (config loading, ...)
*
@ -248,7 +251,7 @@ int RunWorker(const std::vector<std::string>& configs, bool closeConsoleLog = fa
{
std::vector<ConfigItem::Ptr> newItems;
if (!DaemonUtility::LoadConfigFiles(configs, newItems, Configuration::ObjectsPath, Configuration::VarsPath)) {
if (!DaemonUtility::LoadConfigFiles(configs, newItems, l_ObjectsPath, Configuration::VarsPath)) {
Log(LogCritical, "cli", "Config validation failed. Re-run with 'icinga2 daemon -C' after fixing the config.");
NotifyStatus("Config validation failed.");
return EXIT_FAILURE;
@ -626,12 +629,21 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::strin
configs.push_back(configDir + "/icinga2.conf");
}
if (vm.count("dump-objects")) {
if (!vm.count("validate")) {
Log(LogCritical, "cli", "--dump-objects is not allowed without -C");
return EXIT_FAILURE;
}
l_ObjectsPath = Configuration::ObjectsPath;
}
if (vm.count("validate")) {
Log(LogInformation, "cli", "Loading configuration file(s).");
std::vector<ConfigItem::Ptr> newItems;
if (!DaemonUtility::LoadConfigFiles(configs, newItems, Configuration::ObjectsPath, Configuration::VarsPath)) {
if (!DaemonUtility::LoadConfigFiles(configs, newItems, l_ObjectsPath, Configuration::VarsPath)) {
Log(LogCritical, "cli", "Config validation failed. Re-run with 'icinga2 daemon -C' after fixing the config.");
return EXIT_FAILURE;
}

View File

@ -55,7 +55,7 @@ int ObjectListCommand::Run(const boost::program_options::variables_map& vm, cons
if (!Utility::PathExists(objectfile)) {
Log(LogCritical, "cli")
<< "Cannot open objects file '" << Configuration::ObjectsPath << "'.";
Log(LogCritical, "cli", "Run 'icinga2 daemon -C' to validate config and generate the cache file.");
Log(LogCritical, "cli", "Run 'icinga2 daemon -C --dump-objects' to validate config and generate the cache file.");
return 1;
}

View File

@ -48,6 +48,9 @@ void ConfigCompilerContext::WriteObject(const Dictionary::Ptr& object)
void ConfigCompilerContext::CancelObjectsFile()
{
if (!m_ObjectsFP)
return;
delete m_ObjectsFP;
m_ObjectsFP = nullptr;
@ -60,6 +63,9 @@ void ConfigCompilerContext::CancelObjectsFile()
void ConfigCompilerContext::FinishObjectsFile()
{
if (!m_ObjectsFP)
return;
delete m_ObjectsFP;
m_ObjectsFP = nullptr;

View File

@ -22,6 +22,11 @@ public:
void CancelObjectsFile();
void FinishObjectsFile();
inline bool IsOpen() const noexcept
{
return m_ObjectsFP;
}
static ConfigCompilerContext *GetInstance();
private:

View File

@ -285,25 +285,26 @@ ConfigObject::Ptr ConfigItem::Commit(bool discard)
BOOST_THROW_EXCEPTION(ValidationError(dobj, ex.GetPath(), "Circular references are not allowed"));
}
Dictionary::Ptr persistentItem = new Dictionary({
{ "type", type->GetName() },
{ "name", GetName() },
{ "properties", serializedObject },
{ "debug_hints", dhint },
{ "debug_info", new Array({
m_DebugInfo.Path,
m_DebugInfo.FirstLine,
m_DebugInfo.FirstColumn,
m_DebugInfo.LastLine,
m_DebugInfo.LastColumn,
}) }
});
if (ConfigCompilerContext::GetInstance()->IsOpen()) {
Dictionary::Ptr persistentItem = new Dictionary({
{ "type", type->GetName() },
{ "name", GetName() },
{ "properties", serializedObject },
{ "debug_hints", dhint },
{ "debug_info", new Array({
m_DebugInfo.Path,
m_DebugInfo.FirstLine,
m_DebugInfo.FirstColumn,
m_DebugInfo.LastLine,
m_DebugInfo.LastColumn,
}) }
});
ConfigCompilerContext::GetInstance()->WriteObject(persistentItem);
}
dhint.reset();
ConfigCompilerContext::GetInstance()->WriteObject(persistentItem);
persistentItem.reset();
dobj->Register();
m_Object = dobj;