blob: eb07071494bd0892eaf198e91608868ac7097d49 [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;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020026import org.apache.karaf.shell.commands.Argument;
sangho538108b2015-04-08 14:29:20 -070027import org.apache.karaf.shell.commands.Command;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020028import org.apache.karaf.shell.commands.Option;
Yuta HIGUCHI15603842017-01-25 19:26:08 -080029import org.onosproject.cli.AbstractShellCommand;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020030import org.onosproject.net.Device;
sangho538108b2015-04-08 14:29:20 -070031import org.onosproject.net.DeviceId;
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080032import org.onosproject.net.PortNumber;
sangho538108b2015-04-08 14:29:20 -070033import org.onosproject.net.device.DeviceService;
34import org.onosproject.net.device.PortStatistics;
35
36/**
37 * Lists port statistic of all ports in the system.
38 */
39@Command(scope = "onos", name = "portstats",
40 description = "Lists statistics of all ports in the system")
Yuta HIGUCHI15603842017-01-25 19:26:08 -080041public class DevicePortStatsCommand extends AbstractShellCommand {
Dusan Pajin11ff4a82015-08-20 18:03:05 +020042
Saurav Das59232cf2016-04-27 18:35:50 -070043 @Option(name = "-nz", aliases = "--nonzero", description = "Show only non-zero portstats",
44 required = false, multiValued = false)
45 private boolean nonzero = false;
46
Yuta HIGUCHI15603842017-01-25 19:26:08 -080047 @Option(name = "-d", aliases = "--delta",
48 description = "Show delta port statistics,"
Dusan Pajin11ff4a82015-08-20 18:03:05 +020049 + "only for the last polling interval",
50 required = false, multiValued = false)
51 private boolean delta = false;
52
Yuta HIGUCHI15603842017-01-25 19:26:08 -080053 @Option(name = "-t", aliases = "--table",
54 description = "Show delta port statistics in table format "
55 + "using human readable unit",
Dusan Pajin11ff4a82015-08-20 18:03:05 +020056 required = false, multiValued = false)
57 private boolean table = false;
58
59 @Argument(index = 0, name = "uri", description = "Device ID",
60 required = false, multiValued = false)
61 String uri = null;
sangho538108b2015-04-08 14:29:20 -070062
Saurav Dasa2d37502016-03-25 17:50:40 -070063 @Argument(index = 1, name = "portNumber", description = "Port Number",
64 required = false, multiValued = false)
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080065 String portNumberStr = null;
66
67 PortNumber portNumber = null;
Saurav Dasa2d37502016-03-25 17:50:40 -070068
sangho538108b2015-04-08 14:29:20 -070069 private static final String FORMAT =
70 " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s";
71
72 @Override
73 protected void execute() {
74 DeviceService deviceService = get(DeviceService.class);
75
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080076 if (portNumberStr != null) {
77 portNumber = PortNumber.fromString(portNumberStr);
78 }
79
Dusan Pajin11ff4a82015-08-20 18:03:05 +020080 if (uri == null) {
81 for (Device d : getSortedDevices(deviceService)) {
82 if (delta) {
83 if (table) {
84 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
85 } else {
86 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
87 }
88 } else {
89 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
90 }
91 }
92 } else {
93 Device d = deviceService.getDevice(deviceId(uri));
94 if (d == null) {
95 error("No such device %s", uri);
96 } else if (delta) {
97 if (table) {
98 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
99 } else {
100 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
101 }
102 } else {
103 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
104 }
105 }
sangho538108b2015-04-08 14:29:20 -0700106 }
107
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200108 /**
109 * Prints Port Statistics.
110 *
111 * @param deviceId
112 * @param portStats
113 */
sangho538108b2015-04-08 14:29:20 -0700114 private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
115 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800116 for (PortStatistics stat : sortByPort(portStats)) {
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800117 if (isIrrelevant(stat)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700118 continue;
119 }
Saurav Das59232cf2016-04-27 18:35:50 -0700120 if (nonzero && stat.isZero()) {
121 continue;
122 }
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800123 print(FORMAT, stat.portNumber(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
sangho538108b2015-04-08 14:29:20 -0700124 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec());
125 }
126 }
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800127
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800128 private boolean isIrrelevant(PortStatistics stat) {
129 // TODO revisit logical port (e.g., ALL) handling
130 return portNumber != null && !portNumber.equals(stat.portNumber());
131 }
132
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200133 /**
134 * Prints Port delta statistics.
135 *
136 * @param deviceId
137 * @param portStats
138 */
139 private void printPortStatsDelta(DeviceId deviceId, Iterable<PortStatistics> portStats) {
140 final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s,"
141 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
142 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800143 for (PortStatistics stat : sortByPort(portStats)) {
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800144 if (isIrrelevant(stat)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700145 continue;
146 }
Saurav Das59232cf2016-04-27 18:35:50 -0700147 if (nonzero && stat.isZero()) {
148 continue;
149 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200150 float duration = ((float) stat.durationSec()) +
151 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
152 float rateRx = stat.bytesReceived() * 8 / duration;
153 float rateTx = stat.bytesSent() * 8 / duration;
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800154 print(formatDelta, stat.portNumber(),
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200155 stat.packetsReceived(),
156 stat.packetsSent(),
157 stat.bytesReceived(),
158 stat.bytesSent(),
159 String.format("%.1f", rateRx),
160 String.format("%.1f", rateTx),
161 stat.packetsRxDropped(),
162 stat.packetsTxDropped(),
163 String.format("%.3f", duration));
164 }
165 }
166
167 /**
168 * Prints human readable table with delta Port Statistics for specific device.
169 *
170 * @param deviceId
171 * @param portStats
172 */
173 private void printPortStatsDeltaTable(DeviceId deviceId, Iterable<PortStatistics> portStats) {
174 final String formatDeltaTable = "|%5s | %7s | %7s | %7s | %7s | %7s | %7s | %7s | %7s |%9s |";
175 print("+---------------------------------------------------------------------------------------------------+");
Carmelo Cascone868b1bc2017-09-13 15:39:00 +0200176 print("| DeviceId = %-86s |", deviceId);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200177 print("|---------------------------------------------------------------------------------------------------|");
178 print("| | Receive | Transmit | Time [s] |");
179 print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |");
180 print("|---------------------------------------------------------------------------------------------------|");
181
Charles Chan33a79ce2015-12-12 11:14:07 -0800182 for (PortStatistics stat : sortByPort(portStats)) {
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800183 if (isIrrelevant(stat)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700184 continue;
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200185 }
Saurav Das59232cf2016-04-27 18:35:50 -0700186 if (nonzero && stat.isZero()) {
187 continue;
188 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700189 float duration = ((float) stat.durationSec()) +
190 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
Carmelo Cascone868b1bc2017-09-13 15:39:00 +0200191 float rateRx = duration > 0 ? stat.bytesReceived() * 8 / duration : 0;
192 float rateTx = duration > 0 ? stat.bytesSent() * 8 / duration : 0;
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800193 print(formatDeltaTable, stat.portNumber(),
Saurav Dasa2d37502016-03-25 17:50:40 -0700194 humanReadable(stat.packetsReceived()),
195 humanReadable(stat.bytesReceived()),
196 humanReadableBps(rateRx),
197 humanReadable(stat.packetsRxDropped()),
198 humanReadable(stat.packetsSent()),
199 humanReadable(stat.bytesSent()),
200 humanReadableBps(rateTx),
201 humanReadable(stat.packetsTxDropped()),
202 String.format("%.3f", duration));
203 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200204 print("+---------------------------------------------------------------------------------------------------+");
205 }
206
207 /**
208 * Converts bytes to human readable string with Kilo, Mega, Giga, etc.
209 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700210 * @param bytes input byte array
211 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200212 */
213 public static String humanReadable(long bytes) {
214 int unit = 1000;
215 if (bytes < unit) {
216 return String.format("%s ", bytes);
217 }
218 int exp = (int) (Math.log(bytes) / Math.log(unit));
219 Character pre = ("KMGTPE").charAt(exp - 1);
220 return String.format("%.2f%s", bytes / Math.pow(unit, exp), pre);
221 }
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800222
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200223 /**
224 * Converts bps to human readable format.
225 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700226 * @param bps input rate
227 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200228 */
229 public static String humanReadableBps(float bps) {
230 int unit = 1000;
231 if (bps < unit) {
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800232 return String.format("%.0f ", bps);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200233 }
234 int exp = (int) (Math.log(bps) / Math.log(unit));
235 Character pre = ("KMGTPE").charAt(exp - 1);
236 return String.format("%.2f%s", bps / Math.pow(unit, exp), pre);
237 }
Charles Chan33a79ce2015-12-12 11:14:07 -0800238
239 private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) {
240 List<PortStatistics> portStatsList = Lists.newArrayList(portStats);
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800241
242 portStatsList.sort(Comparator.comparing(ps -> ps.portNumber().toLong()));
Charles Chan33a79ce2015-12-12 11:14:07 -0800243 return portStatsList;
244 }
sangho538108b2015-04-08 14:29:20 -0700245}