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