2014-10-14 16:53:40 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* 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"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-10-14 16:53:40 +02:00
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/dynamicobject.hpp"
|
|
|
|
#include "base/dynamictype.hpp"
|
2014-10-26 19:59:49 +01:00
|
|
|
#include "base/json.hpp"
|
2014-10-14 16:53:40 +02:00
|
|
|
#include "base/netstring.hpp"
|
|
|
|
#include "base/stdiostream.hpp"
|
|
|
|
#include "base/debug.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2014-10-17 09:45:46 +02:00
|
|
|
#include "base/console.hpp"
|
2014-10-14 16:53:40 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/algorithm/string/join.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <fstream>
|
2014-10-16 15:33:03 +02:00
|
|
|
#include <iostream>
|
2014-10-14 16:53:40 +02:00
|
|
|
|
|
|
|
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,
|
2014-10-17 15:54:46 +02:00
|
|
|
boost::program_options::options_description& hiddenDesc) const
|
2014-10-14 16:53:40 +02:00
|
|
|
{
|
2014-10-15 14:12:59 +02:00
|
|
|
visibleDesc.add_options()
|
2014-10-15 16:43:10 +02:00
|
|
|
("count,c", "display object counts by types")
|
2014-10-15 14:12:59 +02:00
|
|
|
("name,n", po::value<std::string>(), "filter by name matches")
|
|
|
|
("type,t", po::value<std::string>(), "filter by type matches");
|
2014-10-14 16:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
|
|
|
String objectfile = Application::GetObjectsPath();
|
|
|
|
|
|
|
|
if (!Utility::PathExists(objectfile)) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Cannot open objects file '" << Application::GetObjectsPath() << "'.";
|
2014-10-14 16:53:40 +02:00
|
|
|
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);
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
StdioStream::Ptr sfp = new StdioStream(&fp, false);
|
2014-10-14 16:53:40 +02:00
|
|
|
unsigned long objects_count = 0;
|
|
|
|
std::map<String, int> type_count;
|
|
|
|
|
|
|
|
String message;
|
2014-10-15 18:31:05 +02:00
|
|
|
String name_filter, type_filter;
|
2014-10-15 14:12:59 +02:00
|
|
|
|
|
|
|
if (vm.count("name"))
|
|
|
|
name_filter = vm["name"].as<std::string>();
|
|
|
|
if (vm.count("type"))
|
|
|
|
type_filter = vm["type"].as<std::string>();
|
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
bool first = true;
|
|
|
|
|
2014-10-14 16:53:40 +02:00
|
|
|
while (NetString::ReadStringFromStream(sfp, &message)) {
|
2014-10-16 15:24:41 +02:00
|
|
|
PrintObject(std::cout, first, message, type_count, name_filter, type_filter);
|
2014-10-14 16:53:40 +02:00
|
|
|
objects_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfp->Close();
|
|
|
|
fp.close();
|
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
if (vm.count("count")) {
|
2014-10-16 15:24:41 +02:00
|
|
|
if (!first)
|
|
|
|
std::cout << "\n";
|
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
PrintTypeCounts(std::cout, type_count);
|
|
|
|
std::cout << "\n";
|
|
|
|
}
|
2014-10-14 16:53:40 +02:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "cli")
|
|
|
|
<< "Parsed " << objects_count << " objects.";
|
2014-10-14 16:53:40 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-16 15:24:41 +02:00
|
|
|
void ObjectListCommand::PrintObject(std::ostream& fp, bool& first, const String& message, std::map<String, int>& type_count, const String& name_filter, const String& type_filter)
|
2014-10-14 16:53:40 +02:00
|
|
|
{
|
2014-10-26 19:59:49 +01:00
|
|
|
Dictionary::Ptr object = JsonDecode(message);
|
2014-10-14 16:53:40 +02:00
|
|
|
|
2014-10-15 18:31:05 +02:00
|
|
|
Dictionary::Ptr properties = object->Get("properties");
|
|
|
|
|
|
|
|
String internal_name = properties->Get("__name");
|
2014-10-15 14:12:59 +02:00
|
|
|
String name = object->Get("name");
|
2014-10-14 16:53:40 +02:00
|
|
|
String type = object->Get("type");
|
|
|
|
|
2014-10-15 18:31:05 +02:00
|
|
|
if (!name_filter.IsEmpty() && !Utility::Match(name_filter, name) && !Utility::Match(name_filter, internal_name))
|
2014-10-15 14:12:59 +02:00
|
|
|
return;
|
2014-10-15 18:31:05 +02:00
|
|
|
if (!type_filter.IsEmpty() && !Utility::Match(type_filter, type))
|
2014-10-15 14:12:59 +02:00
|
|
|
return;
|
2014-10-14 16:53:40 +02:00
|
|
|
|
2014-10-16 15:24:41 +02:00
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
fp << "\n";
|
|
|
|
|
2014-10-14 16:53:40 +02:00
|
|
|
Dictionary::Ptr debug_hints = object->Get("debug_hints");
|
|
|
|
|
2014-11-05 18:23:40 +01:00
|
|
|
fp << "Object '" << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << internal_name << ConsoleColorTag(Console_Normal) << "'";
|
2014-10-27 20:16:44 +01:00
|
|
|
fp << " of type '" << ConsoleColorTag(Console_ForegroundMagenta | Console_Bold) << type << ConsoleColorTag(Console_Normal) << "':\n";
|
2014-10-14 16:53:40 +02:00
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
PrintProperties(fp, properties, debug_hints, 2);
|
2014-10-14 16:53:40 +02:00
|
|
|
|
2014-10-15 14:12:59 +02:00
|
|
|
type_count[type]++;
|
2014-10-14 16:53:40 +02:00
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
void ObjectListCommand::PrintProperties(std::ostream& fp, const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent)
|
2014-10-14 16:53:40 +02:00
|
|
|
{
|
|
|
|
/* get debug hint props */
|
|
|
|
Dictionary::Ptr debug_hint_props;
|
|
|
|
if (debug_hints)
|
|
|
|
debug_hint_props = debug_hints->Get("properties");
|
|
|
|
|
|
|
|
int offset = 2;
|
|
|
|
|
2014-10-17 20:47:48 +02:00
|
|
|
ObjectLock olock(props);
|
2014-10-14 16:53:40 +02:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, props) {
|
2014-10-15 18:41:52 +02:00
|
|
|
String key = kv.first;
|
2014-10-14 16:53:40 +02:00
|
|
|
Value val = kv.second;
|
|
|
|
|
|
|
|
/* key & value */
|
2014-10-17 09:45:46 +02:00
|
|
|
fp << std::setw(indent) << " " << "* " << ConsoleColorTag(Console_ForegroundGreen) << key << ConsoleColorTag(Console_Normal);
|
2014-10-14 16:53:40 +02:00
|
|
|
|
|
|
|
/* extract debug hints for key */
|
|
|
|
Dictionary::Ptr debug_hints_fwd;
|
|
|
|
if (debug_hint_props)
|
|
|
|
debug_hints_fwd = debug_hint_props->Get(key);
|
|
|
|
|
|
|
|
/* print dicts recursively */
|
|
|
|
if (val.IsObjectType<Dictionary>()) {
|
2014-10-16 15:11:19 +02:00
|
|
|
fp << "\n";
|
|
|
|
PrintHints(fp, debug_hints_fwd, indent + offset);
|
|
|
|
PrintProperties(fp, val, debug_hints_fwd, indent + offset);
|
2014-10-14 16:53:40 +02:00
|
|
|
} else {
|
2014-10-16 15:11:19 +02:00
|
|
|
fp << " = ";
|
|
|
|
PrintValue(fp, val);
|
|
|
|
fp << "\n";
|
|
|
|
PrintHints(fp, debug_hints_fwd, indent + offset);
|
2014-10-14 16:53:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
void ObjectListCommand::PrintHints(std::ostream& fp, const Dictionary::Ptr& debug_hints, int indent)
|
2014-10-14 16:53:40 +02:00
|
|
|
{
|
|
|
|
if (!debug_hints)
|
2014-10-16 15:11:19 +02:00
|
|
|
return;
|
2014-10-14 16:53:40 +02:00
|
|
|
|
|
|
|
Array::Ptr messages = debug_hints->Get("messages");
|
|
|
|
|
2014-11-17 10:34:11 +01:00
|
|
|
if (messages) {
|
|
|
|
ObjectLock olock(messages);
|
2014-10-17 20:47:48 +02:00
|
|
|
|
2014-11-17 10:34:11 +01:00
|
|
|
BOOST_FOREACH(const Value& msg, messages) {
|
|
|
|
PrintHint(fp, msg, indent);
|
|
|
|
}
|
2014-10-14 16:53:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
void ObjectListCommand::PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent)
|
2014-10-14 16:53:40 +02:00
|
|
|
{
|
2014-10-17 09:45:46 +02:00
|
|
|
fp << std::setw(indent) << " " << ConsoleColorTag(Console_ForegroundCyan) << "% " << msg->Get(0) << " modified in '" << msg->Get(1) << "', lines "
|
|
|
|
<< msg->Get(2) << ":" << msg->Get(3) << "-" << msg->Get(4) << ":" << msg->Get(5) << ConsoleColorTag(Console_Normal) << "\n";
|
2014-10-14 16:53:40 +02:00
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
void ObjectListCommand::PrintTypeCounts(std::ostream& fp, const std::map<String, int>& type_count)
|
2014-10-14 16:53:40 +02:00
|
|
|
{
|
|
|
|
typedef std::map<String, int>::value_type TypeCount;
|
|
|
|
|
2014-10-15 18:41:52 +02:00
|
|
|
BOOST_FOREACH(const TypeCount& kv, type_count) {
|
2014-10-16 15:24:41 +02:00
|
|
|
fp << "Found " << kv.second << " " << kv.first << " object";
|
|
|
|
|
|
|
|
if (kv.second != 1)
|
|
|
|
fp << "s";
|
|
|
|
|
|
|
|
fp << ".\n";
|
2014-10-14 16:53:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
void ObjectListCommand::PrintValue(std::ostream& fp, const Value& val)
|
2014-10-14 16:53:40 +02:00
|
|
|
{
|
2014-10-16 15:11:19 +02:00
|
|
|
if (val.IsObjectType<Array>()) {
|
|
|
|
PrintArray(fp, val);
|
|
|
|
return;
|
|
|
|
}
|
2014-10-14 16:53:40 +02:00
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
if (val.IsString()) {
|
2014-11-03 12:39:59 +01:00
|
|
|
fp << "\"" << Convert::ToString(val) << "\"";
|
2014-10-16 15:11:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-10-14 16:53:40 +02:00
|
|
|
|
2014-11-15 12:19:29 +01:00
|
|
|
if (val.IsEmpty()) {
|
|
|
|
fp << "null";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
fp << Convert::ToString(val);
|
2014-10-14 16:53:40 +02:00
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
void ObjectListCommand::PrintArray(std::ostream& fp, const Array::Ptr& arr)
|
2014-10-14 16:53:40 +02:00
|
|
|
{
|
|
|
|
bool first = true;
|
2014-10-16 15:11:19 +02:00
|
|
|
|
|
|
|
fp << "[ ";
|
2014-10-14 16:53:40 +02:00
|
|
|
|
|
|
|
if (arr) {
|
|
|
|
ObjectLock olock(arr);
|
|
|
|
BOOST_FOREACH(const Value& value, arr) {
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
2014-10-16 15:11:19 +02:00
|
|
|
fp << ", ";
|
2014-10-14 16:53:40 +02:00
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
PrintValue(fp, value);
|
2014-10-14 16:53:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:19 +02:00
|
|
|
if (!first)
|
|
|
|
fp << " ";
|
|
|
|
|
|
|
|
fp << "]";
|
2014-10-14 16:53:40 +02:00
|
|
|
}
|