JMX log all or specific MBeans and all or specific values



  private static Log Log = LogFactory.getLog(LogMBean.class);


  public void LogMBeans(String nameContains, String valueContains) throws IntrospectionException,
      InstanceNotFoundException, ReflectionException
  {
    final List servers = new LinkedList();
    servers.add(ManagementFactory.getPlatformMBeanServer());
    servers.addAll(MBeanServerFactory.findMBeanServer(null));
    for (final MBeanServer server: servers)
    {
      Log.error("Server: " + server.getClass().getName());

      final Set mbeans = new HashSet();
      mbeans.addAll(server.queryNames(null, null));
      for (final ObjectName mbean: mbeans)
      {
        if (nameContains != null && mbean.toString().contains(nameContains) == false)
        {
          continue;
        }

        String mbeanName = mbean.toString();
        if (valueContains == null || valueContains.isEmpty())
        {
          Log.error("mbean: " + mbeanName);
        }

        final MBeanAttributeInfo[] attributes = server.getMBeanInfo(mbean).getAttributes();
        for (final MBeanAttributeInfo attribute: attributes)
        {
          Object value = null;
          try
          {
            value = server.getAttribute(mbean, attribute.getName());
          }
          catch (Exception e)
          {
            value = e.getMessage();
          }

          if (valueContains == null || valueContains.isEmpty())
          {
            Log.error(String.format("Name: %s, Type: %s, Value: %s", attribute.getName(), attribute.getType(), value));
          }
          else
          {
            if (value != null && value.toString().contains(valueContains))
            {
              Log.error("mbean: " + mbeanName);
              Log.error(String.format("Name: %s, Type: %s, Value: %s", attribute.getName(), attribute.getType(), value));
            }
          }
        }
      }
    }
  }

Comments

Popular posts from this blog

Sites, Newsletters, and Blogs

Oracle JDBC ReadTimeout QueryTimeout