Metrics are now also available via JMX directly
authorKai Moritz <kai@juplo.de>
Tue, 2 May 2023 20:30:17 +0000 (22:30 +0200)
committerKai Moritz <kai@juplo.de>
Tue, 2 May 2023 20:30:56 +0000 (22:30 +0200)
README.jmx [new file with mode: 0755]
docker-compose.yml
mbeans/.editorconfig [new file with mode: 0644]
mbeans/.gitignore [new file with mode: 0644]
mbeans/pom.xml [new file with mode: 0644]
mbeans/src/main/java/de/juplo/monitoring/ListMBeans.java [new file with mode: 0644]

diff --git a/README.jmx b/README.jmx
new file mode 100755 (executable)
index 0000000..766e00f
--- /dev/null
@@ -0,0 +1,2 @@
+#!/bin/sh
+jconsole :7001
index 4ca259d..ba8849e 100644 (file)
@@ -5,13 +5,25 @@ services:
   zookeeper:
     build: zookeeper
     environment:
+      JVMFLAGS:
+        -Dcom.sun.management.jmxremote=true
+        -Dcom.sun.management.jmxremote.port=7070
+        -Dcom.sun.management.jmxremote.authenticate=false
       ALLOW_ANONYMOUS_LOGIN: 'yes'
+      ZOO_ENABLE_PROMETHEUS_METRICS: 'yes'
     volumes:
       - zookeeper:/bitnami/zookeeper
+    ports:
+      - 2181:2181
+      - 7000:7070
 
   kafka-1:
     build: kafka
     environment:
+      KAFKA_OPTS:
+        -Dcom.sun.management.jmxremote=true
+        -Dcom.sun.management.jmxremote.port=7000
+        -Dcom.sun.management.jmxremote.authenticate=false
       KAFKA_CFG_ZOOKEEPER_CONNECT: zookeeper:2181
       KAFKA_CFG_LISTENERS: BROKER://:9092, LOCALHOST://:9081
       KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: BROKER:PLAINTEXT, LOCALHOST:PLAINTEXT
@@ -25,12 +37,17 @@ services:
       - kafka-1:/bitnami/kafka
     ports:
       - 9081:9081
+      - 7001:7000
     depends_on:
       - zookeeper
 
   kafka-2:
     build: kafka
     environment:
+      KAFKA_OPTS:
+        -Dcom.sun.management.jmxremote=true
+        -Dcom.sun.management.jmxremote.port=7000
+        -Dcom.sun.management.jmxremote.authenticate=false
       KAFKA_CFG_ZOOKEEPER_CONNECT: zookeeper:2181
       KAFKA_CFG_LISTENERS: BROKER://:9092, LOCALHOST://:9082
       KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: BROKER:PLAINTEXT, LOCALHOST:PLAINTEXT
@@ -45,6 +62,7 @@ services:
     ports:
       - 9092:9082
       - 9082:9082
+      - 7002:7000
     networks:
       default:
         aliases:
@@ -55,6 +73,10 @@ services:
   kafka-3:
     build: kafka
     environment:
+      KAFKA_OPTS:
+        -Dcom.sun.management.jmxremote=true
+        -Dcom.sun.management.jmxremote.port=7000
+        -Dcom.sun.management.jmxremote.authenticate=false
       KAFKA_CFG_ZOOKEEPER_CONNECT: zookeeper:2181
       KAFKA_CFG_LISTENERS: BROKER://:9092, LOCALHOST://:9083
       KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: BROKER:PLAINTEXT, LOCALHOST:PLAINTEXT
@@ -68,6 +90,7 @@ services:
       - kafka-3:/bitnami/kafka
     ports:
       - 9083:9083
+      - 7003:7000
     depends_on:
       - zookeeper
 
