001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import java.io.IOException;
021import java.io.PrintWriter;
022import java.io.StringWriter;
023import java.lang.management.ManagementFactory;
024import javax.management.MBeanServer;
025import javax.management.MalformedObjectNameException;
026import javax.management.ObjectName;
027import org.apache.hadoop.hbase.util.JSONBean;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * Utility for doing JSON and MBeans.
032 */
033@InterfaceAudience.Private
034public final class DumpRegionServerMetrics {
035  /**
036   * Dump out a subset of regionserver mbeans only, not all of them, as json on System.out.
037   */
038  public static String dumpMetrics() throws MalformedObjectNameException, IOException {
039    StringWriter sw = new StringWriter(1024 * 100); // Guess this size
040    try (PrintWriter writer = new PrintWriter(sw)) {
041      JSONBean dumper = new JSONBean();
042      try (JSONBean.Writer jsonBeanWriter = dumper.open(writer)) {
043        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
044        jsonBeanWriter.write(mbeanServer, new ObjectName("java.lang:type=Memory"), null, false);
045        jsonBeanWriter.write(mbeanServer,
046          new ObjectName("Hadoop:service=HBase,name=RegionServer,sub=IPC"), null, false);
047        jsonBeanWriter.write(mbeanServer,
048          new ObjectName("Hadoop:service=HBase,name=RegionServer,sub=Replication"), null, false);
049        jsonBeanWriter.write(mbeanServer,
050          new ObjectName("Hadoop:service=HBase,name=RegionServer,sub=Server"), null, false);
051      }
052    }
053    sw.close();
054    return sw.toString();
055  }
056
057  public static void main(String[] args) throws IOException, MalformedObjectNameException {
058    String str = dumpMetrics();
059    System.out.println(str);
060  }
061
062  private DumpRegionServerMetrics() {
063  }
064}