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          // Do the authorization
149          if (!HttpServer.hasAdministratorAccess(getServletContext(), request,
150              response)) {
151            return;
152          }
153    
154          response.setContentType("application/json; charset=utf8");
155    
156          PrintWriter writer = response.getWriter();
157    
158          JsonFactory jsonFactory = new JsonFactory();
159          JsonGenerator jg = jsonFactory.createJsonGenerator(writer);
160          jg.useDefaultPrettyPrinter();
161          jg.writeStartObject();
162          if (mBeanServer == null) {
163            jg.writeStringField("result", "ERROR");
164            jg.writeStringField("message", "No MBeanServer could be found");
165            jg.close();
166            LOG.error("No MBeanServer could be found.");
167            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
168            return;
169          }
170          
171          // query per mbean attribute
172          String getmethod = request.getParameter("get");
173          if (getmethod != null) {
174            String[] splitStrings = getmethod.split("\\:\\:");
175            if (splitStrings.length != 2) {
176              jg.writeStringField("result", "ERROR");
177              jg.writeStringField("message", "query format is not as expected.");
178              jg.close();
179              response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
180              return;
181            }
182            listBeans(jg, new ObjectName(splitStrings[0]), splitStrings[1],
183                response);
184            jg.close();
185            return;
186          }
187    
188          // query per mbean
189          String qry = request.getParameter("qry");
190          if (qry == null) {
191            qry = "*:*";
192          }
193          listBeans(jg, new ObjectName(qry), null, response);
194          jg.close();
195    
196        } catch ( IOException e ) {
197          LOG.error("Caught an exception while processing JMX request", e);
198          response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
199        } catch ( MalformedObjectNameException e ) {
200          LOG.error("Caught an exception while processing JMX request", e);
201          response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
202        }
203      }
204    
205      // --------------------------------------------------------- Private Methods
206      private void listBeans(JsonGenerator jg, ObjectName qry, String attribute, 
207          HttpServletResponse response) 
208      throws IOException {
209        LOG.debug("Listing beans for "+qry);
210        Set<ObjectName> names = null;
211        names = mBeanServer.queryNames(qry, null);
212    
213        jg.writeArrayFieldStart("beans");
214        Iterator<ObjectName> it = names.iterator();
215        while (it.hasNext()) {
216          ObjectName oname = it.next();
217          MBeanInfo minfo;
218          String code = "";
219          Object attributeinfo = null;
220          try {
221            minfo = mBeanServer.getMBeanInfo(oname);
222            code = minfo.getClassName();
223            String prs = "";
224            try {
225              if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
226                prs = "modelerType";
227                code = (String) mBeanServer.getAttribute(oname, prs);
228              }
229              if (attribute!=null) {
230                prs = attribute;
231                attributeinfo = mBeanServer.getAttribute(oname, prs);
232              }
233            } catch (AttributeNotFoundException e) {
234              // If the modelerType attribute was not found, the class name is used
235              // instead.
236              LOG.error("getting attribute " + prs + " of " + oname
237                  + " threw an exception", e);
238            } catch (MBeanException e) {
239              // The code inside the attribute getter threw an exception so log it,
240              // and fall back on the class name
241              LOG.error("getting attribute " + prs + " of " + oname
242                  + " threw an exception", e);
243            } catch (RuntimeException e) {
244              // For some reason even with an MBeanException available to them
245              // Runtime exceptionscan still find their way through, so treat them
246              // the same as MBeanException
247              LOG.error("getting attribute " + prs + " of " + oname
248                  + " threw an exception", e);
249            } catch ( ReflectionException e ) {
250              // This happens when the code inside the JMX bean (setter?? from the
251              // java docs) threw an exception, so log it and fall back on the 
252              // class name
253              LOG.error("getting attribute " + prs + " of " + oname
254                  + " threw an exception", e);
255            }
256          } catch (InstanceNotFoundException e) {
257            //Ignored for some reason the bean was not found so don't output it
258            continue;
259          } catch ( IntrospectionException e ) {
260            // This is an internal error, something odd happened with reflection so
261            // log it and don't output the bean.
262            LOG.error("Problem while trying to process JMX query: " + qry
263                + " with MBean " + oname, e);
264            continue;
265          } catch ( ReflectionException e ) {
266            // This happens when the code inside the JMX bean threw an exception, so
267            // log it and don't output the bean.
268            LOG.error("Problem while trying to process JMX query: " + qry
269                + " with MBean " + oname, e);
270            continue;
271          }
272    
273          jg.writeStartObject();
274          jg.writeStringField("name", oname.toString());
275          
276          jg.writeStringField("modelerType", code);
277          if ((attribute != null) && (attributeinfo == null)) {
278            jg.writeStringField("result", "ERROR");
279            jg.writeStringField("message", "No attribute with name " + attribute
280                + " was found.");
281            jg.writeEndObject();
282            jg.writeEndArray();
283            jg.close();
284            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
285            return;
286          }
287          
288          if (attribute != null) {
289            writeAttribute(jg, attribute, attributeinfo);
290          } else {
291            MBeanAttributeInfo attrs[] = minfo.getAttributes();
292            for (int i = 0; i < attrs.length; i++) {
293              writeAttribute(jg, oname, attrs[i]);
294            }
295          }
296          jg.writeEndObject();
297        }
298        jg.writeEndArray();
299      }
300    
301      private void writeAttribute(JsonGenerator jg, ObjectName oname, MBeanAttributeInfo attr) throws IOException {
302        if (!attr.isReadable()) {
303          return;
304        }
305        String attName = attr.getName();
306        if ("modelerType".equals(attName)) {
307          return;
308        }
309        if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0
310            || attName.indexOf(" ") >= 0) {
311          return;
312        }
313        Object value = null;
314        try {
315          value = mBeanServer.getAttribute(oname, attName);
316        } catch (RuntimeMBeanException e) {
317          // UnsupportedOperationExceptions happen in the normal course of business,
318          // so no need to log them as errors all the time.
319          if (e.getCause() instanceof UnsupportedOperationException) {
320            LOG.debug("getting attribute "+attName+" of "+oname+" threw an exception", e);
321          } else {
322            LOG.error("getting attribute "+attName+" of "+oname+" threw an exception", e);
323          }
324          return;
325        } catch (AttributeNotFoundException e) {
326          //Ignored the attribute was not found, which should never happen because the bean
327          //just told us that it has this attribute, but if this happens just don't output
328          //the attribute.
329          return;
330        } catch (MBeanException e) {
331          //The code inside the attribute getter threw an exception so log it, and
332          // skip outputting the attribute
333          LOG.error("getting attribute "+attName+" of "+oname+" threw an exception", e);
334          return;
335        } catch (RuntimeException e) {
336          //For some reason even with an MBeanException available to them Runtime exceptions
337          //can still find their way through, so treat them the same as MBeanException
338          LOG.error("getting attribute "+attName+" of "+oname+" threw an exception", e);
339          return;
340        } catch (ReflectionException e) {
341          //This happens when the code inside the JMX bean (setter?? from the java docs)
342          //threw an exception, so log it and skip outputting the attribute
343          LOG.error("getting attribute "+attName+" of "+oname+" threw an exception", e);
344          return;
345        } catch (InstanceNotFoundException e) {
346          //Ignored the mbean itself was not found, which should never happen because we
347          //just accessed it (perhaps something unregistered in-between) but if this
348          //happens just don't output the attribute.
349          return;
350        }
351    
352        writeAttribute(jg, attName, value);
353      }
354      
355      private void writeAttribute(JsonGenerator jg, String attName, Object value) throws IOException {
356        jg.writeFieldName(attName);
357        writeObject(jg, value);
358      }
359      
360      private void writeObject(JsonGenerator jg, Object value) throws IOException {
361        if(value == null) {
362          jg.writeNull();
363        } else {
364          Class<?> c = value.getClass();
365          if (c.isArray()) {
366            jg.writeStartArray();
367            int len = Array.getLength(value);
368            for (int j = 0; j < len; j++) {
369              Object item = Array.get(value, j);
370              writeObject(jg, item);
371            }
372            jg.writeEndArray();
373          } else if(value instanceof Number) {
374            Number n = (Number)value;
375            jg.writeNumber(n.toString());
376          } else if(value instanceof Boolean) {
377            Boolean b = (Boolean)value;
378            jg.writeBoolean(b);
379          } else if(value instanceof CompositeData) {
380            CompositeData cds = (CompositeData)value;
381            CompositeType comp = cds.getCompositeType();
382            Set<String> keys = comp.keySet();
383            jg.writeStartObject();
384            for(String key: keys) {
385              writeAttribute(jg, key, cds.get(key));
386            }
387            jg.writeEndObject();
388          } else if(value instanceof TabularData) {
389            TabularData tds = (TabularData)value;
390            jg.writeStartArray();
391            for(Object entry : tds.values()) {
392              writeObject(jg, entry);
393            }
394            jg.writeEndArray();
395          } else {
396            jg.writeString(value.toString());
397          }
398        }
399      }
400    }