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 public class JMXJsonServlet extends HttpServlet { 118 private static final Log LOG = LogFactory.getLog(JMXJsonServlet.class); 119 120 private static final long serialVersionUID = 1L; 121 122 // ----------------------------------------------------- Instance Variables 123 /** 124 * MBean server. 125 */ 126 protected transient MBeanServer mBeanServer = null; 127 128 // --------------------------------------------------------- Public Methods 129 /** 130 * Initialize this servlet. 131 */ 132 @Override 133 public void init() throws ServletException { 134 // Retrieve the MBean server 135 mBeanServer = ManagementFactory.getPlatformMBeanServer(); 136 } 137 138 /** 139 * Process a GET request for the specified resource. 140 * 141 * @param request 142 * The servlet request we are processing 143 * @param response 144 * The servlet response we are creating 145 */ 146 @Override 147 public void doGet(HttpServletRequest request, HttpServletResponse response) { 148 try { 149 if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(), 150 request, 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 (RuntimeErrorException e) { 326 // RuntimeErrorException happens when an unexpected failure occurs in getAttribute 327 // for example https://issues.apache.org/jira/browse/DAEMON-120 328 LOG.debug("getting attribute "+attName+" of "+oname+" threw an exception", e); 329 return; 330 } catch (AttributeNotFoundException e) { 331 //Ignored the attribute was not found, which should never happen because the bean 332 //just told us that it has this attribute, but if this happens just don't output 333 //the attribute. 334 return; 335 } catch (MBeanException e) { 336 //The code inside the attribute getter threw an exception so log it, and 337 // skip outputting the attribute 338 LOG.error("getting attribute "+attName+" of "+oname+" threw an exception", e); 339 return; 340 } catch (RuntimeException e) { 341 //For some reason even with an MBeanException available to them Runtime exceptions 342 //can still find their way through, so treat them the same as MBeanException 343 LOG.error("getting attribute "+attName+" of "+oname+" threw an exception", e); 344 return; 345 } catch (ReflectionException e) { 346 //This happens when the code inside the JMX bean (setter?? from the java docs) 347 //threw an exception, so log it and skip outputting the attribute 348 LOG.error("getting attribute "+attName+" of "+oname+" threw an exception", e); 349 return; 350 } catch (InstanceNotFoundException e) { 351 //Ignored the mbean itself was not found, which should never happen because we 352 //just accessed it (perhaps something unregistered in-between) but if this 353 //happens just don't output the attribute. 354 return; 355 } 356 357 writeAttribute(jg, attName, value); 358 } 359 360 private void writeAttribute(JsonGenerator jg, String attName, Object value) throws IOException { 361 jg.writeFieldName(attName); 362 writeObject(jg, value); 363 } 364 365 private void writeObject(JsonGenerator jg, Object value) throws IOException { 366 if(value == null) { 367 jg.writeNull(); 368 } else { 369 Class<?> c = value.getClass(); 370 if (c.isArray()) { 371 jg.writeStartArray(); 372 int len = Array.getLength(value); 373 for (int j = 0; j < len; j++) { 374 Object item = Array.get(value, j); 375 writeObject(jg, item); 376 } 377 jg.writeEndArray(); 378 } else if(value instanceof Number) { 379 Number n = (Number)value; 380 jg.writeNumber(n.toString()); 381 } else if(value instanceof Boolean) { 382 Boolean b = (Boolean)value; 383 jg.writeBoolean(b); 384 } else if(value instanceof CompositeData) { 385 CompositeData cds = (CompositeData)value; 386 CompositeType comp = cds.getCompositeType(); 387 Set<String> keys = comp.keySet(); 388 jg.writeStartObject(); 389 for(String key: keys) { 390 writeAttribute(jg, key, cds.get(key)); 391 } 392 jg.writeEndObject(); 393 } else if(value instanceof TabularData) { 394 TabularData tds = (TabularData)value; 395 jg.writeStartArray(); 396 for(Object entry : tds.values()) { 397 writeObject(jg, entry); 398 } 399 jg.writeEndArray(); 400 } else { 401 jg.writeString(value.toString()); 402 } 403 } 404 } 405 }