blob: 8105bff801d5b7b6c5ef1861f01d49115ef9dd5c [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho538108b2015-04-08 14:29:20 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connor7cbbbb72016-04-09 02:13:23 -070016package org.onosproject.cli.net;
sangho538108b2015-04-08 14:29:20 -070017
Yuta HIGUCHI15603842017-01-25 19:26:08 -080018import static org.onosproject.cli.net.DevicesListCommand.getSortedDevices;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020019import static org.onosproject.net.DeviceId.deviceId;
20
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080021import java.util.Comparator;
Charles Chan33a79ce2015-12-12 11:14:07 -080022import java.util.List;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020023import java.util.concurrent.TimeUnit;
24
Charles Chan33a79ce2015-12-12 11:14:07 -080025import com.google.common.collect.Lists;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070026import org.apache.karaf.shell.api.action.Argument;
27import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070028import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070029import org.apache.karaf.shell.api.action.lifecycle.Service;
30import org.apache.karaf.shell.api.action.Option;
Yuta HIGUCHI15603842017-01-25 19:26:08 -080031import org.onosproject.cli.AbstractShellCommand;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020032import org.onosproject.net.Device;
sangho538108b2015-04-08 14:29:20 -070033import org.onosproject.net.DeviceId;
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080034import org.onosproject.net.PortNumber;
sangho538108b2015-04-08 14:29:20 -070035import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.device.PortStatistics;
37
38/**
39 * Lists port statistic of all ports in the system.
40 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Service
sangho538108b2015-04-08 14:29:20 -070042@Command(scope = "onos", name = "portstats",
43 description = "Lists statistics of all ports in the system")
Yuta HIGUCHI15603842017-01-25 19:26:08 -080044public class DevicePortStatsCommand extends AbstractShellCommand {
Dusan Pajin11ff4a82015-08-20 18:03:05 +020045
Saurav Das59232cf2016-04-27 18:35:50 -070046 @Option(name = "-nz", aliases = "--nonzero", description = "Show only non-zero portstats",
47 required = false, multiValued = false)
48 private boolean nonzero = false;
49
Yuta HIGUCHI15603842017-01-25 19:26:08 -080050 @Option(name = "-d", aliases = "--delta",
51 description = "Show delta port statistics,"
Dusan Pajin11ff4a82015-08-20 18:03:05 +020052 + "only for the last polling interval",
53 required = false, multiValued = false)
54 private boolean delta = false;
55
Yuta HIGUCHI15603842017-01-25 19:26:08 -080056 @Option(name = "-t", aliases = "--table",
57 description = "Show delta port statistics in table format "
58 + "using human readable unit",
Dusan Pajin11ff4a82015-08-20 18:03:05 +020059 required = false, multiValued = false)
60 private boolean table = false;
61
62 @Argument(index = 0, name = "uri", description = "Device ID",
63 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070064 @Completion(DeviceIdCompleter.class)
Dusan Pajin11ff4a82015-08-20 18:03:05 +020065 String uri = null;
sangho538108b2015-04-08 14:29:20 -070066
Saurav Dasa2d37502016-03-25 17:50:40 -070067 @Argument(index = 1, name = "portNumber", description = "Port Number",
68 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070069 @Completion(PortNumberCompleter.class)
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080070 String portNumberStr = null;
71
72 PortNumber portNumber = null;
Saurav Dasa2d37502016-03-25 17:50:40 -070073
sangho538108b2015-04-08 14:29:20 -070074 private static final String FORMAT =
Laszlo Papp7cf60372018-01-11 00:06:43 +000075 " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s%s";
sangho538108b2015-04-08 14:29:20 -070076
77 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070078 protected void doExecute() {
sangho538108b2015-04-08 14:29:20 -070079 DeviceService deviceService = get(DeviceService.class);
80
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080081 if (portNumberStr != null) {
82 portNumber = PortNumber.fromString(portNumberStr);
83 }
84
Dusan Pajin11ff4a82015-08-20 18:03:05 +020085 if (uri == null) {
86 for (Device d : getSortedDevices(deviceService)) {
87 if (delta) {
88 if (table) {
89 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
90 } else {
91 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
92 }
93 } else {
94 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
95 }
96 }
97 } else {
98 Device d = deviceService.getDevice(deviceId(uri));
99 if (d == null) {
100 error("No such device %s", uri);
101 } else if (delta) {
102 if (table) {
103 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
104 } else {
105 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
106 }
107 } else {
108 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
109 }
110 }
sangho538108b2015-04-08 14:29:20 -0700111 }
112
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200113 /**
114 * Prints Port Statistics.
115 *
116 * @param deviceId
117 * @param portStats
118 */
sangho538108b2015-04-08 14:29:20 -0700119 private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
120 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800121 for (PortStatistics stat : sortByPort(portStats)) {
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800122 if (isIrrelevant(stat)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700123 continue;
124 }
Saurav Das59232cf2016-04-27 18:35:50 -0700125 if (nonzero && stat.isZero()) {
126 continue;
127 }
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800128 print(FORMAT, stat.portNumber(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
Laszlo Papp7cf60372018-01-11 00:06:43 +0000129 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec(),
130 annotations(stat.annotations()));
sangho538108b2015-04-08 14:29:20 -0700131 }
132 }
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800133
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800134 private boolean isIrrelevant(PortStatistics stat) {
135 // TODO revisit logical port (e.g., ALL) handling
136 return portNumber != null && !portNumber.equals(stat.portNumber());
137 }
138
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200139 /**
140 * Prints Port delta statistics.
141 *
142 * @param deviceId
143 * @param portStats
144 */
145 private void printPortStatsDelta(DeviceId deviceId, Iterable<PortStatistics> portStats) {
146 final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s,"
147 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
148 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800149 for (PortStatistics stat : sortByPort(portStats)) {
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800150 if (isIrrelevant(stat)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700151 continue;
152 }
Saurav Das59232cf2016-04-27 18:35:50 -0700153 if (nonzero && stat.isZero()) {
154 continue;
155 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200156 float duration = ((float) stat.durationSec()) +
157 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
158 float rateRx = stat.bytesReceived() * 8 / duration;
159 float rateTx = stat.bytesSent() * 8 / duration;
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800160 print(formatDelta, stat.portNumber(),
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200161 stat.packetsReceived(),
162 stat.packetsSent(),
163 stat.bytesReceived(),
164 stat.bytesSent(),
165 String.format("%.1f", rateRx),
166 String.format("%.1f", rateTx),
167 stat.packetsRxDropped(),
168 stat.packetsTxDropped(),
169 String.format("%.3f", duration));
170 }
171 }
172
173 /**
174 * Prints human readable table with delta Port Statistics for specific device.
175 *
176 * @param deviceId
177 * @param portStats
178 */
179 private void printPortStatsDeltaTable(DeviceId deviceId, Iterable<PortStatistics> portStats) {
180 final String formatDeltaTable = "|%5s | %7s | %7s | %7s | %7s | %7s | %7s | %7s | %7s |%9s |";
181 print("+---------------------------------------------------------------------------------------------------+");
Carmelo Cascone868b1bc2017-09-13 15:39:00 +0200182 print("| DeviceId = %-86s |", deviceId);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200183 print("|---------------------------------------------------------------------------------------------------|");
184 print("| | Receive | Transmit | Time [s] |");
185 print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |");
186 print("|---------------------------------------------------------------------------------------------------|");
187
Charles Chan33a79ce2015-12-12 11:14:07 -0800188 for (PortStatistics stat : sortByPort(portStats)) {
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800189 if (isIrrelevant(stat)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700190 continue;
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200191 }
Saurav Das59232cf2016-04-27 18:35:50 -0700192 if (nonzero && stat.isZero()) {
193 continue;
194 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700195 float duration = ((float) stat.durationSec()) +
196 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
Carmelo Cascone868b1bc2017-09-13 15:39:00 +0200197 float rateRx = duration > 0 ? stat.bytesReceived() * 8 / duration : 0;
198 float rateTx = duration > 0 ? stat.bytesSent() * 8 / duration : 0;
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800199 print(formatDeltaTable, stat.portNumber(),
Saurav Dasa2d37502016-03-25 17:50:40 -0700200 humanReadable(stat.packetsReceived()),
201 humanReadable(stat.bytesReceived()),
202 humanReadableBps(rateRx),
203 humanReadable(stat.packetsRxDropped()),
204 humanReadable(stat.packetsSent()),
205 humanReadable(stat.bytesSent()),
206 humanReadableBps(rateTx),
207 humanReadable(stat.packetsTxDropped()),
208 String.format("%.3f", duration));
209 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200210 print("+---------------------------------------------------------------------------------------------------+");
211 }
212
213 /**
214 * Converts bytes to human readable string with Kilo, Mega, Giga, etc.
215 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700216 * @param bytes input byte array
217 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200218 */
219 public static String humanReadable(long bytes) {
220 int unit = 1000;
221 if (bytes < unit) {
222 return String.format("%s ", bytes);
223 }
224 int exp = (int) (Math.log(bytes) / Math.log(unit));
225 Character pre = ("KMGTPE").charAt(exp - 1);
226 return String.format("%.2f%s", bytes / Math.pow(unit, exp), pre);
227 }
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800228
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200229 /**
230 * Converts bps to human readable format.
231 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700232 * @param bps input rate
233 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200234 */
235 public static String humanReadableBps(float bps) {
236 int unit = 1000;
237 if (bps < unit) {
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800238 return String.format("%.0f ", bps);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200239 }
240 int exp = (int) (Math.log(bps) / Math.log(unit));
241 Character pre = ("KMGTPE").charAt(exp - 1);
242 return String.format("%.2f%s", bps / Math.pow(unit, exp), pre);
243 }
Charles Chan33a79ce2015-12-12 11:14:07 -0800244
245 private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) {
246 List<PortStatistics> portStatsList = Lists.newArrayList(portStats);
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800247
248 portStatsList.sort(Comparator.comparing(ps -> ps.portNumber().toLong()));
Charles Chan33a79ce2015-12-12 11:14:07 -0800249 return portStatsList;
250 }
sangho538108b2015-04-08 14:29:20 -0700251}