* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
-#include "cli/troubleshootcommand.hpp"
-#include "cli/objectlistutility.hpp"
-#include "cli/featureutility.hpp"
-#include "cli/daemonutility.hpp"
-#include "base/netstring.hpp"
#include "base/application.hpp"
-#include "base/stdiostream.hpp"
+#include "base/convert.hpp"
#include "base/json.hpp"
+#include "base/netstring.hpp"
#include "base/objectlock.hpp"
-#include "base/convert.hpp"
+#include "base/stdiostream.hpp"
+#include "cli/daemonutility.hpp"
+#include "cli/featureutility.hpp"
+#include "cli/objectlistutility.hpp"
+#include "cli/troubleshootcommand.hpp"
+#include "cli/variableutility.hpp"
#include "config/configitembuilder.hpp"
+
+#include <boost/algorithm/string/join.hpp>
#include <boost/circular_buffer.hpp>
+#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
-#include <boost/algorithm/string/join.hpp>
-#include <iostream>
+
#include <fstream>
+#include <iostream>
using namespace icinga;
namespace po = boost::program_options;
return true;
}
-bool TroubleshootCommand::ObjectInfo(InfoLog& log, const boost::program_options::variables_map& vm, Dictionary::Ptr& logs)
+bool TroubleshootCommand::ObjectInfo(InfoLog& log, const boost::program_options::variables_map& vm, Dictionary::Ptr& logs, const String& path)
{
InfoLogLine(log)
<< '\n' << std::string(14, '=') << " OBJECT INFORMATION " << std::string(14, '=') << '\n';
<< "Cannot open object file '" << objectfile << "'.\n"
<< "FAILED: This probably means you have a fault configuration.";
return false;
- } else
- CheckObjectFile(objectfile, log, vm.count("include-objects"), logs, configs);
+ } else {
+ InfoLog *OFile = NULL;
+ if (vm.count("include-objects")) {
+ OFile = new InfoLog(path+"-objects", vm.count("console"));
+ if (!OFile->GetStreamHealth()) {
+ InfoLogLine(log, LogWarning)
+ << "Failed to open Object-write-stream, not printing objects\n";
+ OFile = NULL;
+ } else
+ InfoLogLine(log)
+ << "Printing all objects to " << path+"-objects";
+ }
+ CheckObjectFile(objectfile, log, OFile, logs, configs);
+ if (OFile != NULL)
+ delete OFile;
+ }
+ if (vm.count("include-vars")) {
+ if (PrintVarsFile(path))
+ InfoLogLine(log)
+ << "Successfully printed all variables to " << path+"-vars";
+ else
+ InfoLogLine(log, LogWarning)
+ << "Failed to prin vars to " << path+"-vars";
+ }
+
return true;
}
}
//print is supposed allow the user to print the object file
-void TroubleshootCommand::CheckObjectFile(const String& objectfile, InfoLog& log, const bool print,
+void TroubleshootCommand::CheckObjectFile(const String& objectfile, InfoLog& log, InfoLog *OFile,
Dictionary::Ptr& logs, std::set<String>& configs)
{
InfoLogLine(log)
std::map<String, int> type_count;
bool first = true;
+ std::stringstream sStream;
+
while ((srs = NetString::ReadStringFromStream(sfp, &message, src)) != StatusEof) {
if (srs != StatusNewItem)
continue;
-
- std::stringstream sStream;
ObjectListUtility::PrintObject(sStream, first, message, type_count, "", "");
+ if(OFile != NULL) {
+ InfoLogLine(*OFile)
+ << sStream.str();
+ sStream.flush();
+ }
Dictionary::Ptr object = JsonDecode(message);
Dictionary::Ptr properties = object->Get("properties");
if (!countTotal) {
InfoLogLine(log, LogCritical)
<< "No objects found in objectfile.";
-
return;
}
TroubleshootCommand::PrintObjectOrigin(log, configs);
}
+bool TroubleshootCommand::PrintVarsFile(const String& path) {
+ std::ofstream *ofs = new std::ofstream();
+ ofs->open((path+"-vars").CStr(), std::ios::out | std::ios::trunc);
+ if (!ofs->is_open())
+ return false;
+ else
+ VariableUtility::PrintVariables(*ofs);
+ ofs->close();
+ return true;
+}
+
void TroubleshootCommand::PrintLoggers(InfoLog& log, Dictionary::Ptr& logs)
{
if (!logs->GetLength()) {
visibleDesc.add_options()
("console,c", "print to console instead of file")
("output,o", boost::program_options::value<std::string>(), "path to output file")
-// ("include-objects", "Print the whole objectfile (like `object list`)") TODO
+ ("include-objects", "Print the whole objectfile (like `object list`)")
+ ("include-vars", "Print all Variables (like `variable list`)")
;
}
return 3;
}
}
+
String appName = Utility::BaseName(Application::GetArgV()[0]);
double goTime = Utility::GetTime();
if (!GeneralInfo(*log, vm) ||
!FeatureInfo(*log, vm) ||
- !ObjectInfo(*log, vm, logs) ||
+ !ObjectInfo(*log, vm, logs, path) ||
!ReportInfo(*log, vm, logs) ||
!ConfigInfo(*log, vm)) {
InfoLogLine(*log, LogCritical)
static bool GeneralInfo(InfoLog& log, const boost::program_options::variables_map& vm);
static bool FeatureInfo(InfoLog& log, const boost::program_options::variables_map& vm);
static bool ObjectInfo(InfoLog& log, const boost::program_options::variables_map& vm,
- Dictionary::Ptr& logs);
+ Dictionary::Ptr& logs, const String& path);
static bool ReportInfo(InfoLog& log, const boost::program_options::variables_map& vm,
Dictionary::Ptr& logs);
static bool ConfigInfo(InfoLog& log, const boost::program_options::variables_map& vm);
static bool PrintCrashReports(InfoLog& log);
static bool PrintConf(InfoLog& log, const String& path);
static bool CheckConfig(void);
- static void CheckObjectFile(const String& objectfile, InfoLog& log, const bool print,
+ static void CheckObjectFile(const String& objectfile, InfoLog& log, InfoLog *OFile,
Dictionary::Ptr& logs, std::set<String>& configs);
+ static bool PrintVarsFile(const String& path);
static void PrintLoggers(InfoLog& log, Dictionary::Ptr& logs);
static void PrintObjectOrigin(InfoLog& log, const std::set<String>& configSet);
};