Work on troubleshoot cli

Changes the format timestamps are displayed.
--include-vars and --include-objects now print to console if --console
is given.
Also fixes two memory leaks.

fixes #8564 #8563
refs 3446
This commit is contained in:
Jean-Marcel Flach 2015-03-02 12:17:34 +01:00
parent 5968622108
commit 68e7de7e59
2 changed files with 76 additions and 35 deletions

View File

@ -185,28 +185,44 @@ bool TroubleshootCommand::ObjectInfo(InfoLog& log, const boost::program_options:
return false; return false;
} else { } else {
InfoLog *OFile = NULL; InfoLog *OFile = NULL;
bool OConsole = false;
if (vm.count("include-objects")) { if (vm.count("include-objects")) {
OFile = new InfoLog(path+"-objects", false); if (vm.count("console"))
if (!OFile->GetStreamHealth()) { OConsole = true;
InfoLogLine(log, 0, LogWarning) else {
<< "Failed to open Object-write-stream, not printing objects\n\n"; OFile = new InfoLog(path+"-objects", false);
OFile = NULL; if (!OFile->GetStreamHealth()) {
} else InfoLogLine(log, 0, LogWarning)
InfoLogLine(log) << "Failed to open Object-write-stream, not printing objects\n\n";
<< "Printing all objects to " << path+"-objects\n"; delete OFile;
OFile = NULL;
} else
InfoLogLine(log)
<< "Printing all objects to " << path+"-objects\n";
}
} }
CheckObjectFile(objectfile, log, OFile, logs, configs); CheckObjectFile(objectfile, log, OFile, OConsole, logs, configs);
if (OFile != NULL) if (OFile != NULL)
delete OFile; delete OFile;
} }
if (vm.count("include-vars")) { if (vm.count("include-vars")) {
if (PrintVarsFile(path)) if (vm.count("console")) {
InfoLogLine(log) InfoLogLine(log, Console_ForegroundBlue)
<< "Successfully printed all variables to " << path+"-vars\n"; << "\n[begin: varsfile]\n";
else if (!PrintVarsFile(path, true))
InfoLogLine(log, 0, LogWarning) InfoLogLine(log, 0, LogWarning)
<< "Failed to prin vars to " << path+"-vars\n"; << "Failed to print vars file\n";
InfoLogLine(log, Console_ForegroundBlue)
<< "[end: varsfile]\n";
} else {
if (PrintVarsFile(path, false))
InfoLogLine(log)
<< "Successfully printed all variables to " << path+"-vars\n";
else
InfoLogLine(log, 0, LogWarning)
<< "Failed to print vars to " << path+"-vars\n";
}
} }
return true; return true;
@ -430,7 +446,7 @@ bool TroubleshootCommand::CheckConfig(void)
} }
//print is supposed allow the user to print the object file //print is supposed allow the user to print the object file
void TroubleshootCommand::CheckObjectFile(const String& objectfile, InfoLog& log, InfoLog *OFile, void TroubleshootCommand::CheckObjectFile(const String& objectfile, InfoLog& log, InfoLog *OFile, const bool objectConsole,
Dictionary::Ptr& logs, std::set<String>& configs) Dictionary::Ptr& logs, std::set<String>& configs)
{ {
InfoLogLine(log) InfoLogLine(log)
@ -456,15 +472,24 @@ void TroubleshootCommand::CheckObjectFile(const String& objectfile, InfoLog& log
std::stringstream sStream; std::stringstream sStream;
if (objectConsole)
InfoLogLine(log, Console_ForegroundBlue)
<< "\n[begin: objectfile]\n";
while ((srs = NetString::ReadStringFromStream(sfp, &message, src)) != StatusEof) { while ((srs = NetString::ReadStringFromStream(sfp, &message, src)) != StatusEof) {
if (srs != StatusNewItem) if (srs != StatusNewItem)
continue; continue;
if (objectConsole) {
ObjectListUtility::PrintObject(std::cout, first, message, type_count, "", "");
}
else {
ObjectListUtility::PrintObject(sStream, first, message, type_count, "", ""); ObjectListUtility::PrintObject(sStream, first, message, type_count, "", "");
if(OFile != NULL) { if(OFile != NULL) {
InfoLogLine(*OFile) InfoLogLine(*OFile)
<< sStream.str(); << sStream.str();
sStream.flush(); sStream.flush();
}
} }
Dictionary::Ptr object = JsonDecode(message); Dictionary::Ptr object = JsonDecode(message);
@ -494,6 +519,10 @@ void TroubleshootCommand::CheckObjectFile(const String& objectfile, InfoLog& log
} }
} }
if (objectConsole)
InfoLogLine(log, Console_ForegroundBlue)
<< "\n[end: objectfile]\n";
if (!countTotal) { if (!countTotal) {
InfoLogLine(log, 0, LogCritical) InfoLogLine(log, 0, LogCritical)
<< "No objects found in objectfile.\n"; << "No objects found in objectfile.\n";
@ -517,14 +546,18 @@ void TroubleshootCommand::CheckObjectFile(const String& objectfile, InfoLog& log
TroubleshootCommand::PrintObjectOrigin(log, configs); TroubleshootCommand::PrintObjectOrigin(log, configs);
} }
bool TroubleshootCommand::PrintVarsFile(const String& path) { bool TroubleshootCommand::PrintVarsFile(const String& path, const bool console) {
std::ofstream *ofs = new std::ofstream(); if (!console) {
ofs->open((path+"-vars").CStr(), std::ios::out | std::ios::trunc); std::ofstream *ofs = new std::ofstream();
if (!ofs->is_open()) ofs->open((path+"-vars").CStr(), std::ios::out | std::ios::trunc);
return false; if (!ofs->is_open())
else return false;
VariableUtility::PrintVariables(*ofs); else
ofs->close(); VariableUtility::PrintVariables(*ofs);
ofs->close();
} else
VariableUtility::PrintVariables(std::cout);
return true; return true;
} }
@ -594,6 +627,7 @@ int TroubleshootCommand::Run(const boost::program_options::variables_map& vm, co
log = new InfoLog(path, false); log = new InfoLog(path, false);
if (!log->GetStreamHealth()) { if (!log->GetStreamHealth()) {
Log(LogCritical, "troubleshoot", "Failed to open file to write: " + path); Log(LogCritical, "troubleshoot", "Failed to open file to write: " + path);
delete log;
return 3; return 3;
} }
} }
@ -604,7 +638,7 @@ int TroubleshootCommand::Run(const boost::program_options::variables_map& vm, co
InfoLogLine(*log) InfoLogLine(*log)
<< appName << " -- Troubleshooting help:\n" << appName << " -- Troubleshooting help:\n"
<< "Should you run into problems with Icinga please add this file to your help request\n" << "Should you run into problems with Icinga please add this file to your help request\n"
<< "Began procedure at timestamp " << Convert::ToString(goTime) << "\n\n"; << "Began procedure at " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", goTime) << "\n\n";
if (appName.GetLength() > 3 && appName.SubStr(0, 3) == "lt-") if (appName.GetLength() > 3 && appName.SubStr(0, 3) == "lt-")
appName = appName.SubStr(3, appName.GetLength() - 3); appName = appName.SubStr(3, appName.GetLength() - 3);
@ -626,13 +660,20 @@ int TroubleshootCommand::Run(const boost::program_options::variables_map& vm, co
double endTime = Utility::GetTime(); double endTime = Utility::GetTime();
InfoLogLine(*log) InfoLogLine(*log)
<< "\nFinished collection at timestamp " << Convert::ToString(endTime) << "\nFinished collection at " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", endTime)
<< "\nTook " << Convert::ToString(endTime - goTime) << " seconds\n"; << "\nTook " << Utility::FormatDateTime("%S", (endTime - goTime)) << " seconds\n";
if (!vm.count("console")) { if (!vm.count("console")) {
std::cout std::cout
<< "Finished collection. See '" << path << "'\n" << "Finished collection. See '" << path << '\'';
<< "Please compress these files with tar or zip before uploading them\n"; if (vm.count("include-vars"))
std::cout
<< " and '" << path << "-vars'";
if (vm.count("include-objects"))
std::cout
<< " and '" << path << "-objects'";
std::cout
<< "\nPlease compress the files with tar and zip before uploading them\n";
} }
delete log; delete log;

View File

@ -60,9 +60,9 @@ private:
static bool PrintCrashReports(InfoLog& log); static bool PrintCrashReports(InfoLog& log);
static bool PrintConf(InfoLog& log, const String& path); static bool PrintConf(InfoLog& log, const String& path);
static bool CheckConfig(void); static bool CheckConfig(void);
static void CheckObjectFile(const String& objectfile, InfoLog& log, InfoLog *OFile, static void CheckObjectFile(const String& objectfile, InfoLog& log, InfoLog *OFile, const bool objectConsole,
Dictionary::Ptr& logs, std::set<String>& configs); Dictionary::Ptr& logs, std::set<String>& configs);
static bool PrintVarsFile(const String& path); static bool PrintVarsFile(const String& path, const bool console);
static void PrintLoggers(InfoLog& log, Dictionary::Ptr& logs); static void PrintLoggers(InfoLog& log, Dictionary::Ptr& logs);
static void PrintObjectOrigin(InfoLog& log, const std::set<String>& configSet); static void PrintObjectOrigin(InfoLog& log, const std::set<String>& configSet);
}; };