sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 1 | package org.onosproject.cli.net; |
| 2 | |
| 3 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 4 | * Copyright 2015-present Open Networking Laboratory |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 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 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; |
| 28 | import org.onosproject.net.Device; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | import 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 Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 38 | public 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; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 52 | |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 53 | @Argument(index = 1, name = "portNumber", description = "Port Number", |
| 54 | required = false, multiValued = false) |
| 55 | Integer portNumber = null; |
| 56 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 57 | private static final String FORMAT = |
| 58 | " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s"; |
| 59 | |
| 60 | @Override |
| 61 | protected void execute() { |
| 62 | DeviceService deviceService = get(DeviceService.class); |
| 63 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 64 | if (uri == null) { |
| 65 | for (Device d : getSortedDevices(deviceService)) { |
| 66 | if (delta) { |
| 67 | if (table) { |
| 68 | printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id())); |
| 69 | } else { |
| 70 | printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id())); |
| 71 | } |
| 72 | } else { |
| 73 | printPortStats(d.id(), deviceService.getPortStatistics(d.id())); |
| 74 | } |
| 75 | } |
| 76 | } else { |
| 77 | Device d = deviceService.getDevice(deviceId(uri)); |
| 78 | if (d == null) { |
| 79 | error("No such device %s", uri); |
| 80 | } else if (delta) { |
| 81 | if (table) { |
| 82 | printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id())); |
| 83 | } else { |
| 84 | printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id())); |
| 85 | } |
| 86 | } else { |
| 87 | printPortStats(d.id(), deviceService.getPortStatistics(d.id())); |
| 88 | } |
| 89 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 92 | /** |
| 93 | * Prints Port Statistics. |
| 94 | * |
| 95 | * @param deviceId |
| 96 | * @param portStats |
| 97 | */ |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 98 | private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) { |
| 99 | print("deviceId=%s", deviceId); |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 100 | for (PortStatistics stat : sortByPort(portStats)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 101 | if (portNumber != null && stat.port() != portNumber) { |
| 102 | continue; |
| 103 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 104 | print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(), |
| 105 | stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec()); |
| 106 | } |
| 107 | } |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 108 | /** |
| 109 | * Prints Port delta statistics. |
| 110 | * |
| 111 | * @param deviceId |
| 112 | * @param portStats |
| 113 | */ |
| 114 | private void printPortStatsDelta(DeviceId deviceId, Iterable<PortStatistics> portStats) { |
| 115 | final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s," |
| 116 | + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s"; |
| 117 | print("deviceId=%s", deviceId); |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 118 | for (PortStatistics stat : sortByPort(portStats)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 119 | if (portNumber != null && stat.port() != portNumber) { |
| 120 | continue; |
| 121 | } |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 122 | float duration = ((float) stat.durationSec()) + |
| 123 | (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1)); |
| 124 | float rateRx = stat.bytesReceived() * 8 / duration; |
| 125 | float rateTx = stat.bytesSent() * 8 / duration; |
| 126 | print(formatDelta, stat.port(), |
| 127 | stat.packetsReceived(), |
| 128 | stat.packetsSent(), |
| 129 | stat.bytesReceived(), |
| 130 | stat.bytesSent(), |
| 131 | String.format("%.1f", rateRx), |
| 132 | String.format("%.1f", rateTx), |
| 133 | stat.packetsRxDropped(), |
| 134 | stat.packetsTxDropped(), |
| 135 | String.format("%.3f", duration)); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Prints human readable table with delta Port Statistics for specific device. |
| 141 | * |
| 142 | * @param deviceId |
| 143 | * @param portStats |
| 144 | */ |
| 145 | private void printPortStatsDeltaTable(DeviceId deviceId, Iterable<PortStatistics> portStats) { |
| 146 | final String formatDeltaTable = "|%5s | %7s | %7s | %7s | %7s | %7s | %7s | %7s | %7s |%9s |"; |
| 147 | print("+---------------------------------------------------------------------------------------------------+"); |
| 148 | print("| DeviceId = %s |", deviceId); |
| 149 | print("|---------------------------------------------------------------------------------------------------|"); |
| 150 | print("| | Receive | Transmit | Time [s] |"); |
| 151 | print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |"); |
| 152 | print("|---------------------------------------------------------------------------------------------------|"); |
| 153 | |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 154 | for (PortStatistics stat : sortByPort(portStats)) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 155 | if (portNumber != null && stat.port() != portNumber) { |
| 156 | continue; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 157 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 158 | float duration = ((float) stat.durationSec()) + |
| 159 | (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1)); |
| 160 | float rateRx = stat.bytesReceived() * 8 / duration; |
| 161 | float rateTx = stat.bytesSent() * 8 / duration; |
| 162 | print(formatDeltaTable, stat.port(), |
| 163 | humanReadable(stat.packetsReceived()), |
| 164 | humanReadable(stat.bytesReceived()), |
| 165 | humanReadableBps(rateRx), |
| 166 | humanReadable(stat.packetsRxDropped()), |
| 167 | humanReadable(stat.packetsSent()), |
| 168 | humanReadable(stat.bytesSent()), |
| 169 | humanReadableBps(rateTx), |
| 170 | humanReadable(stat.packetsTxDropped()), |
| 171 | String.format("%.3f", duration)); |
| 172 | } |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 173 | print("+---------------------------------------------------------------------------------------------------+"); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Converts bytes to human readable string with Kilo, Mega, Giga, etc. |
| 178 | * |
Madan Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 179 | * @param bytes input byte array |
| 180 | * @return human readble string |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 181 | */ |
| 182 | public static String humanReadable(long bytes) { |
| 183 | int unit = 1000; |
| 184 | if (bytes < unit) { |
| 185 | return String.format("%s ", bytes); |
| 186 | } |
| 187 | int exp = (int) (Math.log(bytes) / Math.log(unit)); |
| 188 | Character pre = ("KMGTPE").charAt(exp - 1); |
| 189 | return String.format("%.2f%s", bytes / Math.pow(unit, exp), pre); |
| 190 | } |
| 191 | /** |
| 192 | * Converts bps to human readable format. |
| 193 | * |
Madan Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 194 | * @param bps input rate |
| 195 | * @return human readble string |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 196 | */ |
| 197 | public static String humanReadableBps(float bps) { |
| 198 | int unit = 1000; |
| 199 | if (bps < unit) { |
| 200 | return String.format("%.0f ", (float) bps); |
| 201 | } |
| 202 | int exp = (int) (Math.log(bps) / Math.log(unit)); |
| 203 | Character pre = ("KMGTPE").charAt(exp - 1); |
| 204 | return String.format("%.2f%s", bps / Math.pow(unit, exp), pre); |
| 205 | } |
Charles Chan | 33a79ce | 2015-12-12 11:14:07 -0800 | [diff] [blame] | 206 | |
| 207 | private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) { |
| 208 | List<PortStatistics> portStatsList = Lists.newArrayList(portStats); |
| 209 | portStatsList.sort((PortStatistics o1, PortStatistics o2) -> |
| 210 | o1.port() - o2.port()); |
| 211 | return portStatsList; |
| 212 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 213 | } |