Skip to content

Commit

Permalink
CLI: Add 'troubleshoot collect' command
Browse files Browse the repository at this point in the history
By calling `icinga2 troubleshoot collect [--console]` a small file
containing basic application information and a tail of all found logs
and the latest crash report will be created [or displayed].
It does not collect config files at the moment.

refs #3446
  • Loading branch information
Crunsher authored and Michael Friedrich committed Feb 15, 2015
1 parent 4a64d49 commit cc5a8da
Show file tree
Hide file tree
Showing 8 changed files with 628 additions and 96 deletions.
3 changes: 2 additions & 1 deletion lib/cli/CMakeLists.txt
Expand Up @@ -20,13 +20,14 @@ set(cli_SOURCES
nodesetcommand.cpp nodesetupcommand.cpp nodeupdateconfigcommand.cpp nodewizardcommand.cpp nodeutility.cpp
clicommand.cpp
consolecommand.cpp
daemoncommand.cpp
daemoncommand.cpp daemonutility.cpp
featureenablecommand.cpp featuredisablecommand.cpp featurelistcommand.cpp featureutility.cpp
objectlistcommand.cpp
pkinewcacommand.cpp pkinewcertcommand.cpp pkisigncsrcommand.cpp pkirequestcommand.cpp pkisavecertcommand.cpp pkiticketcommand.cpp
pkiutility.cpp
repositoryclearchangescommand.cpp repositorycommitcommand.cpp repositoryobjectcommand.cpp repositoryutility.cpp
variablegetcommand.cpp variablelistcommand.cpp variableutility.cpp
troubleshootcollectcommand.cpp
)

if(ICINGA2_UNITY_BUILD)
Expand Down
99 changes: 8 additions & 91 deletions lib/cli/daemoncommand.cpp
Expand Up @@ -18,12 +18,12 @@
******************************************************************************/

#include "cli/daemoncommand.hpp"
#include "cli/daemonutility.hpp"
#include "config/configcompiler.hpp"
#include "config/configcompilercontext.hpp"
#include "config/configitembuilder.hpp"
#include "base/logger.hpp"
#include "base/application.hpp"
#include "base/logger.hpp"
#include "base/timer.hpp"
#include "base/utility.hpp"
#include "base/exception.hpp"
Expand Down Expand Up @@ -60,95 +60,6 @@ static String LoadAppType(const String& typeSpec)
return typeSpec.SubStr(index + 1);
}

static bool ExecuteExpression(Expression *expression)
{
if (!expression)
return false;

try {
ScriptFrame frame;
expression->Evaluate(frame);
} catch (const std::exception& ex) {
Log(LogCritical, "config", DiagnosticInformation(ex));
Application::Exit(EXIT_FAILURE);
}

return true;
}

static void IncludeZoneDirRecursive(const String& path)
{
String zoneName = Utility::BaseName(path);

std::vector<Expression *> expressions;
Utility::GlobRecursive(path, "*.conf", boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zoneName), GlobFile);
DictExpression expr(expressions);
ExecuteExpression(&expr);
}

static void IncludeNonLocalZone(const String& zonePath)
{
String etcPath = Application::GetZonesDir() + "/" + Utility::BaseName(zonePath);

if (Utility::PathExists(etcPath) || Utility::PathExists(zonePath + "/.authoritative"))
return;

IncludeZoneDirRecursive(zonePath);
}

