From: Kai Moritz Date: Tue, 2 May 2023 20:30:17 +0000 (+0200) Subject: Metrics are now also available via JMX directly X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=ddcd0fa674d1a659c481d8a874c53d3a3322c68c;p=demos%2Fkafka%2Fmonitoring Metrics are now also available via JMX directly --- diff --git a/README.jmx b/README.jmx new file mode 100755 index 0000000..766e00f --- /dev/null +++ b/README.jmx @@ -0,0 +1,2 @@ +#!/bin/sh +jconsole :7001 diff --git a/docker-compose.yml b/docker-compose.yml index 4ca259d..ba8849e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 index 0000000..633c98a --- /dev/null +++ b/mbeans/.editorconfig @@ -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 index 0000000..6240411 --- /dev/null +++ b/mbeans/.gitignore @@ -0,0 +1,3 @@ +*.iml +.idea +target diff --git a/mbeans/pom.xml b/mbeans/pom.xml new file mode 100644 index 0000000..3b3eb7c --- /dev/null +++ b/mbeans/pom.xml @@ -0,0 +1,63 @@ + + + + 4.0.0 + + de.juplo.monitoring + mbeans + 1.0-SNAPSHOT + ListMBeans + + + UTF-8 + 1.7 + 1.7 + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + 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 index 0000000..148c728 --- /dev/null +++ b/mbeans/src/main/java/de/juplo/monitoring/ListMBeans.java @@ -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("-------------------------------------------------"); + } +}