Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
JVM monitoring in a Docker environment
public final class BusyApp {
public static void main(String []args) throws Exception {
final java.util.Random generator = new java.util.Random();
while (true) {
generator.ints(1000000, 0, 100).sorted();
Thread.sleep(5000L);
}
}
}
jvmapp:
build: .
dockerfile: Dockerfile-jvmapp
container_name: jvmapp
environment:
JMX_HOST: jvmapp
JMX_PORT: 4000
ports:
- "4000:4000"
jmxtrans:
build: .
dockerfile: Dockerfile-jmxtrans
container_name: jmxtrans
environment:
JMX_HOST: jvmapp
JMX_PORT: 4000
STATSD_HOST: dashboard
STATSD_PORT: 8125
links:
- jvmapp
- dashboard
dashboard:
image: kamon/grafana_graphite
container_name: dashboard
ports:
- "80:80"
- "81:81"
- "8125:8125/udp"
- "8126:8126"
FROM openjdk:8
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
RUN wget http://central.maven.org/maven2/org/jmxtrans/jmxtrans/259/jmxtrans-259.deb
RUN dpkg -i jmxtrans-259.deb
COPY run-jmxtrans.sh /run-jmxtrans.sh
RUN chmod 755 /run-jmxtrans.sh
CMD /run-jmxtrans.sh
FROM openjdk:8
COPY BusyApp.java /BusyApp.java
RUN javac BusyApp.java
ENV JMX_HOST=jvm-app
ENV JMX_PORT=4000
CMD java \
-Djava.rmi.server.hostname=$JMX_HOST \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=$JMX_PORT \
-Dcom.sun.management.jmxremote.rmi.port=$JMX_PORT \
-Dcom.sun.management.jmxremote.local.only=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
BusyApp
#!/bin/bash
JMXTRANS_HEAP_SIZE=${JMXTRANS_HEAP_SIZE:-"512"}
JMXTRANS_LOG_LEVEL=${JMXTRANS_LOG_LEVEL:-"info"}
JMXTRANS_PERIOD=${JMXTRANS_PERIOD:-"10"}
JMXTRANS_JAR="/usr/share/jmxtrans/lib/jmxtrans-all.jar"
JMXTRANS_CONFIG="/jmxtrans-config.json"
cat <<EOF > $JMXTRANS_CONFIG
{
"servers": [
{
"host": "${JMX_HOST}",
"port": "${JMX_PORT}",
"queries": [
{
"obj": "java.lang:type=Memory",
"attr": [ "HeapMemoryUsage", "NonHeapMemoryUsage" ],
"outputWriters": [
{
"@class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"@class": "com.googlecode.jmxtrans.model.output.StatsDWriterFactory",
"host": "${STATSD_HOST}",
"port": "${STATSD_PORT}",
"bucketType" : "c"
}
]
}
]
}
]
}
EOF
JAVA_OPTS="-Xms${JMXTRANS_HEAP_SIZE}m -Xmx${JMXTRANS_HEAP_SIZE}m -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true"
JMXTRANS_OPTS="-Djmxtrans.log.level=${JMXTRANS_LOG_LEVEL}"
MONITOR_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=${JMX_PORT}"
EXEC="-jar $JMXTRANS_JAR -e -f $JMXTRANS_CONFIG -s $JMXTRANS_PERIOD -c false"
java -server $JAVA_OPTS $JMXTRANS_OPTS $MONITOR_OPTS $EXEC

There should be - "80:3000" in dashboard's ports section.
https://gist.github.com/sbilinski/24809966114a6b5089c2ada53eb0b4b7#file-docker-compose-yml-L26
Because Graphana UI appears to listen on port 3000 by default.
http://docs.grafana.org/installation/configuration/#http-port

Thanks for this great gist, btw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment