Use kafka.tools.JmxTool
.
Assuming you have enabled JMX on your broker on the default port of 9999, you can simply run this example command to print out jmx metrics for BrokerTopics via STDOUT
$ ./bin/kafka-run-class.sh kafka.tools.JmxTool \
--object-name kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec
Trying to connect to JMX url: service:jmx:rmi:///jndi/rmi://:9999/jmxrmi.
"time","kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec:Count","kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec:EventType","kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec:FifteenMinuteRate","kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec:FiveMinuteRate","kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec:MeanRate","kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec:OneMinuteRate","kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec:RateUnit"
1567587563143,0,messages,0.0,0.0,0.0,0.0,SECONDS
1567587565148,0,messages,0.0,0.0,0.0,0.0,SECONDS
1567587567150,0,messages,0.0,0.0,0.0,0.0,SECONDS
...
JmxTool
prints out JMX metrics every --reporting-interval
option.
--reporting-interval <Integer: ms>
Interval (in millis) with which to poll jmx stats
Default: 2000
(2 seconds)
Value of -1
is equivalent to --one-time true