Setting up jmx monitoring for Apache Kafka

I was looking to setup jmx monitoring for Apache Kafka.

Seems straightforward. Edit the server start and add something like what is below.

if [ “x$KAFKA_JMX_OPTS” = “x” ]; then
export KAFKA_JMX_OPTS=" -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9000 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.password.file=/home/kafka/jmx11_remote/jmxremote.password -Dcom.sun.management.jmxremote.access.file=/home/kafka/jmx11_remote/jmxremote.access"
fi

The issue I have is if I have a systemd file kafka@.service that calls the common server start file for multiple brokers, I will have port collision.

Is there a better way of doing this?

Figured it out

Defining the env variable in the systemd file sets the port as a variable.

Environment=“JMX_PORT=900%i”

Adding the call to the jmx file as an argiment in EXTRA_ARGS then loads the remainder of the jmx parameters.