* The entry point for the "object list" CLI command. * * @returns An exit status. */
| 64 | * @returns An exit status. |
| 65 | */ |
| 66 | int ObjectListCommand::Run(const boost::program_options::variables_map& vm, [[maybe_unused]] const std::vector<std::string>& ap) const |
| 67 | { |
| 68 | String objectfile = Configuration::ObjectsPath; |
| 69 | |
| 70 | if (!Utility::PathExists(objectfile)) { |
| 71 | Log(LogCritical, "cli") |
| 72 | << "Cannot open objects file '" << Configuration::ObjectsPath << "'."; |
| 73 | Log(LogCritical, "cli", "Run 'icinga2 daemon -C --dump-objects' to validate config and generate the cache file."); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | std::fstream fp; |
| 78 | fp.open(objectfile.CStr(), std::ios_base::in); |
| 79 | |
| 80 | StdioStream::Ptr sfp = new StdioStream(&fp, false); |
| 81 | unsigned long objects_count = 0; |
| 82 | std::map<String, int> type_count; |
| 83 | |
| 84 | String name_filter, type_filter; |
| 85 | |
| 86 | if (vm.count("name")) |
| 87 | name_filter = vm["name"].as<std::string>(); |
| 88 | if (vm.count("type")) |
| 89 | type_filter = vm["type"].as<std::string>(); |
| 90 | |
| 91 | bool first = true; |
| 92 | |
| 93 | String message; |
| 94 | StreamReadContext src; |
| 95 | for (;;) { |
| 96 | StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src); |
| 97 | |
| 98 | if (srs == StatusEof) |
| 99 | break; |
| 100 | |
| 101 | if (srs != StatusNewItem) |
| 102 | continue; |
| 103 | |
| 104 | ObjectListUtility::PrintObject(std::cout, first, message, type_count, name_filter, type_filter); |
| 105 | objects_count++; |
| 106 | } |
| 107 | |
| 108 | sfp->Close(); |
| 109 | fp.close(); |
| 110 | |
| 111 | if (vm.count("count")) { |
| 112 | if (!first) |
| 113 | std::cout << "\n"; |
| 114 | |
| 115 | PrintTypeCounts(std::cout, type_count); |
| 116 | std::cout << "\n"; |
| 117 | } |
| 118 | |
| 119 | Log(LogNotice, "cli") |
| 120 | << "Parsed " << objects_count << " objects."; |
| 121 | |
| 122 | auto objectsPathCtime (GetCtime(Configuration::ObjectsPath)); |
| 123 | auto varsPathCtime (GetCtime(Configuration::VarsPath)); |