diff --git a/mbeans/.editorconfig b/mbeans/.editorconfig
new file mode 100644 (file)
index 0000000..633c98a
--- /dev/null
@@ -0,0 +1,13 @@
+root = true
+
+[*]
+indent_style = space
+indent_size = tab
+tab_width = 2
+charset = utf-8
+end_of_line = lf
+trim_trailing_whitespace = true
+insert_final_newline = false
+
+[*.properties]
+charset = latin1
\ No newline at end of file
diff --git a/mbeans/.gitignore b/mbeans/.gitignore
new file mode 100644 (file)
index 0000000..6240411
--- /dev/null
@@ -0,0 +1,3 @@
+*.iml
+.idea
+target
diff --git a/mbeans/pom.xml b/mbeans/pom.xml
new file mode 100644 (file)
index 0000000..3b3eb7c
--- /dev/null
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>de.juplo.monitoring</groupId>
+  <artifactId>mbeans</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <name>ListMBeans</name>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <maven.compiler.source>1.7</maven.compiler.source>
+    <maven.compiler.target>1.7</maven.compiler.target>
+  </properties>
+
+  <build>
+    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
+      <plugins>
+        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
+        <plugin>
+          <artifactId>maven-clean-plugin</artifactId>
+          <version>3.1.0</version>
+        </plugin>
+        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
+        <plugin>
+          <artifactId>maven-resources-plugin</artifactId>
+          <version>3.0.2</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-compiler-plugin</artifactId>
+          <version>3.8.0</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <version>2.22.1</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-jar-plugin</artifactId>
+          <version>3.0.2</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-install-plugin</artifactId>
+          <version>2.5.2</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-deploy-plugin</artifactId>
+          <version>2.8.2</version>
+        </plugin>
+        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
+        <plugin>
+          <artifactId>maven-site-plugin</artifactId>
+          <version>3.7.1</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-project-info-reports-plugin</artifactId>
+          <version>3.0.0</version>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+  </build>
+</project>
diff --git a/mbeans/src/main/java/de/juplo/monitoring/ListMBeans.java b/mbeans/src/main/java/de/juplo/monitoring/ListMBeans.java
new file mode 100644 (file)
index 0000000..148c728
--- /dev/null
@@ -0,0 +1,71 @@
+package de.juplo.monitoring;
+
+import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanOperationInfo;
+import javax.management.MBeanServerConnection;
+import javax.management.ObjectName;
+import javax.management.remote.JMXConnector;
+import javax.management.remote.JMXConnectorFactory;
+import javax.management.remote.JMXServiceURL;
+import java.util.Iterator;
+import java.util.Set;
+
+
+public class ListMBeans
+{
+    public static void main(String[] args)
+    {
+        String url = args.length > 0 ? args[0] : "service:jmx:rmi:///jndi/rmi://:7001/jmxrmi";
+        System.out.println("-------------------------------------------------");
+        try {
+            JMXServiceURL jmxServiceURL = new JMXServiceURL(url);
+            JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceURL, null);
+
+            MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();
+
+            Set result = connection.queryNames(new ObjectName("*:*"), null);
+            Iterator it = result.iterator();
+
+            while (it.hasNext())
+            {
+                ObjectName objectName = (ObjectName) it.next();
+
+                try
+                {
+                    System.out.println("--> " + objectName.getCanonicalName());
+                    MBeanAttributeInfo[] attributes = connection.getMBeanInfo(objectName).getAttributes();
+
+                    for (int i = 0; i < attributes.length; i++)
+                    {
+                        System.out.println("         Attribute: " + attributes[i].getName() + "   of Type : " + attributes[i].getType());
+                    }
+
+                    MBeanOperationInfo[] operations = connection.getMBeanInfo(objectName).getOperations();
+
+                    for (int i = 0; i < operations.length; i++)
+                    {
+                        System.out.print("         Operation: " + operations[i].getReturnType() + "  " + operations[i].getName() + "(");
+                        for (int j = 0; j < operations[i].getSignature().length;j++)
+                        {
+                          System.out.print(operations[i].getSignature()[j].getName() +
+                            ":" +
+                            operations[i].getSignature()[j].getType() +
+                            "  ");
+                        }
+                        System.out.println(")");
+                    }
+                } catch (Exception e)
+                {
+                    e.printStackTrace();
+                }
+            }
+
+            jmxConnector.close();
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        System.out.println("-------------------------------------------------");
+    }
+}