blob: 6ba7fb0f632b6a373f8969ad5715ac106788e833 [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 Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Li85060ac2016-02-04 09:58:56 -080021import org.onosproject.cli.AbstractShellCommand;
Jian Li85060ac2016-02-04 09:58:56 -080022import org.onosproject.cluster.NodeId;
Jian Li67e1e152016-04-18 17:52:58 -070023import org.onosproject.cpman.ControlLoadSnapshot;
Jian Li85060ac2016-02-04 09:58:56 -080024import org.onosproject.cpman.ControlMetricType;
25import org.onosproject.cpman.ControlPlaneMonitorService;
26import org.onosproject.net.DeviceId;
27
28import java.util.Optional;
29import java.util.Set;
30
31import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
32import static org.onosproject.cpman.ControlResource.CPU_METRICS;
33import static org.onosproject.cpman.ControlResource.DISK_METRICS;
34import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
35import static org.onosproject.cpman.ControlResource.NETWORK_METRICS;
36
37/**
38 * Lists all stats information of control plane metrics.
39 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070040@Service
Jian Li85060ac2016-02-04 09:58:56 -080041@Command(scope = "onos", name = "cpman-stats-list",
42 description = "Lists control metrics statistics")
43public 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 Li67e1e152016-04-18 17:52:58 -070049 @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 Li85060ac2016-02-04 09:58:56 -080054 description = "Resource type (cpu|memory|disk|network|control_message)",
55 required = true, multiValued = false)
56 String type = null;
57
Jian Li67e1e152016-04-18 17:52:58 -070058 @Argument(index = 2, name = "name", description = "Resource name (or Device Id)",
Jian Li85060ac2016-02-04 09:58:56 -080059 required = false, multiValued = false)
60 String name = null;
61
62 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070063 protected void doExecute() {
Jian Li85060ac2016-02-04 09:58:56 -080064 ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class);
Jian Li67e1e152016-04-18 17:52:58 -070065 NodeId nodeId = NodeId.nodeId(node);
Jian Li85060ac2016-02-04 09:58:56 -080066 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 Li67e1e152016-04-18 17:52:58 -070081 printMetricsStats(service, nodeId, CONTROL_MESSAGE_METRICS,
82 DeviceId.deviceId(name));
Jian Li85060ac2016-02-04 09:58:56 -080083 }
84 break;
85 default:
86 print(INVALID_TYPE);
87 break;
88 }
89 }
90
Jian Lif248aa22016-05-09 10:33:02 -070091 /**
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 Li85060ac2016-02-04 09:58:56 -080098 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
99 Set<ControlMetricType> typeSet) {
100 printMetricsStats(service, nodeId, typeSet, null, null);
101 }
102
Jian Lif248aa22016-05-09 10:33:02 -0700103 /**
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 Li85060ac2016-02-04 09:58:56 -0800111 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
Jian Li67e1e152016-04-18 17:52:58 -0700112 Set<ControlMetricType> typeSet, String resName) {
113 printMetricsStats(service, nodeId, typeSet, resName, null);
Jian Li85060ac2016-02-04 09:58:56 -0800114 }
115
Jian Lif248aa22016-05-09 10:33:02 -0700116 /**
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 Li85060ac2016-02-04 09:58:56 -0800124 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
125 Set<ControlMetricType> typeSet, DeviceId did) {
126 printMetricsStats(service, nodeId, typeSet, null, did);
127 }
128
Jian Lif248aa22016-05-09 10:33:02 -0700129 /**
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 Li85060ac2016-02-04 09:58:56 -0800138 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
Jian Li67e1e152016-04-18 17:52:58 -0700139 Set<ControlMetricType> typeSet, String resName, DeviceId did) {
140 if (resName == null && did == null) {
141 typeSet.forEach(s -> {
Jian Lif248aa22016-05-09 10:33:02 -0700142 ControlLoadSnapshot cls = service.getLoadSync(nodeId, s, Optional.empty());
143 printControlLoadSnapshot(s, cls);
Jian Li67e1e152016-04-18 17:52:58 -0700144 });
Jian Lif248aa22016-05-09 10:33:02 -0700145 } else if (resName == null) {
Jian Li67e1e152016-04-18 17:52:58 -0700146 typeSet.forEach(s -> {
Jian Lif248aa22016-05-09 10:33:02 -0700147 ControlLoadSnapshot cls = service.getLoadSync(nodeId, s, Optional.of(did));
148 printControlLoadSnapshot(s, cls);
Jian Li67e1e152016-04-18 17:52:58 -0700149 });
Jian Lif248aa22016-05-09 10:33:02 -0700150 } else if (did == 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, resName);
153 printControlLoadSnapshot(s, cls);
Jian Li67e1e152016-04-18 17:52:58 -0700154 });
Jian Li85060ac2016-02-04 09:58:56 -0800155 }
156 }
157
Jian Lif248aa22016-05-09 10:33:02 -0700158 /**
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 Li67e1e152016-04-18 17:52:58 -0700165 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 Li85060ac2016-02-04 09:58:56 -0800169 }
170 }
171}