sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 18 | import static org.onosproject.net.DeviceId.deviceId; |
| 19 | |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 20 | import java.util.List; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 21 | import java.util.concurrent.TimeUnit; |
| 22 | |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Lists; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Argument; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Command; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 26 | import org.apache.karaf.shell.commands.Option; |
| 27 | import org.onosproject.net.Device; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.device.DeviceService; |
| 30 | import 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 Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 37 | public class DevicePortStatsCommand extends DevicesListCommand { |
| 38 | |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 39 | @Option(name = "-nz", aliases = "--nonzero", description = "Show only non-zero portstats", |
| 40 | required = false, multiValued = false) |
| 41 | private boolean nonzero = false; |
| 42 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 43 | @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; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 55 | |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 56 | @Argument(index = 1, name = "portNumber", description = "Port Number", |
| 57 | required = false, multiValued = false) |
| 58 | Integer portNumber = null; |
| 59 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 60 | 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 Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 67 | 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 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 95 | /** |
| 96 | * Prints Port Statistics. |
| 97 | * |
| 98 | * @param deviceId |
| 99 | * @param portStats |
| 100 | */ |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 101 | private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) { |
| 102 | print("deviceId=%s", deviceId); |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 103 | for (PortStatistics stat : sortByPort(portStats)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 104 | if (portNumber != null && stat.port() != portNumber) { |
| 105 | continue; |
| 106 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 107 | if (nonzero && stat.isZero()) { |
| 108 | continue; |
| 109 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 110 | print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(), |
| 111 | stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec()); |
| 112 | } |
| 113 | } |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 114 | /** |
| 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 Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 124 | for (PortStatistics stat : sortByPort(portStats)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 125 | if (portNumber != null && stat.port() != portNumber) { |
| 126 | continue; |
| 127 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 128 | if (nonzero && stat.isZero()) { |
| 129 | continue; |
| 130 | } |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 131 | 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 Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 163 | for (PortStatistics stat : sortByPort(portStats)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 164 | if (portNumber != null && stat.port() != portNumber) { |
| 165 | continue; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 166 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 167 | if (nonzero && stat.isZero()) { |
| 168 | continue; |
| 169 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 170 | 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 Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 185 | print("+---------------------------------------------------------------------------------------------------+"); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Converts bytes to human readable string with Kilo, Mega, Giga, etc. |
| 190 | * |
Madan Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 191 | * @param bytes input byte array |
| 192 | * @return human readble string |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 193 | */ |
| 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 Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 206 | * @param bps input rate |
| 207 | * @return human readble string |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 208 | */ |
| 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 Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 218 | |
| 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 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 225 | } |