blob: 30d1a187103e395d67b67c40db2fe5328cffa6aa [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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
Jian Li85060ac2016-02-04 09:58:56 -080021import org.onosproject.cluster.NodeId;
Jian Li67e1e152016-04-18 17:52:58 -070022import org.onosproject.cpman.ControlLoadSnapshot;
Jian Li85060ac2016-02-04 09:58:56 -080023import org.onosproject.cpman.ControlMetricType;
24import org.onosproject.cpman.ControlPlaneMonitorService;
25import org.onosproject.net.DeviceId;
26
27import java.util.Optional;
28import java.util.Set;
29
30import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
31import static org.onosproject.cpman.ControlResource.CPU_METRICS;
32import static org.onosproject.cpman.ControlResource.DISK_METRICS;
33import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
34import 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")
41public 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 Li67e1e152016-04-18 17:52:58 -070047 @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 Li85060ac2016-02-04 09:58:56 -080052 description = "Resource type (cpu|memory|disk|network|control_message)",
53 required = true, multiValued = false)
54 String type = null;
55
Jian Li67e1e152016-04-18 17:52:58 -070056 @Argument(index = 2, name = "name", description = "Resource name (or Device Id)",
Jian Li85060ac2016-02-04 09:58:56 -080057 required = false, multiValued = false)
58 String name = null;
59
60 @Override
61 protected void execute() {
62 ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class);
Jian Li67e1e152016-04-18 17:52:58 -070063 NodeId nodeId = NodeId.nodeId(node);
Jian Li85060ac2016-02-04 09:58:56 -080064 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 Li67e1e152016-04-18 17:52:58 -070079 printMetricsStats(service, nodeId, CONTROL_MESSAGE_METRICS,
80 DeviceId.deviceId(name));
Jian Li85060ac2016-02-04 09:58:56 -080081 }
82 break;
83 default:
84 print(INVALID_TYPE);
85 break;
86 }
87 }
88
Jian Lif248aa22016-05-09 10:33:02 -070089 /**
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 Li85060ac2016-02-04 09:58:56 -080096 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
97 Set<ControlMetricType> typeSet) {
98 printMetricsStats(service, nodeId, typeSet, null, null);
99 }
100
Jian Lif248aa22016-05-09 10:33:02 -0700101 /**
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 Li85060ac2016-02-04 09:58:56 -0800109 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
Jian Li67e1e152016-04-18 17:52:58 -0700110 Set<ControlMetricType> typeSet, String resName) {
111 printMetricsStats(service, nodeId, typeSet, resName, null);
Jian Li85060ac2016-02-04 09:58:56 -0800112 }
113
Jian Lif248aa22016-05-09 10:33:02 -0700114 /**
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 Li85060ac2016-02-04 09:58:56 -0800122 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
123 Set<ControlMetricType> typeSet, DeviceId did) {
124 printMetricsStats(service, nodeId, typeSet, null, did);
125 }
126
Jian Lif248aa22016-05-09 10:33:02 -0700127 /**
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 Li85060ac2016-02-04 09:58:56 -0800136 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
Jian Li67e1e152016-04-18 17:52:58 -0700137 Set<ControlMetricType> typeSet, String resName, DeviceId did) {
138 if (resName == null && did == null) {
139 typeSet.forEach(s -> {
Jian Lif248aa22016-05-09 10:33:02 -0700140 ControlLoadSnapshot cls = service.getLoadSync(nodeId, s, Optional.empty());
141 printControlLoadSnapshot(s, cls);
Jian Li67e1e152016-04-18 17:52:58 -0700142 });
Jian Lif248aa22016-05-09 10:33:02 -0700143 } else if (resName == null) {
Jian Li67e1e152016-04-18 17:52:58 -0700144 typeSet.forEach(s -> {
Jian Lif248aa22016-05-09 10:33:02 -0700145 ControlLoadSnapshot cls = service.getLoadSync(nodeId, s, Optional.of(did));
146 printControlLoadSnapshot(s, cls);
Jian Li67e1e152016-04-18 17:52:58 -0700147 });
Jian Lif248aa22016-05-09 10:33:02 -0700148 } else if (did == null) {
Jian Li67e1e152016-04-18 17:52:58 -0700149 typeSet.forEach(s -> {
Jian Lif248aa22016-05-09 10:33:02 -0700150 ControlLoadSnapshot cls = service.getLoadSync(nodeId, s, resName);
151 printControlLoadSnapshot(s, cls);
Jian Li67e1e152016-04-18 17:52:58 -0700152 });
Jian Li85060ac2016-02-04 09:58:56 -0800153 }
154 }
155
Jian Lif248aa22016-05-09 10:33:02 -0700156 /**
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 Li67e1e152016-04-18 17:52:58 -0700163 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 Li85060ac2016-02-04 09:58:56 -0800167 }
168 }
169}