]> granicus.if.org Git - icinga2/commitdiff
Cli: Always generate an agent master zone parent (setup & update-config)
authorMichael Friedrich <michael.friedrich@netways.de>
Wed, 29 Oct 2014 10:09:55 +0000 (11:09 +0100)
committerMichael Friedrich <michael.friedrich@netways.de>
Wed, 29 Oct 2014 10:09:55 +0000 (11:09 +0100)
refs #7423
refs #7249

lib/cli/agentupdateconfigcommand.cpp
lib/cli/agentutility.cpp

index b137e6b4269439280362b6cce905a4ab67c57f37..bd88ba0ec05498a5f969f3287639acbf5e27271a 100644 (file)
@@ -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<String> 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<Dictionary>();
 
                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");
 
index a378309ef65d12b1fdf8971f85ea254d0be7341c..ee00c7ee4532ac6eb64c91812192fca7962b2696 100644 (file)
@@ -242,6 +242,8 @@ int AgentUtility::GenerateAgentIcingaConfig(const std::vector<std::string>& endp
        Dictionary::Ptr my_master_zone = make_shared<Dictionary>();
        Array::Ptr my_master_zone_members = make_shared<Array>();
 
+       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<std::string>& 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<std::string>& 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);