static bool LoadConfigFiles(const boost::program_options::variables_map& vm, const String& appType,
const String& objectsFile = String(), const String& varsfile = String())
{
if (!objectsFile.IsEmpty())
ConfigCompilerContext::GetInstance()->OpenObjectsFile(objectsFile);

if (vm.count("config") > 0) {
BOOST_FOREACH(const String& configPath, vm["config"].as<std::vector<std::string> >()) {
Expression *expression = ConfigCompiler::CompileFile(configPath);
ExecuteExpression(expression);
delete expression;
}
} else if (!vm.count("no-config")) {
Expression *expression = ConfigCompiler::CompileFile(Application::GetSysconfDir() + "/icinga2/icinga2.conf");
ExecuteExpression(expression);
delete expression;
}

/* Load cluster config files - this should probably be in libremote but
* unfortunately moving it there is somewhat non-trivial. */
String zonesEtcDir = Application::GetZonesDir();
if (!zonesEtcDir.IsEmpty() && Utility::PathExists(zonesEtcDir))
Utility::Glob(zonesEtcDir + "/*", &IncludeZoneDirRecursive, GlobDirectory);

String zonesVarDir = Application::GetLocalStateDir() + "/lib/icinga2/api/zones";
if (Utility::PathExists(zonesVarDir))
Utility::Glob(zonesVarDir + "/*", &IncludeNonLocalZone, GlobDirectory);

String name, fragment;
BOOST_FOREACH(boost::tie(name, fragment), ConfigFragmentRegistry::GetInstance()->GetItems()) {
Expression *expression = ConfigCompiler::CompileText(name, fragment);
ExecuteExpression(expression);
delete expression;
}

ConfigItemBuilder::Ptr builder = new ConfigItemBuilder();
builder->SetType(appType);
builder->SetName("application");
ConfigItem::Ptr item = builder->Compile();
item->Register();

bool result = ConfigItem::CommitItems();

if (!result)
return false;

ConfigCompilerContext::GetInstance()->FinishObjectsFile();

ScriptGlobal::WriteToFile(varsfile);

return true;
}

#ifndef _WIN32
static void SigHupHandler(int)
{
Expand Down Expand Up @@ -339,7 +250,13 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::strin
}
}

if (!LoadConfigFiles(vm, appType, Application::GetObjectsPath(), Application::GetVarsPath()))
std::vector<std::string> configs;
if (vm.count("config") > 0)
configs = vm["config"].as < std::vector<std::string> >() ;
else if (!vm.count("no-config"))
configs.push_back(Application::GetSysconfDir() + "/icinga2/icinga2.conf");

if (!DaemonUtility::LoadConfigFiles(configs, appType, Application::GetObjectsPath(), Application::GetVarsPath()))
return EXIT_FAILURE;

if (vm.count("validate")) {
Expand Down
131 changes: 131 additions & 0 deletions lib/cli/daemonutility.cpp
@@ -0,0 +1,131 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/

#include "cli/daemonutility.hpp"
#include "base/utility.hpp"
#include "base/logger.hpp"
#include "base/application.hpp"
#include "config/configcompiler.hpp"
#include "config/configcompilercontext.hpp"
#include "config/configitembuilder.hpp"


using namespace icinga;

bool ExecuteExpression(Expression *expression)
{
if (!expression)
return false;

try {
ScriptFrame frame;
expression->Evaluate(frame);
} catch (const std::exception& ex) {
Log(LogCritical, "config", DiagnosticInformation(ex));
return false;
}

return true;
}

void IncludeZoneDirRecursive(const String& path, bool& success)
{
String zoneName = Utility::BaseName(path);

std::vector<Expression *> expressions;
Utility::GlobRecursive(path, "*.conf", boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zoneName), GlobFile);
DictExpression expr(expressions);
if (!ExecuteExpression(&expr))
success = false;
}

void IncludeNonLocalZone(const String& zonePath, bool& success)
{
String etcPath = Application::GetZonesDir() + "/" + Utility::BaseName(zonePath);

if (Utility::PathExists(etcPath) || Utility::PathExists(zonePath + "/.authoritative"))
return;

IncludeZoneDirRecursive(zonePath, success);
}

