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