Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [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 | */ |
| 16 | package org.onosproject.cpman.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 21 | import org.onosproject.cluster.NodeId; |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 22 | import org.onosproject.cpman.ControlLoadSnapshot; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 23 | import org.onosproject.cpman.ControlMetricType; |
| 24 | import org.onosproject.cpman.ControlPlaneMonitorService; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | |
| 27 | import java.util.Optional; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS; |
| 31 | import static org.onosproject.cpman.ControlResource.CPU_METRICS; |
| 32 | import static org.onosproject.cpman.ControlResource.DISK_METRICS; |
| 33 | import static org.onosproject.cpman.ControlResource.MEMORY_METRICS; |
| 34 | import static org.onosproject.cpman.ControlResource.NETWORK_METRICS; |
| 35 | |
| 36 | /** |
| 37 | * Lists all stats information of control plane metrics. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "cpman-stats-list", |
| 40 | description = "Lists control metrics statistics") |
| 41 | public class ControlMetricsStatsListCommand extends AbstractShellCommand { |
| 42 | |
| 43 | private static final String FMT = "metricType=%s, latestValue=%d, " + |
| 44 | "averageValue=%d, latestTime=%s"; |
| 45 | private static final String INVALID_TYPE = "Invalid control resource type."; |
| 46 | |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 47 | @Argument(index = 0, name = "node", description = "ONOS node identifier", |
| 48 | required = true, multiValued = false) |
| 49 | String node = null; |
| 50 | |
| 51 | @Argument(index = 1, name = "type", |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 52 | description = "Resource type (cpu|memory|disk|network|control_message)", |
| 53 | required = true, multiValued = false) |
| 54 | String type = null; |
| 55 | |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 56 | @Argument(index = 2, name = "name", description = "Resource name (or Device Id)", |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 57 | required = false, multiValued = false) |
| 58 | String name = null; |
| 59 | |
| 60 | @Override |
| 61 | protected void execute() { |
| 62 | ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 63 | NodeId nodeId = NodeId.nodeId(node); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 64 | switch (type) { |
| 65 | case "cpu": |
| 66 | printMetricsStats(service, nodeId, CPU_METRICS); |
| 67 | break; |
| 68 | case "memory": |
| 69 | printMetricsStats(service, nodeId, MEMORY_METRICS); |
| 70 | break; |
| 71 | case "disk": |
| 72 | printMetricsStats(service, nodeId, DISK_METRICS, name); |
| 73 | break; |
| 74 | case "network": |
| 75 | printMetricsStats(service, nodeId, NETWORK_METRICS, name); |
| 76 | break; |
| 77 | case "control_message": |
| 78 | if (name != null) { |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 79 | printMetricsStats(service, nodeId, CONTROL_MESSAGE_METRICS, |
| 80 | DeviceId.deviceId(name)); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 81 | } |
| 82 | break; |
| 83 | default: |
| 84 | print(INVALID_TYPE); |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Prints system metric statistic information. |
| 91 | * |
| 92 | * @param service monitor service |
| 93 | * @param nodeId node identifier |
| 94 | * @param typeSet control metric type |
| 95 | */ |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 96 | private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId, |
| 97 | Set<ControlMetricType> typeSet) { |
| 98 | printMetricsStats(service, nodeId, typeSet, null, null); |
| 99 | } |
| 100 | |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Prints disk and network metric statistic information. |
| 103 | * |
| 104 | * @param service monitor service |
| 105 | * @param nodeId node identifier |
| 106 | * @param typeSet control metric type |
| 107 | * @param resName resource name |
| 108 | */ |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 109 | private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId, |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 110 | Set<ControlMetricType> typeSet, String resName) { |
| 111 | printMetricsStats(service, nodeId, typeSet, resName, null); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Prints control message metric statistic information. |
| 116 | * |
| 117 | * @param service monitor service |
| 118 | * @param nodeId node identifier |
| 119 | * @param typeSet control metric type |
| 120 | * @param did device identifier |
| 121 | */ |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 122 | private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId, |
| 123 | Set<ControlMetricType> typeSet, DeviceId did) { |
| 124 | printMetricsStats(service, nodeId, typeSet, null, did); |
| 125 | } |
| 126 | |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Prints control plane metric statistic information. |
| 129 | * |
| 130 | * @param service monitor service |
| 131 | * @param nodeId node identifier |
| 132 | * @param typeSet control metric type |
| 133 | * @param resName resource name |
| 134 | * @param did device identifier |
| 135 | */ |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 136 | private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId, |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 137 | Set<ControlMetricType> typeSet, String resName, DeviceId did) { |
| 138 | if (resName == null && did == null) { |
| 139 | typeSet.forEach(s -> { |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 140 | ControlLoadSnapshot cls = service.getLoadSync(nodeId, s, Optional.empty()); |
| 141 | printControlLoadSnapshot(s, cls); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 142 | }); |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 143 | } else if (resName == null) { |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 144 | typeSet.forEach(s -> { |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 145 | ControlLoadSnapshot cls = service.getLoadSync(nodeId, s, Optional.of(did)); |
| 146 | printControlLoadSnapshot(s, cls); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 147 | }); |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 148 | } else if (did == null) { |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 149 | typeSet.forEach(s -> { |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 150 | ControlLoadSnapshot cls = service.getLoadSync(nodeId, s, resName); |
| 151 | printControlLoadSnapshot(s, cls); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 152 | }); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Prints control load snapshot. |
| 158 | * |
| 159 | * @param cmType control metric type |
| 160 | * @param cls control load snapshot |
| 161 | */ |
| 162 | private void printControlLoadSnapshot(ControlMetricType cmType, ControlLoadSnapshot cls) { |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 163 | if (cls != null) { |
| 164 | print(FMT, cmType.toString(), cls.latest(), cls.average(), cls.time()); |
| 165 | } else { |
| 166 | print("Failed to retrieve metric value for type {}", cmType.toString()); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | } |