******************************************************************************/
#include "cli/agentsetcommand.hpp"
+#include "cli/agentutility.hpp"
#include "base/logger.hpp"
#include "base/application.hpp"
#include <boost/foreach.hpp>
void AgentSetCommand::InitParameters(boost::program_options::options_description& visibleDesc,
boost::program_options::options_description& hiddenDesc) const
{
-
+ visibleDesc.add_options()
+ ("host", po::value<std::string>(), "Icinga 2 host")
+ ("port", po::value<std::string>(), "Icinga 2 port")
+ ("log_duration", po::value<double>(), "Log duration (in seconds)");
}
int AgentSetCommand::GetMinArguments(void) const
*/
int AgentSetCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
{
- Log(LogWarning, "cli", "TODO: Not implemented yet.");
+ String repoFile = AgentUtility::GetAgentRepositoryFile(ap[0]);
+
+ if (!Utility::PathExists(repoFile)) {
+ Log(LogCritical, "cli")
+ << "Agent '" << ap[0] << "' does not exist.";
+ return 1;
+ }
+
+ String host, port = "5665";
+ double log_duration = 24 * 60 * 60;
+
+ if (vm.count("host"))
+ host = vm["host"].as<std::string>();
+
+ if (vm.count("port"))
+ port = vm["port"].as<std::string>();
+
+ if (vm.count("log_duration"))
+ log_duration = vm["log_duration"].as<double>();
+
+ AgentUtility::AddAgentSettings(ap[0], host, port, log_duration);
return 0;
}
#include "base/application.hpp"
#include "base/tlsutility.hpp"
#include "base/convert.hpp"
+#include "base/utility.hpp"
#include "base/json.hpp"
#include "base/netstring.hpp"
#include "base/stdiostream.hpp"
fp << "Agent '"
<< ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << agent->Get("endpoint") << ConsoleColorTag(Console_Normal)
- << "' (last seen: " << Utility::FormatDateTime("%c", agent->Get("seen")) << ")\n";
+ << "' (";
+
+ Dictionary::Ptr settings = agent->Get("settings");
+
+ if (settings) {
+ String host = settings->Get("host");
+ String port = settings->Get("port");
+ double log_duration = settings->Get("log_duration");
+
+ if (!host.IsEmpty() && !port.IsEmpty())
+ fp << "host: " << host << ", port: " << port << ", ";
+
+ fp << "log duration: " << Utility::FormatDuration(log_duration) << ", ";
+ }
+
+ fp << "last seen: " << Utility::FormatDateTime("%c", agent->Get("seen")) << ")\n";
PrintAgentRepository(fp, agent->Get("repository"));
}
fp << JsonEncode(result);
}
-bool AgentUtility::AddAgent(const String& name)
+void AgentUtility::AddAgent(const String& name)
{
String path = GetAgentRepositoryFile(name);
if (Utility::PathExists(path) ) {
- Log(LogCritical, "cli")
- << "Cannot add agent repo. '" << path << "' already exists.\n";
- return false;
+ Log(LogInformation, "cli")
+ << "Agent '" << name << "' exists already.";
}
Dictionary::Ptr agent = make_shared<Dictionary>();
agent->Set("zone", name);
agent->Set("repository", Empty);
- return WriteAgentToRepository(path, agent);
+ Utility::SaveJsonFile(path, agent);
}
-bool AgentUtility::AddAgentSettings(const String& name, const String& host, const String& port)
+void AgentUtility::AddAgentSettings(const String& name, const String& host,
+ const String& port, double log_duration)
{
- String path = GetAgentSettingsFile(name);
+ Dictionary::Ptr settings = make_shared<Dictionary>();
- Dictionary::Ptr peer = make_shared<Dictionary>();
+ settings->Set("host", host);
+ settings->Set("port", port);
+ settings->Set("log_duration", log_duration);
- peer->Set("agent_host", host);
- peer->Set("agent_port", port);
-
- return WriteAgentToRepository(path, peer);
+ Utility::SaveJsonFile(GetAgentSettingsFile(name), settings);
}
-bool AgentUtility::RemoveAgent(const String& name)
+void AgentUtility::RemoveAgent(const String& name)
{
- if (!RemoveAgentFile(GetAgentRepositoryFile(name))) {
- Log(LogCritical, "cli")
- << "Cannot remove agent repo. '" << GetAgentRepositoryFile(name) << "' does not exist.\n";
- return false;
- }
-
- if (Utility::PathExists(GetAgentSettingsFile(name))) {
- if (!RemoveAgentFile(GetAgentSettingsFile(name))) {
- Log(LogWarning, "cli")
- << "Cannot remove agent settings. '" << GetAgentSettingsFile(name) << "' does not exist.\n";
- return false;
- }
- }
+ String repoPath = GetAgentRepositoryFile(name);
- return true;
-}
-
-bool AgentUtility::RemoveAgentFile(const String& path)
-{
- if (!Utility::PathExists(path)) {
- Log(LogCritical, "cli")
- << "Cannot remove '" << path << "'. Does not exist.";
- return false;
- }
+ if (!Utility::PathExists(repoPath))
+ return;
- if (unlink(path.CStr()) < 0) {
+ if (unlink(repoPath.CStr()) < 0) {
Log(LogCritical, "cli")
- << "Cannot remove file '" << path
+ << "Cannot remove file '" << repoPath
<< "'. Failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) + "\".";
- return false;
- }
-
- return true;
-}
+ BOOST_THROW_EXCEPTION(posix_error()
+ << boost::errinfo_api_function("unlink")
+ << boost::errinfo_errno(errno)
+ << boost::errinfo_file_name(repoPath));
+ }
-bool AgentUtility::SetAgentAttribute(const String& name, const String& attr, const Value& val)
-{
- String repo_path = GetAgentRepositoryFile(name);
- Dictionary::Ptr repo = GetAgentFromRepository(repo_path);
+ String settingsPath = GetAgentSettingsFile(name);
- if (repo) {
- repo->Set(attr, val);
- WriteAgentToRepository(repo_path, repo);
- return true;
+ if (Utility::PathExists(settingsPath)) {
+ if (unlink(settingsPath.CStr()) < 0) {
+ Log(LogCritical, "cli")
+ << "Cannot remove file '" << settingsPath
+ << "'. Failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) + "\".";
+ BOOST_THROW_EXCEPTION(posix_error()
+ << boost::errinfo_api_function("unlink")
+ << boost::errinfo_errno(errno)
+ << boost::errinfo_file_name(settingsPath));
+ }
}
-
- return false;
}
-bool AgentUtility::WriteAgentToRepository(const String& filename, const Dictionary::Ptr& item)
+std::vector<Dictionary::Ptr> AgentUtility::GetAgents(void)
{
- Log(LogInformation, "cli")
- << "Dumping agent to file '" << filename << "'";
-
- String tempFilename = filename + ".tmp";
-
- std::ofstream fp(tempFilename.CStr(), std::ofstream::out | std::ostream::trunc);
- fp << JsonEncode(item);
- fp.close();
-
-#ifdef _WIN32
- _unlink(filename.CStr());
-#endif /* _WIN32 */
+ std::vector<Dictionary::Ptr> agents;
- 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));
- }
+ Utility::Glob(GetRepositoryPath() + "/*.repo",
+ boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile);
- return true;
+ return agents;
}
-Dictionary::Ptr AgentUtility::GetAgentFromRepository(const String& filename)
+Dictionary::Ptr AgentUtility::LoadAgentFile(const String& agent_file)
{
- std::fstream fp;
- fp.open(filename.CStr(), std::ifstream::in);
+ Dictionary::Ptr agent = Utility::LoadJsonFile(agent_file);
- if (!fp)
+ if (!agent)
return Dictionary::Ptr();
- String content((std::istreambuf_iterator<char>(fp)), std::istreambuf_iterator<char>());
-
- fp.close();
+ String settingsFile = GetAgentSettingsFile(agent->Get("endpoint"));
- return JsonDecode(content);
-}
+ if (Utility::PathExists(settingsFile))
+ agent->Set("settings", Utility::LoadJsonFile(settingsFile));
+ else
+ agent->Remove("settings");
-std::vector<Dictionary::Ptr> AgentUtility::GetAgents(void)
-{
- std::vector<Dictionary::Ptr> agents;
-
- Utility::Glob(GetRepositoryPath() + "/*.repo",
- boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile);
-
- return agents;
+ return agent;
}
void AgentUtility::CollectAgents(const String& agent_file, std::vector<Dictionary::Ptr>& agents)
{
- Dictionary::Ptr agent = GetAgentFromRepository(agent_file);
+ Dictionary::Ptr agent = LoadAgentFile(agent_file);
if (!agent)
return;