blob: 0319f56847925eb841a03ba7b173173231204c4e [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
Dusan Pajin11ff4a82015-08-20 18:03:05 +020018import static org.onosproject.net.DeviceId.deviceId;
19
Charles Chan33a79ce2015-12-12 11:14:07 -080020import java.util.List;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020021import java.util.concurrent.TimeUnit;
22
Charles Chan33a79ce2015-12-12 11:14:07 -080023import com.google.common.collect.Lists;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020024import org.apache.karaf.shell.commands.Argument;
sangho538108b2015-04-08 14:29:20 -070025import org.apache.karaf.shell.commands.Command;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020026import org.apache.karaf.shell.commands.Option;
27import org.onosproject.net.Device;
sangho538108b2015-04-08 14:29:20 -070028import org.onosproject.net.DeviceId;
29import org.onosproject.net.device.DeviceService;
30import org.onosproject.net.device.PortStatistics;
31
32/**
33 * Lists port statistic of all ports in the system.
34 */
35@Command(scope = "onos", name = "portstats",
36 description = "Lists statistics of all ports in the system")
Dusan Pajin11ff4a82015-08-20 18:03:05 +020037public class DevicePortStatsCommand extends DevicesListCommand {
38
Saurav Das59232cf2016-04-27 18:35:50 -070039 @Option(name = "-nz", aliases = "--nonzero", description = "Show only non-zero portstats",
40 required = false, multiValued = false)
41 private boolean nonzero = false;
42
Dusan Pajin11ff4a82015-08-20 18:03:05 +020043 @Option(name = "-d", aliases = "--delta", description = "Show Delta Port Statistics,"
44 + "only for the last polling interval",
45 required = false, multiValued = false)
46 private boolean delta = false;
47
48 @Option(name = "-t", aliases = "--table", description = "Show human readable table format for statistics",
49 required = false, multiValued = false)
50 private boolean table = false;
51
52 @Argument(index = 0, name = "uri", description = "Device ID",
53 required = false, multiValued = false)
54 String uri = null;
sangho538108b2015-04-08 14:29:20 -070055
Saurav Dasa2d37502016-03-25 17:50:40 -070056 @Argument(index = 1, name = "portNumber", description = "Port Number",
57 required = false, multiValued = false)
58 Integer portNumber = null;
59
sangho538108b2015-04-08 14:29:20 -070060 private static final String FORMAT =
61 " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s";
62
63 @Override
64 protected void execute() {
65 DeviceService deviceService = get(DeviceService.class);
66
Dusan Pajin11ff4a82015-08-20 18:03:05 +020067 if (uri == null) {
68 for (Device d : getSortedDevices(deviceService)) {
69 if (delta) {
70 if (table) {
71 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
72 } else {
73 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
74 }
75 } else {
76 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
77 }
78 }
79 } else {
80 Device d = deviceService.getDevice(deviceId(uri));
81 if (d == null) {
82 error("No such device %s", uri);
83 } else if (delta) {
84 if (table) {
85 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
86 } else {
87 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
88 }
89 } else {
90 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
91 }
92 }
sangho538108b2015-04-08 14:29:20 -070093 }
94
Dusan Pajin11ff4a82015-08-20 18:03:05 +020095 /**
96 * Prints Port Statistics.
97 *
98 * @param deviceId
99 * @param portStats
100 */
sangho538108b2015-04-08 14:29:20 -0700101 private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
102 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800103 for (PortStatistics stat : sortByPort(portStats)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700104 if (portNumber != null && stat.port() != portNumber) {
105 continue;
106 }
Saurav Das59232cf2016-04-27 18:35:50 -0700107 if (nonzero && stat.isZero()) {
108 continue;
109 }
sangho538108b2015-04-08 14:29:20 -0700110 print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
111 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec());
112 }
113 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200114 /**
115 * Prints Port delta statistics.
116 *
117 * @param deviceId
118 * @param portStats
119 */
120 private void printPortStatsDelta(DeviceId deviceId, Iterable<PortStatistics> portStats) {
121 final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s,"
122 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
123 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800124 for (PortStatistics stat : sortByPort(portStats)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700125 if (portNumber != null && stat.port() != portNumber) {
126 continue;
127 }
Saurav Das59232cf2016-04-27 18:35:50 -0700128 if (nonzero && stat.isZero()) {
129 continue;
130 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200131 float duration = ((float) stat.durationSec()) +
132 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
133 float rateRx = stat.bytesReceived() * 8 / duration;
134 float rateTx = stat.bytesSent() * 8 / duration;
135 print(formatDelta, stat.port(),
136 stat.packetsReceived(),
137 stat.packetsSent(),
138 stat.bytesReceived(),
139 stat.bytesSent(),
140 String.format("%.1f", rateRx),
141 String.format("%.1f", rateTx),
142 stat.packetsRxDropped(),
143 stat.packetsTxDropped(),
144 String.format("%.3f", duration));
145 }
146 }
147
148 /**
149 * Prints human readable table with delta Port Statistics for specific device.
150 *
151 * @param deviceId
152 * @param portStats
153 */
154 private void printPortStatsDeltaTable(DeviceId deviceId, Iterable<PortStatistics> portStats) {
155 final String formatDeltaTable = "|%5s | %7s | %7s | %7s | %7s | %7s | %7s | %7s | %7s |%9s |";
156 print("+---------------------------------------------------------------------------------------------------+");
157 print("| DeviceId = %s |", deviceId);
158 print("|---------------------------------------------------------------------------------------------------|");
159 print("| | Receive | Transmit | Time [s] |");
160 print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |");
161 print("|---------------------------------------------------------------------------------------------------|");
162
Charles Chan33a79ce2015-12-12 11:14:07 -0800163 for (PortStatistics stat : sortByPort(portStats)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700164 if (portNumber != null && stat.port() != portNumber) {
165 continue;
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200166 }
Saurav Das59232cf2016-04-27 18:35:50 -0700167 if (nonzero && stat.isZero()) {
168 continue;
169 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700170 float duration = ((float) stat.durationSec()) +
171 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
172 float rateRx = stat.bytesReceived() * 8 / duration;
173 float rateTx = stat.bytesSent() * 8 / duration;
174 print(formatDeltaTable, stat.port(),
175 humanReadable(stat.packetsReceived()),
176 humanReadable(stat.bytesReceived()),
177 humanReadableBps(rateRx),
178 humanReadable(stat.packetsRxDropped()),
179 humanReadable(stat.packetsSent()),
180 humanReadable(stat.bytesSent()),
181 humanReadableBps(rateTx),
182 humanReadable(stat.packetsTxDropped()),
183 String.format("%.3f", duration));
184 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200185 print("+---------------------------------------------------------------------------------------------------+");
186 }
187
188 /**
189 * Converts bytes to human readable string with Kilo, Mega, Giga, etc.
190 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700191 * @param bytes input byte array
192 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200193 */
194 public static String humanReadable(long bytes) {
195 int unit = 1000;
196 if (bytes < unit) {
197 return String.format("%s ", bytes);
198 }
199 int exp = (int) (Math.log(bytes) / Math.log(unit));
200 Character pre = ("KMGTPE").charAt(exp - 1);
201 return String.format("%.2f%s", bytes / Math.pow(unit, exp), pre);
202 }
203 /**
204 * Converts bps to human readable format.
205 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700206 * @param bps input rate
207 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200208 */
209 public static String humanReadableBps(float bps) {
210 int unit = 1000;
211 if (bps < unit) {
212 return String.format("%.0f ", (float) bps);
213 }
214 int exp = (int) (Math.log(bps) / Math.log(unit));
215 Character pre = ("KMGTPE").charAt(exp - 1);
216 return String.format("%.2f%s", bps / Math.pow(unit, exp), pre);
217 }
Charles Chan33a79ce2015-12-12 11:14:07 -0800218
219 private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) {
220 List<PortStatistics> portStatsList = Lists.newArrayList(portStats);
221 portStatsList.sort((PortStatistics o1, PortStatistics o2) ->
222 o1.port() - o2.port());
223 return portStatsList;
224 }
sangho538108b2015-04-08 14:29:20 -0700225}