diff --git core/src/main/scala/kafka/tools/ConsumerGroupChecker.scala core/src/main/scala/kafka/tools/ConsumerGroupChecker.scala new file mode 100644 index 0000000..e6c81eb --- /dev/null +++ core/src/main/scala/kafka/tools/ConsumerGroupChecker.scala @@ -0,0 +1,82 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kafka.tools + +import joptsimple._ +import org.I0Itec.zkclient.ZkClient +import kafka.utils._ +import scala.collection._ + +import scala.util.control.NonFatal + +object ConsumerGroupChecker extends Logging { + + private def getGroups(zkClient: ZkClient) { + try { + val groups = ZkUtils.getChildren(zkClient, ZkUtils.ConsumersPath); + if (groups.size < 1) { + println("No consumer group found") + } else { + for (group <- groups) { + println(group) + } + } + } catch { + case NonFatal(t) => + error(s"could not get groups because of ${t.getMessage}", t) + Seq() + } + } + + def main(args: Array[String]) { + val parser = new OptionParser() + + val zkConnectOpt = parser.accepts("zookeeper", "ZooKeeper connect string."). + withRequiredArg().defaultsTo("localhost:2181").ofType(classOf[String]) + + parser.accepts("help", "Print this message.") + + if(args.length == 0) + CommandLineUtils.printUsageAndDie(parser, "Check available consumer groups.") + + val options = parser.parse(args : _*) + + if (options.has("help")) { + parser.printHelpOn(System.out) + System.exit(0) + } + + CommandLineUtils.checkRequiredArgs(parser, options, zkConnectOpt) + + val zkConnect = options.valueOf(zkConnectOpt) + + var zkClient: ZkClient = null + try { + zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer) + getGroups(zkClient) + } + catch { + case t: Throwable => + println("Exiting due to: %s.".format(t.getMessage)) + } + finally { + if (zkClient != null) + zkClient.close() + } + } +}