Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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; |
| 21 | import org.onosproject.cluster.ClusterService; |
| 22 | import org.onosproject.cluster.NodeId; |
| 23 | import org.onosproject.cpman.ControlLoad; |
| 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 | */ |
| 40 | @Command(scope = "onos", name = "cpman-stats-list", |
| 41 | description = "Lists control metrics statistics") |
| 42 | public class ControlMetricsStatsListCommand extends AbstractShellCommand { |
| 43 | |
| 44 | private static final String FMT = "metricType=%s, latestValue=%d, " + |
| 45 | "averageValue=%d, latestTime=%s"; |
| 46 | private static final String INVALID_TYPE = "Invalid control resource type."; |
| 47 | |
| 48 | @Argument(index = 0, name = "type", |
| 49 | description = "Resource type (cpu|memory|disk|network|control_message)", |
| 50 | required = true, multiValued = false) |
| 51 | String type = null; |
| 52 | |
| 53 | @Argument(index = 1, name = "name", description = "Resource name (or Device Id)", |
| 54 | required = false, multiValued = false) |
| 55 | String name = null; |
| 56 | |
| 57 | @Override |
| 58 | protected void execute() { |
| 59 | ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class); |
| 60 | ClusterService clusterService = get(ClusterService.class); |
| 61 | NodeId nodeId = clusterService.getLocalNode().id(); |
| 62 | switch (type) { |
| 63 | case "cpu": |
| 64 | printMetricsStats(service, nodeId, CPU_METRICS); |
| 65 | break; |
| 66 | case "memory": |
| 67 | printMetricsStats(service, nodeId, MEMORY_METRICS); |
| 68 | break; |
| 69 | case "disk": |
| 70 | printMetricsStats(service, nodeId, DISK_METRICS, name); |
| 71 | break; |
| 72 | case "network": |
| 73 | printMetricsStats(service, nodeId, NETWORK_METRICS, name); |
| 74 | break; |
| 75 | case "control_message": |
| 76 | if (name != null) { |
| 77 | printMetricsStats(service, nodeId, CONTROL_MESSAGE_METRICS, DeviceId.deviceId(name)); |
| 78 | } |
| 79 | break; |
| 80 | default: |
| 81 | print(INVALID_TYPE); |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId, |
| 87 | Set<ControlMetricType> typeSet) { |
| 88 | printMetricsStats(service, nodeId, typeSet, null, null); |
| 89 | } |
| 90 | |
| 91 | private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId, |
| 92 | Set<ControlMetricType> typeSet, String name) { |
| 93 | printMetricsStats(service, nodeId, typeSet, name, null); |
| 94 | } |
| 95 | |
| 96 | private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId, |
| 97 | Set<ControlMetricType> typeSet, DeviceId did) { |
| 98 | printMetricsStats(service, nodeId, typeSet, null, did); |
| 99 | } |
| 100 | |
| 101 | private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId, |
| 102 | Set<ControlMetricType> typeSet, String name, DeviceId did) { |
| 103 | if (name == null && did == null) { |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 104 | typeSet.forEach(s -> print(s, service.getLocalLoad(s, Optional.ofNullable(null)))); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 105 | } else if (name == null && did != null) { |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 106 | typeSet.forEach(s -> print(s, service.getLocalLoad(s, Optional.of(did)))); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 107 | } else if (name != null && did == null) { |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 108 | typeSet.forEach(s -> print(s, service.getLocalLoad(s, name))); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | private void print(ControlMetricType type, ControlLoad cl) { |
| 113 | if (cl != null) { |
| 114 | print(FMT, type.toString(), cl.latest(), cl.average(), cl.time()); |
| 115 | } |
| 116 | } |
| 117 | } |