blob: 2aa631f95f3c443d05c85351e804f6e49dbfe4af [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
Charles Chan33a79ce2015-12-12 11:14:07 -080021import java.util.List;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020022import java.util.concurrent.TimeUnit;
23
Charles Chan33a79ce2015-12-12 11:14:07 -080024import com.google.common.collect.Lists;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020025import org.apache.karaf.shell.commands.Argument;
sangho538108b2015-04-08 14:29:20 -070026import org.apache.karaf.shell.commands.Command;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020027import org.apache.karaf.shell.commands.Option;
Yuta HIGUCHI15603842017-01-25 19:26:08 -080028import org.onosproject.cli.AbstractShellCommand;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020029import org.onosproject.net.Device;
sangho538108b2015-04-08 14:29:20 -070030import org.onosproject.net.DeviceId;
31import org.onosproject.net.device.DeviceService;
32import org.onosproject.net.device.PortStatistics;
33
34/**
35 * Lists port statistic of all ports in the system.
36 */
37@Command(scope = "onos", name = "portstats",
38 description = "Lists statistics of all ports in the system")
Yuta HIGUCHI15603842017-01-25 19:26:08 -080039public class DevicePortStatsCommand extends AbstractShellCommand {
Dusan Pajin11ff4a82015-08-20 18:03:05 +020040
Saurav Das59232cf2016-04-27 18:35:50 -070041 @Option(name = "-nz", aliases = "--nonzero", description = "Show only non-zero portstats",
42 required = false, multiValued = false)
43 private boolean nonzero = false;
44
Yuta HIGUCHI15603842017-01-25 19:26:08 -080045 @Option(name = "-d", aliases = "--delta",
46 description = "Show delta port statistics,"
Dusan Pajin11ff4a82015-08-20 18:03:05 +020047 + "only for the last polling interval",
48 required = false, multiValued = false)
49 private boolean delta = false;
50
Yuta HIGUCHI15603842017-01-25 19:26:08 -080051 @Option(name = "-t", aliases = "--table",
52 description = "Show delta port statistics in table format "
53 + "using human readable unit",
Dusan Pajin11ff4a82015-08-20 18:03:05 +020054 required = false, multiValued = false)
55 private boolean table = false;
56
57 @Argument(index = 0, name = "uri", description = "Device ID",
58 required = false, multiValued = false)
59 String uri = null;
sangho538108b2015-04-08 14:29:20 -070060
Saurav Dasa2d37502016-03-25 17:50:40 -070061 @Argument(index = 1, name = "portNumber", description = "Port Number",
62 required = false, multiValued = false)
63 Integer portNumber = null;
64
sangho538108b2015-04-08 14:29:20 -070065 private static final String FORMAT =
66 " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s";
67
68 @Override
69 protected void execute() {
70 DeviceService deviceService = get(DeviceService.class);
71
Dusan Pajin11ff4a82015-08-20 18:03:05 +020072 if (uri == null) {
73 for (Device d : getSortedDevices(deviceService)) {
74 if (delta) {
75 if (table) {
76 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
77 } else {
78 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
79 }
80 } else {
81 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
82 }
83 }
84 } else {
85 Device d = deviceService.getDevice(deviceId(uri));
86 if (d == null) {
87 error("No such device %s", uri);
88 } else if (delta) {
89 if (table) {
90 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
91 } else {
92 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
93 }
94 } else {
95 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
96 }
97 }
sangho538108b2015-04-08 14:29:20 -070098 }
99
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200100 /**
101 * Prints Port Statistics.
102 *
103 * @param deviceId
104 * @param portStats
105 */
sangho538108b2015-04-08 14:29:20 -0700106 private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
107 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800108 for (PortStatistics stat : sortByPort(portStats)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700109 if (portNumber != null && stat.port() != portNumber) {
110 continue;
111 }
Saurav Das59232cf2016-04-27 18:35:50 -0700112 if (nonzero && stat.isZero()) {
113 continue;
114 }
sangho538108b2015-04-08 14:29:20 -0700115 print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
116 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec());
117 }
118 }
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800119
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200120 /**
121 * Prints Port delta statistics.
122 *
123 * @param deviceId
124 * @param portStats
125 */
126 private void printPortStatsDelta(DeviceId deviceId, Iterable<PortStatistics> portStats) {
127 final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s,"
128 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
129 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800130 for (PortStatistics stat : sortByPort(portStats)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700131 if (portNumber != null && stat.port() != portNumber) {
132 continue;
133 }
Saurav Das59232cf2016-04-27 18:35:50 -0700134 if (nonzero && stat.isZero()) {
135 continue;
136 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200137 float duration = ((float) stat.durationSec()) +
138 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
139 float rateRx = stat.bytesReceived() * 8 / duration;
140 float rateTx = stat.bytesSent() * 8 / duration;
141 print(formatDelta, stat.port(),
142 stat.packetsReceived(),
143 stat.packetsSent(),
144 stat.bytesReceived(),
145 stat.bytesSent(),
146 String.format("%.1f", rateRx),
147 String.format("%.1f", rateTx),
148 stat.packetsRxDropped(),
149 stat.packetsTxDropped(),
150 String.format("%.3f", duration));
151 }
152 }
153
154 /**
155 * Prints human readable table with delta Port Statistics for specific device.
156 *
157 * @param deviceId
158 * @param portStats
159 */
160 private void printPortStatsDeltaTable(DeviceId deviceId, Iterable<PortStatistics> portStats) {
161 final String formatDeltaTable = "|%5s | %7s | %7s | %7s | %7s | %7s | %7s | %7s | %7s |%9s |";
162 print("+---------------------------------------------------------------------------------------------------+");
Carmelo Cascone868b1bc2017-09-13 15:39:00 +0200163 print("| DeviceId = %-86s |", deviceId);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200164 print("|---------------------------------------------------------------------------------------------------|");
165 print("| | Receive | Transmit | Time [s] |");
166 print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |");
167 print("|---------------------------------------------------------------------------------------------------|");
168
Charles Chan33a79ce2015-12-12 11:14:07 -0800169 for (PortStatistics stat : sortByPort(portStats)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700170 if (portNumber != null && stat.port() != portNumber) {
171 continue;
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200172 }
Saurav Das59232cf2016-04-27 18:35:50 -0700173 if (nonzero && stat.isZero()) {
174 continue;
175 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700176 float duration = ((float) stat.durationSec()) +
177 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
Carmelo Cascone868b1bc2017-09-13 15:39:00 +0200178 float rateRx = duration > 0 ? stat.bytesReceived() * 8 / duration : 0;
179 float rateTx = duration > 0 ? stat.bytesSent() * 8 / duration : 0;
Saurav Dasa2d37502016-03-25 17:50:40 -0700180 print(formatDeltaTable, stat.port(),
181 humanReadable(stat.packetsReceived()),
182 humanReadable(stat.bytesReceived()),
183 humanReadableBps(rateRx),
184 humanReadable(stat.packetsRxDropped()),
185 humanReadable(stat.packetsSent()),
186 humanReadable(stat.bytesSent()),
187 humanReadableBps(rateTx),
188 humanReadable(stat.packetsTxDropped()),
189 String.format("%.3f", duration));
190 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200191 print("+---------------------------------------------------------------------------------------------------+");
192 }
193
194 /**
195 * Converts bytes to human readable string with Kilo, Mega, Giga, etc.
196 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700197 * @param bytes input byte array
198 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200199 */
200 public static String humanReadable(long bytes) {
201 int unit = 1000;
202 if (bytes < unit) {
203 return String.format("%s ", bytes);
204 }
205 int exp = (int) (Math.log(bytes) / Math.log(unit));
206 Character pre = ("KMGTPE").charAt(exp - 1);
207 return String.format("%.2f%s", bytes / Math.pow(unit, exp), pre);
208 }
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800209
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200210 /**
211 * Converts bps to human readable format.
212 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700213 * @param bps input rate
214 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200215 */
216 public static String humanReadableBps(float bps) {
217 int unit = 1000;
218 if (bps < unit) {
Yuta HIGUCHI15603842017-01-25 19:26:08 -0800219 return String.format("%.0f ", bps);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200220 }
221 int exp = (int) (Math.log(bps) / Math.log(unit));
222 Character pre = ("KMGTPE").charAt(exp - 1);
223 return String.format("%.2f%s", bps / Math.pow(unit, exp), pre);
224 }
Charles Chan33a79ce2015-12-12 11:14:07 -0800225
226 private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) {
227 List<PortStatistics> portStatsList = Lists.newArrayList(portStats);
228 portStatsList.sort((PortStatistics o1, PortStatistics o2) ->
229 o1.port() - o2.port());
230 return portStatsList;
231 }
sangho538108b2015-04-08 14:29:20 -0700232}