mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-29 16:44:29 +02:00
Clean up code a bit
This commit is contained in:
parent
69bf146469
commit
5e7e49708c
@ -56,7 +56,8 @@ int AgentAddCommand::Run(const boost::program_options::variables_map& vm, const
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!AgentUtility::AddAgent(ap[0])) {
|
if (!AgentUtility::AddAgent(ap[0])) {
|
||||||
Log(LogCritical, "cli", "Cannot add agent '" + ap[0] + "'.");
|
Log(LogCritical, "cli")
|
||||||
|
<< "Cannot add agent '" << ap[0] << "'.";
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,9 +27,9 @@ namespace icinga
|
|||||||
|
|
||||||
enum BlackAndWhitelistCommandType
|
enum BlackAndWhitelistCommandType
|
||||||
{
|
{
|
||||||
BlackAndWhitelistCommandAdd,
|
BlackAndWhitelistCommandAdd,
|
||||||
BlackAndWhitelistCommandRemove,
|
BlackAndWhitelistCommandRemove,
|
||||||
BlackAndWhitelistCommandList
|
BlackAndWhitelistCommandList
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -100,13 +100,11 @@ int AgentSetupCommand::SetupMaster(const boost::program_options::variables_map&
|
|||||||
<< "Generating new CA.";
|
<< "Generating new CA.";
|
||||||
|
|
||||||
if (PkiUtility::NewCa() > 0) {
|
if (PkiUtility::NewCa() > 0) {
|
||||||
Log(LogWarning, "cli")
|
Log(LogWarning, "cli", "Found CA, skipping and using the existing one.\n");
|
||||||
<< "Found CA, skipping and using the existing one.\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 2. Generate a self signed certificate */
|
/* 2. Generate a self signed certificate */
|
||||||
Log(LogInformation, "cli")
|
Log(LogInformation, "cli", "Generating new self-signed certificate.");
|
||||||
<< "Generating new self-signed certificate.";
|
|
||||||
|
|
||||||
String local_pki_path = PkiUtility::GetLocalPkiPath();
|
String local_pki_path = PkiUtility::GetLocalPkiPath();
|
||||||
|
|
||||||
@ -126,8 +124,7 @@ int AgentSetupCommand::SetupMaster(const boost::program_options::variables_map&
|
|||||||
String cafile = PkiUtility::GetLocalCaPath() + "/ca.crt";
|
String cafile = PkiUtility::GetLocalCaPath() + "/ca.crt";
|
||||||
|
|
||||||
if (PkiUtility::NewCert(cn, keyfile, Empty, certfile) > 0) {
|
if (PkiUtility::NewCert(cn, keyfile, Empty, certfile) > 0) {
|
||||||
Log(LogCritical, "cli")
|
Log(LogCritical, "cli", "Failed to create self-signed certificate");
|
||||||
<< "Failed to create self-signed certificate";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 3. Copy certificates to /etc/icinga2/pki */
|
/* 3. Copy certificates to /etc/icinga2/pki */
|
||||||
@ -148,14 +145,12 @@ int AgentSetupCommand::SetupMaster(const boost::program_options::variables_map&
|
|||||||
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
||||||
|
|
||||||
/* 4. read zones.conf and update with zone + endpoint information */
|
/* 4. read zones.conf and update with zone + endpoint information */
|
||||||
Log(LogInformation, "cli")
|
Log(LogInformation, "cli", "Generating zone and object configuration.");
|
||||||
<< "Generating zone and object configuration.";
|
|
||||||
|
|
||||||
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
||||||
|
|
||||||
/* 5. enable the ApiListener config (verifiy its data) */
|
/* 5. enable the ApiListener config (verifiy its data) */
|
||||||
Log(LogInformation, "cli")
|
Log(LogInformation, "cli", "Enabling the APIListener feature.");
|
||||||
<< "Enabling the APIListener feature.";
|
|
||||||
|
|
||||||
String api_path = FeatureUtility::GetFeaturesEnabledPath() + "/api.conf";
|
String api_path = FeatureUtility::GetFeaturesEnabledPath() + "/api.conf";
|
||||||
|
|
||||||
@ -166,8 +161,7 @@ int AgentSetupCommand::SetupMaster(const boost::program_options::variables_map&
|
|||||||
<< "Edit the api feature config file '" << api_path << "' and set a secure 'ticket_salt' attribute.";
|
<< "Edit the api feature config file '" << api_path << "' and set a secure 'ticket_salt' attribute.";
|
||||||
|
|
||||||
/* 6. tell the user to reload icinga2 */
|
/* 6. tell the user to reload icinga2 */
|
||||||
Log(LogInformation, "cli")
|
Log(LogInformation, "cli", "Make sure to restart Icinga 2.");
|
||||||
<< "Make sure to restart Icinga 2.";
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -214,8 +208,7 @@ int AgentSetupCommand::SetupAgent(const boost::program_options::variables_map& v
|
|||||||
String pki_path = PkiUtility::GetPkiPath();
|
String pki_path = PkiUtility::GetPkiPath();
|
||||||
|
|
||||||
/* 4. pki request a signed certificate from the master */
|
/* 4. pki request a signed certificate from the master */
|
||||||
Log(LogInformation, "cli")
|
Log(LogInformation, "cli", "Requesting a signed certificate from the master.");
|
||||||
<< "Requesting a signed certificate from the master.";
|
|
||||||
|
|
||||||
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
||||||
|
|
||||||
@ -226,20 +219,17 @@ int AgentSetupCommand::SetupAgent(const boost::program_options::variables_map& v
|
|||||||
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
||||||
|
|
||||||
/* 6. generate local zones.conf with zone+endpoint */
|
/* 6. generate local zones.conf with zone+endpoint */
|
||||||
Log(LogInformation, "cli")
|
Log(LogInformation, "cli", "Generating zone and object configuration.");
|
||||||
<< "Generating zone and object configuration.";
|
|
||||||
|
|
||||||
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
||||||
|
|
||||||
/* 7. update constants.conf with NodeName = CN */
|
/* 7. update constants.conf with NodeName = CN */
|
||||||
Log(LogInformation, "cli")
|
Log(LogInformation, "cli", "Updating configuration with NodeName constant.");
|
||||||
<< "Updating configuration with NodeName constant.";
|
|
||||||
|
|
||||||
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
|
||||||
|
|
||||||
/* 8. tell the user to reload icinga2 */
|
/* 8. tell the user to reload icinga2 */
|
||||||
Log(LogInformation, "cli")
|
Log(LogInformation, "cli", "Make sure to restart Icinga 2.");
|
||||||
<< "Make sure to restart Icinga 2.";
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -117,7 +117,7 @@ bool AgentUtility::AddAgent(const String& name)
|
|||||||
|
|
||||||
if (Utility::PathExists(path) ) {
|
if (Utility::PathExists(path) ) {
|
||||||
Log(LogCritical, "cli")
|
Log(LogCritical, "cli")
|
||||||
<< "Cannot add agent repo. '" + path + "' already exists.\n";
|
<< "Cannot add agent repo. '" << path << "' already exists.\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,13 +147,13 @@ bool AgentUtility::RemoveAgent(const String& name)
|
|||||||
{
|
{
|
||||||
if (!RemoveAgentFile(GetAgentRepositoryFile(name))) {
|
if (!RemoveAgentFile(GetAgentRepositoryFile(name))) {
|
||||||
Log(LogCritical, "cli")
|
Log(LogCritical, "cli")
|
||||||
<< "Cannot remove agent repo. '" + GetAgentRepositoryFile(name) + "' does not exist.\n";
|
<< "Cannot remove agent repo. '" << GetAgentRepositoryFile(name) << "' does not exist.\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (Utility::PathExists(GetAgentSettingsFile(name))) {
|
if (Utility::PathExists(GetAgentSettingsFile(name))) {
|
||||||
if (!RemoveAgentFile(GetAgentSettingsFile(name))) {
|
if (!RemoveAgentFile(GetAgentSettingsFile(name))) {
|
||||||
Log(LogWarning, "cli")
|
Log(LogWarning, "cli")
|
||||||
<< "Cannot remove agent settings. '" + GetAgentSettingsFile(name) + "' does not exist.\n";
|
<< "Cannot remove agent settings. '" << GetAgentSettingsFile(name) << "' does not exist.\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -164,13 +164,13 @@ bool AgentUtility::RemoveAgent(const String& name)
|
|||||||
bool AgentUtility::RemoveAgentFile(const String& path)
|
bool AgentUtility::RemoveAgentFile(const String& path)
|
||||||
{
|
{
|
||||||
if (!Utility::PathExists(path)) {
|
if (!Utility::PathExists(path)) {
|
||||||
Log(LogCritical, "cli", "Cannot remove '" + path + "'. Does not exist.");
|
Log(LogCritical, "cli", "Cannot remove '" << path << "'. Does not exist.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlink(path.CStr()) < 0) {
|
if (unlink(path.CStr()) < 0) {
|
||||||
Log(LogCritical, "cli", "Cannot remove file '" + path +
|
Log(LogCritical, "cli", "Cannot remove file '" << path <<
|
||||||
"'. Failed with error code " + Convert::ToString(errno) + ", \"" + Utility::FormatErrorNumber(errno) + "\".");
|
"'. Failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) + "\".");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,13 +193,13 @@ bool AgentUtility::SetAgentAttribute(const String& name, const String& attr, con
|
|||||||
|
|
||||||
bool AgentUtility::WriteAgentToRepository(const String& filename, const Dictionary::Ptr& item)
|
bool AgentUtility::WriteAgentToRepository(const String& filename, const Dictionary::Ptr& item)
|
||||||
{
|
{
|
||||||
Log(LogInformation, "cli", "Dumping agent to file '" + filename + "'");
|
Log(LogInformation, "cli", "Dumping agent to file '" << filename << "'");
|
||||||
|
|
||||||
String tempFilename = filename + ".tmp";
|
String tempFilename = filename + ".tmp";
|
||||||
|
|
||||||
std::ofstream fp(tempFilename.CStr(), std::ofstream::out | std::ostream::trunc);
|
std::ofstream fp(tempFilename.CStr(), std::ofstream::out | std::ostream::trunc);
|
||||||
fp << JsonSerialize(item);
|
fp << JsonSerialize(item);
|
||||||
fp.close();
|
fp.close();
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
_unlink(filename.CStr());
|
_unlink(filename.CStr());
|
||||||
@ -238,7 +238,7 @@ bool AgentUtility::GetAgents(std::vector<String>& agents)
|
|||||||
|
|
||||||
if (!Utility::Glob(path + "/*.repo",
|
if (!Utility::Glob(path + "/*.repo",
|
||||||
boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile)) {
|
boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile)) {
|
||||||
Log(LogCritical, "cli", "Cannot access path '" + path + "'.");
|
Log(LogCritical, "cli", "Cannot access path '" << path << "'.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,6 +250,6 @@ void AgentUtility::CollectAgents(const String& agent_file, std::vector<String>&
|
|||||||
String agent = Utility::BaseName(agent_file);
|
String agent = Utility::BaseName(agent_file);
|
||||||
boost::algorithm::replace_all(agent, ".repo", "");
|
boost::algorithm::replace_all(agent, ".repo", "");
|
||||||
|
|
||||||
Log(LogDebug, "cli", "Adding agent: " + agent);
|
Log(LogDebug, "cli", "Adding agent: " << agent);
|
||||||
agents.push_back(agent);
|
agents.push_back(agent);
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ public:
|
|||||||
|
|
||||||
virtual String GetDescription(void) const;
|
virtual String GetDescription(void) const;
|
||||||
virtual String GetShortDescription(void) const;
|
virtual String GetShortDescription(void) const;
|
||||||
virtual std::vector<String> GetPositionalSuggestions(const String& word) 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;
|
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -37,7 +37,7 @@ public:
|
|||||||
|
|
||||||
virtual String GetDescription(void) const;
|
virtual String GetDescription(void) const;
|
||||||
virtual String GetShortDescription(void) const;
|
virtual String GetShortDescription(void) const;
|
||||||
virtual std::vector<String> GetPositionalSuggestions(const String& word) 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;
|
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
#include <boost/algorithm/string/join.hpp>
|
#include <boost/algorithm/string/join.hpp>
|
||||||
#include <boost/algorithm/string/replace.hpp>
|
#include <boost/algorithm/string/replace.hpp>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
using namespace icinga;
|
using namespace icinga;
|
||||||
|
|
||||||
|
@ -36,22 +36,22 @@ namespace icinga
|
|||||||
class ObjectListCommand : public CLICommand
|
class ObjectListCommand : public CLICommand
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
DECLARE_PTR_TYPEDEFS(ObjectListCommand);
|
DECLARE_PTR_TYPEDEFS(ObjectListCommand);
|
||||||
|
|
||||||
virtual String GetDescription(void) const;
|
virtual String GetDescription(void) const;
|
||||||
virtual String GetShortDescription(void) const;
|
virtual String GetShortDescription(void) const;
|
||||||
virtual void InitParameters(boost::program_options::options_description& visibleDesc,
|
virtual void InitParameters(boost::program_options::options_description& visibleDesc,
|
||||||
boost::program_options::options_description& hiddenDesc) const;
|
boost::program_options::options_description& hiddenDesc) const;
|
||||||
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
|
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void PrintObject(std::ostream& fp, bool& first, const String& message, std::map<String, int>& type_count, const String& name_filter, const String& type_filter);
|
static void PrintObject(std::ostream& fp, bool& first, const String& message, std::map<String, int>& type_count, const String& name_filter, const String& type_filter);
|
||||||
static void PrintProperties(std::ostream& fp, const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent = 0);
|
static void PrintProperties(std::ostream& fp, const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent = 0);
|
||||||
static void PrintHints(std::ostream& fp, const Dictionary::Ptr& hints, int indent = 0);
|
static void PrintHints(std::ostream& fp, const Dictionary::Ptr& hints, int indent = 0);
|
||||||
static void PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent = 0);
|
static void PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent = 0);
|
||||||
static void PrintTypeCounts(std::ostream& fp, const std::map<String, int>& type_count);
|
static void PrintTypeCounts(std::ostream& fp, const std::map<String, int>& type_count);
|
||||||
static void PrintValue(std::ostream& fp, const Value& val);
|
static void PrintValue(std::ostream& fp, const Value& val);
|
||||||
static void PrintArray(std::ostream& fp, const Array::Ptr& arr);
|
static void PrintArray(std::ostream& fp, const Array::Ptr& arr);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -262,17 +262,17 @@ int PkiUtility::RequestCertificate(const String& host, const String& port, const
|
|||||||
bool PkiUtility::CopyCertFile(const String& source, const String& target)
|
bool PkiUtility::CopyCertFile(const String& source, const String& target)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
if (PathExists(target)) {
|
if (PathExists(target)) {
|
||||||
Log(LogWarning, "Utility")
|
Log(LogWarning, "Utility")
|
||||||
<< "Target file '" << target << "' already exists.";
|
<< "Target file '" << target << "' already exists.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ifstream ifs(source, std::ios::binary);
|
std::ifstream ifs(source, std::ios::binary);
|
||||||
std::ofstream ofs(target, std::ios::binary);
|
std::ofstream ofs(target, std::ios::binary);
|
||||||
|
|
||||||
ofs << ifs.rdbuf();
|
ofs << ifs.rdbuf();
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -36,13 +36,13 @@ namespace icinga
|
|||||||
class VariableGetCommand : public CLICommand
|
class VariableGetCommand : public CLICommand
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
DECLARE_PTR_TYPEDEFS(VariableGetCommand);
|
DECLARE_PTR_TYPEDEFS(VariableGetCommand);
|
||||||
|
|
||||||
virtual String GetDescription(void) const;
|
virtual String GetDescription(void) const;
|
||||||
virtual String GetShortDescription(void) const;
|
virtual String GetShortDescription(void) const;
|
||||||
void InitParameters(boost::program_options::options_description& visibleDesc,
|
void InitParameters(boost::program_options::options_description& visibleDesc,
|
||||||
boost::program_options::options_description& hiddenDesc) const;
|
boost::program_options::options_description& hiddenDesc) const;
|
||||||
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
|
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,9 +38,9 @@ class VariableListCommand : public CLICommand
|
|||||||
public:
|
public:
|
||||||
DECLARE_PTR_TYPEDEFS(VariableListCommand);
|
DECLARE_PTR_TYPEDEFS(VariableListCommand);
|
||||||
|
|
||||||
virtual String GetDescription(void) const;
|
virtual String GetDescription(void) const;
|
||||||
virtual String GetShortDescription(void) const;
|
virtual String GetShortDescription(void) const;
|
||||||
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
|
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void PrintVariable(std::ostream& fp, const String& message);
|
static void PrintVariable(std::ostream& fp, const String& message);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user