]> granicus.if.org Git - icinga2/blob - lib/cli/objectlistcommand.cpp
Added ca restore command+docs to undo effects of ca remove
[icinga2] / lib / cli / objectlistcommand.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "cli/objectlistcommand.hpp"
4 #include "cli/objectlistutility.hpp"
5 #include "base/logger.hpp"
6 #include "base/application.hpp"
7 #include "base/convert.hpp"
8 #include "base/configobject.hpp"
9 #include "base/configtype.hpp"
10 #include "base/json.hpp"
11 #include "base/netstring.hpp"
12 #include "base/stdiostream.hpp"
13 #include "base/debug.hpp"
14 #include "base/objectlock.hpp"
15 #include "base/console.hpp"
16 #include <boost/algorithm/string/join.hpp>
17 #include <boost/algorithm/string/replace.hpp>
18 #include <fstream>
19 #include <iostream>
20 #include <iomanip>
21
22 using namespace icinga;
23 namespace po = boost::program_options;
24
25 REGISTER_CLICOMMAND("object/list", ObjectListCommand);
26
27 String ObjectListCommand::GetDescription() const
28 {
29         return "Lists all Icinga 2 objects.";
30 }
31
32 String ObjectListCommand::GetShortDescription() const
33 {
34         return "lists all objects";
35 }
36
37 void ObjectListCommand::InitParameters(boost::program_options::options_description& visibleDesc,
38         boost::program_options::options_description& hiddenDesc) const
39 {
40         visibleDesc.add_options()
41                 ("count,c", "display object counts by types")
42                 ("name,n", po::value<std::string>(), "filter by name matches")
43                 ("type,t", po::value<std::string>(), "filter by type matches");
44 }
45
46 /**
47  * The entry point for the "object list" CLI command.
48  *
49  * @returns An exit status.
50  */
51 int ObjectListCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
52 {
53         String objectfile = Configuration::ObjectsPath;
54
55         if (!Utility::PathExists(objectfile)) {
56                 Log(LogCritical, "cli")
57                         << "Cannot open objects file '" << Configuration::ObjectsPath << "'.";
58                 Log(LogCritical, "cli", "Run 'icinga2 daemon -C' to validate config and generate the cache file.");
59                 return 1;
60         }
61
62         std::fstream fp;
63         fp.open(objectfile.CStr(), std::ios_base::in);
64
65         StdioStream::Ptr sfp = new StdioStream(&fp, false);
66         unsigned long objects_count = 0;
67         std::map<String, int> type_count;
68
69         String name_filter, type_filter;
70
71         if (vm.count("name"))
72                 name_filter = vm["name"].as<std::string>();
73         if (vm.count("type"))
74                 type_filter = vm["type"].as<std::string>();
75
76         bool first = true;
77
78         String message;
79         StreamReadContext src;
80         for (;;) {
81                 StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src);
82
83                 if (srs == StatusEof)
84                         break;
85
86                 if (srs != StatusNewItem)
87                         continue;
88
89                 ObjectListUtility::PrintObject(std::cout, first, message, type_count, name_filter, type_filter);
90                 objects_count++;
91         }
92
93         sfp->Close();
94         fp.close();
95
96         if (vm.count("count")) {
97                 if (!first)
98                         std::cout << "\n";
99
100                 PrintTypeCounts(std::cout, type_count);
101                 std::cout << "\n";
102         }
103
104         Log(LogNotice, "cli")
105                 << "Parsed " << objects_count << " objects.";
106
107         return 0;
108 }
109
110 void ObjectListCommand::PrintTypeCounts(std::ostream& fp, const std::map<String, int>& type_count)
111 {
112         typedef std::map<String, int>::value_type TypeCount;
113
114         for (const TypeCount& kv : type_count) {
115                 fp << "Found " << kv.second << " " << kv.first << " object";
116
117                 if (kv.second != 1)
118                         fp << "s";
119
120                 fp << ".\n";
121         }
122 }