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 | |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 18 | import org.hyperic.sigar.CpuPerc; |
| 19 | import org.hyperic.sigar.FileSystemUsage; |
| 20 | import org.hyperic.sigar.Mem; |
| 21 | import org.hyperic.sigar.Sigar; |
| 22 | import org.hyperic.sigar.SigarException; |
| 23 | import org.onlab.util.KryoNamespace; |
| 24 | import org.onlab.util.Tools; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 25 | import org.onosproject.cfg.ComponentConfigService; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.cluster.NodeId; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 30 | import org.onosproject.nodemetrics.NodeCpuUsage; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 31 | import org.onosproject.nodemetrics.NodeDiskUsage; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 32 | import org.onosproject.nodemetrics.NodeMemoryUsage; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 33 | import org.onosproject.nodemetrics.NodeMetricsService; |
| 34 | import org.onosproject.nodemetrics.Units; |
| 35 | import org.onosproject.store.serializers.KryoNamespaces; |
| 36 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 37 | import org.onosproject.store.service.LogicalClockService; |
| 38 | import org.onosproject.store.service.StorageService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 39 | import org.osgi.service.component.ComponentContext; |
| 40 | import org.osgi.service.component.annotations.Activate; |
| 41 | import org.osgi.service.component.annotations.Component; |
| 42 | import org.osgi.service.component.annotations.Deactivate; |
| 43 | import org.osgi.service.component.annotations.Modified; |
| 44 | import org.osgi.service.component.annotations.Reference; |
| 45 | import org.osgi.service.component.annotations.ReferenceCardinality; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 49 | import java.util.Dictionary; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 50 | import java.util.Map; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 51 | import java.util.Objects; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 52 | import java.util.concurrent.Executors; |
| 53 | import java.util.concurrent.ScheduledExecutorService; |
| 54 | import java.util.concurrent.ScheduledFuture; |
| 55 | import java.util.concurrent.TimeUnit; |
| 56 | import java.util.stream.Collectors; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 57 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 58 | import static org.onlab.util.Tools.getIntegerProperty; |
| 59 | |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 60 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 61 | @Component(immediate = true, service = NodeMetricsService.class) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 62 | public class NodeMetricsManager implements NodeMetricsService { |
| 63 | private static final int DEFAULT_POLL_FREQUENCY_SECONDS = 15; |
| 64 | private static final String SLASH = "/"; |
| 65 | private static final Double PERCENTAGE_MULTIPLIER = 100.0; |
| 66 | private final Logger log = LoggerFactory |
| 67 | .getLogger(this.getClass()); |
| 68 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 70 | protected CoreService coreService; |
| 71 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 73 | protected StorageService storageService; |
| 74 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 76 | protected ClusterService clusterService; |
| 77 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 79 | protected LogicalClockService clockService; |
| 80 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 82 | protected ComponentConfigService cfgService; |
| 83 | |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 84 | private ScheduledExecutorService metricsExecutor; |
| 85 | private ScheduledFuture<?> scheduledTask; |
| 86 | |
| 87 | private ApplicationId appId; |
| 88 | private NodeId localNodeId; |
| 89 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 90 | private EventuallyConsistentMap<NodeId, NodeMemoryUsage> memoryStore; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 91 | private EventuallyConsistentMap<NodeId, NodeDiskUsage> diskStore; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 92 | private EventuallyConsistentMap<NodeId, NodeCpuUsage> cpuStore; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 93 | |
| 94 | private Sigar sigar; |
| 95 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 96 | //@Property(name = "metricPollFrequencySeconds", intValue = DEFAULT_POLL_FREQUENCY_SECONDS, |
| 97 | // label = "Frequency (in seconds) for polling controller metrics") |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 98 | protected int metricPollFrequencySeconds = DEFAULT_POLL_FREQUENCY_SECONDS; |
| 99 | |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 100 | @Activate |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 101 | public void activate(ComponentContext context) { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 102 | appId = coreService |
| 103 | .registerApplication("org.onosproject.nodemetrics"); |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 104 | cfgService.registerProperties(getClass()); |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 105 | metricsExecutor = Executors.newSingleThreadScheduledExecutor( |
| 106 | Tools.groupedThreads("nodemetrics/pollingStatics", |
| 107 | "statistics-executor-%d", log)); |
| 108 | |
| 109 | localNodeId = clusterService.getLocalNode().id(); |
| 110 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 111 | .register(KryoNamespaces.API) |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 112 | .register(NodeMemoryUsage.class) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 113 | .register(NodeDiskUsage.class) |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 114 | .register(NodeCpuUsage.class) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 115 | .register(Units.class); |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 116 | memoryStore = storageService.<NodeId, NodeMemoryUsage>eventuallyConsistentMapBuilder() |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 117 | .withSerializer(serializer) |
| 118 | .withTimestampProvider((nodeId, memory) -> clockService.getTimestamp()) |
| 119 | .withName("nodemetrics-memory") |
| 120 | .build(); |
| 121 | |
| 122 | diskStore = storageService.<NodeId, NodeDiskUsage>eventuallyConsistentMapBuilder() |
| 123 | .withSerializer(serializer) |
| 124 | .withTimestampProvider((nodeId, disk) -> clockService.getTimestamp()) |
| 125 | .withName("nodemetrics-disk") |
| 126 | .build(); |
| 127 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 128 | cpuStore = storageService.<NodeId, NodeCpuUsage>eventuallyConsistentMapBuilder() |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 129 | .withSerializer(serializer) |
| 130 | .withTimestampProvider((nodeId, cpu) -> clockService.getTimestamp()) |
| 131 | .withName("nodemetrics-cpu") |
| 132 | .build(); |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 133 | modified(context); |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 134 | sigar = new Sigar(); |
| 135 | pollMetrics(); |
| 136 | } |
| 137 | |
| 138 | @Deactivate |
| 139 | public void deactivate() { |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 140 | cfgService.unregisterProperties(getClass(), false); |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 141 | scheduledTask.cancel(true); |
| 142 | metricsExecutor.shutdown(); |
| 143 | sigar.close(); |
| 144 | } |
| 145 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 146 | @Modified |
| 147 | public void modified(ComponentContext context) { |
| 148 | if (context == null) { |
| 149 | log.info("No component configuration"); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | Dictionary<?, ?> properties = context.getProperties(); |
| 154 | int newPollFrequency = getNewPollFrequency(properties); |
| 155 | //First time call to this modified method is when app activates |
| 156 | if (Objects.isNull(scheduledTask)) { |
| 157 | metricPollFrequencySeconds = newPollFrequency; |
| 158 | scheduledTask = schedulePolling(); |
| 159 | } else { |
| 160 | if (newPollFrequency != metricPollFrequencySeconds) { |
| 161 | metricPollFrequencySeconds = newPollFrequency; |
| 162 | //stops the old scheduled task |
| 163 | scheduledTask.cancel(true); |
| 164 | //schedules new task at the new polling rate |
| 165 | scheduledTask = schedulePolling(); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 170 | @Override |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 171 | public Map<NodeId, NodeMemoryUsage> memory() { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 172 | return this.ecToMap(memoryStore); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public Map<NodeId, NodeDiskUsage> disk() { |
| 177 | return this.ecToMap(diskStore); |
| 178 | } |
| 179 | |
| 180 | @Override |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 181 | public Map<NodeId, NodeCpuUsage> cpu() { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 182 | return this.ecToMap(cpuStore); |
| 183 | } |
| 184 | |
| 185 | @Override |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 186 | public NodeMemoryUsage memory(NodeId nodeid) { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 187 | return memoryStore.get(nodeid); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public NodeDiskUsage disk(NodeId nodeid) { |
| 192 | return diskStore.get(nodeid); |
| 193 | } |
| 194 | |
| 195 | @Override |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 196 | public NodeCpuUsage cpu(NodeId nodeid) { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 197 | return cpuStore.get(nodeid); |
| 198 | } |
| 199 | |
| 200 | private ScheduledFuture schedulePolling() { |
| 201 | return metricsExecutor.scheduleAtFixedRate(this::pollMetrics, |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 202 | metricPollFrequencySeconds / 4, |
| 203 | metricPollFrequencySeconds, TimeUnit.SECONDS); |
| 204 | } |
| 205 | |
| 206 | private int getNewPollFrequency(Dictionary<?, ?> properties) { |
| 207 | int newPollFrequency; |
| 208 | try { |
| 209 | newPollFrequency = getIntegerProperty(properties, "metricPollFrequencySeconds"); |
| 210 | //String s = getIntegerProperty(properties, "metricPollFrequencySeconds"); |
| 211 | //newPollFrequency = isNullOrEmpty(s) ? pollFrequency : Integer.parseInt(s.trim()); |
| 212 | } catch (NumberFormatException | ClassCastException e) { |
| 213 | newPollFrequency = DEFAULT_POLL_FREQUENCY_SECONDS; |
| 214 | } |
| 215 | return newPollFrequency; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | private <K, V> Map<K, V> ecToMap(EventuallyConsistentMap<K, V> ecMap) { |
| 219 | return ecMap.entrySet() |
| 220 | .stream() |
| 221 | .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 222 | } |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 223 | |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 224 | private void pollMetrics() { |
| 225 | try { |
| 226 | CpuPerc cpu = sigar.getCpuPerc(); |
| 227 | Mem mem = sigar.getMem(); |
| 228 | FileSystemUsage disk = sigar.getFileSystemUsage(SLASH); |
| 229 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 230 | NodeMemoryUsage memoryNode = new NodeMemoryUsage.Builder().free(mem.getFree()) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 231 | .used(mem.getUsed()).total(mem.getTotal()).withUnit(Units.BYTES) |
| 232 | .withNode(localNodeId).build(); |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 233 | NodeCpuUsage cpuNode = new NodeCpuUsage.Builder().withNode(localNodeId) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 234 | .usage(cpu.getCombined() * PERCENTAGE_MULTIPLIER).build(); |
| 235 | NodeDiskUsage diskNode = new NodeDiskUsage.Builder().withNode(localNodeId) |
| 236 | .free(disk.getFree()).used(disk.getUsed()).withUnit(Units.KBYTES) |
| 237 | .total(disk.getTotal()).build(); |
| 238 | diskStore.put(localNodeId, diskNode); |
| 239 | memoryStore.put(localNodeId, memoryNode); |
| 240 | cpuStore.put(localNodeId, cpuNode); |
| 241 | |
| 242 | } catch (SigarException e) { |
| 243 | log.error("Exception occurred ", e); |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | } |