Skip to content

Commit

Permalink
CLI: Add agent command functionality (WIP)
Browse files Browse the repository at this point in the history
refs #7248
  • Loading branch information
Michael Friedrich committed Oct 18, 2014
1 parent b0d7d8d commit 0073dfe
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 7 deletions.
7 changes: 6 additions & 1 deletion lib/cli/agentaddcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
******************************************************************************/

#include "cli/agentaddcommand.hpp"
#include "cli/agentutility.hpp"
#include "base/logger_fwd.hpp"
#include "base/clicommand.hpp"
#include "base/application.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand Down Expand Up @@ -54,7 +56,10 @@ int AgentAddCommand::Run(const boost::program_options::variables_map& vm, const
return 1;
}

//ap[0] must contain name
String name = ap[0];

if (!AgentUtility::AddAgent(name))
return 1;

return 0;
}
1 change: 1 addition & 0 deletions lib/cli/agentblackandwhitelistcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "base/application.hpp"
#include "base/tlsutility.hpp"
#include <boost/algorithm/string/case_conv.hpp>
#include <iostream>
#include <fstream>

using namespace icinga;
Expand Down
7 changes: 7 additions & 0 deletions lib/cli/agentlistcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
******************************************************************************/

#include "cli/agentlistcommand.hpp"
#include "cli/agentutility.hpp"
#include "base/logger_fwd.hpp"
#include "base/clicommand.hpp"
#include "base/application.hpp"
#include "base/console.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand Down Expand Up @@ -53,5 +56,9 @@ int AgentListCommand::Run(const boost::program_options::variables_map& vm, const
Log(LogWarning, "cli", "Ignoring parameters: " + boost::algorithm::join(ap, " "));
}

std::cout << "Configured agents: \n";
AgentUtility::PrintAgents(std::cout);
std::cout << "\n";

return 0;
}
11 changes: 11 additions & 0 deletions lib/cli/agentremovecommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
******************************************************************************/

#include "cli/agentremovecommand.hpp"
#include "cli/agentutility.hpp"
#include "base/logger_fwd.hpp"
#include "base/clicommand.hpp"
#include "base/application.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand All @@ -42,6 +44,11 @@ String AgentRemoveCommand::GetShortDescription(void) const
return "remove agent";
}

std::vector<String> AgentRemoveCommand::GetPositionalSuggestions(const String& word) const
{
return AgentUtility::GetFieldCompletionSuggestions(word);
}

/**
* The entry point for the "agent remove" CLI command.
*
Expand All @@ -55,6 +62,10 @@ int AgentRemoveCommand::Run(const boost::program_options::variables_map& vm, con
}

//ap[0] must contain name
String name = ap[0];

if (!AgentUtility::RemoveAgent(name))
return 1;

return 0;
}
1 change: 1 addition & 0 deletions lib/cli/agentremovecommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class AgentRemoveCommand : public CLICommand

virtual String GetDescription(void) const;
virtual String GetShortDescription(void) const;
virtual std::vector<String> GetPositionalSuggestions(const String& word) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
};

Expand Down
1 change: 1 addition & 0 deletions lib/cli/agentsetcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand Down
1 change: 1 addition & 0 deletions lib/cli/agentsetupcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand Down
1 change: 1 addition & 0 deletions lib/cli/agentupdateconfigcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand Down
175 changes: 172 additions & 3 deletions lib/cli/agentutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,194 @@
******************************************************************************/

#include "cli/agentutility.hpp"
#include "base/logger_fwd.hpp"
#include "base/clicommand.hpp"
#include "base/application.hpp"
#include "base/convert.hpp"
#include "base/serializer.hpp"
#include "base/netstring.hpp"
#include "base/stdiostream.hpp"
#include "base/debug.hpp"
#include "base/objectlock.hpp"
#include "base/console.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <fstream>
#include <iostream>

using namespace icinga;

void AgentUtility::ListAgents(void)
String AgentUtility::GetRepositoryPath(void)
{
return Application::GetLocalStateDir() + "/lib/icinga2/api/repository/";
}

std::vector<String> AgentUtility::GetFieldCompletionSuggestions(const String& word)
{
std::vector<String> cache;
std::vector<String> suggestions;

GetAgents(cache);

std::sort(cache.begin(), cache.end());

BOOST_FOREACH(const String& suggestion, cache) {
if (suggestion.Find(word) == 0)
suggestions.push_back(suggestion);
}

return suggestions;
}

void AgentUtility::PrintAgents(std::ostream& fp)
{
std::vector<String> agents;
GetAgents(agents);

BOOST_FOREACH(const String& agent, agents) {
Dictionary::Ptr agent_obj = GetAgentFromRepository(GetRepositoryPath() + agent + ".repo");
fp << "=======================\n";
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";
}
fp << "=======================\n";
}

