Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 org.apache.kafka.tools;
import joptsimple.OptionSpec;
import org.apache.kafka.common.utils.Exit;
import org.apache.kafka.common.utils.Utils;
import org.apache.kafka.server.util.CommandDefaultOptions;
import org.apache.kafka.server.util.CommandLineUtils;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.MBeanFeatureInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* A program for reading JMX metrics from a given endpoint.
*
* This tool only works reliably if the JmxServer is fully initialized prior to invoking the tool.
* See KAFKA-4620 for details.
*/
public class JmxTool {
public static void main(String[] args) {
try {
JmxToolOptions options = new JmxToolOptions(args);
if (CommandLineUtils.isPrintHelpNeeded(options)) {
CommandLineUtils.printUsageAndExit(options.parser, "Dump JMX values to standard output.");
return;
}
if (CommandLineUtils.isPrintVersionNeeded(options)) {
CommandLineUtils.printVersionAndExit();
return;
}
Optional attributesInclude = options.attributesInclude();
Optional dateFormat = options.dateFormat();
String reportFormat = options.parseFormat();
boolean keepGoing = true;
MBeanServerConnection conn = connectToBeanServer(options);
List queries = options.queries();
boolean hasPatternQueries = queries.stream().filter(Objects::nonNull).anyMatch(ObjectName::isPattern);
Set found = findObjects(options, conn, queries, hasPatternQueries);
Map numExpectedAttributes =
findNumExpectedAttributes(conn, attributesInclude, hasPatternQueries, queries, found);
List keys = new ArrayList<>();
keys.add("time");
keys.addAll(new TreeSet<>(queryAttributes(conn, found, attributesInclude).keySet()));
maybePrintCsvHeader(reportFormat, keys, numExpectedAttributes);
while (keepGoing) {
long start = System.currentTimeMillis();
Map attributes = queryAttributes(conn, found, attributesInclude);
attributes.put("time", dateFormat.isPresent() ? dateFormat.get().format(new Date()) : String.valueOf(System.currentTimeMillis()));
maybePrintDataRows(reportFormat, numExpectedAttributes, keys, attributes);
if (options.isOneTime()) {
keepGoing = false;
} else {
TimeUnit.MILLISECONDS.sleep(Math.max(0, options.interval() - (System.currentTimeMillis() - start)));
}
}
Exit.exit(0);
} catch (TerseException e) {
System.err.println(e.getMessage());
Exit.exit(1);
} catch (Throwable e) {
System.err.println(e.getMessage());
System.err.println(Utils.stackTrace(e));
Exit.exit(1);
}
}
private static String mkString(Stream