blob: 32c4c9d63eadb9295e45299623cbe70953906ffc [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001package org.onosproject.cli.net;
2
3/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07004 * Copyright 2015-present Open Networking Laboratory
sangho538108b2015-04-08 14:29:20 -07005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
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;
28import org.onosproject.net.Device;
sangho538108b2015-04-08 14:29:20 -070029import org.onosproject.net.DeviceId;
30import org.onosproject.net.device.DeviceService;
31import org.onosproject.net.device.PortStatistics;
32
33/**
34 * Lists port statistic of all ports in the system.
35 */
36@Command(scope = "onos", name = "portstats",
37 description = "Lists statistics of all ports in the system")
Dusan Pajin11ff4a82015-08-20 18:03:05 +020038public class DevicePortStatsCommand extends DevicesListCommand {
39
40 @Option(name = "-d", aliases = "--delta", description = "Show Delta Port Statistics,"
41 + "only for the last polling interval",
42 required = false, multiValued = false)
43 private boolean delta = false;
44
45 @Option(name = "-t", aliases = "--table", description = "Show human readable table format for statistics",
46 required = false, multiValued = false)
47 private boolean table = false;
48
49 @Argument(index = 0, name = "uri", description = "Device ID",
50 required = false, multiValued = false)
51 String uri = null;
sangho538108b2015-04-08 14:29:20 -070052
Saurav Dasa2d37502016-03-25 17:50:40 -070053 @Argument(index = 1, name = "portNumber", description = "Port Number",
54 required = false, multiValued = false)
55 Integer portNumber = null;
56
sangho538108b2015-04-08 14:29:20 -070057 private static final String FORMAT =
58 " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s";
59
60 @Override
61 protected void execute() {
62 DeviceService deviceService = get(DeviceService.class);
63
Dusan Pajin11ff4a82015-08-20 18:03:05 +020064 if (uri == null) {
65 for (Device d : getSortedDevices(deviceService)) {
66 if (delta) {
67 if (table) {
68 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
69 } else {
70 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
71 }
72 } else {
73 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
74 }
75 }
76 } else {
77 Device d = deviceService.getDevice(deviceId(uri));
78 if (d == null) {
79 error("No such device %s", uri);
80 } else if (delta) {
81 if (table) {
82 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
83 } else {
84 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
85 }
86 } else {
87 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
88 }
89 }
sangho538108b2015-04-08 14:29:20 -070090 }
91
Dusan Pajin11ff4a82015-08-20 18:03:05 +020092 /**
93 * Prints Port Statistics.
94 *
95 * @param deviceId
96 * @param portStats
97 */
sangho538108b2015-04-08 14:29:20 -070098 private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
99 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800100 for (PortStatistics stat : sortByPort(portStats)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700101 if (portNumber != null && stat.port() != portNumber) {
102 continue;
103 }
sangho538108b2015-04-08 14:29:20 -0700104 print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
105 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec());
106 }
107 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200108 /**
109 * Prints Port delta statistics.
110 *
111 * @param deviceId
112 * @param portStats
113 */
114 private void printPortStatsDelta(DeviceId deviceId, Iterable<PortStatistics> portStats) {
115 final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s,"
116 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
117 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800118 for (PortStatistics stat : sortByPort(portStats)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700119 if (portNumber != null && stat.port() != portNumber) {
120 continue;
121 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200122 float duration = ((float) stat.durationSec()) +
123 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
124 float rateRx = stat.bytesReceived() * 8 / duration;
125 float rateTx = stat.bytesSent() * 8 / duration;
126 print(formatDelta, stat.port(),
127 stat.packetsReceived(),
128 stat.packetsSent(),
129 stat.bytesReceived(),
130 stat.bytesSent(),
131 String.format("%.1f", rateRx),
132 String.format("%.1f", rateTx),
133 stat.packetsRxDropped(),
134 stat.packetsTxDropped(),
135 String.format("%.3f", duration));
136 }
137 }
138
139 /**
140 * Prints human readable table with delta Port Statistics for specific device.
141 *
142 * @param deviceId
143 * @param portStats
144 */
145 private void printPortStatsDeltaTable(DeviceId deviceId, Iterable<PortStatistics> portStats) {
146 final String formatDeltaTable = "|%5s | %7s | %7s | %7s | %7s | %7s | %7s | %7s | %7s |%9s |";
147 print("+---------------------------------------------------------------------------------------------------+");
148 print("| DeviceId = %s |", deviceId);
149 print("|---------------------------------------------------------------------------------------------------|");
150 print("| | Receive | Transmit | Time [s] |");
151 print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |");
152 print("|---------------------------------------------------------------------------------------------------|");
153
Charles Chan33a79ce2015-12-12 11:14:07 -0800154 for (PortStatistics stat : sortByPort(portStats)) {
Saurav Dasa2d37502016-03-25 17:50:40 -0700155 if (portNumber != null && stat.port() != portNumber) {
156 continue;
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200157 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700158 float duration = ((float) stat.durationSec()) +
159 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
160 float rateRx = stat.bytesReceived() * 8 / duration;
161 float rateTx = stat.bytesSent() * 8 / duration;
162 print(formatDeltaTable, stat.port(),
163 humanReadable(stat.packetsReceived()),
164 humanReadable(stat.bytesReceived()),
165 humanReadableBps(rateRx),
166 humanReadable(stat.packetsRxDropped()),
167 humanReadable(stat.packetsSent()),
168 humanReadable(stat.bytesSent()),
169 humanReadableBps(rateTx),
170 humanReadable(stat.packetsTxDropped()),
171 String.format("%.3f", duration));
172 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200173 print("+---------------------------------------------------------------------------------------------------+");
174 }
175
176 /**
177 * Converts bytes to human readable string with Kilo, Mega, Giga, etc.
178 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700179 * @param bytes input byte array
180 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200181 */
182 public static String humanReadable(long bytes) {
183 int unit = 1000;
184 if (bytes < unit) {
185 return String.format("%s ", bytes);
186 }
187 int exp = (int) (Math.log(bytes) / Math.log(unit));
188 Character pre = ("KMGTPE").charAt(exp - 1);
189 return String.format("%.2f%s", bytes / Math.pow(unit, exp), pre);
190 }
191 /**
192 * Converts bps to human readable format.
193 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700194 * @param bps input rate
195 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200196 */
197 public static String humanReadableBps(float bps) {
198 int unit = 1000;
199 if (bps < unit) {
200 return String.format("%.0f ", (float) bps);
201 }
202 int exp = (int) (Math.log(bps) / Math.log(unit));
203 Character pre = ("KMGTPE").charAt(exp - 1);
204 return String.format("%.2f%s", bps / Math.pow(unit, exp), pre);
205 }
Charles Chan33a79ce2015-12-12 11:14:07 -0800206
207 private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) {
208 List<PortStatistics> portStatsList = Lists.newArrayList(portStats);
209 portStatsList.sort((PortStatistics o1, PortStatistics o2) ->
210 o1.port() - o2.port());
211 return portStatsList;
212 }
sangho538108b2015-04-08 14:29:20 -0700213}