fp << "All agents: " << boost::algorithm::join(agents, " ") << "\n";
}

void AgentUtility::PrintAgentRepository(std::ostream& fp, const Dictionary::Ptr& repository)
{
//TODO better formatting
fp << JsonSerialize(repository);
}

bool AgentUtility::AddAgent(const String& name)
{
return true;
String path = GetRepositoryPath() + name + ".repo";

Dictionary::Ptr agent = make_shared<Dictionary>();

agent->Set("seen", Utility::GetTime());
agent->Set("endpoint", name);
agent->Set("zone", name);
agent->Set("repository", Empty);

return WriteAgentToRepository(path, agent);
}

bool AgentUtility::AddAgentPeer(const String& name, const String& host, const String& port)
{
String path = GetRepositoryPath() + name + ".peer";

Dictionary::Ptr peer = make_shared<Dictionary>();

peer->Set("agent_host", host);
peer->Set("agent_port", port);

return WriteAgentToRepository(path, peer);
}

bool AgentUtility::RemoveAgent(const String& name)
{
String path = GetRepositoryPath() + name + ".repo";

if (!RemoveAgentFile(path))
return false;

return true;
}

bool AgentUtility::RemoveAgentFile(const String& path)
{
if (!Utility::PathExists(path) ) {
Log(LogCritical, "cli", "Cannot remove '" + path + "'. Does not exist.");
return false;
}

if (unlink(path.CStr()) < 0) {
Log(LogCritical, "cli", "Cannot remove file '" + path +
"'. Failed with error code " + Convert::ToString(errno) + ", \"" + Utility::FormatErrorNumber(errno) + "\".");
return false;
}

return true;
}

bool AgentUtility::SetAgentAttribute(const String& name, const String& attr, const Value& val)
{
GetAgentFromRepository(GetRepositoryPath() + name + ".repo");


return true;
}

bool AgentUtility::SetAgentAttribute(const String& attr, const Value& val)
bool AgentUtility::WriteAgentToRepository(const String& filename, const Dictionary::Ptr& item)
{
Log(LogInformation, "cli", "Dumping config items to file '" + filename + "'");

String tempFilename = filename + ".tmp";

std::ofstream fp(tempFilename.CStr(), std::ofstream::out | std::ostream::trunc);
fp << JsonSerialize(item);
fp.close();

#ifdef _WIN32
_unlink(filename.CStr());
#endif /* _WIN32 */

if (rename(tempFilename.CStr(), filename.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempFilename));
}

return true;
}

Dictionary::Ptr AgentUtility::GetAgentFromRepository(const String& filename)
{
std::fstream fp;
fp.open(filename.CStr(), std::ifstream::in);

if (!fp)
return Dictionary::Ptr();

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)
{
String path = GetRepositoryPath();

if (!Utility::Glob(path + "/*.repo",
boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile)) {
Log(LogCritical, "cli", "Cannot access path '" + path + "'.");
return false;
}

return true;
}

void AgentUtility::CollectAgents(const String& agent_file, std::vector<String>& agents)
{
String agent = Utility::BaseName(agent_file);
boost::algorithm::replace_all(agent, ".repo", "");

Log(LogDebug, "cli", "Adding agent: " + agent);
agents.push_back(agent);
}
19 changes: 16 additions & 3 deletions lib/cli/agentutility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#define AGENTUTILITY_H

#include "base/i2-base.hpp"
#include "base/value.hpp"
#include "base/dictionary.hpp"
#include "base/qstring.hpp"

namespace icinga
{
Expand All @@ -32,13 +33,25 @@ namespace icinga
class AgentUtility
{
public:
static void ListAgents(void);
static String GetRepositoryPath(void);
static std::vector<String> GetFieldCompletionSuggestions(const String& word);

static void PrintAgents(std::ostream& fp);
static void PrintAgentRepository(std::ostream& fp, const Dictionary::Ptr& repository);
static bool AddAgent(const String& name);
static bool AddAgentPeer(const String& name, const String& host, const String& port);
static bool RemoveAgent(const String& name);
static bool SetAgentAttribute(const String& attr, const Value& val);
static bool SetAgentAttribute(const String& name, const String& attr, const Value& val);

static bool WriteAgentToRepository(const String& filename, const Dictionary::Ptr& item);
static Dictionary::Ptr GetAgentFromRepository(const String& filename);

static bool GetAgents(std::vector<String>& agents);

private:
AgentUtility(void);
static bool RemoveAgentFile(const String& path);
static void CollectAgents(const String& agent_file, std::vector<String>& agents);
};

}
Expand Down
1 change: 1 addition & 0 deletions lib/cli/agentwizardcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand Down

0 comments on commit 0073dfe

Please sign in to comment.