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 | package org.onosproject.nodemetrics.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.hyperic.sigar.CpuPerc; |
| 25 | import org.hyperic.sigar.FileSystemUsage; |
| 26 | import org.hyperic.sigar.Mem; |
| 27 | import org.hyperic.sigar.Sigar; |
| 28 | import org.hyperic.sigar.SigarException; |
| 29 | import org.onlab.util.KryoNamespace; |
| 30 | import org.onlab.util.Tools; |
| 31 | import org.onosproject.cluster.ClusterService; |
| 32 | import org.onosproject.cluster.NodeId; |
| 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreService; |
| 35 | import org.onosproject.nodemetrics.NodeCpu; |
| 36 | import org.onosproject.nodemetrics.NodeDiskUsage; |
| 37 | import org.onosproject.nodemetrics.NodeMemory; |
| 38 | import org.onosproject.nodemetrics.NodeMetricsService; |
| 39 | import org.onosproject.nodemetrics.Units; |
| 40 | import org.onosproject.store.serializers.KryoNamespaces; |
| 41 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 42 | import org.onosproject.store.service.LogicalClockService; |
| 43 | import org.onosproject.store.service.StorageService; |
| 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
| 47 | import java.util.Map; |
| 48 | import java.util.concurrent.Executors; |
| 49 | import java.util.concurrent.ScheduledExecutorService; |
| 50 | import java.util.concurrent.ScheduledFuture; |
| 51 | import java.util.concurrent.TimeUnit; |
| 52 | import java.util.stream.Collectors; |
| 53 | |
| 54 | @Service |
| 55 | @Component(immediate = true) |
| 56 | public class NodeMetricsManager implements NodeMetricsService { |
| 57 | private static final int DEFAULT_POLL_FREQUENCY_SECONDS = 15; |
| 58 | private static final String SLASH = "/"; |
| 59 | private static final Double PERCENTAGE_MULTIPLIER = 100.0; |
| 60 | private final Logger log = LoggerFactory |
| 61 | .getLogger(this.getClass()); |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected CoreService coreService; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected StorageService storageService; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected ClusterService clusterService; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected LogicalClockService clockService; |
| 74 | |
| 75 | private ScheduledExecutorService metricsExecutor; |
| 76 | private ScheduledFuture<?> scheduledTask; |
| 77 | |
| 78 | private ApplicationId appId; |
| 79 | private NodeId localNodeId; |
| 80 | |
| 81 | private EventuallyConsistentMap<NodeId, NodeMemory> memoryStore; |
| 82 | private EventuallyConsistentMap<NodeId, NodeDiskUsage> diskStore; |
| 83 | private EventuallyConsistentMap<NodeId, NodeCpu> cpuStore; |
| 84 | |
| 85 | private Sigar sigar; |
| 86 | |
| 87 | @Activate |
| 88 | public void activate() { |
| 89 | appId = coreService |
| 90 | .registerApplication("org.onosproject.nodemetrics"); |
| 91 | metricsExecutor = Executors.newSingleThreadScheduledExecutor( |
| 92 | Tools.groupedThreads("nodemetrics/pollingStatics", |
| 93 | "statistics-executor-%d", log)); |
| 94 | |
| 95 | localNodeId = clusterService.getLocalNode().id(); |
| 96 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 97 | .register(KryoNamespaces.API) |
| 98 | .register(NodeMemory.class) |
| 99 | .register(NodeDiskUsage.class) |
| 100 | .register(NodeCpu.class) |
| 101 | .register(Units.class); |
| 102 | memoryStore = storageService.<NodeId, NodeMemory>eventuallyConsistentMapBuilder() |
| 103 | .withSerializer(serializer) |
| 104 | .withTimestampProvider((nodeId, memory) -> clockService.getTimestamp()) |
| 105 | .withName("nodemetrics-memory") |
| 106 | .build(); |
| 107 | |
| 108 | diskStore = storageService.<NodeId, NodeDiskUsage>eventuallyConsistentMapBuilder() |
| 109 | .withSerializer(serializer) |
| 110 | .withTimestampProvider((nodeId, disk) -> clockService.getTimestamp()) |
| 111 | .withName("nodemetrics-disk") |
| 112 | .build(); |
| 113 | |
| 114 | cpuStore = storageService.<NodeId, NodeCpu>eventuallyConsistentMapBuilder() |
| 115 | .withSerializer(serializer) |
| 116 | .withTimestampProvider((nodeId, cpu) -> clockService.getTimestamp()) |
| 117 | .withName("nodemetrics-cpu") |
| 118 | .build(); |
| 119 | |
| 120 | scheduledTask = schedulePolling(); |
| 121 | sigar = new Sigar(); |
| 122 | pollMetrics(); |
| 123 | } |
| 124 | |
| 125 | @Deactivate |
| 126 | public void deactivate() { |
| 127 | scheduledTask.cancel(true); |
| 128 | metricsExecutor.shutdown(); |
| 129 | sigar.close(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Map<NodeId, NodeMemory> memory() { |
| 134 | return this.ecToMap(memoryStore); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Map<NodeId, NodeDiskUsage> disk() { |
| 139 | return this.ecToMap(diskStore); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Map<NodeId, NodeCpu> cpu() { |
| 144 | return this.ecToMap(cpuStore); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public NodeMemory memory(NodeId nodeid) { |
| 149 | return memoryStore.get(nodeid); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public NodeDiskUsage disk(NodeId nodeid) { |
| 154 | return diskStore.get(nodeid); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public NodeCpu cpu(NodeId nodeid) { |
| 159 | return cpuStore.get(nodeid); |
| 160 | } |
| 161 | |
| 162 | private ScheduledFuture schedulePolling() { |
| 163 | return metricsExecutor.scheduleAtFixedRate(this::pollMetrics, |
| 164 | DEFAULT_POLL_FREQUENCY_SECONDS / 4, |
| 165 | DEFAULT_POLL_FREQUENCY_SECONDS, TimeUnit.SECONDS); |
| 166 | } |
| 167 | |
| 168 | private <K, V> Map<K, V> ecToMap(EventuallyConsistentMap<K, V> ecMap) { |
| 169 | return ecMap.entrySet() |
| 170 | .stream() |
| 171 | .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 172 | } |
| 173 | private void pollMetrics() { |
| 174 | try { |
| 175 | CpuPerc cpu = sigar.getCpuPerc(); |
| 176 | Mem mem = sigar.getMem(); |
| 177 | FileSystemUsage disk = sigar.getFileSystemUsage(SLASH); |
| 178 | |
| 179 | NodeMemory memoryNode = new NodeMemory.Builder().free(mem.getFree()) |
| 180 | .used(mem.getUsed()).total(mem.getTotal()).withUnit(Units.BYTES) |
| 181 | .withNode(localNodeId).build(); |
| 182 | NodeCpu cpuNode = new NodeCpu.Builder().withNode(localNodeId) |
| 183 | .usage(cpu.getCombined() * PERCENTAGE_MULTIPLIER).build(); |
| 184 | NodeDiskUsage diskNode = new NodeDiskUsage.Builder().withNode(localNodeId) |
| 185 | .free(disk.getFree()).used(disk.getUsed()).withUnit(Units.KBYTES) |
| 186 | .total(disk.getTotal()).build(); |
| 187 | diskStore.put(localNodeId, diskNode); |
| 188 | memoryStore.put(localNodeId, memoryNode); |
| 189 | cpuStore.put(localNodeId, cpuNode); |
| 190 | |
| 191 | } catch (SigarException e) { |
| 192 | log.error("Exception occurred ", e); |
| 193 | } |
| 194 | |
| 195 | } |
| 196 | } |