From dfd7741a1d38e932cda519e2ef097e688db345fb Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Mon, 27 Oct 2014 17:58:39 +0100 Subject: [PATCH] Cli: Add implementation for 'agent update-config' command refs #7249 --- lib/cli/agentupdateconfigcommand.cpp | 58 +++++++++++++++++++++++++++- lib/cli/agentupdateconfigcommand.hpp | 1 + 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/lib/cli/agentupdateconfigcommand.cpp b/lib/cli/agentupdateconfigcommand.cpp index 133503eb9..5210210a1 100644 --- a/lib/cli/agentupdateconfigcommand.cpp +++ b/lib/cli/agentupdateconfigcommand.cpp @@ -18,8 +18,11 @@ ******************************************************************************/ #include "cli/agentupdateconfigcommand.hpp" +#include "cli/agentutility.hpp" +#include "cli/repositoryutility.hpp" #include "base/logger.hpp" #include "base/application.hpp" +#include "base/objectlock.hpp" #include #include #include @@ -42,14 +45,65 @@ String AgentUpdateConfigCommand::GetShortDescription(void) const return "update agent config"; } +ImpersonationLevel AgentUpdateConfigCommand::GetImpersonationLevel(void) const +{ + return ImpersonateRoot; +} + /** - * The entry point for the "agetn update-config" CLI command. + * The entry point for the "agent update-config" CLI command. * * @returns An exit status. */ int AgentUpdateConfigCommand::Run(const boost::program_options::variables_map& vm, const std::vector& ap) const { - Log(LogWarning, "cli", "TODO: Not implemented yet."); + Log(LogInformation, "cli") + << "Updating agent configuration for "; + + AgentUtility::PrintAgents(std::cout); + + BOOST_FOREACH(const Dictionary::Ptr& agent, AgentUtility::GetAgents()) { + Dictionary::Ptr repository = agent->Get("repository"); + + ObjectLock olock(repository); + BOOST_FOREACH(const Dictionary::Pair& kv, repository) { + String host = kv.first; + + /* add a new host to the config repository */ + Dictionary::Ptr host_attrs = make_shared(); + host_attrs->Set("check_command", "dummy"); //TODO: add a repository-host template + + if (!RepositoryUtility::AddObject(host, "Host", host_attrs)) { + Log(LogCritical, "cli") + << "Cannot add agent host '" << host << "' to the config repository!\n"; + continue; + } + + Array::Ptr services = kv.second; + ObjectLock xlock(services); + BOOST_FOREACH(const String& service, services) { + + /* add a new service for this host to the config repository */ + Dictionary::Ptr service_attrs = make_shared(); + service_attrs->Set("host_name", host); //Required for host-service relation + service_attrs->Set("check_command", "dummy"); //TODO: add a repository-service template + + if (!RepositoryUtility::AddObject(service, "Service", service_attrs)) { + Log(LogCritical, "cli") + << "Cannot add agent host '" << host << "' to the config repository!\n"; + continue; + } + } + } + } + + Log(LogInformation, "cli", "Committing agent configuration."); + + RepositoryUtility::PrintChangeLog(std::cout); + std::cout << "\n"; + RepositoryUtility::CommitChangeLog(); + + std::cout << "Make sure to reload Icinga 2 for these changes to take effect." << std::endl; return 0; } diff --git a/lib/cli/agentupdateconfigcommand.hpp b/lib/cli/agentupdateconfigcommand.hpp index 8b7d2c699..9a4df06d1 100644 --- a/lib/cli/agentupdateconfigcommand.hpp +++ b/lib/cli/agentupdateconfigcommand.hpp @@ -38,6 +38,7 @@ public: virtual String GetDescription(void) const; virtual String GetShortDescription(void) const; virtual int Run(const boost::program_options::variables_map& vm, const std::vector& ap) const; + virtual ImpersonationLevel GetImpersonationLevel(void) const; }; }