From f72f7d87c7951e72701264a90839806715ad732c Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 29 Oct 2014 11:09:55 +0100 Subject: [PATCH] Cli: Always generate an agent master zone parent (setup & update-config) refs #7423 refs #7249 --- lib/cli/agentupdateconfigcommand.cpp | 22 +++++++++++++++------- lib/cli/agentutility.cpp | 6 ++++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/cli/agentupdateconfigcommand.cpp b/lib/cli/agentupdateconfigcommand.cpp index b137e6b42..bd88ba0ec 100644 --- a/lib/cli/agentupdateconfigcommand.cpp +++ b/lib/cli/agentupdateconfigcommand.cpp @@ -62,7 +62,7 @@ int AgentUpdateConfigCommand::Run(const boost::program_options::variables_map& v if (RepositoryUtility::ChangeLogHasPendingChanges()) { Log(LogWarning, "cli") << "There are pending changes for commit.\n" - << "Please review and commit them using 'icinga2 repository commit [--simulate]\n" + << "Please review and commit them using 'icinga2 repository commit [--simulate]'\n" << "or drop them using 'icinga2 repository commit --clear' before proceeding."; return 1; } @@ -86,14 +86,13 @@ int AgentUpdateConfigCommand::Run(const boost::program_options::variables_map& v std::vector object_paths = RepositoryUtility::GetObjects(); BOOST_FOREACH(const Dictionary::Ptr& agent, AgentUtility::GetAgents()) { - - /* store existing structure in index */ - inventory->Set(agent->Get("endpoint"), agent); - Dictionary::Ptr repository = agent->Get("repository"); String zone = agent->Get("zone"); String endpoint = agent->Get("endpoint"); + /* store existing structure in index */ + inventory->Set(endpoint, agent); + Dictionary::Ptr host_services = make_shared(); ObjectLock olock(repository); @@ -209,7 +208,17 @@ int AgentUpdateConfigCommand::Run(const boost::program_options::variables_map& v zone_attrs->Set("__name", zone); zone_attrs->Set("name", zone); zone_attrs->Set("endpoints", zone_members); - zone_attrs->Set("parent", agent->Get("parent_zone")); + + String parent_zone = "master"; //hardcode the name + + if (!agent->Contains("parent_zone") || !agent->Get("parent_zone")) { + Log(LogWarning, "cli") + << "Agent '" << endpoint << "' does not have any parent zone defined. Using 'master' as default. Please verify the generated configuration."; + } else { + parent_zone = agent->Get("parent_zone"); + } + + zone_attrs->Set("parent", parent_zone); if (!RepositoryUtility::AddObject(zone, "Zone", zone_attrs)) { Log(LogCritical, "cli") @@ -240,7 +249,6 @@ int AgentUpdateConfigCommand::Run(const boost::program_options::variables_map& v RepositoryUtility::RemoveObject(host, "Host", host_attrs); //this removes all services for this host as well } - //TODO: Remove zone/endpoint information as well String zone = old_agent->Get("zone"); String endpoint = old_agent->Get("endpoint"); diff --git a/lib/cli/agentutility.cpp b/lib/cli/agentutility.cpp index a378309ef..ee00c7ee4 100644 --- a/lib/cli/agentutility.cpp +++ b/lib/cli/agentutility.cpp @@ -242,6 +242,8 @@ int AgentUtility::GenerateAgentIcingaConfig(const std::vector& endp Dictionary::Ptr my_master_zone = make_shared(); Array::Ptr my_master_zone_members = make_shared(); + String master_zone_name = "master"; //TODO: Find a better name. + BOOST_FOREACH(const std::string& endpoint, endpoints) { /* extract all --endpoint arguments and store host,port info */ @@ -265,9 +267,8 @@ int AgentUtility::GenerateAgentIcingaConfig(const std::vector& endp my_config->Add(my_master_endpoint); } - /* add the master zone to the config */ - my_master_zone->Set("__name", "master"); //hardcoded name + my_master_zone->Set("__name", master_zone_name); my_master_zone->Set("__type", "Zone"); my_master_zone->Set("endpoints", my_master_zone_members); @@ -285,6 +286,7 @@ int AgentUtility::GenerateAgentIcingaConfig(const std::vector& endp my_zone->Set("__name", nodename); my_zone->Set("__type", "Zone"); + my_zone->Set("parent", master_zone_name); //set the master zone as parent my_zone->Set("//this is the local agent", nodename); my_zone->Set("endpoints", my_zone_members); -- 2.40.0