]> granicus.if.org Git - icinga2/commitdiff
Make the "agent list" and "agent remove" commands work
authorGunnar Beutner <gunnar@beutner.name>
Fri, 24 Oct 2014 10:04:14 +0000 (12:04 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Fri, 24 Oct 2014 10:04:14 +0000 (12:04 +0200)
refs #7245

lib/cli/agentlistcommand.cpp
lib/cli/agentremovecommand.cpp
lib/cli/agentutility.cpp
lib/cli/agentutility.hpp

index 670c319e13277aacb41807a89b737188c197f1c3..4d237b9b45a29cf49789acaf69feb25c38df5e5a 100644 (file)
@@ -63,15 +63,10 @@ int AgentListCommand::Run(const boost::program_options::variables_map& vm, const
                    << "Ignoring parameters: " << boost::algorithm::join(ap, " ");
        }
 
-       if (vm.count("batch")) {
+       if (vm.count("batch"))
                AgentUtility::PrintAgentsJson(std::cout);
-               std::cout << "\n";
-               return 0;
-       }
-
-       std::cout << "Configured agents: \n";
-       AgentUtility::PrintAgents(std::cout);
-       std::cout << "\n";
+       else
+               AgentUtility::PrintAgents(std::cout);
 
        return 0;
 }
index 245589823a4b73b27cda6697657a69ffceba1387..b6014626bf7b0d87b2cceca1ff5e0e2a7fd2ce09 100644 (file)
@@ -45,7 +45,7 @@ String AgentRemoveCommand::GetShortDescription(void) const
 
 std::vector<String> AgentRemoveCommand::GetPositionalSuggestions(const String& word) const
 {
-       return AgentUtility::GetFieldCompletionSuggestions(word);
+       return AgentUtility::GetAgentCompletionSuggestions(word);
 }
 
 /**
index dcd37b5c4e62cfff47664bb62880ffaf217804ac..58690cb3bdd8752a67fafa55101570aaa27cd5a1 100644 (file)
@@ -54,18 +54,15 @@ String AgentUtility::GetAgentSettingsFile(const String& name)
        return GetRepositoryPath() + "/" + SHA256(name) + ".settings";
 }
 
-std::vector<String> AgentUtility::GetFieldCompletionSuggestions(const String& word)
+std::vector<String> AgentUtility::GetAgentCompletionSuggestions(const String& word)
 {
-       std::vector<String> cache;
        std::vector<String> suggestions;
 
-       GetAgents(cache);
+       BOOST_FOREACH(const Dictionary::Ptr& agent, GetAgents()) {
+               String agent_name = agent->Get("endpoint");
 
-       std::sort(cache.begin(), cache.end());
-
-       BOOST_FOREACH(const String& suggestion, cache) {
-               if (suggestion.Find(word) == 0)
-                       suggestions.push_back(suggestion);
+               if (agent_name.Find(word) == 0)
+                       suggestions.push_back(agent_name);
        }
 
        return suggestions;
@@ -73,43 +70,46 @@ std::vector<String> AgentUtility::GetFieldCompletionSuggestions(const String& wo
 
 void AgentUtility::PrintAgents(std::ostream& fp)
 {
-       std::vector<String> agents;
-       GetAgents(agents);
-
-       BOOST_FOREACH(const String& agent, agents) {
-               Dictionary::Ptr agent_obj = GetAgentFromRepository(GetAgentRepositoryFile(agent));
-               fp << "Agent Name: " << agent << "\n";
-
-               if (agent_obj) {
-                       fp << "Endpoint: " << agent_obj->Get("endpoint") << "\n";
-                       fp << "Zone: " << agent_obj->Get("zone") << "\n";
-                       fp << "Repository: ";
-                       fp << std::setw(4);
-                       PrintAgentRepository(fp, agent_obj->Get("repository"));
-                       fp << std::setw(0) << "\n";
-               }
-       }
+       bool first = false;
+
+       BOOST_FOREACH(const Dictionary::Ptr& agent, GetAgents()) {
+               if (first)
+                       first = false;
+               else
+                       fp << "\n";
 
-       fp << "All agents: " << boost::algorithm::join(agents, " ") << "\n";
+               fp << "Agent '"
+                  << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << agent->Get("endpoint") << ConsoleColorTag(Console_Normal)
+                  << "' (last seen: " << Utility::FormatDateTime("%c", agent->Get("seen")) << ")\n";
+
+               PrintAgentRepository(fp, agent->Get("repository"));
+       }
 }
 
 void AgentUtility::PrintAgentRepository(std::ostream& fp, const Dictionary::Ptr& repository)
 {
-       //TODO better formatting
-       fp << JsonSerialize(repository);
+       ObjectLock olock(repository);
+       BOOST_FOREACH(const Dictionary::Pair& kv, repository) {
+               fp << std::setw(4) << " "
+                  << "* Host '" << ConsoleColorTag(Console_ForegroundGreen | Console_Bold) << kv.first << ConsoleColorTag(Console_Normal) << "'\n";
+
+               Array::Ptr services = kv.second;
+               ObjectLock xlock(services);
+               BOOST_FOREACH(const String& service, services) {
+                       fp << std::setw(8) << " " << "* Service '" << ConsoleColorTag(Console_ForegroundGreen | Console_Bold) << service << ConsoleColorTag(Console_Normal) << "'\n";
+               }
+       }
 }
 
 void AgentUtility::PrintAgentsJson(std::ostream& fp)
 {
-       std::vector<String> agents;
-       GetAgents(agents);
+       Dictionary::Ptr result = make_shared<Dictionary>();
 
-       BOOST_FOREACH(const String& agent, agents) {
-               Dictionary::Ptr agent_obj = GetAgentFromRepository(GetAgentRepositoryFile(agent));
-               if (agent_obj) {
-                       fp << JsonSerialize(agent_obj);
-               }
+       BOOST_FOREACH(const Dictionary::Ptr& agent, GetAgents()) {
+               result->Set(agent->Get("endpoint"), agent);
        }
+
+       fp << JsonSerialize(result);
 }
 
 bool AgentUtility::AddAgent(const String& name)
@@ -230,34 +230,27 @@ Dictionary::Ptr AgentUtility::GetAgentFromRepository(const String& filename)
 
        String content((std::istreambuf_iterator<char>(fp)), std::istreambuf_iterator<char>());
 
-       std::cout << "Content: " << content << "\n";
-
        fp.close();
 
        return JsonDeserialize(content);
 }
 
-bool AgentUtility::GetAgents(std::vector<String>& agents)
+std::vector<Dictionary::Ptr> AgentUtility::GetAgents(void)
 {
-       String path = GetRepositoryPath();
+       std::vector<Dictionary::Ptr> agents;
 
-       if (!Utility::Glob(path + "/*.repo",
-           boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile)) {
-               Log(LogCritical, "cli")
-                   << "Cannot access path '" << path << "'.";
-               return false;
-       }
+       Utility::Glob(GetRepositoryPath() + "/*.repo",
+           boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile);
 
-       return true;
+       return agents;
 }
 
-void AgentUtility::CollectAgents(const String& agent_file, std::vector<String>& agents)
+void AgentUtility::CollectAgents(const String& agent_file, std::vector<Dictionary::Ptr>& agents)
 {
-       String agent = Utility::BaseName(agent_file);
-       boost::algorithm::replace_all(agent, ".repo", "");
+       Dictionary::Ptr agent = GetAgentFromRepository(agent_file);
 
-       Log(LogDebug, "cli")
-           << "Adding agent: " << agent;
+       if (!agent)
+               return;
 
        agents.push_back(agent);
 }
index d15ae95b5861ba87288ffa93e3e0e3c30c8dfbad..dae441d9a84013bcb95c35a3a47de3015c26f3ac 100644 (file)
@@ -39,7 +39,7 @@ public:
        static String GetRepositoryPath(void);
        static String GetAgentRepositoryFile(const String& name);
        static String GetAgentSettingsFile(const String& name);
-       static std::vector<String> GetFieldCompletionSuggestions(const String& word);
+       static std::vector<String> GetAgentCompletionSuggestions(const String& word);
 
        static void PrintAgents(std::ostream& fp);
        static void PrintAgentsJson(std::ostream& fp);
@@ -52,7 +52,7 @@ public:
        static bool WriteAgentToRepository(const String& filename, const Dictionary::Ptr& item);
        static Dictionary::Ptr GetAgentFromRepository(const String& filename);
 
-       static bool GetAgents(std::vector<String>& agents);
+       static std::vector<Dictionary::Ptr> GetAgents(void);
 
        static bool CreateBackupFile(const String& target);
 
@@ -67,7 +67,7 @@ public:
 private:
        AgentUtility(void);
        static bool RemoveAgentFile(const String& path);
-       static void CollectAgents(const String& agent_file, std::vector<String>& agents);
+       static void CollectAgents(const String& agent_file, std::vector<Dictionary::Ptr>& agents);
 
        static void SerializeObject(std::ostream& fp, const String& name, const String& type, const Dictionary::Ptr& object);
        static void FormatValue(std::ostream& fp, const Value& val);