-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: Add object command (list) (WIP)
refs #7251
- Loading branch information
Michael Friedrich
committed
Oct 15, 2014
1 parent
9df1b6b
commit def7278
Showing
4 changed files
with
272 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/****************************************************************************** | ||
* Icinga 2 * | ||
* Copyright (C) 2012-2014 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/objectlistcommand.hpp" | ||
#include "base/logger_fwd.hpp" | ||
#include "base/clicommand.hpp" | ||
#include "base/application.hpp" | ||
#include "base/convert.hpp" | ||
#include "base/dynamicobject.hpp" | ||
#include "base/dynamictype.hpp" | ||
#include "base/serializer.hpp" | ||
#include "base/netstring.hpp" | ||
#include "base/stdiostream.hpp" | ||
#include "base/debug.hpp" | ||
#include "base/objectlock.hpp" | ||
#include <boost/foreach.hpp> | ||
#include <boost/algorithm/string/join.hpp> | ||
#include <boost/algorithm/string/replace.hpp> | ||
#include <fstream> | ||
#include <vector> | ||
#include <string> | ||
#include <unistd.h> | ||
|
||
using namespace icinga; | ||
namespace po = boost::program_options; | ||
|
||
REGISTER_CLICOMMAND("object/list", ObjectListCommand); | ||
|
||
String ObjectListCommand::GetDescription(void) const | ||
{ | ||
return "Lists all Icinga 2 objects."; | ||
} | ||
|
||
String ObjectListCommand::GetShortDescription(void) const | ||
{ | ||
return "lists all objects"; | ||
} | ||
|
||
void ObjectListCommand::InitParameters(boost::program_options::options_description& visibleDesc, | ||
boost::program_options::options_description& hiddenDesc, | ||
ArgumentCompletionDescription& argCompletionDesc) const | ||
{ | ||
/* Command doesn't support any parameters. */ | ||
//TODO Add filter regex #7199 | ||
} | ||
|
||
/** | ||
* The entry point for the "object list" CLI command. | ||
* | ||
* @returns An exit status. | ||
*/ | ||
int ObjectListCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const | ||
{ | ||
if (!ap.empty()) { | ||
Log(LogWarning, "cli", "Ignoring parameters: " + boost::algorithm::join(ap, " ")); | ||
} | ||
|
||
String objectfile = Application::GetObjectsPath(); | ||
|
||
if (!Utility::PathExists(objectfile)) { | ||
Log(LogCritical, "cli", "Cannot parse objects file '" + Application::GetObjectsPath() + "'."); | ||
Log(LogCritical, "cli", "Run 'icinga2 daemon -C' to validate config and generate the cache file."); | ||
return 1; | ||
} | ||
|
||
std::fstream fp; | ||
fp.open(objectfile.CStr(), std::ios_base::in); | ||
|
||
StdioStream::Ptr sfp = make_shared<StdioStream>(&fp, false); | ||
|
||
unsigned long objects_count = 0; | ||
|
||
String message; | ||
while (NetString::ReadStringFromStream(sfp, &message)) { | ||
ReadObject(message); | ||
objects_count++; | ||
} | ||
|
||
sfp->Close(); | ||
fp.close(); | ||
|
||
Log(LogInformation, "cli", "Listing " + Convert::ToString(objects_count) + " objects."); | ||
|
||
return 0; | ||
} | ||
|
||
void ObjectListCommand::ReadObject(const String& message) | ||
{ | ||
Dictionary::Ptr object = JsonDeserialize(message); | ||
|
||
String type = object->Get("type"); | ||
String name = object->Get("name"); | ||
bool abstract = object->Get("abstract"); | ||
Dictionary::Ptr properties = object->Get("properties"); | ||
Dictionary::Ptr debug_hints = object->Get("debug_hints"); | ||
|
||
//Log(LogInformation, "cli", "Found object '" + name + "' of type '" + type + "'."); | ||
|
||
std::ostringstream msgbuf; | ||
|
||
if (abstract) | ||
msgbuf << "Template '"; | ||
else | ||
msgbuf << "Object '"; | ||
|
||
msgbuf << properties->Get("__name") << "' of type '" << type << "':\n"; | ||
|
||
msgbuf << FormatProperties(properties, debug_hints, 0); | ||
|
||
std::cout << msgbuf.str() << "\n"; | ||
|
||
} | ||
|
||
String ObjectListCommand::FormatProperties(const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent) | ||
{ | ||
std::ostringstream msgbuf; | ||
|
||
BOOST_FOREACH(const Dictionary::Pair& kv, props) { | ||
Value key = kv.first; | ||
Value val = kv.second; | ||
|
||
/* print debug hints */ | ||
//msgbuf << FormatHints(debug_hints); | ||
|
||
/* key & value */ | ||
//TODO: Add colors | ||
msgbuf << std::setw(indent) << " " << key; | ||
|
||
if (val.IsObjectType<Dictionary>()) { | ||
msgbuf << FormatProperties(val, debug_hints, indent + 2); | ||
} else { | ||
msgbuf << " = " << FormatValue(val) << "\n"; | ||
} | ||
} | ||
|
||
return msgbuf.str(); | ||
} | ||
|
||
String ObjectListCommand::FormatHints(const Dictionary::Ptr& debug_hints, int indent) | ||
{ | ||
Array::Ptr messages = debug_hints->Get("messages"); | ||
|
||
std::ostringstream msgbuf; | ||
|
||
BOOST_FOREACH(const Value& msg, messages) { | ||
msgbuf << FormatHint(msg, indent + 2); | ||
} | ||
|
||
return msgbuf.str(); | ||
} | ||
|
||
String ObjectListCommand::FormatHint(const Array::Ptr& msg, int indent) | ||
{ | ||
std::ostringstream msgbuf; | ||
|
||
//TODO: Add colors | ||
msgbuf << std::setw(indent) << msg->Get(0) << " modified in '" << msg->Get(1) << "', lines " | ||
<< msg->Get(2) << ":" << msg->Get(3) << "-" << msg->Get(4) << ":" << msg->Get(5) << "\n"; | ||
|
||
return msgbuf.str(); | ||
} | ||
|
||
String ObjectListCommand::FormatValue(const Value& val) | ||
{ | ||
if (val.IsObjectType<Array>()) | ||
return "[ " + FormatArray(val) + " ]"; | ||
|
||
if (val.IsObjectType<String>()) | ||
return "'" + Convert::ToString(val) + "'"; | ||
|
||
return Convert::ToString(val); | ||
} | ||
|
||
|
||
String ObjectListCommand::FormatArray(const Array::Ptr& arr) | ||
{ | ||
bool first = true; | ||
String str; | ||
|
||
if (arr) { | ||
ObjectLock olock(arr); | ||
BOOST_FOREACH(const Value& value, arr) { | ||
if (first) | ||
first = false; | ||
else | ||
str += ", "; | ||
|
||
str += "'" + Convert::ToString(value) + "'"; | ||
} | ||
} | ||
|
||
return str; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/****************************************************************************** | ||
* Icinga 2 * | ||
* Copyright (C) 2012-2014 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 OBJECTLISTCOMMAND_H | ||
#define OBJECTLISTCOMMAND_H | ||
|
||
#include "base/qstring.hpp" | ||
#include "base/dictionary.hpp" | ||
#include "base/array.hpp" | ||
#include "base/clicommand.hpp" | ||
|
||
namespace icinga | ||
{ | ||
|
||
/** | ||
* The "object list" command. | ||
* | ||
* @ingroup cli | ||
*/ | ||
class ObjectListCommand : public CLICommand | ||
{ | ||
public: | ||
DECLARE_PTR_TYPEDEFS(ObjectListCommand); | ||
|
||
virtual String GetDescription(void) const; | ||
virtual String GetShortDescription(void) const; | ||
virtual void InitParameters(boost::program_options::options_description& visibleDesc, | ||
boost::program_options::options_description& hiddenDesc, | ||
ArgumentCompletionDescription& argCompletionDesc) const; | ||
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const; | ||
|
||
private: | ||
static void ReadObject(const String& message); | ||
static String FormatProperties(const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent = 0); | ||
static String FormatHints(const Dictionary::Ptr& hints, int indent = 0); | ||
static String FormatHint(const Array::Ptr& msg, int indent = 0); | ||
static String FormatValue(const Value& val); | ||
static String FormatArray(const Array::Ptr& arr); | ||
}; | ||
|
||
} | ||
|
||
#endif /* OBJECTLISTCOMMAND_H */ |