status.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "status.hpp"
28 #include "channel-module.hpp"
29 #include "face-module.hpp"
30 #include "fib-module.hpp"
31 #include "rib-module.hpp"
32 #include "cs-module.hpp"
34 
35 #include <ndn-cxx/security/validator-null.hpp>
36 
37 namespace nfd::tools::nfdc {
38 
39 void
41 {
42  StatusReport report;
43 
44  if (options.wantForwarderGeneral) {
45  report.sections.push_back(make_unique<ForwarderGeneralModule>());
46  }
47 
48  if (options.wantChannels) {
49  report.sections.push_back(make_unique<ChannelModule>());
50  }
51 
52  if (options.wantFaces) {
53  report.sections.push_back(make_unique<FaceModule>());
54  }
55 
56  if (options.wantFib) {
57  report.sections.push_back(make_unique<FibModule>());
58  }
59 
60  if (options.wantRib) {
61  report.sections.push_back(make_unique<RibModule>());
62  }
63 
64  if (options.wantCs) {
65  report.sections.push_back(make_unique<CsModule>());
66  }
67 
68  if (options.wantStrategyChoice) {
69  report.sections.push_back(make_unique<StrategyChoiceModule>());
70  }
71 
72  uint32_t code = report.collect(ctx.face, ctx.keyChain,
73  ndn::security::getAcceptAllValidator(),
74  CommandOptions());
75  if (code != 0) {
76  ctx.exitCode = 1;
77  // Give a simple error code for end user.
78  // Technical support personnel:
79  // 1. get the exact command from end user
80  // 2. code div 1000000 is zero-based section index
81  // 3. code mod 1000000 is a Controller.fetch error code
82  ctx.err << "Error while collecting status report (" << code << ").\n";
83  }
84 
85  switch (options.output) {
86  case ReportFormat::XML:
87  report.formatXml(ctx.out);
88  break;
89  case ReportFormat::TEXT:
90  report.formatText(ctx.out);
91  break;
92  }
93 }
94 
97 static void
99 {
100  StatusReportOptions options;
101  options.*wantSection = true;
102  reportStatus(ctx, options);
103 }
104 
107 static void
109 {
110  StatusReportOptions options;
111  options.output = ctx.args.get<ReportFormat>("format", ReportFormat::TEXT);
112  options.wantForwarderGeneral = options.wantChannels = options.wantFaces = options.wantFib =
113  options.wantRib = options.wantCs = options.wantStrategyChoice = true;
114  reportStatus(ctx, options);
115 }
116 
117 void
119 {
120  CommandDefinition defStatusReport("status", "report");
121  defStatusReport
122  .setTitle("print full status report")
124  parser.addCommand(defStatusReport, &reportStatusComprehensive);
125 
126  CommandDefinition defStatusShow("status", "show");
127  defStatusShow
128  .setTitle("print general status");
129  parser.addCommand(defStatusShow,
131  parser.addAlias("status", "show", "");
132 
133  CommandDefinition defChannelList("channel", "list");
134  defChannelList
135  .setTitle("print channel list");
136  parser.addCommand(defChannelList,
138  parser.addAlias("channel", "list", "");
139 
140  CommandDefinition defFibList("fib", "list");
141  defFibList
142  .setTitle("print FIB entries");
143  parser.addCommand(defFibList,
145  parser.addAlias("fib", "list", "");
146 
147  CommandDefinition defCsInfo("cs", "info");
148  defCsInfo
149  .setTitle("print CS information");
150  parser.addCommand(defCsInfo,
152  parser.addAlias("cs", "info", "");
153 }
154 
155 } // namespace nfd::tools::nfdc
T get(std::string_view key, const T &defaultValue=T()) const
CommandDefinition & setTitle(std::string_view title)
Set one-line description.
CommandDefinition & addArg(const std::string &name, ArgValueType valueType, Required isRequired=Required::NO, Positional allowPositional=Positional::NO, const std::string &metavar="")
Declare an argument.
CommandParser & addCommand(const CommandDefinition &def, const ExecuteCommand &execute, std::underlying_type_t< AvailableIn > modes=AVAILABLE_IN_ALL)
Add an available command.
CommandParser & addAlias(const std::string &noun, const std::string &verb, const std::string &verb2)
Add an alias "noun verb2" to existing command "noun verb".
Context for command execution.
std::ostream & out
output stream
const CommandArguments & args
std::ostream & err
error stream
Collects and prints NFD status report.
void formatText(std::ostream &os) const
Print a text report.
void formatXml(std::ostream &os) const
Print an XML report.
uint32_t collect(Face &face, KeyChain &keyChain, Validator &validator, const CommandOptions &options)
Collect status via chosen sections.
std::vector< unique_ptr< Module > > sections
Modules through which status is collected.
@ NO
argument is optional
static void reportStatusComprehensive(ExecuteContext &ctx)
The 'status report' command.
Definition: status.cpp:108
void reportStatus(ExecuteContext &ctx, const StatusReportOptions &options)
Collect a status report and write to stdout.
Definition: status.cpp:40
void registerStatusCommands(CommandParser &parser)
Registers status commands.
Definition: status.cpp:118
@ YES
argument can be specified as positional
@ REPORT_FORMAT
Report format 'xml' or 'text'.
static void reportStatusSingleSection(ExecuteContext &ctx, bool StatusReportOptions::*wantSection)
Single-section status command.
Definition: status.cpp:98