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 e98d719 commit 0fc7c2c
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/cli/agentaddcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
******************************************************************************/

#include "cli/agentaddcommand.hpp"
#include "cli/agentutility.hpp"
#include "base/logger_fwd.hpp"
#include "base/clicommand.hpp"
#include "base/application.hpp"
Expand Down Expand Up @@ -55,6 +56,9 @@ int AgentAddCommand::Run(const boost::program_options::variables_map& vm, const
}

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

AgentUtility::AddAgent(name);

return 0;
}
143 changes: 141 additions & 2 deletions lib/cli/agentutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,164 @@
******************************************************************************/

#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 <fstream>
#include <iostream>

using namespace icinga;

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

void AgentUtility::ListAgents(void)
{

}

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

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

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";
String path_peer = GetRepositoryPath() + name + ".peer";

if (!RemoveAgentFile(path) || !RemoveAgentFile(path_peer))
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);


return true;
}

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

String tempFilename = filename + ".tmp";

std::fstream fp;
fp.open(tempFilename.CStr(), std::ios_base::out);

if (!fp)
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + tempFilename + "' file"));

StdioStream::Ptr sfp = make_shared<StdioStream>(&fp, false);

String json = JsonSerialize(item);

NetString::WriteStringToStream(sfp, json);

sfp->Close();

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;
}

bool AgentUtility::SetAgentAttribute(const String& attr, const Value& val)
Dictionary::Ptr AgentUtility::GetAgentFromRepository(const String& filename)
{
/*
std::fstream fp;
fp.open(filename.CStr(), std::ios_base::in);
StdioStream::Ptr sfp = make_shared<StdioStream>(&fp, false);
while (NetString::ReadStringFromStream(sfp, &message)) {
PrintObject(std::cout, first, message, type_count, name_filter, type_filter);
objects_count++;
}
sfp->Close();
fp.close();
*/
return Dictionary::Ptr();
}

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);
}
14 changes: 12 additions & 2 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,22 @@ namespace icinga
class AgentUtility
{
public:
static String GetRepositoryPath(void);
static void ListAgents(void);
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

0 comments on commit 0fc7c2c

Please sign in to comment.