kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.nodemetrics.cli; |
| 18 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.cluster.NodeId; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 23 | import org.onosproject.nodemetrics.NodeCpuUsage; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 24 | import org.onosproject.nodemetrics.NodeMetricsService; |
| 25 | |
| 26 | import java.util.Collection; |
| 27 | import 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") |
| 34 | public 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 Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 43 | protected void doExecute() { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 44 | if (nodeId != null) { |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 45 | NodeCpuUsage cpu = nodeService.cpu(NodeId.nodeId(nodeId)); |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 46 | if (Objects.nonNull(cpu)) { |
| 47 | print("CPU usage : %s ", cpu); |
| 48 | } else { |
| 49 | print("Node %s doesn't exists", nodeId); |
| 50 | } |
| 51 | } else { |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 52 | Collection<NodeCpuUsage> cpu = nodeService.cpu().values(); |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 53 | printCpuUsage(cpu); |
| 54 | } |
| 55 | } |
| 56 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 57 | private void printCpuUsage(Collection<NodeCpuUsage> cpuList) { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 58 | cpuList.forEach(cpu -> print("%s", cpu)); |
| 59 | } |
| 60 | } |
| 61 | |