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 */
017
018package org.apache.hadoop.jmx;
019
020import java.io.IOException;
021import java.io.PrintWriter;
022import java.lang.management.ManagementFactory;
023import java.lang.reflect.Array;
024import java.util.Iterator;
025import java.util.Set;
026
027import javax.management.AttributeNotFoundException;
028import javax.management.InstanceNotFoundException;
029import javax.management.IntrospectionException;
030import javax.management.MBeanAttributeInfo;
031import javax.management.MBeanException;
032import javax.management.MBeanInfo;
033import javax.management.MBeanServer;
034import javax.management.MalformedObjectNameException;
035import javax.management.ObjectName;
036import javax.management.ReflectionException;
037import javax.management.RuntimeErrorException;
038import javax.management.RuntimeMBeanException;
039import javax.management.openmbean.CompositeData;
040import javax.management.openmbean.CompositeType;
041import javax.management.openmbean.TabularData;
042import javax.servlet.ServletException;
043import javax.servlet.http.HttpServlet;
044import javax.servlet.http.HttpServletRequest;
045import javax.servlet.http.HttpServletResponse;
046
047import org.apache.commons.logging.Log;
048import org.apache.commons.logging.LogFactory;
049import org.apache.hadoop.http.HttpServer2;
050import org.codehaus.jackson.JsonFactory;
051import org.codehaus.jackson.JsonGenerator;
052
053/*
054 * This servlet is based off of the JMXProxyServlet from Tomcat 7.0.14. It has
055 * been rewritten to be read only and to output in a JSON format so it is not
056 * really that close to the original.
057 */
058/**
059 * Provides Read only web access to JMX.
060 * <p>
061 * This servlet generally will be placed under the /jmx URL for each
062 * HttpServer.  It provides read only
063 * access to JMX metrics.  The optional <code>qry</code> parameter
064 * may be used to query only a subset of the JMX Beans.  This query
065 * functionality is provided through the
066 * {@link MBeanServer#queryNames(ObjectName, javax.management.QueryExp)}
067 * method.
068 * <p>
069 * For example <code>http://.../jmx?qry=Hadoop:*</code> will return
070 * all hadoop metrics exposed through JMX.
071 * <p>
072 * The optional <code>get</code> parameter is used to query an specific 
073 * attribute of a JMX bean.  The format of the URL is
074 * <code>http://.../jmx?get=MXBeanName::AttributeName<code>
075 * <p>
076 * For example 
077 * <code>
078 * http://../jmx?get=Hadoop:service=NameNode,name=NameNodeInfo::ClusterId
079 * </code> will return the cluster id of the namenode mxbean.
080 * <p>
081 * If the <code>qry</code> or the <code>get</code> parameter is not formatted 
082 * correctly then a 400 BAD REQUEST http response code will be returned. 
083 * <p>
084 * If a resouce such as a mbean or attribute can not be found, 
085 * a 404 SC_NOT_FOUND http response code will be returned. 
086 * <p>
087 * The return format is JSON and in the form
088 * <p>
089 *  <code><pre>
090 *  {
091 *    "beans" : [
092 *      {
093 *        "name":"bean-name"
094 *        ...
095 *      }
096 *    ]
097 *  }
098 *  </pre></code>
099 *  <p>
100 *  The servlet attempts to convert the the JMXBeans into JSON. Each
101 *  bean's attributes will be converted to a JSON object member.
102 *  
103 *  If the attribute is a boolean, a number, a string, or an array
104 *  it will be converted to the JSON equivalent. 
105 *  
106 *  If the value is a {@link CompositeData} then it will be converted
107 *  to a JSON object with the keys as the name of the JSON member and
108 *  the value is converted following these same rules.
109 *  
110 *  If the value is a {@link TabularData} then it will be converted
111 *  to an array of the {@link CompositeData} elements that it contains.
112 *  
113 *  All other objects will be converted to a string and output as such.
114 *  
115 *  The bean's name and modelerType will be returned for all beans.
116 *
117 *  Optional paramater "callback" should be used to deliver JSONP response.
118 *  
119 */
120public class JMXJsonServlet extends HttpServlet {
121  private static final Log LOG = LogFactory.getLog(JMXJsonServlet.class);
122
123  private static final long serialVersionUID = 1L;
124
125  // ----------------------------------------------------- Instance Variables
126  private static final String CALLBACK_PARAM = "callback";
127
128  /**
129   * MBean server.
130   */
131  protected transient MBeanServer mBeanServer = null;
132
133  // --------------------------------------------------------- Public Methods
134  /**
135   * Initialize this servlet.
136   */
137  @Override
138  public void init() throws ServletException {
139    // Retrieve the MBean server
140    mBeanServer = ManagementFactory.getPlatformMBeanServer();
141  }
142
143  protected boolean isInstrumentationAccessAllowed(HttpServletRequest request,
144      HttpServletResponse response) throws IOException {
145    return HttpServer2.isInstrumentationAccessAllowed(getServletContext(),
146        request, response);
147  }
148
149  /**
150   * Process a GET request for the specified resource.
151   * 
152   * @param request
153   *          The servlet request we are processing
154   * @param response
155   *          The servlet response we are creating
156   */
157  @Override
158  public void doGet(HttpServletRequest request, HttpServletResponse response) {
159    String jsonpcb = null;
160    PrintWriter writer = null;
161    try {
162      if (!isInstrumentationAccessAllowed(request, response)) {
163        return;
164      }
165      
166      JsonGenerator jg = null;
167
168      writer = response.getWriter();
169 
170      // "callback" parameter implies JSONP outpout
171      jsonpcb = request.getParameter(CALLBACK_PARAM);
172      if (jsonpcb != null) {
173        response.setContentType("application/javascript; charset=utf8");
174        writer.write(jsonpcb + "(");
175      } else {
176        response.setContentType("application/json; charset=utf8");
177      }
178
179      JsonFactory jsonFactory = new JsonFactory();
180      jg = jsonFactory.createJsonGenerator(writer);
181      jg.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
182      jg.useDefaultPrettyPrinter();
183      jg.writeStartObject();
184
185      if (mBeanServer == null) {
186        jg.writeStringField("result", "ERROR");
187        jg.writeStringField("message", "No MBeanServer could be found");
188        jg.close();
189        LOG.error("No MBeanServer could be found.");
190        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
191        return;
192      }
193      
194      // query per mbean attribute
195      String getmethod = request.getParameter("get");
196      if (getmethod != null) {
197        String[] splitStrings = getmethod.split("\\:\\:");
198        if (splitStrings.length != 2) {
199          jg.writeStringField("result", "ERROR");
200          jg.writeStringField("message", "query format is not as expected.");
201          jg.close();
202          response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
203          return;
204        }
205        listBeans(jg, new ObjectName(splitStrings[0]), splitStrings[1],
206            response);
207        jg.close();
208        return;
209        
210      }
211
212      // query per mbean
213      String qry = request.getParameter("qry");
214      if (qry == null) {
215        qry = "*:*";
216      }
217      listBeans(jg, new ObjectName(qry), null, response);
218      jg.close();
219
220    } catch ( IOException e ) {
221      LOG.error("Caught an exception while processing JMX request", e);
222      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
223    } catch ( MalformedObjectNameException e ) {
224      LOG.error("Caught an exception while processing JMX request", e);
225      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
226    } finally {
227      if (jsonpcb != null) {
228         writer.write(");");
229      }
230      if (writer != null) {
231        writer.close();
232      }
233    }
234  }
235
236  // --------------------------------------------------------- Private Methods
237  private void listBeans(JsonGenerator jg, ObjectName qry, String attribute, 
238      HttpServletResponse response) 
239  throws IOException {
240    LOG.debug("Listing beans for "+qry);
241    Set<ObjectName> names = null;
242    names = mBeanServer.queryNames(qry, null);
243
244    jg.writeArrayFieldStart("beans");
245    Iterator<ObjectName> it = names.iterator();
246    while (it.hasNext()) {
247      ObjectName oname = it.next();
248      MBeanInfo minfo;
249      String code = "";
250      Object attributeinfo = null;
251      try {
252        minfo = mBeanServer.getMBeanInfo(oname);
253        code = minfo.getClassName();
254        String prs = "";
255        try {
256          if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
257            prs = "modelerType";
258            code = (String) mBeanServer.getAttribute(oname, prs);
259          }
260          if (attribute!=null) {
261            prs = attribute;
262            attributeinfo = mBeanServer.getAttribute(oname, prs);
263          }
264        } catch (AttributeNotFoundException e) {
265          // If the modelerType attribute was not found, the class name is used
266          // instead.
267          LOG.error("getting attribute " + prs + " of " + oname
268              + " threw an exception", e);
269        } catch (MBeanException e) {
270          // The code inside the attribute getter threw an exception so log it,
271          // and fall back on the class name
272          LOG.error("getting attribute " + prs + " of " + oname
273              + " threw an exception", e);
274        } catch (RuntimeException e) {
275          // For some reason even with an MBeanException available to them
276          // Runtime exceptionscan still find their way through, so treat them
277          // the same as MBeanException
278          LOG.error("getting attribute " + prs + " of " + oname
279              + " threw an exception", e);
280        } catch ( ReflectionException e ) {
281          // This happens when the code inside the JMX bean (setter?? from the
282          // java docs) threw an exception, so log it and fall back on the 
283          // class name
284          LOG.error("getting attribute " + prs + " of " + oname
285              + " threw an exception", e);
286        }
287      } catch (InstanceNotFoundException e) {
288        //Ignored for some reason the bean was not found so don't output it
289        continue;
290      } catch ( IntrospectionException e ) {
291        // This is an internal error, something odd happened with reflection so
292        // log it and don't output the bean.
293        LOG.error("Problem while trying to process JMX query: " + qry
294            + " with MBean " + oname, e);
295        continue;
296      } catch ( ReflectionException e ) {
297        // This happens when the code inside the JMX bean threw an exception, so
298        // log it and don't output the bean.
299        LOG.error("Problem while trying to process JMX query: " + qry
300            + " with MBean " + oname, e);
301        continue;
302      }
303
304      jg.writeStartObject();
305      jg.writeStringField("name", oname.toString());
306      
307      jg.writeStringField("modelerType", code);
308      if ((attribute != null) && (attributeinfo == null)) {
309        jg.writeStringField("result", "ERROR");
310        jg.writeStringField("message", "No attribute with name " + attribute
311            + " was found.");
312        jg.writeEndObject();
313        jg.writeEndArray();
314        jg.close();
315        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
316        return;
317      }
318      
319      if (attribute != null) {
320        writeAttribute(jg, attribute, attributeinfo);
321      } else {
322        MBeanAttributeInfo attrs[] = minfo.getAttributes();
323        for (int i = 0; i < attrs.length; i++) {
324          writeAttribute(jg, oname, attrs[i]);
325        }
326      }
327      jg.writeEndObject();
328    }
329    jg.writeEndArray();
330  }
331
332  private void writeAttribute(JsonGenerator jg, ObjectName oname, MBeanAttributeInfo attr) throws IOException {
333    if (!attr.isReadable()) {
334      return;
335    }
336    String attName = attr.getName();
337    if ("modelerType".equals(attName)) {
338      return;
339    }
340    if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0
341        || attName.indexOf(" ") >= 0) {
342      return;
343    }
344    Object value = null;
345    try {
346      value = mBeanServer.getAttribute(oname, attName);
347    } catch (RuntimeMBeanException e) {
348      // UnsupportedOperationExceptions happen in the normal course of business,
349      // so no need to log them as errors all the time.
350      if (e.getCause() instanceof UnsupportedOperationException) {
351        LOG.debug("getting attribute "+attName+" of "+oname+" threw an exception", e);
352      } else {
353        LOG.error("getting attribute "+attName+" of "+oname+" threw an exception", e);
354      }
355      return;
356    } catch (RuntimeErrorException e) {
357      // RuntimeErrorException happens when an unexpected failure occurs in getAttribute
358      // for example https://issues.apache.org/jira/browse/DAEMON-120
359      LOG.debug("getting attribute "+attName+" of "+oname+" threw an exception", e);
360      return;
361    } catch (AttributeNotFoundException e) {
362      //Ignored the attribute was not found, which should never happen because the bean
363      //just told us that it has this attribute, but if this happens just don't output
364      //the attribute.
365      return;
366    } catch (MBeanException e) {
367      //The code inside the attribute getter threw an exception so log it, and
368      // skip outputting the attribute
369      LOG.error("getting attribute "+attName+" of "+oname+" threw an exception", e);
370      return;
371    } catch (RuntimeException e) {
372      //For some reason even with an MBeanException available to them Runtime exceptions
373      //can still find their way through, so treat them the same as MBeanException
374      LOG.error("getting attribute "+attName+" of "+oname+" threw an exception", e);
375      return;
376    } catch (ReflectionException e) {
377      //This happens when the code inside the JMX bean (setter?? from the java docs)
378      //threw an exception, so log it and skip outputting the attribute
379      LOG.error("getting attribute "+attName+" of "+oname+" threw an exception", e);
380      return;
381    } catch (InstanceNotFoundException e) {
382      //Ignored the mbean itself was not found, which should never happen because we
383      //just accessed it (perhaps something unregistered in-between) but if this
384      //happens just don't output the attribute.
385      return;
386    }
387
388    writeAttribute(jg, attName, value);
389  }
390  
391  private void writeAttribute(JsonGenerator jg, String attName, Object value) throws IOException {
392    jg.writeFieldName(attName);
393    writeObject(jg, value);
394  }
395  
396  private void writeObject(JsonGenerator jg, Object value) throws IOException {
397    if(value == null) {
398      jg.writeNull();
399    } else {
400      Class<?> c = value.getClass();
401      if (c.isArray()) {
402        jg.writeStartArray();
403        int len = Array.getLength(value);
404        for (int j = 0; j < len; j++) {
405          Object item = Array.get(value, j);
406          writeObject(jg, item);
407        }
408        jg.writeEndArray();
409      } else if(value instanceof Number) {
410        Number n = (Number)value;
411        jg.writeNumber(n.toString());
412      } else if(value instanceof Boolean) {
413        Boolean b = (Boolean)value;
414        jg.writeBoolean(b);
415      } else if(value instanceof CompositeData) {
416        CompositeData cds = (CompositeData)value;
417        CompositeType comp = cds.getCompositeType();
418        Set<String> keys = comp.keySet();
419        jg.writeStartObject();
420        for(String key: keys) {
421          writeAttribute(jg, key, cds.get(key));
422        }
423        jg.writeEndObject();
424      } else if(value instanceof TabularData) {
425        TabularData tds = (TabularData)value;
426        jg.writeStartArray();
427        for(Object entry : tds.values()) {
428          writeObject(jg, entry);
429        }
430        jg.writeEndArray();
431      } else {
432        jg.writeString(value.toString());
433      }
434    }
435  }
436}