blob: 1fc7d43fa434795f940587ec26bb4658b9007ccd [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001package org.onosproject.cli.net;
2
3/*
4 * Copyright 2015 Open Networking Laboratory
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 Pajin11ff4a82015-08-20 18:03:05 +020019import static org.onosproject.net.DeviceId.deviceId;
20
21import java.util.concurrent.TimeUnit;
22
23import org.apache.karaf.shell.commands.Argument;
sangho538108b2015-04-08 14:29:20 -070024import org.apache.karaf.shell.commands.Command;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020025import org.apache.karaf.shell.commands.Option;
26import org.onosproject.net.Device;
sangho538108b2015-04-08 14:29:20 -070027import org.onosproject.net.DeviceId;
28import org.onosproject.net.device.DeviceService;
29import org.onosproject.net.device.PortStatistics;
30
31/**
32 * Lists port statistic of all ports in the system.
33 */
34@Command(scope = "onos", name = "portstats",
35 description = "Lists statistics of all ports in the system")
Dusan Pajin11ff4a82015-08-20 18:03:05 +020036public class DevicePortStatsCommand extends DevicesListCommand {
37
38 @Option(name = "-d", aliases = "--delta", description = "Show Delta Port Statistics,"
39 + "only for the last polling interval",
40 required = false, multiValued = false)
41 private boolean delta = false;
42
43 @Option(name = "-t", aliases = "--table", description = "Show human readable table format for statistics",
44 required = false, multiValued = false)
45 private boolean table = false;
46
47 @Argument(index = 0, name = "uri", description = "Device ID",
48 required = false, multiValued = false)
49 String uri = null;
sangho538108b2015-04-08 14:29:20 -070050
51 private static final String FORMAT =
52 " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s";
53
54 @Override
55 protected void execute() {
56 DeviceService deviceService = get(DeviceService.class);
57
Dusan Pajin11ff4a82015-08-20 18:03:05 +020058 if (uri == null) {
59 for (Device d : getSortedDevices(deviceService)) {
60 if (delta) {
61 if (table) {
62 printPortStatsDeltaTable(d.id(), deviceService.getPortDeltaStatistics(d.id()));
63 } else {
64 printPortStatsDelta(d.id(), deviceService.getPortDeltaStatistics(d.id()));
65 }
66 } else {
67 printPortStats(d.id(), deviceService.getPortStatistics(d.id()));
68 }
69 }
70 } else {
71 Device d = deviceService.getDevice(deviceId(uri));
72 if (d == null) {
73 error("No such device %s", uri);
74 } else 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 }
sangho538108b2015-04-08 14:29:20 -070084 }
85
Dusan Pajin11ff4a82015-08-20 18:03:05 +020086 /**
87 * Prints Port Statistics.
88 *
89 * @param deviceId
90 * @param portStats
91 */
sangho538108b2015-04-08 14:29:20 -070092 private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
Dusan Pajin11ff4a82015-08-20 18:03:05 +020093
sangho538108b2015-04-08 14:29:20 -070094 print("deviceId=%s", deviceId);
95 for (PortStatistics stat : portStats) {
96 print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
97 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec());
98 }
99 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200100 /**
101 * Prints Port delta statistics.
102 *
103 * @param deviceId
104 * @param portStats
105 */
106 private void printPortStatsDelta(DeviceId deviceId, Iterable<PortStatistics> portStats) {
107 final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s,"
108 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
109 print("deviceId=%s", deviceId);
110 for (PortStatistics stat : portStats) {
111 float duration = ((float) stat.durationSec()) +
112 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
113 float rateRx = stat.bytesReceived() * 8 / duration;
114 float rateTx = stat.bytesSent() * 8 / duration;
115 print(formatDelta, stat.port(),
116 stat.packetsReceived(),
117 stat.packetsSent(),
118 stat.bytesReceived(),
119 stat.bytesSent(),
120 String.format("%.1f", rateRx),
121 String.format("%.1f", rateTx),
122 stat.packetsRxDropped(),
123 stat.packetsTxDropped(),
124 String.format("%.3f", duration));
125 }
126 }
127
128 /**
129 * Prints human readable table with delta Port Statistics for specific device.
130 *
131 * @param deviceId
132 * @param portStats
133 */
134 private void printPortStatsDeltaTable(DeviceId deviceId, Iterable<PortStatistics> portStats) {
135 final String formatDeltaTable = "|%5s | %7s | %7s | %7s | %7s | %7s | %7s | %7s | %7s |%9s |";
136 print("+---------------------------------------------------------------------------------------------------+");
137 print("| DeviceId = %s |", deviceId);
138 print("|---------------------------------------------------------------------------------------------------|");
139 print("| | Receive | Transmit | Time [s] |");
140 print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |");
141 print("|---------------------------------------------------------------------------------------------------|");
142
143 for (PortStatistics stat : portStats) {
144 float duration = ((float) stat.durationSec()) +
145 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
146 float rateRx = stat.bytesReceived() * 8 / duration;
147 float rateTx = stat.bytesSent() * 8 / duration;
148 print(formatDeltaTable, stat.port(),
149 humanReadable(stat.packetsReceived()),
150 humanReadable(stat.bytesReceived()),
151 humanReadableBps(rateRx),
152 humanReadable(stat.packetsRxDropped()),
153 humanReadable(stat.packetsSent()),
154 humanReadable(stat.bytesSent()),
155 humanReadableBps(rateTx),
156 humanReadable(stat.packetsTxDropped()),
157 String.format("%.3f", duration));
158 }
159 print("+---------------------------------------------------------------------------------------------------+");
160 }
161
162 /**
163 * Converts bytes to human readable string with Kilo, Mega, Giga, etc.
164 *
165 * @param bytes
166 * @return
167 */
168 public static String humanReadable(long bytes) {
169 int unit = 1000;
170 if (bytes < unit) {
171 return String.format("%s ", bytes);
172 }
173 int exp = (int) (Math.log(bytes) / Math.log(unit));
174 Character pre = ("KMGTPE").charAt(exp - 1);
175 return String.format("%.2f%s", bytes / Math.pow(unit, exp), pre);
176 }
177 /**
178 * Converts bps to human readable format.
179 *
180 * @param bps
181 * @return
182 */
183 public static String humanReadableBps(float bps) {
184 int unit = 1000;
185 if (bps < unit) {
186 return String.format("%.0f ", (float) bps);
187 }
188 int exp = (int) (Math.log(bps) / Math.log(unit));
189 Character pre = ("KMGTPE").charAt(exp - 1);
190 return String.format("%.2f%s", bps / Math.pow(unit, exp), pre);
191 }
sangho538108b2015-04-08 14:29:20 -0700192}