2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-10-14 16:53:40 +02:00
|
|
|
|
|
|
|
#include "cli/objectlistcommand.hpp"
|
2015-02-18 10:23:27 +01:00
|
|
|
#include "cli/objectlistutility.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"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configobject.hpp"
|
|
|
|
#include "base/configtype.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/algorithm/string/join.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <fstream>
|
2014-10-16 15:33:03 +02:00
|
|
|
#include <iostream>
|
2014-12-15 10:16:06 +01:00
|
|
|
#include <iomanip>
|
2014-10-14 16:53:40 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
|
|
REGISTER_CLICOMMAND("object/list", ObjectListCommand);
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String ObjectListCommand::GetDescription() const
|
2014-10-14 16:53:40 +02:00
|
|
|
{
|
|
|
|
return "Lists all Icinga 2 objects.";
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String ObjectListCommand::GetShortDescription() const
|
2014-10-14 16:53:40 +02:00
|
|
|
{
|
|
|
|
return "lists all objects";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectListCommand::InitParameters(boost::program_options::options_description& visibleDesc,
|
2017-12-19 15:50:05 +01: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
|
|
|
|
{
|
2018-08-09 15:37:23 +02:00
|
|
|
String objectfile = Configuration::ObjectsPath;
|
2014-10-14 16:53:40 +02:00
|
|
|
|
|
|
|
if (!Utility::PathExists(objectfile)) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "cli")
|
2018-08-09 15:37:23 +02:00
|
|
|
<< "Cannot open objects file '" << Configuration::ObjectsPath << "'.";
|
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;
|
|
|
|
|
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;
|
|
|
|
|
2015-02-15 14:37:16 +01:00
|
|
|
String message;
|
|
|
|
StreamReadContext src;
|
|
|
|
for (;;) {
|
|
|
|
StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src);
|
|
|
|
|
|
|
|
if (srs == StatusEof)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (srs != StatusNewItem)
|
|
|
|
continue;
|
|
|
|
|
2015-02-18 10:23:27 +01:00
|
|
|
ObjectListUtility::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")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Parsed " << objects_count << " objects.";
|
2014-10-14 16:53:40 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (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
|
|
|
}
|
2015-09-18 13:04:09 +02:00
|
|
|
}
|