blob: 18413a73f997c1689173a454eaf7481e3a45ba25 [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 =
Laszlo Papp7cf60372018-01-11 00:06:43 +000070 " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s%s";
sangho538108b2015-04-08 14:29:20 -070071
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(),
Laszlo Papp7cf60372018-01-11 00:06:43 +0000124 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec(),
125 annotations(stat.annotations()));
sangho538108b2015-04-08 14:29:20 -0700126 }
127 }
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800128
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800129 private boolean isIrrelevant(PortStatistics stat) {
130 // TODO revisit logical port (e.g., ALL) handling
131 return portNumber != null && !portNumber.equals(stat.portNumber());
132 }
133
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200134 /**
135 * Prints Port delta statistics.
136 *
137 * @param deviceId
138 * @param portStats
139 */
140 private void printPortStatsDelta(DeviceId deviceId, Iterable<PortStatistics> portStats) {
141 final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s,"
142 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
143 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800144 for (PortStatistics stat : sortByPort(portStats)) {
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800145 if (isIrrelevant(stat)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700146 continue;
147 }
Saurav Das59232cf2016-04-27 18:35:50 -0700148 if (nonzero && stat.isZero()) {
149 continue;
150 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200151 float duration = ((float) stat.durationSec()) +
152 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
153 float rateRx = stat.bytesReceived() * 8 / duration;
154 float rateTx = stat.bytesSent() * 8 / duration;
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800155 print(formatDelta, stat.portNumber(),
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200156 stat.packetsReceived(),
157 stat.packetsSent(),
158 stat.bytesReceived(),
159 stat.bytesSent(),
160 String.format("%.1f", rateRx),
161 String.format("%.1f", rateTx),
162 stat.packetsRxDropped(),
163 stat.packetsTxDropped(),
164 String.format("%.3f", duration));
165 }
166 }
167
168 /**
169 * Prints human readable table with delta Port Statistics for specific device.
170 *
171 * @param deviceId
172 * @param portStats
173 */
174 private void printPortStatsDeltaTable(DeviceId deviceId, Iterable<PortStatistics> portStats) {
175 final String formatDeltaTable = "|%5s | %7s | %7s | %7s | %7s | %7s | %7s | %7s | %7s |%9s |";
176 print("+---------------------------------------------------------------------------------------------------+");
Carmelo Cascone868b1bc2017-09-13 15:39:00 +0200177 print("| DeviceId = %-86s |", deviceId);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200178 print("|---------------------------------------------------------------------------------------------------|");
179 print("| | Receive | Transmit | Time [s] |");
180 print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |");
181 print("|---------------------------------------------------------------------------------------------------|");
182
Charles Chan33a79ce2015-12-12 11:14:07 -0800183 for (PortStatistics stat : sortByPort(portStats)) {
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800184 if (isIrrelevant(stat)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700185 continue;
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200186 }
Saurav Das59232cf2016-04-27 18:35:50 -0700187 if (nonzero && stat.isZero()) {
188 continue;
189 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700190 float duration = ((float) stat.durationSec()) +
191 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
Carmelo Cascone868b1bc2017-09-13 15:39:00 +0200192 float rateRx = duration > 0 ? stat.bytesReceived() * 8 / duration : 0;
193 float rateTx = duration > 0 ? stat.bytesSent() * 8 / duration : 0;
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800194 print(formatDeltaTable, stat.portNumber(),
Saurav Dasa2d37502016-03-25 17:50:40 -0700195 humanReadable(stat.packetsReceived()),
196 humanReadable(stat.bytesReceived()),
197 humanReadableBps(rateRx),
198 humanReadable(stat.packetsRxDropped()),
199 humanReadable(stat.packetsSent()),
200 humanReadable(stat.bytesSent()),
201 humanReadableBps(rateTx),
202 humanReadable(stat.packetsTxDropped()),
203 String.format("%.3f", duration));
204 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200205 print("+---------------------------------------------------------------------------------------------------+");
206 }
207
208 /**
209 * Converts bytes to human readable string with Kilo, Mega, Giga, etc.
210 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700211 * @param bytes input byte array
212 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200213 */
214 public static String humanReadable(long bytes) {
215 int unit = 1000;
216 if (bytes < unit) {
217 return String.format("%s ", bytes);
218 }
219 int exp = (int) (Math.log(bytes) / Math.log(unit));
220 Character pre = ("KMGTPE").charAt(exp - 1);
221 return String.format("%.2f%s", bytes / Math.pow(unit, exp), pre);
222 }
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800223
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200224 /**
225 * Converts bps to human readable format.
226 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700227 * @param bps input rate
228 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200229 */
230 public static String humanReadableBps(float bps) {
231 int unit = 1000;
232 if (bps < unit) {
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800233 return String.format("%.0f ", bps);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200234 }
235 int exp = (int) (Math.log(bps) / Math.log(unit));
236 Character pre = ("KMGTPE").charAt(exp - 1);
237 return String.format("%.2f%s", bps / Math.pow(unit, exp), pre);
238 }
Charles Chan33a79ce2015-12-12 11:14:07 -0800239
240 private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) {
241 List<PortStatistics> portStatsList = Lists.newArrayList(portStats);
Yuta HIGUCHI820f0342017-11-28 11:27:42 -0800242
243 portStatsList.sort(Comparator.comparing(ps -> ps.portNumber().toLong()));
Charles Chan33a79ce2015-12-12 11:14:07 -0800244 return portStatsList;
245 }
sangho538108b2015-04-08 14:29:20 -0700246}