blob: 3613d16a95c3dc719f782558ca649239d84e9ed3 [file] [log] [blame]
kdarapu97843dc2018-05-10 12:46:32 +05301/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.nodemetrics.cli;
18
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
kdarapu97843dc2018-05-10 12:46:32 +053021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.cluster.NodeId;
nitinanand920fcb42018-05-18 17:30:36 +053023import org.onosproject.nodemetrics.NodeCpuUsage;
kdarapu97843dc2018-05-10 12:46:32 +053024import org.onosproject.nodemetrics.NodeMetricsService;
25
26import java.util.Collection;
27import java.util.Objects;
28
29/**
30 * Lists cpu usage across nodes.
31 */
32@Command(scope = "onos", name = "node-cpu",
33 description = "Lists all node cpu utilization")
34public class ShowNodeCpuUsageCommand extends AbstractShellCommand {
35
36 @Argument(index = 0, name = "nodeId", description = "Node identity",
37 required = false, multiValued = false)
38 String nodeId = null;
39 private NodeMetricsService nodeService = AbstractShellCommand
40 .get(NodeMetricsService.class);
41
42 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070043 protected void doExecute() {
kdarapu97843dc2018-05-10 12:46:32 +053044 if (nodeId != null) {
nitinanand920fcb42018-05-18 17:30:36 +053045 NodeCpuUsage cpu = nodeService.cpu(NodeId.nodeId(nodeId));
kdarapu97843dc2018-05-10 12:46:32 +053046 if (Objects.nonNull(cpu)) {
47 print("CPU usage : %s ", cpu);
48 } else {
49 print("Node %s doesn't exists", nodeId);
50 }
51 } else {
nitinanand920fcb42018-05-18 17:30:36 +053052 Collection<NodeCpuUsage> cpu = nodeService.cpu().values();
kdarapu97843dc2018-05-10 12:46:32 +053053 printCpuUsage(cpu);
54 }
55 }
56
nitinanand920fcb42018-05-18 17:30:36 +053057 private void printCpuUsage(Collection<NodeCpuUsage> cpuList) {
kdarapu97843dc2018-05-10 12:46:32 +053058 cpuList.forEach(cpu -> print("%s", cpu));
59 }
60}
61