├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── pom.xml
└── src
├── deb
└── control
│ └── control
└── main
├── java
└── org
│ └── graylog
│ └── outputs
│ └── metrics
│ ├── MetricsOutput.java
│ ├── MetricsOutputMetaData.java
│ ├── MetricsOutputModule.java
│ └── MetricsOutputPlugin.java
└── resources
└── META-INF
└── services
└── org.graylog2.plugin.Plugin
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | *.iml
3 | *.ipr
4 | *.iws
5 | .classpath
6 | .project
7 | .settings/
8 | target/
9 | dependency-reduced-pom.xml
10 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | language: java
3 | jdk:
4 | - oraclejdk8
5 | addons:
6 | apt:
7 | packages:
8 | - rpm
9 | before_deploy:
10 | - mvn jdeb:jdeb && export RELEASE_DEB_FILE=$(ls target/*.deb)
11 | - mvn rpm:rpm && export RELEASE_RPM_FILE=$(find target/ -name '*.rpm' | tail -1)
12 | - rm -f target/original-*.jar
13 | - export RELEASE_PKG_FILE=$(ls target/*.jar)
14 | - echo "Deploying release to GitHub releases"
15 | deploy:
16 | provider: releases
17 | api_key:
18 | secure: Nht4FgXnQqVOFqFcoRsb6TBPe0q7oThTIgWe3TomLwd1vXC/Fn3DuFo1wtrc9VPtIt/azs7AQvu+Tj1C9wU3EWh4R7I1rWwzy0GfEA6orRxg2IG37ErptnXIo3XCRQ4J9mhs1feihI871fIlURGCU95whUQVi/s1qjFVJGB/3RI=
19 | file:
20 | - "${RELEASE_PKG_FILE}"
21 | - "${RELEASE_DEB_FILE}"
22 | - "${RELEASE_RPM_FILE}"
23 | skip_cleanup: true
24 | on:
25 | tags: true
26 | jdk: oraclejdk8
27 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Metrics Plugin for Graylog
2 | ==========================
3 |
4 | [](https://github.com/graylog-labs/graylog-plugin-metrics/releases)
5 | [](https://github.com/graylog-labs/graylog-plugin-metrics/releases)
6 | [](https://travis-ci.org/graylog-labs/graylog-plugin-metrics)
7 |
8 | An output plugin for integrating [Graphite](http://graphite.readthedocs.org), [Ganglia](http://ganglia.info) and [StatsD](https://github.com/etsy/statsd) with [Graylog](https://www.graylog.org).
9 |
10 | **Required Graylog version:** 2.0 and later
11 |
12 | Please use version 1.2.0 of this plugin if you run Graylog 1.x
13 |
14 | ## Installation
15 |
16 | [Download the plugin](https://github.com/graylog-labs/graylog-plugin-metrics/releases)
17 | and place the `.jar` file in your Graylog plugin directory. The plugin directory
18 | is the `plugins/` folder relative from your `graylog-server` directory by default
19 | and can be configured in your `graylog.conf` file.
20 |
21 | Restart `graylog-server` and you are done.
22 |
23 | ## Build
24 |
25 | This project is using Maven and requires Java 8 or higher.
26 |
27 | You can build a plugin (JAR) with `mvn package`.
28 |
29 | DEB and RPM packages can be build with `mvn jdeb:jdeb` and `mvn rpm:rpm` respectively.
30 |
31 | ## Plugin Release
32 |
33 | We are using the maven release plugin:
34 |
35 | ```
36 | $ mvn release:prepare
37 | [...]
38 | $ mvn release:perform
39 | ```
40 |
41 | This sets the version numbers, creates a tag and pushes to GitHub. TravisCI will build the release artifacts and upload to GitHub automatically.
42 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | org.graylog.plugins
6 | graylog-plugin-metrics
7 | 1.3.1-SNAPSHOT
8 | jar
9 |
10 | ${project.artifactId}
11 | Graylog ${project.artifactId} plugin.
12 | https://www.graylog.org
13 |
14 |
15 |
16 |
17 | false
18 |
19 | bintray-readytalk-maven
20 | bintray
21 | http://dl.bintray.com/readytalk/maven
22 |
23 |
24 |
25 |
26 | UTF-8
27 | 1.7
28 | 1.7
29 | 2.0.3
30 | /usr/share/graylog-server/plugin
31 | 3.1.2
32 | 4.1.2
33 |
34 |
35 |
36 | scm:git:git@github.com:Graylog2/graylog-plugin-metrics.git
37 | scm:git:git@github.com:Graylog2/graylog-plugin-metrics.git
38 | https://github.com/Graylog2/graylog-plugin-metrics
39 | HEAD
40 |
41 |
42 |
43 |
44 | org.graylog2
45 | graylog2-server
46 | ${graylog2.version}
47 | provided
48 |
49 |
50 | io.dropwizard.metrics
51 | metrics-core
52 | ${metrics.version}
53 |
54 |
55 | io.dropwizard.metrics
56 | metrics-graphite
57 | ${metrics.version}
58 |
59 |
60 | io.dropwizard.metrics
61 | metrics-ganglia
62 | ${metrics.version}
63 |
64 |
65 | com.readytalk
66 | metrics3-statsd
67 | ${metrics.statsd.version}
68 |
69 |
70 |
71 |
72 |
73 |
74 | org.apache.maven.plugins
75 | maven-shade-plugin
76 | 2.3
77 |
78 | false
79 |
80 |
81 |
82 | package
83 |
84 | shade
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 | org.apache.maven.plugins
97 | maven-release-plugin
98 | 2.5.2
99 |
100 | true
101 | forked-path
102 | @{project.version}
103 | clean test
104 | package
105 |
106 |
107 |
108 |
109 | jdeb
110 | org.vafer
111 | 1.3
112 |
113 | ${project.build.directory}/${project.artifactId}-${project.version}.deb
114 |
115 |
116 | ${project.build.directory}/${project.build.finalName}.jar
117 | file
118 |
119 | perm
120 | ${graylog2.plugin-dir}
121 | 644
122 | root
123 | root
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 | org.codehaus.mojo
132 | rpm-maven-plugin
133 | 2.1.2
134 |
135 | Application/Internet
136 |
137 | /usr
138 |
139 |
140 | _unpackaged_files_terminate_build 0
141 | _binaries_in_noarch_packages_terminate_build 0
142 |
143 | 644
144 | 755
145 | root
146 | root
147 |
148 |
149 | ${graylog2.plugin-dir}
150 |
151 |
152 | ${project.build.directory}/
153 |
154 | ${project.build.finalName}.jar
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
--------------------------------------------------------------------------------
/src/deb/control/control:
--------------------------------------------------------------------------------
1 | Package: [[name]]
2 | Version: [[version]]
3 | Architecture: all
4 | Maintainer: Graylog, Inc.
5 | Section: web
6 | Priority: optional
7 | Depends: graylog-server
8 | Description: [[description]]
9 |
--------------------------------------------------------------------------------
/src/main/java/org/graylog/outputs/metrics/MetricsOutput.java:
--------------------------------------------------------------------------------
1 | package org.graylog.outputs.metrics;
2 |
3 | import com.codahale.metrics.*;
4 | import com.codahale.metrics.ganglia.GangliaReporter;
5 | import com.codahale.metrics.graphite.Graphite;
6 | import com.codahale.metrics.graphite.GraphiteReporter;
7 | import com.google.common.util.concurrent.AtomicLongMap;
8 | import com.google.inject.assistedinject.Assisted;
9 | import com.readytalk.metrics.StatsD;
10 | import com.readytalk.metrics.StatsDReporter;
11 | import info.ganglia.gmetric4j.gmetric.GMetric;
12 | import org.graylog2.plugin.Message;
13 | import org.graylog2.plugin.configuration.Configuration;
14 | import org.graylog2.plugin.configuration.ConfigurationRequest;
15 | import org.graylog2.plugin.configuration.fields.BooleanField;
16 | import org.graylog2.plugin.configuration.fields.ConfigurationField;
17 | import org.graylog2.plugin.configuration.fields.NumberField;
18 | import org.graylog2.plugin.configuration.fields.TextField;
19 | import org.graylog2.plugin.outputs.MessageOutput;
20 | import org.graylog2.plugin.outputs.MessageOutputConfigurationException;
21 | import org.graylog2.plugin.streams.Stream;
22 | import org.slf4j.Logger;
23 | import org.slf4j.LoggerFactory;
24 |
25 | import javax.inject.Inject;
26 | import java.io.IOException;
27 | import java.net.InetSocketAddress;
28 | import java.net.URI;
29 | import java.net.URISyntaxException;
30 | import java.util.Arrays;
31 | import java.util.List;
32 | import java.util.Map;
33 | import java.util.SortedSet;
34 | import java.util.concurrent.ConcurrentHashMap;
35 | import java.util.concurrent.TimeUnit;
36 | import java.util.concurrent.atomic.AtomicBoolean;
37 |
38 | public class MetricsOutput implements MessageOutput {
39 | private static final Logger LOG = LoggerFactory.getLogger(MetricsOutput.class);
40 |
41 | public static final String CK_URL = "url";
42 | public static final String CK_PREFIX = "prefix";
43 | public static final String CK_RUN_RATE = "run_rate";
44 | public static final String CK_FIELDS = "fields";
45 | public static final String CK_INCLUDE_SOURCE = "include_source";
46 | public static final String CK_INCLUDE_TYPE = "include_type";
47 | public static final String CK_INCLUDE_FIELD_VALUE = "include_field_value";
48 |
49 | private final AtomicBoolean isRunning = new AtomicBoolean(false);
50 | private Configuration configuration;
51 |
52 | private GangliaReporter gangliaReporter;
53 | private GraphiteReporter graphiteReporter;
54 | private StatsDReporter statsDReporter;
55 |
56 | private final MetricRegistry registry = new MetricRegistry();
57 | // private AtomicLongMap metricBuffer = AtomicLongMap.create();
58 | private ConcurrentHashMap metricBuffer = new ConcurrentHashMap<>();
59 | private List metricFields;
60 |
61 | @Inject
62 | public MetricsOutput(@Assisted Stream stream, @Assisted Configuration configuration) throws MessageOutputConfigurationException {
63 | this.configuration = configuration;
64 | metricFields = Arrays.asList(configuration.getString(CK_FIELDS).split(","));
65 |
66 | metricBuffer.clear();
67 |
68 | if (!checkConfiguration(configuration)) {
69 | throw new RuntimeException("Missing configuration parameters.");
70 | }
71 |
72 | URI uri = parseUrl(configuration.getString(CK_URL));
73 |
74 | switch (uri.getScheme()) {
75 | case "graphite":
76 | graphiteReporter = createGraphiteReporter(uri);
77 | break;
78 | case "ganglia":
79 | gangliaReporter = createGangliaReporter(uri);
80 | break;
81 | case "statsd":
82 | statsDReporter = createStatsDReporter(uri);
83 | break;
84 | default:
85 | LOG.error("Metrics backend not supported!");
86 | break;
87 | }
88 |
89 | isRunning.set(true);
90 | }
91 |
92 | @Override
93 | public void write(Message message) throws Exception {
94 | SortedSet currentMetrics = registry.getNames();
95 | final List validTypes = Arrays.asList("gauge", "counter", "histogram", "meter");
96 |
97 | for (String field : metricFields) {
98 | field = field.trim();
99 | String fieldType = "gauge";
100 | if(field.contains(":")) {
101 | String[] tupel = field.split(":");
102 | String type = tupel[tupel.length-1];
103 | if(validTypes.contains(type)) {
104 | fieldType = type;
105 | field = tupel[0];
106 | }
107 | }
108 |
109 | LOG.trace("Trying to read field [{}] from message <{}>.", field, message.getId());
110 | if (!message.getFields().containsKey(field)) {
111 | LOG.debug("Message <{}> does not contain field [{}]:[{}]. Can not send data to metrics store.", message.getId(), field, fieldType);
112 | continue;
113 | }
114 |
115 | // Get value
116 | Object messageValue = message.getField(field);
117 | Number metricValue;
118 | if (messageValue instanceof Long) {
119 | metricValue = (Long) messageValue;
120 | } else if (messageValue instanceof Integer) {
121 | metricValue = (Integer) messageValue;
122 | } else if (messageValue instanceof Float) {
123 | metricValue = (Float) messageValue;
124 | } else if (messageValue instanceof Double) {
125 | metricValue = (Double) messageValue;
126 | } else if (fieldType.equals("counter")) {
127 | metricValue = 1;
128 | } else {
129 | LOG.error("Field [{}] of message <{}> is not of numeric type. Not sending to metrics store.",
130 | field, message.getId());
131 | continue;
132 | }
133 |
134 | final String metricName = getMetricName(field, fieldType, message.getFields(), message.getSource());
135 |
136 | switch (fieldType.toLowerCase()) {
137 | case "gauge":
138 | // Register metric
139 | if (!currentMetrics.contains(metricName)) {
140 | registry.register(metricName, new Gauge() {
141 | @Override
142 | public Number getValue() {
143 | return metricBuffer.get(metricName);
144 | }
145 | });
146 | }
147 | // Update metric
148 | metricBuffer.put(metricName, metricValue.doubleValue());
149 | break;
150 | case "counter":
151 | final Counter counter = registry.counter(metricName);
152 | counter.inc();
153 | break;
154 | case "histogram":
155 | final Histogram histogram = registry.histogram(metricName);
156 | histogram.update(metricValue.longValue());
157 | break;
158 | case "meter":
159 | final Meter meter = registry.meter(metricName);
160 | meter.mark(metricValue.longValue());
161 | break;
162 | default:
163 | LOG.error("Unknown metric field type for [{}]: {}", metricName, fieldType);
164 | }
165 | LOG.debug("Metrics in Registry: {}", registry.getNames());
166 |
167 | }
168 | }
169 |
170 | @Override
171 | public void write(List messages) throws Exception {
172 | for (Message message : messages) {
173 | write(message);
174 | }
175 | }
176 |
177 | @Override
178 | public boolean isRunning() {
179 | return isRunning.get();
180 | }
181 |
182 | @Override
183 | public void stop() {
184 | LOG.info("Stopping Metrics output");
185 |
186 | registry.removeMatching(MetricFilter.ALL);
187 | metricBuffer.clear();
188 |
189 | if (gangliaReporter != null) {
190 | gangliaReporter.close();
191 | }
192 | if (graphiteReporter != null) {
193 | graphiteReporter.close();
194 | }
195 | if (statsDReporter != null) {
196 | statsDReporter.close();
197 | }
198 |
199 | isRunning.set(false);
200 | }
201 |
202 | public interface Factory extends MessageOutput.Factory {
203 | @Override
204 | MetricsOutput create(Stream stream, Configuration configuration);
205 | @Override
206 | Config getConfig();
207 | @Override
208 | Descriptor getDescriptor();
209 | }
210 |
211 | public static class Config extends MessageOutput.Config {
212 | @Override
213 | public ConfigurationRequest getRequestedConfiguration() {
214 | final ConfigurationRequest configurationRequest = new ConfigurationRequest();
215 |
216 | configurationRequest.addField(new TextField(
217 | CK_URL,
218 | "URL of metrics endpoint",
219 | "graphite://localhost:2003",
220 | "URL of your Graphite/Ganglia/StatsD instance",
221 | ConfigurationField.Optional.NOT_OPTIONAL)
222 | );
223 |
224 | configurationRequest.addField(new TextField(
225 | CK_PREFIX,
226 | "Prefix for metric names",
227 | "org.graylog",
228 | "Metric name will be 'prefix + field name'.",
229 | ConfigurationField.Optional.OPTIONAL)
230 | );
231 |
232 | configurationRequest.addField(new TextField(
233 | CK_FIELDS,
234 | "Message fields to submit to the metrics store",
235 | "response_time,db_time,view_time",
236 | "A comma separated list of message field names that should be transmitted as gauge values." +
237 | "Types like counter, meter, histogram can be set like: cache_hit:counter",
238 | ConfigurationField.Optional.NOT_OPTIONAL)
239 | );
240 |
241 | configurationRequest.addField(new NumberField(
242 | CK_RUN_RATE,
243 | "Submission frequency (seconds)",
244 | 30,
245 | "The period (in seconds) at which Graylog will submit metrics to the endpoint. " +
246 | "Keep this number high to not flood the metrics store.",
247 | ConfigurationField.Optional.NOT_OPTIONAL,
248 | NumberField.Attribute.ONLY_POSITIVE)
249 | );
250 |
251 | configurationRequest.addField(new TextField(
252 | CK_INCLUDE_FIELD_VALUE,
253 | "Append the value of the given field to the end of the metric name.",
254 | "",
255 | "Metric name will be 'prefix + field name + value of another field'.",
256 | ConfigurationField.Optional.OPTIONAL)
257 | );
258 |
259 | configurationRequest.addField(new BooleanField(
260 | CK_INCLUDE_SOURCE,
261 | "Include message source in metric name",
262 | false,
263 | "Metric name will be 'prefix + source + field name'.")
264 | );
265 |
266 | configurationRequest.addField(new BooleanField(
267 | CK_INCLUDE_TYPE,
268 | "Include field type in metric name",
269 | false,
270 | "Metric name will be 'prefix + field name + type'.")
271 | );
272 |
273 | return configurationRequest;
274 | }
275 | }
276 |
277 | private boolean checkConfiguration(Configuration config) {
278 | return config.stringIsSet(CK_URL)
279 | && config.stringIsSet(CK_FIELDS)
280 | && config.intIsSet(CK_RUN_RATE)
281 | && config.getInt(CK_RUN_RATE) > 0;
282 | }
283 |
284 | public static class Descriptor extends MessageOutput.Descriptor {
285 | public Descriptor() {
286 | super("Metrics Output", false, "",
287 | "Forwards selected field values of your messages to Graphite/Ganglia/StatsD.");
288 | }
289 | }
290 |
291 | private String getMetricName(String field, String fieldType, Map fields, String messageSource) {
292 | String metricName;
293 |
294 | /* prefix message source to metric name */
295 | if (configuration.getBoolean(CK_INCLUDE_SOURCE)) {
296 | metricName = messageSource + "." + field;
297 | } else {
298 | metricName = field;
299 | }
300 |
301 | /* append field type to metric name */
302 | if (configuration.getBoolean(CK_INCLUDE_TYPE)) {
303 | metricName = metricName + "." + fieldType;
304 | }
305 |
306 | /* append field value to metric name */
307 | if (!configuration.getString(CK_INCLUDE_FIELD_VALUE).isEmpty()) {
308 | for (Map.Entry fieldEntry : fields.entrySet()) {
309 | if (fieldEntry.getKey().equals(configuration.getString(CK_INCLUDE_FIELD_VALUE))) {
310 | metricName = metricName + "." + fieldEntry.getValue();
311 | }
312 | }
313 | }
314 |
315 | return metricName;
316 | }
317 |
318 | private URI parseUrl(String url) {
319 | URI uri = null;
320 | try {
321 | uri = new URI(url);
322 | } catch (URISyntaxException e) {
323 | LOG.error("Malformed metrics URL.");
324 | }
325 | return uri;
326 | }
327 |
328 | private GraphiteReporter createGraphiteReporter(URI uri) {
329 | final Graphite graphite = new Graphite(new InetSocketAddress(uri.getHost(), uri.getPort()));
330 | GraphiteReporter graphiteReporter = GraphiteReporter.forRegistry(registry)
331 | .prefixedWith(configuration.getString(CK_PREFIX))
332 | .convertRatesTo(TimeUnit.SECONDS)
333 | .convertDurationsTo(TimeUnit.MILLISECONDS)
334 | .filter(MetricFilter.ALL)
335 | .build(graphite);
336 | graphiteReporter.start(configuration.getInt(CK_RUN_RATE), TimeUnit.SECONDS);
337 | return graphiteReporter;
338 | }
339 |
340 | private GangliaReporter createGangliaReporter(URI uri) {
341 | GangliaReporter gangliaReporter = null;
342 | try {
343 | final GMetric ganglia = new GMetric(uri.getHost(), uri.getPort(), GMetric.UDPAddressingMode.MULTICAST, 1);
344 | gangliaReporter = GangliaReporter.forRegistry(registry)
345 | .convertRatesTo(TimeUnit.SECONDS)
346 | .convertDurationsTo(TimeUnit.MILLISECONDS)
347 | .prefixedWith(configuration.getString(CK_PREFIX))
348 | .build(ganglia);
349 | gangliaReporter.start(configuration.getInt(CK_RUN_RATE), TimeUnit.SECONDS);
350 | } catch (IOException e) {
351 | LOG.error("Can not connect to Ganglia server");
352 | }
353 | return gangliaReporter;
354 | }
355 |
356 | private StatsDReporter createStatsDReporter(URI uri) {
357 | StatsDReporter statsDReporter = StatsDReporter.forRegistry(registry)
358 | .prefixedWith(configuration.getString(CK_PREFIX))
359 | .convertRatesTo(TimeUnit.SECONDS)
360 | .convertDurationsTo(TimeUnit.MILLISECONDS)
361 | .filter(MetricFilter.ALL)
362 | .build(uri.getHost(), uri.getPort());
363 | statsDReporter.start(configuration.getInt(CK_RUN_RATE), TimeUnit.SECONDS);
364 | return statsDReporter;
365 | }
366 | }
367 |
--------------------------------------------------------------------------------
/src/main/java/org/graylog/outputs/metrics/MetricsOutputMetaData.java:
--------------------------------------------------------------------------------
1 | package org.graylog.outputs.metrics;
2 |
3 | import org.graylog2.plugin.PluginMetaData;
4 | import org.graylog2.plugin.ServerStatus;
5 | import org.graylog2.plugin.Version;
6 |
7 | import java.net.URI;
8 | import java.util.Collections;
9 | import java.util.Set;
10 |
11 | /**
12 | * Implement the PluginMetaData interface here.
13 | */
14 | public class MetricsOutputMetaData implements PluginMetaData {
15 | @Override
16 | public String getUniqueId() {
17 | return "org.graylog.outputs.metrics.MetricsOutputPlugin";
18 | }
19 |
20 | @Override
21 | public String getName() {
22 | return "MetricsOutput";
23 | }
24 |
25 | @Override
26 | public String getAuthor() {
27 | return "Graylog, Inc.";
28 | }
29 |
30 | @Override
31 | public URI getURL() {
32 | return URI.create("https://www.graylog.org/");
33 | }
34 |
35 | @Override
36 | public Version getVersion() {
37 | return new Version(1, 3, 0, "SNAPSHOT");
38 | }
39 |
40 | @Override
41 | public String getDescription() {
42 | return "Forwards selected field values of your messages to Graphite/Ganglia/StatsD.";
43 | }
44 |
45 | @Override
46 | public Version getRequiredVersion() {
47 | return new Version(2, 0, 0);
48 | }
49 |
50 | @Override
51 | public Set getRequiredCapabilities() {
52 | return Collections.emptySet();
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/org/graylog/outputs/metrics/MetricsOutputModule.java:
--------------------------------------------------------------------------------
1 | package org.graylog.outputs.metrics;
2 |
3 | import com.google.inject.multibindings.MapBinder;
4 | import org.graylog2.plugin.PluginConfigBean;
5 | import org.graylog2.plugin.PluginModule;
6 | import org.graylog2.plugin.outputs.MessageOutput;
7 |
8 | import java.util.Collections;
9 | import java.util.Set;
10 |
11 | public class MetricsOutputModule extends PluginModule {
12 | @Override
13 | public Set extends PluginConfigBean> getConfigBeans() {
14 | return Collections.emptySet();
15 | }
16 |
17 | @Override
18 | protected void configure() {
19 | final MapBinder> outputMapBinder = outputsMapBinder();
20 | installOutput(outputMapBinder, MetricsOutput.class, MetricsOutput.Factory.class);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/org/graylog/outputs/metrics/MetricsOutputPlugin.java:
--------------------------------------------------------------------------------
1 | package org.graylog.outputs.metrics;
2 |
3 | import org.graylog2.plugin.Plugin;
4 | import org.graylog2.plugin.PluginMetaData;
5 | import org.graylog2.plugin.PluginModule;
6 |
7 | import java.util.Arrays;
8 | import java.util.Collection;
9 |
10 | public class MetricsOutputPlugin implements Plugin {
11 | @Override
12 | public PluginMetaData metadata() {
13 | return new MetricsOutputMetaData();
14 | }
15 |
16 | @Override
17 | public Collection modules () {
18 | return Arrays.asList(new MetricsOutputModule());
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/resources/META-INF/services/org.graylog2.plugin.Plugin:
--------------------------------------------------------------------------------
1 | org.graylog.outputs.metrics.MetricsOutputPlugin
--------------------------------------------------------------------------------