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