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