Thomas Vachuska | 7ef2671 | 2018-04-25 18:03:54 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-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 | package org.onosproject.rest.resources; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onosproject.cluster.ClusterMetadataService; |
| 21 | import org.onosproject.cluster.ClusterService; |
| 22 | import org.onosproject.cluster.ControllerNode; |
| 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.core.Version; |
| 25 | import org.onosproject.net.device.DeviceService; |
| 26 | import org.onosproject.net.flow.FlowRuleService; |
| 27 | import org.onosproject.net.host.HostService; |
| 28 | import org.onosproject.net.intent.IntentService; |
| 29 | import org.onosproject.net.link.LinkService; |
| 30 | import org.onosproject.net.topology.TopologyService; |
| 31 | import org.onosproject.rest.AbstractWebResource; |
| 32 | |
| 33 | import javax.ws.rs.GET; |
| 34 | import javax.ws.rs.Path; |
| 35 | import javax.ws.rs.Produces; |
| 36 | import javax.ws.rs.core.MediaType; |
| 37 | import javax.ws.rs.core.Response; |
| 38 | import java.lang.management.ManagementFactory; |
| 39 | import java.lang.management.MemoryUsage; |
| 40 | import java.lang.management.ThreadMXBean; |
| 41 | import java.util.Set; |
| 42 | |
| 43 | /** |
| 44 | * Provides high-level system information, version, basic sumaries, memory usage, etc. |
| 45 | */ |
| 46 | @Path("system") |
| 47 | public class SystemInfoWebResource extends AbstractWebResource { |
| 48 | |
| 49 | private long activeNodes(Set<ControllerNode> nodes) { |
| 50 | ClusterService clusterService = get(ClusterService.class); |
| 51 | return nodes.stream() |
| 52 | .map(node -> clusterService.getState(node.id())) |
| 53 | .filter(ControllerNode.State::isActive) |
| 54 | .count(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get high-level system information, version, basic sumaries, memory usage, etc. |
| 59 | * |
| 60 | * @return 200 OK with a system summary |
| 61 | */ |
| 62 | @GET |
| 63 | @Produces(MediaType.APPLICATION_JSON) |
| 64 | public Response getSystemInfo() { |
| 65 | IpAddress nodeIp = get(ClusterService.class).getLocalNode().ip(); |
| 66 | Version version = get(CoreService.class).version(); |
| 67 | long numNodes = activeNodes(get(ClusterService.class).getNodes()); |
| 68 | int numDevices = get(DeviceService.class).getDeviceCount(); |
| 69 | int numLinks = get(LinkService.class).getLinkCount(); |
| 70 | int numHosts = get(HostService.class).getHostCount(); |
| 71 | int numScc = get(TopologyService.class).currentTopology().clusterCount(); |
| 72 | int numFlows = get(FlowRuleService.class).getFlowRuleCount(); |
| 73 | long numIntents = get(IntentService.class).getIntentCount(); |
| 74 | String clusterId = get(ClusterMetadataService.class).getClusterMetadata().getName(); |
| 75 | |
| 76 | ObjectNode root = mapper().createObjectNode() |
| 77 | .put("node", nodeIp.toString()) |
| 78 | .put("version", version.toString()) |
| 79 | .put("clusterId", clusterId) |
| 80 | .put("nodes", numNodes) |
| 81 | .put("devices", numDevices) |
| 82 | .put("links", numLinks) |
| 83 | .put("hosts", numHosts) |
| 84 | .put("sccs", numScc) |
| 85 | .put("flows", numFlows) |
| 86 | .put("intents", numIntents); |
| 87 | |
| 88 | MemoryUsage mem = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); |
| 89 | newObject(root, "mem") |
| 90 | .put("current", mem.getUsed()) |
| 91 | .put("max", mem.getMax()) |
| 92 | .put("committed", mem.getCommitted()); |
| 93 | |
| 94 | ThreadMXBean threads = ManagementFactory.getThreadMXBean(); |
| 95 | newObject(root, "threads") |
| 96 | .put("live", threads.getThreadCount()) |
| 97 | .put("daemon", threads.getDaemonThreadCount()) |
| 98 | .put("peak", threads.getPeakThreadCount()); |
| 99 | |
| 100 | return ok(root).build(); |
| 101 | } |
| 102 | |
| 103 | } |