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