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 | |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 21 | import java.util.Comparator; |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 22 | import java.util.List; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 23 | import java.util.concurrent.TimeUnit; |
| 24 | |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 25 | import com.google.common.collect.Lists; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 26 | import org.apache.karaf.shell.commands.Argument; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 27 | import org.apache.karaf.shell.commands.Command; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 28 | import org.apache.karaf.shell.commands.Option; |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 29 | import org.onosproject.cli.AbstractShellCommand; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 30 | import org.onosproject.net.Device; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 32 | import org.onosproject.net.PortNumber; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 33 | import org.onosproject.net.device.DeviceService; |
| 34 | import org.onosproject.net.device.PortStatistics; |
| 35 | |
| 36 | /** |
| 37 | * Lists port statistic of all ports in the system. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "portstats", |
| 40 | description = "Lists statistics of all ports in the system") |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 41 | public class DevicePortStatsCommand extends AbstractShellCommand { |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 42 | |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 43 | @Option(name = "-nz", aliases = "--nonzero", description = "Show only non-zero portstats", |
| 44 | required = false, multiValued = false) |
| 45 | private boolean nonzero = false; |
| 46 | |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 47 | @Option(name = "-d", aliases = "--delta", |
| 48 | description = "Show delta port statistics," |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 49 | + "only for the last polling interval", |
| 50 | required = false, multiValued = false) |
| 51 | private boolean delta = false; |
| 52 | |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 53 | @Option(name = "-t", aliases = "--table", |
| 54 | description = "Show delta port statistics in table format " |
| 55 | + "using human readable unit", |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 56 | required = false, multiValued = false) |
| 57 | private boolean table = false; |
| 58 | |
| 59 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 60 | required = false, multiValued = false) |
| 61 | String uri = null; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 62 | |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 63 | @Argument(index = 1, name = "portNumber", description = "Port Number", |
| 64 | required = false, multiValued = false) |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 65 | String portNumberStr = null; |
| 66 | |
| 67 | PortNumber portNumber = null; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 68 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 69 | private static final String FORMAT = |
| 70 | " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s"; |
| 71 | |
| 72 | @Override |
| 73 | protected void execute() { |
| 74 | DeviceService deviceService = get(DeviceService.class); |
| 75 | |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 76 | if (portNumberStr != null) { |
| 77 | portNumber = PortNumber.fromString(portNumberStr); |
| 78 | } |
| 79 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 80 | if (uri == null) { |
| 81 | for (Device d : getSortedDevices(deviceService)) { |
| 82 | if (delta) { |
| 83 | if (table) { |
| 84 | printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id())); |
| 85 | } else { |
| 86 | printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id())); |
| 87 | } |
| 88 | } else { |
| 89 | printPortStats(d.id(), deviceService.getPortStatistics(d.id())); |
| 90 | } |
| 91 | } |
| 92 | } else { |
| 93 | Device d = deviceService.getDevice(deviceId(uri)); |
| 94 | if (d == null) { |
| 95 | error("No such device %s", uri); |
| 96 | } else if (delta) { |
| 97 | if (table) { |
| 98 | printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id())); |
| 99 | } else { |
| 100 | printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id())); |
| 101 | } |
| 102 | } else { |
| 103 | printPortStats(d.id(), deviceService.getPortStatistics(d.id())); |
| 104 | } |
| 105 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 108 | /** |
| 109 | * Prints Port Statistics. |
| 110 | * |
| 111 | * @param deviceId |
| 112 | * @param portStats |
| 113 | */ |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 114 | private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) { |
| 115 | print("deviceId=%s", deviceId); |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 116 | for (PortStatistics stat : sortByPort(portStats)) { |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 117 | if (isIrrelevant(stat)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 118 | continue; |
| 119 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 120 | if (nonzero && stat.isZero()) { |
| 121 | continue; |
| 122 | } |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 123 | print(FORMAT, stat.portNumber(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(), |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 124 | stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec()); |
| 125 | } |
| 126 | } |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 127 | |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 128 | private boolean isIrrelevant(PortStatistics stat) { |
| 129 | // TODO revisit logical port (e.g., ALL) handling |
| 130 | return portNumber != null && !portNumber.equals(stat.portNumber()); |
| 131 | } |
| 132 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 133 | /** |
| 134 | * Prints Port delta statistics. |
| 135 | * |
| 136 | * @param deviceId |
| 137 | * @param portStats |
| 138 | */ |
| 139 | private void printPortStatsDelta(DeviceId deviceId, Iterable<PortStatistics> portStats) { |
| 140 | final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s," |
| 141 | + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s"; |
| 142 | print("deviceId=%s", deviceId); |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 143 | for (PortStatistics stat : sortByPort(portStats)) { |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 144 | if (isIrrelevant(stat)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 145 | continue; |
| 146 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 147 | if (nonzero && stat.isZero()) { |
| 148 | continue; |
| 149 | } |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 150 | float duration = ((float) stat.durationSec()) + |
| 151 | (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1)); |
| 152 | float rateRx = stat.bytesReceived() * 8 / duration; |
| 153 | float rateTx = stat.bytesSent() * 8 / duration; |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 154 | print(formatDelta, stat.portNumber(), |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 155 | stat.packetsReceived(), |
| 156 | stat.packetsSent(), |
| 157 | stat.bytesReceived(), |
| 158 | stat.bytesSent(), |
| 159 | String.format("%.1f", rateRx), |
| 160 | String.format("%.1f", rateTx), |
| 161 | stat.packetsRxDropped(), |
| 162 | stat.packetsTxDropped(), |
| 163 | String.format("%.3f", duration)); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Prints human readable table with delta Port Statistics for specific device. |
| 169 | * |
| 170 | * @param deviceId |
| 171 | * @param portStats |
| 172 | */ |
| 173 | private void printPortStatsDeltaTable(DeviceId deviceId, Iterable<PortStatistics> portStats) { |
| 174 | final String formatDeltaTable = "|%5s | %7s | %7s | %7s | %7s | %7s | %7s | %7s | %7s |%9s |"; |
| 175 | print("+---------------------------------------------------------------------------------------------------+"); |
Carmelo Cascone | 868b1bc | 2017-09-13 15:39:00 +0200 | [diff] [blame] | 176 | print("| DeviceId = %-86s |", deviceId); |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 177 | print("|---------------------------------------------------------------------------------------------------|"); |
| 178 | print("| | Receive | Transmit | Time [s] |"); |
| 179 | print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |"); |
| 180 | print("|---------------------------------------------------------------------------------------------------|"); |
| 181 | |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 182 | for (PortStatistics stat : sortByPort(portStats)) { |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 183 | if (isIrrelevant(stat)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 184 | continue; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 185 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 186 | if (nonzero && stat.isZero()) { |
| 187 | continue; |
| 188 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 189 | float duration = ((float) stat.durationSec()) + |
| 190 | (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1)); |
Carmelo Cascone | 868b1bc | 2017-09-13 15:39:00 +0200 | [diff] [blame] | 191 | float rateRx = duration > 0 ? stat.bytesReceived() * 8 / duration : 0; |
| 192 | float rateTx = duration > 0 ? stat.bytesSent() * 8 / duration : 0; |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 193 | print(formatDeltaTable, stat.portNumber(), |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 194 | humanReadable(stat.packetsReceived()), |
| 195 | humanReadable(stat.bytesReceived()), |
| 196 | humanReadableBps(rateRx), |
| 197 | humanReadable(stat.packetsRxDropped()), |
| 198 | humanReadable(stat.packetsSent()), |
| 199 | humanReadable(stat.bytesSent()), |
| 200 | humanReadableBps(rateTx), |
| 201 | humanReadable(stat.packetsTxDropped()), |
| 202 | String.format("%.3f", duration)); |
| 203 | } |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 204 | print("+---------------------------------------------------------------------------------------------------+"); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Converts bytes to human readable string with Kilo, Mega, Giga, etc. |
| 209 | * |
Madan Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 210 | * @param bytes input byte array |
| 211 | * @return human readble string |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 212 | */ |
| 213 | public static String humanReadable(long bytes) { |
| 214 | int unit = 1000; |
| 215 | if (bytes < unit) { |
| 216 | return String.format("%s ", bytes); |
| 217 | } |
| 218 | int exp = (int) (Math.log(bytes) / Math.log(unit)); |
| 219 | Character pre = ("KMGTPE").charAt(exp - 1); |
| 220 | return String.format("%.2f%s", bytes / Math.pow(unit, exp), pre); |
| 221 | } |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 222 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 223 | /** |
| 224 | * Converts bps to human readable format. |
| 225 | * |
Madan Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 226 | * @param bps input rate |
| 227 | * @return human readble string |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 228 | */ |
| 229 | public static String humanReadableBps(float bps) { |
| 230 | int unit = 1000; |
| 231 | if (bps < unit) { |
Yuta HIGUCHI | 1560384 | 2017-01-25 19:26:08 -0800 | [diff] [blame] | 232 | return String.format("%.0f ", bps); |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 233 | } |
| 234 | int exp = (int) (Math.log(bps) / Math.log(unit)); |
| 235 | Character pre = ("KMGTPE").charAt(exp - 1); |
| 236 | return String.format("%.2f%s", bps / Math.pow(unit, exp), pre); |
| 237 | } |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 238 | |
| 239 | private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) { |
| 240 | List<PortStatistics> portStatsList = Lists.newArrayList(portStats); |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame^] | 241 | |
| 242 | portStatsList.sort(Comparator.comparing(ps -> ps.portNumber().toLong())); |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 243 | return portStatsList; |
| 244 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 245 | } |