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