bool DaemonUtility::ValidateConfigFiles(const std::vector<std::string>& configs, const String& objectsFile)
{
bool success;
if (!objectsFile.IsEmpty())
ConfigCompilerContext::GetInstance()->OpenObjectsFile(objectsFile);

if (!configs.empty()) {
BOOST_FOREACH(const String& configPath, configs)
{
Expression *expression = ConfigCompiler::CompileFile(configPath);
success = ExecuteExpression(expression);
delete expression;
if (!success)
return false;
}
}

/* Load cluster config files - this should probably be in libremote but
* unfortunately moving it there is somewhat non-trivial. */
String zonesEtcDir = Application::GetZonesDir();
if (!zonesEtcDir.IsEmpty() && Utility::PathExists(zonesEtcDir))
Utility::Glob(zonesEtcDir + "/*", boost::bind(&IncludeZoneDirRecursive, _1, boost::ref(success)), GlobDirectory);
if (!success)
return false;

String zonesVarDir = Application::GetLocalStateDir() + "/lib/icinga2/api/zones";
if (Utility::PathExists(zonesVarDir))
Utility::Glob(zonesVarDir + "/*", boost::bind(&IncludeNonLocalZone, _1, boost::ref(success)), GlobDirectory);
if (!success)
return false;

String name, fragment;
BOOST_FOREACH(boost::tie(name, fragment), ConfigFragmentRegistry::GetInstance()->GetItems())
{
Expression *expression = ConfigCompiler::CompileText(name, fragment);
success = ExecuteExpression(expression);
delete expression;
if (!success)
return false;
}

return true;
}

bool DaemonUtility::LoadConfigFiles(const std::vector<std::string>& configs, const String& appType,
const String& objectsFile, const String& varsfile)
{
if (!DaemonUtility::ValidateConfigFiles(configs, objectsFile))
return false;

ConfigItemBuilder::Ptr builder = new ConfigItemBuilder();
builder->SetType(appType);
builder->SetName("application");
ConfigItem::Ptr item = builder->Compile();
item->Register();

bool result = ConfigItem::CommitItems();

if (!result)
return false;

ConfigCompilerContext::GetInstance()->FinishObjectsFile();
ScriptGlobal::WriteToFile(varsfile);
}
36 changes: 36 additions & 0 deletions lib/cli/daemonutility.hpp
@@ -0,0 +1,36 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/

#ifndef DAEMONUTILIT_H
#define DAEMONUTILIT_H

//#include "base/i2-base.hpp"
#include "base/string.hpp"
#include <boost/program_options.hpp>

namespace icinga
{
class DaemonUtility
{
public:
static bool ValidateConfigFiles(const std::vector<std::string>& configs, const String& objectsFile = String());
static bool LoadConfigFiles(const std::vector<std::string>& configs, const String& appType, const String& objectsFile = String(), const String& varsfile = String());
};
}
#endif /*DAEMONULITIY_H*/
6 changes: 3 additions & 3 deletions lib/cli/featureutility.cpp
Expand Up @@ -175,21 +175,21 @@ int FeatureUtility::DisableFeatures(const std::vector<std::string>& features)
return 0;
}

int FeatureUtility::ListFeatures(void)
int FeatureUtility::ListFeatures(std::ostream& os)
{
std::vector<String> disabled_features;
std::vector<String> enabled_features;

if (!FeatureUtility::GetFeatures(disabled_features, true))
return 1;

std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "Disabled features: " << ConsoleColorTag(Console_Normal)
os << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "Disabled features: " << ConsoleColorTag(Console_Normal)
<< boost::algorithm::join(disabled_features, " ") << "\n";

if (!FeatureUtility::GetFeatures(enabled_features, false))
return 1;

std::cout << ConsoleColorTag(Console_ForegroundGreen | Console_Bold) << "Enabled features: " << ConsoleColorTag(Console_Normal)
os << ConsoleColorTag(Console_ForegroundGreen | Console_Bold) << "Enabled features: " << ConsoleColorTag(Console_Normal)
<< boost::algorithm::join(enabled_features, " ") << "\n";

return 0;
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/featureutility.hpp
Expand Up @@ -40,7 +40,7 @@ class FeatureUtility

static int EnableFeatures(const std::vector<std::string>& features);
static int DisableFeatures(const std::vector<std::string>& features);
static int ListFeatures(void);
static int ListFeatures(std::ostream& os = std::cout);

static bool GetFeatures(std::vector<String>& features, bool enable);

Expand Down

0 comments on commit cc5a8da

Please sign in to comment.