diff --git a/README.md b/README.md index 3e1ea9c..7ba287f 100644 --- a/README.md +++ b/README.md @@ -52,13 +52,13 @@ there are two ways to specify the host in the jolokia URL so this URL will be re **Optional environment variables** - GRAPHITE_PORT: Protocol port of graphite. Defaults to 2004. +- GRAPHITE_SSL: Boolean to enable SSL sockets. Standard `javax.net.ssl` system properties are used for keystores, etc. - SERVICE_HOST: By default the host is taken from Jolokia URL and serves as the service host, unless you use this variable. - INTERVAL_IN_SEC: By default 30 seconds unless you use this variable. - LOG_LEVEL: Configure Log Level [any of OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL] - WHITE_LIST_REGEX: filter out unwanted metrics with whitelist regex. - BLACK_LIST_REGEX: filter out unwanted metrics with blacklist regex. - ### Docker with config file Create a .conf file, set the input parameter and provide it to the docker. diff --git a/src/main/java/io/logz/jmx2graphite/GraphiteClient.java b/src/main/java/io/logz/jmx2graphite/GraphiteClient.java index ec874cb..bb995ce 100644 --- a/src/main/java/io/logz/jmx2graphite/GraphiteClient.java +++ b/src/main/java/io/logz/jmx2graphite/GraphiteClient.java @@ -13,6 +13,7 @@ import org.slf4j.LoggerFactory; import javax.net.SocketFactory; +import javax.net.ssl.SSLSocketFactory; import java.io.Closeable; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -37,7 +38,7 @@ public class GraphiteClient implements Closeable { private int failuresAtLastWrite = 0; public GraphiteClient(String serviceHost, String serviceName, String graphiteHostname, int graphitePort, - int connectTimeout, int socketTimeout, int writeTimeoutMs, GraphiteProtocol protocol) { + boolean ssl, int connectTimeout, int socketTimeout, int writeTimeoutMs, GraphiteProtocol protocol) { List prefixElements = Lists.newArrayList(); if (serviceName != null && !serviceName.isEmpty()) { prefixElements.add(sanitizeMetricName(serviceName)); @@ -54,7 +55,7 @@ public GraphiteClient(String serviceHost, String serviceName, String graphiteHos logger.info("Graphite metrics prefix: {}", metricsPrefix); logger.info("Graphite Client: using writeTimeoutMs of {} [ms]. Establishing connection...", writeTimeoutMs); - SocketFactory socketFactory = new SocketFactoryWithTimeouts(connectTimeout, socketTimeout); + SocketFactory socketFactory = new SocketFactoryWithTimeouts(connectTimeout, socketTimeout, ssl); if (protocol == UDP) { graphite = new GraphiteUDP(new InetSocketAddress(graphiteHostname, graphitePort)); } else if (protocol == TCP) { @@ -157,19 +158,20 @@ public GraphiteWriteFailed(String message, Throwable cause) { } private static class SocketFactoryWithTimeouts extends SocketFactory { - private SocketFactory socketFactory = SocketFactory.getDefault(); + private final SocketFactory socketFactory; // FIXME make it use this connect timeout (I can't find a way to do it private int connectTimeout; private int socketTimeout; - public SocketFactoryWithTimeouts(int connectTimeout, int socketTimeout) { + public SocketFactoryWithTimeouts(int connectTimeout, int socketTimeout, boolean ssl) { this.connectTimeout = connectTimeout; this.socketTimeout = socketTimeout; + this.socketFactory = ssl ? SSLSocketFactory.getDefault() : SocketFactory.getDefault(); } public static SocketFactory getDefault() { - return new SocketFactoryWithTimeouts(5, 10); + return new SocketFactoryWithTimeouts(5, 10, false); } @Override diff --git a/src/main/java/io/logz/jmx2graphite/Jmx2GraphiteConfiguration.java b/src/main/java/io/logz/jmx2graphite/Jmx2GraphiteConfiguration.java index c51b1ea..d8d94d0 100644 --- a/src/main/java/io/logz/jmx2graphite/Jmx2GraphiteConfiguration.java +++ b/src/main/java/io/logz/jmx2graphite/Jmx2GraphiteConfiguration.java @@ -60,6 +60,7 @@ public enum MetricClientType { private class Graphite { public String hostname; public int port; + public boolean ssl; } public Jmx2GraphiteConfiguration(Config config) throws IllegalConfiguration { @@ -104,6 +105,7 @@ else if (config.hasPath("service.poller.mbean-direct")) { graphite = new Graphite(); graphite.hostname = config.getString("graphite.hostname"); graphite.port = config.getInt("graphite.port"); + graphite.ssl = config.getBoolean("graphite.ssl"); metricsPollingIntervalInSeconds = config.getInt("metricsPollingIntervalInSeconds"); serviceName = config.getString("service.name"); @@ -176,6 +178,14 @@ public void setGraphitePort(int graphitePort) { this.graphite.port = graphitePort; } + public boolean getGraphiteSsl() { + return graphite.ssl; + } + + public void setGraphiteSsl(boolean ssl) { + this.graphite.ssl = ssl; + } + public String getServiceName() { return serviceName; } diff --git a/src/main/java/io/logz/jmx2graphite/Jmx2GraphiteJavaAgent.java b/src/main/java/io/logz/jmx2graphite/Jmx2GraphiteJavaAgent.java index 68a64fd..77f110c 100644 --- a/src/main/java/io/logz/jmx2graphite/Jmx2GraphiteJavaAgent.java +++ b/src/main/java/io/logz/jmx2graphite/Jmx2GraphiteJavaAgent.java @@ -75,6 +75,8 @@ private static String getArgumentConfigurationRepresentation(String key) throws return "graphite.hostname"; case "GRAPHITE_PORT": return "graphite.port"; + case "GRAPHITE_SSL": + return "graphite.ssl"; case "SERVICE_NAME": return "service.name"; case "SERVICE_HOST": diff --git a/src/main/java/io/logz/jmx2graphite/MetricsPipeline.java b/src/main/java/io/logz/jmx2graphite/MetricsPipeline.java index 7484fe4..b0ec3e5 100644 --- a/src/main/java/io/logz/jmx2graphite/MetricsPipeline.java +++ b/src/main/java/io/logz/jmx2graphite/MetricsPipeline.java @@ -31,7 +31,7 @@ public class MetricsPipeline { public MetricsPipeline(Jmx2GraphiteConfiguration conf, MBeanClient client) { this.graphiteClient = new GraphiteClient(conf.getServiceHost(), conf.getServiceName(), conf.getGraphiteHostname(), - conf.getGraphitePort(), conf.getGraphiteConnectTimeout(), + conf.getGraphitePort(), conf.getGraphiteSsl(), conf.getGraphiteConnectTimeout(), conf.getGraphiteSocketTimeout(), conf.getGraphiteWriteTimeoutMs(), conf.getGraphiteProtocol()); this.client = client; diff --git a/src/main/resources/javaagent.conf b/src/main/resources/javaagent.conf index b2dd418..f9a712f 100644 --- a/src/main/resources/javaagent.conf +++ b/src/main/resources/javaagent.conf @@ -20,6 +20,7 @@ graphite { hostname = ${?GRAPHITE_HOST} port = ${?GRAPHITE_PORT} protocol = ${?GRAPHITE_PROTOCOL} + ssl = ${?GRAPHITE_SSL} } filter { diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf index 02d5c3e..8586f53 100644 --- a/src/main/resources/reference.conf +++ b/src/main/resources/reference.conf @@ -4,4 +4,5 @@ graphite { port = 2004 connectTimeout = 10 socketTimeout = 5 + ssl = false } diff --git a/src/test/java/io/logz/jmx2graphite/GraphiteClientTest.java b/src/test/java/io/logz/jmx2graphite/GraphiteClientTest.java index efb27d3..50536bf 100644 --- a/src/test/java/io/logz/jmx2graphite/GraphiteClientTest.java +++ b/src/test/java/io/logz/jmx2graphite/GraphiteClientTest.java @@ -67,7 +67,7 @@ public void testOnServerRestart() throws InterruptedException { int connectTimeout = 1000; int socketTimeout = 1000; GraphiteClient client = new GraphiteClient("bla-host.com", "bla-service", "localhost", - port, connectTimeout, socketTimeout, 20000, + port, false, connectTimeout, socketTimeout, 20000, null); ArrayList dummyMetrics = Lists.newArrayList(new MetricValue("dice", 4, TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()))); diff --git a/src/test/java/io/logz/jmx2graphite/GraphiteITest.java b/src/test/java/io/logz/jmx2graphite/GraphiteITest.java index 1d22274..0996656 100644 --- a/src/test/java/io/logz/jmx2graphite/GraphiteITest.java +++ b/src/test/java/io/logz/jmx2graphite/GraphiteITest.java @@ -120,7 +120,7 @@ private GraphiteClient createGraphiteClient() { int socketTimeout = (int) Duration.ofSeconds(5).toMillis(); int writeTimeoutMs = (int) Duration.ofSeconds(5).toMillis(); - return new GraphiteClient(TEST_SERVICE_HOST, TEST_SERVICE_NAME, "localhost", 2003, + return new GraphiteClient(TEST_SERVICE_HOST, TEST_SERVICE_NAME, "localhost", 2003, false, connectTimeout, socketTimeout, writeTimeoutMs, GraphiteProtocol.TCP); }