blob: d545094991c04deace91031c1b2802523f231513 [file] [log] [blame]
Jian Li85060ac2016-02-04 09:58:56 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Li85060ac2016-02-04 09:58:56 -08003 *
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 */
16package org.onosproject.cpman.cli;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Ray Milkeya04e4442018-10-03 11:04:35 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Li85060ac2016-02-04 09:58:56 -080022import org.onosproject.cli.AbstractShellCommand;
Ray Milkeya04e4442018-10-03 11:04:35 -070023import org.onosproject.cli.NodeIdCompleter;
Jian Li85060ac2016-02-04 09:58:56 -080024import org.onosproject.cluster.NodeId;
Jian Li67e1e152016-04-18 17:52:58 -070025import org.onosproject.cpman.ControlLoadSnapshot;
Jian Li85060ac2016-02-04 09:58:56 -080026import org.onosproject.cpman.ControlMetricType;
27import org.onosproject.cpman.ControlPlaneMonitorService;
28import org.onosproject.net.DeviceId;
29
30import java.util.Optional;
31import java.util.Set;
32
33import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
34import static org.onosproject.cpman.ControlResource.CPU_METRICS;
35import static org.onosproject.cpman.ControlResource.DISK_METRICS;
36import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
37import static org.onosproject.cpman.ControlResource.NETWORK_METRICS;
38
39/**
40 * Lists all stats information of control plane metrics.
41 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070042@Service
Jian Li85060ac2016-02-04 09:58:56 -080043@Command(scope = "onos", name = "cpman-stats-list",
44 description = "Lists control metrics statistics")
45public 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 Li67e1e152016-04-18 17:52:58 -070051 @Argument(index = 0, name = "node", description = "ONOS node identifier",
52 required = true, multiValued = false)
Ray Milkeya04e4442018-10-03 11:04:35 -070053 @Completion(NodeIdCompleter.class)
Jian Li67e1e152016-04-18 17:52:58 -070054 String node = null;
55
56 @Argument(index = 1, name = "type",
Jian Li85060ac2016-02-04 09:58:56 -080057 description = "Resource type (cpu|memory|disk|network|control_message)",
58 required = true, multiValued = false)
Ray Milkeya04e4442018-10-03 11:04:35 -070059 @Completion(ControlResourceTypeCompleter.class)
Jian Li85060ac2016-02-04 09:58:56 -080060 String type = null;
61
Jian Li67e1e152016-04-18 17:52:58 -070062 @Argument(index = 2, name = "name", description = "Resource name (or Device Id)",
Jian Li85060ac2016-02-04 09:58:56 -080063 required = false, multiValued = false)
Ray Milkeya04e4442018-10-03 11:04:35 -070064 @Completion(ResourceNameCompleter.class)
Jian Li85060ac2016-02-04 09:58:56 -080065 String name = null;
66
67 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070068 protected void doExecute() {
Jian Li85060ac2016-02-04 09:58:56 -080069 ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class);
Jian Li67e1e152016-04-18 17:52:58 -070070 NodeId nodeId = NodeId.nodeId(node);
Jian Li85060ac2016-02-04 09:58:56 -080071 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 Li67e1e152016-04-18 17:52:58 -070086 printMetricsStats(service, nodeId, CONTROL_MESSAGE_METRICS,
87 DeviceId.deviceId(name));
Jian Li85060ac2016-02-04 09:58:56 -080088 }
89 break;
90 default:
91 print(INVALID_TYPE);
92 break;
93 }
94 }
95
Jian Lif248aa22016-05-09 10:33:02 -070096 /**
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 Li85060ac2016-02-04 09:58:56 -0800103 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
104 Set<ControlMetricType> typeSet) {
105 printMetricsStats(service, nodeId, typeSet, null, null);
106 }
107
Jian Lif248aa22016-05-09 10:33:02 -0700108 /**
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 Li85060ac2016-02-04 09:58:56 -0800116 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
Jian Li67e1e152016-04-18 17:52:58 -0700117 Set<ControlMetricType> typeSet, String resName) {
118 printMetricsStats(service, nodeId, typeSet, resName, null);
Jian Li85060ac2016-02-04 09:58:56 -0800119 }
120
Jian Lif248aa22016-05-09 10:33:02 -0700121 /**
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 Li85060ac2016-02-04 09:58:56 -0800129 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
130 Set<ControlMetricType> typeSet, DeviceId did) {
131 printMetricsStats(service, nodeId, typeSet, null, did);
132 }
133
Jian Lif248aa22016-05-09 10:33:02 -0700134 /**
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 Li85060ac2016-02-04 09:58:56 -0800143 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
Jian Li67e1e152016-04-18 17:52:58 -0700144 Set<ControlMetricType> typeSet, String resName, DeviceId did) {
145 if (resName == null && did == null) {
146 typeSet.forEach(s -> {
Jian Lif248aa22016-05-09 10:33:02 -0700147 ControlLoadSnapshot cls = service.getLoadSync(nodeId, s, Optional.empty());
148 printControlLoadSnapshot(s, cls);
Jian Li67e1e152016-04-18 17:52:58 -0700149 });
Jian Lif248aa22016-05-09 10:33:02 -0700150 } else if (resName == null) {
Jian Li67e1e152016-04-18 17:52:58 -0700151 typeSet.forEach(s -> {
Jian Lif248aa22016-05-09 10:33:02 -0700152 ControlLoadSnapshot cls = service.getLoadSync(nodeId, s, Optional.of(did));
153 printControlLoadSnapshot(s, cls);
Jian Li67e1e152016-04-18 17:52:58 -0700154 });
Jian Lif248aa22016-05-09 10:33:02 -0700155 } else if (did == null) {
Jian Li67e1e152016-04-18 17:52:58 -0700156 typeSet.forEach(s -> {
Jian Lif248aa22016-05-09 10:33:02 -0700157 ControlLoadSnapshot cls = service.getLoadSync(nodeId, s, resName);
158 printControlLoadSnapshot(s, cls);
Jian Li67e1e152016-04-18 17:52:58 -0700159 });
Jian Li85060ac2016-02-04 09:58:56 -0800160 }
161 }
162
Jian Lif248aa22016-05-09 10:33:02 -0700163 /**
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 Li67e1e152016-04-18 17:52:58 -0700170 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 Li85060ac2016-02-04 09:58:56 -0800174 }
175 }
176}