Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 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.gangliametrics; |
| 17 | |
| 18 | import com.codahale.metrics.MetricRegistry; |
| 19 | import com.codahale.metrics.ganglia.GangliaReporter; |
| 20 | import info.ganglia.gmetric4j.gmetric.GMetric; |
| 21 | import org.apache.commons.lang.StringUtils; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 22 | import org.onlab.metrics.MetricsService; |
| 23 | import org.onlab.util.Tools; |
| 24 | import org.onosproject.cfg.ComponentConfigService; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 27 | import org.osgi.service.component.annotations.Activate; |
| 28 | import org.osgi.service.component.annotations.Component; |
| 29 | import org.osgi.service.component.annotations.Deactivate; |
| 30 | import org.osgi.service.component.annotations.Modified; |
| 31 | import org.osgi.service.component.annotations.Reference; |
| 32 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.util.Dictionary; |
| 37 | import java.util.concurrent.TimeUnit; |
| 38 | |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 39 | import static org.onosproject.gangliametrics.OsgiPropertyConstants.ADDRESS; |
| 40 | import static org.onosproject.gangliametrics.OsgiPropertyConstants.ADDRESS_DEFAULT; |
| 41 | import static org.onosproject.gangliametrics.OsgiPropertyConstants.METRIC_NAMES; |
| 42 | import static org.onosproject.gangliametrics.OsgiPropertyConstants.METRIC_NAMES_DEFAULT; |
| 43 | import static org.onosproject.gangliametrics.OsgiPropertyConstants.MONITOR_ALL; |
| 44 | import static org.onosproject.gangliametrics.OsgiPropertyConstants.MONITOR_ALL_DEFAULT; |
| 45 | import static org.onosproject.gangliametrics.OsgiPropertyConstants.PORT; |
| 46 | import static org.onosproject.gangliametrics.OsgiPropertyConstants.PORT_DEFAULT; |
| 47 | import static org.onosproject.gangliametrics.OsgiPropertyConstants.TTL; |
| 48 | import static org.onosproject.gangliametrics.OsgiPropertyConstants.TTL_DEFAULT; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 49 | import static org.slf4j.LoggerFactory.getLogger; |
| 50 | |
| 51 | /** |
| 52 | * A metric report that reports all metrics value to ganglia monitoring server. |
| 53 | */ |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 54 | @Component( |
| 55 | immediate = true, |
| 56 | property = { |
| 57 | MONITOR_ALL + ":Boolean=" + MONITOR_ALL_DEFAULT, |
| 58 | METRIC_NAMES + "=" + METRIC_NAMES_DEFAULT, |
| 59 | ADDRESS + "=" + ADDRESS_DEFAULT, |
| 60 | PORT + ":Integer=" + PORT_DEFAULT, |
| 61 | TTL + ":Integer=" + TTL_DEFAULT |
| 62 | } |
| 63 | ) |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 64 | public class DefaultGangliaMetricsReporter implements GangliaMetricsReporter { |
| 65 | private final Logger log = getLogger(getClass()); |
| 66 | |
| 67 | // we will use uni-cast mode to transfer the metrics value by default |
| 68 | private static final GMetric.UDPAddressingMode GANGLIA_MODE = |
| 69 | GMetric.UDPAddressingMode.UNICAST; |
| 70 | private static final int REPORT_PERIOD = 1; |
| 71 | private static final TimeUnit REPORT_TIME_UNIT = TimeUnit.MINUTES; |
| 72 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 74 | protected CoreService coreService; |
| 75 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 77 | protected MetricsService metricsService; |
| 78 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 80 | protected ComponentConfigService cfgService; |
| 81 | |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 82 | /** Enable to monitor all of metrics stored in metric registry default is true. */ |
| 83 | protected boolean monitorAll = MONITOR_ALL_DEFAULT; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 84 | |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 85 | /** Names of metric to be monitored; default metric names are 'default'. */ |
| 86 | protected String metricNames = METRIC_NAMES_DEFAULT; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 87 | |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 88 | /** IP address of ganglia monitoring server; default is localhost. */ |
| 89 | protected String address = ADDRESS_DEFAULT; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 90 | |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 91 | /** Port number of ganglia monitoring server; default is 8649. */ |
| 92 | protected int port = PORT_DEFAULT; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 93 | |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 94 | /** TTL value of ganglia monitoring server; default is 1. */ |
| 95 | protected int ttl = TTL_DEFAULT; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 96 | |
| 97 | private GMetric ganglia; |
| 98 | private GangliaReporter gangliaReporter; |
| 99 | |
| 100 | @Activate |
| 101 | public void activate() { |
| 102 | cfgService.registerProperties(getClass()); |
Jian Li | 279130d | 2016-04-06 16:29:44 -0700 | [diff] [blame] | 103 | coreService.registerApplication("org.onosproject.gangliametrics"); |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 104 | metricsService.registerReporter(this); |
| 105 | |
| 106 | startReport(); |
| 107 | log.info("Started"); |
| 108 | } |
| 109 | |
| 110 | @Deactivate |
| 111 | public void deactivate() { |
| 112 | cfgService.unregisterProperties(getClass(), false); |
| 113 | |
| 114 | stopReport(); |
| 115 | metricsService.unregisterReporter(this); |
| 116 | |
| 117 | log.info("Stopped"); |
| 118 | } |
| 119 | |
| 120 | @Modified |
| 121 | public void modified(ComponentContext context) { |
| 122 | readComponentConfiguration(context); |
| 123 | stopReport(); |
| 124 | startReport(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void startReport() { |
| 129 | configGMetric(); |
| 130 | gangliaReporter = buildReporter(ganglia); |
Jian Li | 279130d | 2016-04-06 16:29:44 -0700 | [diff] [blame] | 131 | |
| 132 | try { |
| 133 | gangliaReporter.start(REPORT_PERIOD, REPORT_TIME_UNIT); |
| 134 | } catch (Exception e) { |
| 135 | log.error("Errors during reporting to ganglia, msg: {}" + e.getMessage()); |
| 136 | } |
| 137 | |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 138 | log.info("Start to report metrics to ganglia server."); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void stopReport() { |
| 143 | gangliaReporter.stop(); |
| 144 | ganglia = null; |
| 145 | gangliaReporter = null; |
| 146 | log.info("Stop reporting metrics to ganglia server."); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void restartReport() { |
| 151 | stopReport(); |
| 152 | startReport(); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void notifyMetricsChange() { |
| 157 | gangliaReporter.stop(); |
| 158 | gangliaReporter = buildReporter(ganglia); |
Jian Li | 279130d | 2016-04-06 16:29:44 -0700 | [diff] [blame] | 159 | |
| 160 | try { |
| 161 | gangliaReporter.start(REPORT_PERIOD, REPORT_TIME_UNIT); |
| 162 | } catch (Exception e) { |
| 163 | log.error("Errors during reporting to ganglia, msg: {}" + e.getMessage()); |
| 164 | } |
| 165 | |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 166 | log.info("Metric registry has been changed, apply changes."); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Filters the metrics to only include a set of the given metrics. |
| 171 | * |
| 172 | * @param metricRegistry original metric registry |
| 173 | * @return filtered metric registry |
| 174 | */ |
| 175 | protected MetricRegistry filter(MetricRegistry metricRegistry) { |
| 176 | if (!monitorAll) { |
| 177 | final MetricRegistry filtered = new MetricRegistry(); |
| 178 | metricRegistry.getNames().stream().filter(name -> |
| 179 | containsName(name, metricNames)).forEach(name -> |
| 180 | filtered.register(name, metricRegistry.getMetrics().get(name))); |
| 181 | return filtered; |
| 182 | } else { |
| 183 | return metricRegistry; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Looks up whether the metric name contains the given prefix keywords. |
| 189 | * Note that the keywords are separated with comma as delimiter |
| 190 | * |
| 191 | * @param full the original metric name that to be compared with |
| 192 | * @param prefixes the prefix keywords that are matched against with the metric name |
| 193 | * @return boolean value that denotes whether the metric name starts with the given prefix |
| 194 | */ |
| 195 | protected boolean containsName(String full, String prefixes) { |
| 196 | String[] prefixArray = StringUtils.split(prefixes, ","); |
| 197 | for (String prefix : prefixArray) { |
| 198 | if (StringUtils.startsWith(full, StringUtils.trimToEmpty(prefix))) { |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Extracts properties from the component configuration context. |
| 207 | * |
| 208 | * @param context the component context |
| 209 | */ |
| 210 | private void readComponentConfiguration(ComponentContext context) { |
| 211 | Dictionary<?, ?> properties = context.getProperties(); |
| 212 | |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 213 | String addressStr = Tools.get(properties, ADDRESS); |
| 214 | address = addressStr != null ? addressStr : ADDRESS_DEFAULT; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 215 | log.info("Configured. Ganglia server address is {}", address); |
| 216 | |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 217 | String metricNameStr = Tools.get(properties, METRIC_NAMES); |
| 218 | metricNames = metricNameStr != null ? metricNameStr : METRIC_NAMES_DEFAULT; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 219 | log.info("Configured. Metric name is {}", metricNames); |
| 220 | |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 221 | Integer portConfigured = Tools.getIntegerProperty(properties, PORT); |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 222 | if (portConfigured == null) { |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 223 | port = PORT_DEFAULT; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 224 | log.info("Ganglia port is not configured, default value is {}", port); |
| 225 | } else { |
| 226 | port = portConfigured; |
| 227 | log.info("Configured. Ganglia port is configured to {}", port); |
| 228 | } |
| 229 | |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 230 | Integer ttlConfigured = Tools.getIntegerProperty(properties, TTL); |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 231 | if (ttlConfigured == null) { |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 232 | ttl = TTL_DEFAULT; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 233 | log.info("Ganglia TTL is not configured, default value is {}", ttl); |
| 234 | } else { |
| 235 | ttl = ttlConfigured; |
| 236 | log.info("Configured. Ganglia TTL is configured to {}", ttl); |
| 237 | } |
| 238 | |
Ray Milkey | 2110e4c | 2018-10-30 15:49:10 -0700 | [diff] [blame] | 239 | Boolean monitorAllEnabled = Tools.isPropertyEnabled(properties, MONITOR_ALL); |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 240 | if (monitorAllEnabled == null) { |
| 241 | log.info("Monitor all metrics is not configured, " + |
| 242 | "using current value of {}", monitorAll); |
| 243 | } else { |
| 244 | monitorAll = monitorAllEnabled; |
| 245 | log.info("Configured. Monitor all metrics is {}", |
| 246 | monitorAll ? "enabled" : "disabled"); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Configures parameters for GMetric. |
| 252 | */ |
| 253 | private void configGMetric() { |
| 254 | try { |
| 255 | ganglia = new GMetric(address, port, GANGLIA_MODE, ttl); |
| 256 | } catch (IOException e) { |
| 257 | log.error("Fail to connect to given ganglia server!"); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Builds reporter with the given ganglia metric. |
| 263 | * |
| 264 | * @param gMetric ganglia metric |
| 265 | * @return reporter |
| 266 | */ |
| 267 | private GangliaReporter buildReporter(GMetric gMetric) { |
| 268 | MetricRegistry mr = metricsService.getMetricRegistry(); |
| 269 | |
| 270 | return GangliaReporter.forRegistry(filter(mr)) |
| 271 | .convertRatesTo(TimeUnit.SECONDS) |
| 272 | .convertDurationsTo(TimeUnit.MILLISECONDS) |
| 273 | .build(gMetric); |
| 274 | } |
| 275 | } |