sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 3 | * |
| 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'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 18 | import static org.onosproject.cli.net.DevicesListCommand.getSortedDevices; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 19 | import static org.onosproject.net.DeviceId.deviceId; |
| 20 | |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 21 | import java.util.List; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 24 | import com.google.common.collect.Lists; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Argument; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 26 | import org.apache.karaf.shell.commands.Command; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 27 | import org.apache.karaf.shell.commands.Option; |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 28 | import org.onosproject.cli.AbstractShellCommand; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 29 | import org.onosproject.net.Device; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import 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 HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 39 | public class DevicePortStatsCommand extends AbstractShellCommand { |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 40 | |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 41 | @Option(name = "-nz", aliases = "--nonzero", description = "Show only non-zero portstats", |
| 42 | required = false, multiValued = false) |
| 43 | private boolean nonzero = false; |
| 44 | |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 45 | @Option(name = "-d", aliases = "--delta", |
| 46 | description = "Show delta port statistics," |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 47 | + "only for the last polling interval", |
| 48 | required = false, multiValued = false) |
| 49 | private boolean delta = false; |
| 50 | |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 51 | @Option(name = "-t", aliases = "--table", |
| 52 | description = "Show delta port statistics in table format " |
| 53 | + "using human readable unit", |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 54 | 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; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 60 | |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 61 | @Argument(index = 1, name = "portNumber", description = "Port Number", |
| 62 | required = false, multiValued = false) |
| 63 | Integer portNumber = null; |
| 64 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 65 | 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 Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 72 | 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 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 100 | /** |
| 101 | * Prints Port Statistics. |
| 102 | * |
| 103 | * @param deviceId |
| 104 | * @param portStats |
| 105 | */ |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 106 | private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) { |
| 107 | print("deviceId=%s", deviceId); |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 108 | for (PortStatistics stat : sortByPort(portStats)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 109 | if (portNumber != null && stat.port() != portNumber) { |
| 110 | continue; |
| 111 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 112 | if (nonzero && stat.isZero()) { |
| 113 | continue; |
| 114 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 115 | print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(), |
| 116 | stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec()); |
| 117 | } |
| 118 | } |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 119 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 120 | /** |
| 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 Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 130 | for (PortStatistics stat : sortByPort(portStats)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 131 | if (portNumber != null && stat.port() != portNumber) { |
| 132 | continue; |
| 133 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 134 | if (nonzero && stat.isZero()) { |
| 135 | continue; |
| 136 | } |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 137 | 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("+---------------------------------------------------------------------------------------------------+"); |
| 163 | print("| DeviceId = %s |", deviceId); |
| 164 | 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 Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 169 | for (PortStatistics stat : sortByPort(portStats)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 170 | if (portNumber != null && stat.port() != portNumber) { |
| 171 | continue; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 172 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 173 | if (nonzero && stat.isZero()) { |
| 174 | continue; |
| 175 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 176 | float duration = ((float) stat.durationSec()) + |
| 177 | (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1)); |
| 178 | float rateRx = stat.bytesReceived() * 8 / duration; |
| 179 | float rateTx = stat.bytesSent() * 8 / duration; |
| 180 | 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 Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 191 | print("+---------------------------------------------------------------------------------------------------+"); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Converts bytes to human readable string with Kilo, Mega, Giga, etc. |
| 196 | * |
Madan Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 197 | * @param bytes input byte array |
| 198 | * @return human readble string |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 199 | */ |
| 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 HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 209 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 210 | /** |
| 211 | * Converts bps to human readable format. |
| 212 | * |
Madan Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 213 | * @param bps input rate |
| 214 | * @return human readble string |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 215 | */ |
| 216 | public static String humanReadableBps(float bps) { |
| 217 | int unit = 1000; |
| 218 | if (bps < unit) { |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 219 | return String.format("%.0f ", bps); |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 220 | } |
| 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 Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 225 | |
| 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 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 232 | } |