001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.hadoop.hbase.regionserver;
018
019import java.io.IOException;
020import java.io.PrintWriter;
021import java.io.StringWriter;
022import java.lang.management.ManagementFactory;
023import javax.management.MBeanServer;
024import javax.management.MalformedObjectNameException;
025import javax.management.ObjectName;
026import org.apache.hadoop.hbase.util.JSONBean;
027import org.apache.yetus.audience.InterfaceAudience;
028
029/**
030 * Utility for doing JSON and MBeans.
031 */
032@InterfaceAudience.Private
033public final class DumpRegionServerMetrics {
034  /**
035   * Dump out a subset of regionserver mbeans only, not all of them, as json on System.out.
036   */
037  public static String dumpMetrics() throws MalformedObjectNameException, IOException {
038    StringWriter sw = new StringWriter(1024 * 100); // Guess this size
039    try (PrintWriter writer = new PrintWriter(sw)) {
040      JSONBean dumper = new JSONBean();
041      try (JSONBean.Writer jsonBeanWriter = dumper.open(writer)) {
042        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
043        jsonBeanWriter.write(mbeanServer,
044          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}