blob: f9a823768706ff449be5b36f0c80ff548758ddda [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001package org.onosproject.cli.net;
2
3/*
4 * Copyright 2015 Open Networking Laboratory
5 *
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
53 private static final String FORMAT =
54 " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s";
55
56 @Override
57 protected void execute() {
58 DeviceService deviceService = get(DeviceService.class);
59
Dusan Pajin11ff4a82015-08-20 18:03:05 +020060 if (uri == null) {
61 for (Device d : getSortedDevices(deviceService)) {
62 if (delta) {
63 if (table) {
64 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
65 } else {
66 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
67 }
68 } else {
69 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
70 }
71 }
72 } else {
73 Device d = deviceService.getDevice(deviceId(uri));
74 if (d == null) {
75 error("No such device %s", uri);
76 } else if (delta) {
77 if (table) {
78 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
79 } else {
80 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
81 }
82 } else {
83 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
84 }
85 }
sangho538108b2015-04-08 14:29:20 -070086 }
87
Dusan Pajin11ff4a82015-08-20 18:03:05 +020088 /**
89 * Prints Port Statistics.
90 *
91 * @param deviceId
92 * @param portStats
93 */
sangho538108b2015-04-08 14:29:20 -070094 private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
95 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -080096 for (PortStatistics stat : sortByPort(portStats)) {
sangho538108b2015-04-08 14:29:20 -070097 print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
98 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec());
99 }
100 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200101 /**
102 * Prints Port delta statistics.
103 *
104 * @param deviceId
105 * @param portStats
106 */
107 private void printPortStatsDelta(DeviceId deviceId, Iterable<PortStatistics> portStats) {
108 final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s,"
109 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
110 print("deviceId=%s", deviceId);
Charles Chan33a79ce2015-12-12 11:14:07 -0800111 for (PortStatistics stat : sortByPort(portStats)) {
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200112 float duration = ((float) stat.durationSec()) +
113 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
114 float rateRx = stat.bytesReceived() * 8 / duration;
115 float rateTx = stat.bytesSent() * 8 / duration;
116 print(formatDelta, stat.port(),
117 stat.packetsReceived(),
118 stat.packetsSent(),
119 stat.bytesReceived(),
120 stat.bytesSent(),
121 String.format("%.1f", rateRx),
122 String.format("%.1f", rateTx),
123 stat.packetsRxDropped(),
124 stat.packetsTxDropped(),
125 String.format("%.3f", duration));
126 }
127 }
128
129 /**
130 * Prints human readable table with delta Port Statistics for specific device.
131 *
132 * @param deviceId
133 * @param portStats
134 */
135 private void printPortStatsDeltaTable(DeviceId deviceId, Iterable<PortStatistics> portStats) {
136 final String formatDeltaTable = "|%5s | %7s | %7s | %7s | %7s | %7s | %7s | %7s | %7s |%9s |";
137 print("+---------------------------------------------------------------------------------------------------+");
138 print("| DeviceId = %s |", deviceId);
139 print("|---------------------------------------------------------------------------------------------------|");
140 print("| | Receive | Transmit | Time [s] |");
141 print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |");
142 print("|---------------------------------------------------------------------------------------------------|");
143
Charles Chan33a79ce2015-12-12 11:14:07 -0800144 for (PortStatistics stat : sortByPort(portStats)) {
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200145 float duration = ((float) stat.durationSec()) +
146 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
147 float rateRx = stat.bytesReceived() * 8 / duration;
148 float rateTx = stat.bytesSent() * 8 / duration;
149 print(formatDeltaTable, stat.port(),
150 humanReadable(stat.packetsReceived()),
151 humanReadable(stat.bytesReceived()),
152 humanReadableBps(rateRx),
153 humanReadable(stat.packetsRxDropped()),
154 humanReadable(stat.packetsSent()),
155 humanReadable(stat.bytesSent()),
156 humanReadableBps(rateTx),
157 humanReadable(stat.packetsTxDropped()),
158 String.format("%.3f", duration));
159 }
160 print("+---------------------------------------------------------------------------------------------------+");
161 }
162
163 /**
164 * Converts bytes to human readable string with Kilo, Mega, Giga, etc.
165 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700166 * @param bytes input byte array
167 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200168 */
169 public static String humanReadable(long bytes) {
170 int unit = 1000;
171 if (bytes < unit) {
172 return String.format("%s ", bytes);
173 }
174 int exp = (int) (Math.log(bytes) / Math.log(unit));
175 Character pre = ("KMGTPE").charAt(exp - 1);
176 return String.format("%.2f%s", bytes / Math.pow(unit, exp), pre);
177 }
178 /**
179 * Converts bps to human readable format.
180 *
Madan Jampanif97edc12015-08-31 14:41:01 -0700181 * @param bps input rate
182 * @return human readble string
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200183 */
184 public static String humanReadableBps(float bps) {
185 int unit = 1000;
186 if (bps < unit) {
187 return String.format("%.0f ", (float) bps);
188 }
189 int exp = (int) (Math.log(bps) / Math.log(unit));
190 Character pre = ("KMGTPE").charAt(exp - 1);
191 return String.format("%.2f%s", bps / Math.pow(unit, exp), pre);
192 }
Charles Chan33a79ce2015-12-12 11:14:07 -0800193
194 private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) {
195 List<PortStatistics> portStatsList = Lists.newArrayList(portStats);
196 portStatsList.sort((PortStatistics o1, PortStatistics o2) ->
197 o1.port() - o2.port());
198 return portStatsList;
199 }
sangho538108b2015-04-08 14:29:20 -0700200}