blob: e3f707d96c7b800c228e256106995f33c876cd91 [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;
28import org.apache.karaf.shell.api.action.lifecycle.Service;
29import org.apache.karaf.shell.api.action.Option;
Yuta HIGUCHI15603842017-01-25 19:26:08 -080030import org.onosproject.cli.AbstractShellCommand;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020031import org.onosproject.net.Device;
sangho538108b2015-04-08 14:29:20 -070032import org.onosproject.net.DeviceId;
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080033import org.onosproject.net.PortNumber;
sangho538108b2015-04-08 14:29:20 -070034import org.onosproject.net.device.DeviceService;
35import org.onosproject.net.device.PortStatistics;
36
37/**
38 * Lists port statistic of all ports in the system.
39 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
sangho538108b2015-04-08 14:29:20 -070041@Command(scope = "onos", name = "portstats",
42 description = "Lists statistics of all ports in the system")
Yuta HIGUCHI15603842017-01-25 19:26:08 -080043public class DevicePortStatsCommand extends AbstractShellCommand {
Dusan Pajin11ff4a82015-08-20 18:03:05 +020044
Saurav Das59232cf2016-04-27 18:35:50 -070045 @Option(name = "-nz", aliases = "--nonzero", description = "Show only non-zero portstats",
46 required = false, multiValued = false)
47 private boolean nonzero = false;
48
Yuta HIGUCHI15603842017-01-25 19:26:08 -080049 @Option(name = "-d", aliases = "--delta",
50 description = "Show delta port statistics,"
Dusan Pajin11ff4a82015-08-20 18:03:05 +020051 + "only for the last polling interval",
52 required = false, multiValued = false)
53 private boolean delta = false;
54
Yuta HIGUCHI15603842017-01-25 19:26:08 -080055 @Option(name = "-t", aliases = "--table",
56 description = "Show delta port statistics in table format "
57 + "using human readable unit",
Dusan Pajin11ff4a82015-08-20 18:03:05 +020058 required = false, multiValued = false)
59 private boolean table = false;
60
61 @Argument(index = 0, name = "uri", description = "Device ID",
62 required = false, multiValued = false)
63 String uri = null;
sangho538108b2015-04-08 14:29:20 -070064
Saurav Dasa2d37502016-03-25 17:50:40 -070065 @Argument(index = 1, name = "portNumber", description = "Port Number",
66 required = false, multiValued = false)
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080067 String portNumberStr = null;
68
69 PortNumber portNumber = null;
Saurav Dasa2d37502016-03-25 17:50:40 -070070
sangho538108b2015-04-08 14:29:20 -070071 private static final String FORMAT =
Laszlo Papp7cf60372018-01-11 00:06:43 +000072 " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s%s";
sangho538108b2015-04-08 14:29:20 -070073
74 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070075 protected void doExecute() {
sangho538108b2015-04-08 14:29:20 -070076 DeviceService deviceService = get(DeviceService.class);
77
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080078 if (portNumberStr != null) {
79 portNumber = PortNumber.fromString(portNumberStr);
80 }
81
Dusan Pajin11ff4a82015-08-20 18:03:05 +020082 if (uri == null) {
83 for (Device d : getSortedDevices(deviceService)) {
84 if (delta) {
85 if (table) {
86 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
87 } else {
88 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
89 }
90 } else {
91 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
92 }
93 }
94 } else {
95 Device d = deviceService.getDevice(deviceId(uri));
96 if (d == null) {
97 error("No such device %s", uri);
98 } else if (delta) {
99 if (table) {
100 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
101 } else {
102 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
103 }
104 } else {
105 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
106 }
107 }
sangho538108b2015-04-08 14:29:20 -0700108 }
109
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200110 /**
111 * Prints Port Statistics.
112 *
113 * @param deviceId
114 * @param portStats
115 */
sangho538108b2015-04-08 14:29:20 -0700116 private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
117 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800118 for (PortStatistics stat : sortByPort(portStats)) {
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800119 if (isIrrelevant(stat)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700120 continue;
121 }
Saurav Das59232cf2016-04-27 18:35:50 -0700122 if (nonzero && stat.isZero()) {
123 continue;
124 }
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800125 print(FORMAT, stat.portNumber(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
Laszlo Papp7cf60372018-01-11 00:06:43 +0000126 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec(),
127 annotations(stat.annotations()));
sangho538108b2015-04-08 14:29:20 -0700128 }
129 }
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800130
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800131 private boolean isIrrelevant(PortStatistics stat) {
132 // TODO revisit logical port (e.g., ALL) handling
133 return portNumber != null && !portNumber.equals(stat.portNumber());
134 }
135
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200136 /**
137 * Prints Port delta statistics.
138 *
139 * @param deviceId
140 * @param portStats
141 */
142 private void printPortStatsDelta(DeviceId deviceId, Iterable<PortStatistics> portStats) {
143 final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s,"
144 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
145 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800146 for (PortStatistics stat : sortByPort(portStats)) {
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800147 if (isIrrelevant(stat)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700148 continue;
149 }
Saurav Das59232cf2016-04-27 18:35:50 -0700150 if (nonzero && stat.isZero()) {
151 continue;
152 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200153 float duration = ((float) stat.durationSec()) +
154 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
155 float rateRx = stat.bytesReceived() * 8 / duration;
156 float rateTx = stat.bytesSent() * 8 / duration;
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800157 print(formatDelta, stat.portNumber(),
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200158 stat.packetsReceived(),
159 stat.packetsSent(),
160 stat.bytesReceived(),
161 stat.bytesSent(),
162 String.format("%.1f", rateRx),
163 String.format("%.1f", rateTx),
164 stat.packetsRxDropped(),
165 stat.packetsTxDropped(),
166 String.format("%.3f", duration));
167 }
168 }
169
170 /**
171 * Prints human readable table with delta Port Statistics for specific device.
172 *
173 * @param deviceId
174 * @param portStats
175 */
176 private void printPortStatsDeltaTable(DeviceId deviceId, Iterable<PortStatistics> portStats) {
177 final String formatDeltaTable = "|%5s | %7s | %7s | %7s | %7s | %7s | %7s | %7s | %7s |%9s |";
178 print("+---------------------------------------------------------------------------------------------------+");
Carmelo Cascone868b1bc2017-09-13 15:39:00 +0200179 print("| DeviceId = %-86s |", deviceId);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200180 print("|---------------------------------------------------------------------------------------------------|");
181 print("| | Receive | Transmit | Time [s] |");
182 print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |");
183 print("|---------------------------------------------------------------------------------------------------|");
184
Charles Chan33a79ce2015-12-12 11:14:07 -0800185 for (PortStatistics stat : sortByPort(portStats)) {
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800186 if (isIrrelevant(stat)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700187 continue;
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200188 }
Saurav Das59232cf2016-04-27 18:35:50 -0700189 if (nonzero && stat.isZero()) {
190 continue;
191 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700192 float duration = ((float) stat.durationSec()) +
193 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
Carmelo Cascone868b1bc2017-09-13 15:39:00 +0200194 float rateRx = duration > 0 ? stat.bytesReceived() * 8 / duration : 0;
195 float rateTx = duration > 0 ? stat.bytesSent() * 8 / duration : 0;
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800196 print(formatDeltaTable, stat.portNumber(),
Saurav Dasa2d37502016-03-25 17:50:40 -0700197 humanReadable(stat.packetsReceived()),
198 humanReadable(stat.bytesReceived()),
199 humanReadableBps(rateRx),
200 humanReadable(stat.packetsRxDropped()),
201 humanReadable(stat.packetsSent()),
202 humanReadable(stat.bytesSent()),
203 humanReadableBps(rateTx),
204 humanReadable(stat.packetsTxDropped()),
205 String.format("%.3f", duration));
206 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200207 print("+---------------------------------------------------------------------------------------------------+");
208 }
209
210 /**
211 * Converts bytes to human readable string with Kilo, Mega, Giga, etc.
212 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700213 * @param bytes input byte array
214 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200215 */
216 public static String humanReadable(long bytes) {
217 int unit = 1000;
218 if (bytes < unit) {
219 return String.format("%s ", bytes);
220 }
221 int exp = (int) (Math.log(bytes) / Math.log(unit));
222 Character pre = ("KMGTPE").charAt(exp - 1);
223 return String.format("%.2f%s", bytes / Math.pow(unit, exp), pre);
224 }
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800225
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200226 /**
227 * Converts bps to human readable format.
228 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700229 * @param bps input rate
230 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200231 */
232 public static String humanReadableBps(float bps) {
233 int unit = 1000;
234 if (bps < unit) {
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800235 return String.format("%.0f ", bps);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200236 }
237 int exp = (int) (Math.log(bps) / Math.log(unit));
238 Character pre = ("KMGTPE").charAt(exp - 1);
239 return String.format("%.2f%s", bps / Math.pow(unit, exp), pre);
240 }
Charles Chan33a79ce2015-12-12 11:14:07 -0800241
242 private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) {
243 List<PortStatistics> portStatsList = Lists.newArrayList(portStats);
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800244
245 portStatsList.sort(Comparator.comparing(ps -> ps.portNumber().toLong()));
Charles Chan33a79ce2015-12-12 11:14:07 -0800246 return portStatsList;
247 }
sangho538108b2015-04-08 14:29:20 -0700248}