@Override
 public AttributeList getAttributes(ObjectName name, String[] attributes)
     throws InstanceNotFoundException, ReflectionException, IOException {
   try {
     return connection.getAttributes(name, attributes);
   } catch (IOException e) {
     checkConnection();
     return connection.getAttributes(name, attributes);
   }
 }
Пример #2
0
 @SuppressWarnings("unchecked")
 private void listAll()
     throws IOException, InstanceNotFoundException, IntrospectionException, ReflectionException {
   Set<ObjectName> mbeans = connection.queryNames(null, null);
   for (ObjectName name : mbeans) {
     MBeanInfo info = connection.getMBeanInfo(name);
     MBeanAttributeInfo[] attrs = info.getAttributes();
     String[] attrNames = new String[attrs.length];
     for (int i = 0; i < attrs.length; i++) {
       attrNames[i] = attrs[i].getName();
     }
     try {
       List<Attribute> attributes = connection.getAttributes(name, attrNames).asList();
       for (Attribute attribute : attributes) {
         output(name.getCanonicalName() + "%" + attribute.getName(), attribute.getValue());
       }
     } catch (Exception e) {
       System.err.println("error getting " + name + ":" + e.getMessage());
     }
   }
 }
Пример #3
0
 private synchronized NameValueMap getCachedAttributes(ObjectName objName, Set<String> attrNames)
     throws InstanceNotFoundException, ReflectionException, IOException {
   NameValueMap values = cachedValues.get(objName);
   if (values != null && values.keySet().containsAll(attrNames)) {
     return values;
   }
   attrNames = new TreeSet<String>(attrNames);
   Set<String> oldNames = cachedNames.get(objName);
   if (oldNames != null) {
     attrNames.addAll(oldNames);
   }
   values = new NameValueMap();
   final AttributeList attrs =
       conn.getAttributes(objName, attrNames.toArray(new String[attrNames.size()]));
   for (Attribute attr : attrs.asList()) {
     values.put(attr.getName(), attr.getValue());
   }
   cachedValues.put(objName, values);
   cachedNames.put(objName, attrNames);
   return values;
 }
Пример #4
0
 private static List<Attribute> batchLoadAttributes(
     MBeanServerConnection server, ObjectName objectName, List<String> matchingNames)
     throws InstanceNotFoundException, ReflectionException, IOException {
   final String[] namesArray = matchingNames.toArray(new String[matchingNames.size()]);
   return server.getAttributes(objectName, namesArray).asList();
 }
Пример #5
0
  /**
   * {@inheritDoc}
   *
   * <p>Perform data collection.
   */
  @Override
  public CollectionSet collect(CollectionAgent agent, EventProxy eproxy, Map<String, Object> map) {
    InetAddress ipaddr = agent.getAddress();
    JMXNodeInfo nodeInfo = agent.getAttribute(NODE_INFO_KEY);
    Map<String, BeanInfo> mbeans = nodeInfo.getMBeans();
    String collDir = serviceName;

    boolean useMbeanForRrds = ParameterMap.getKeyedBoolean(map, "use-mbean-name-for-rrds", false);
    String port = ParameterMap.getKeyedString(map, "port", null);
    String friendlyName = ParameterMap.getKeyedString(map, "friendly-name", port);
    if (useFriendlyName) {
      collDir = friendlyName;
    }

    JMXCollectionSet collectionSet = new JMXCollectionSet(agent, collDir);
    collectionSet.setCollectionTimestamp(new Date());
    JMXCollectionResource collectionResource = collectionSet.getResource();

    ConnectionWrapper connection = null;

    LogUtils.debugf(
        this, "collecting %s on node ID %d", InetAddressUtils.str(ipaddr), nodeInfo.getNodeId());

    try {
      connection = getMBeanServerConnection(map, ipaddr);

      if (connection == null) {
        return collectionSet;
      }

      MBeanServerConnection mbeanServer = connection.getMBeanServer();

      int retry = ParameterMap.getKeyedInteger(map, "retry", 3);
      for (int attempts = 0; attempts <= retry; attempts++) {
        try {
          /*
           * Iterate over the mbeans, for each object name perform a
           * getAttributes, the update the RRD.
           */

          for (Iterator<BeanInfo> iter = mbeans.values().iterator(); iter.hasNext(); ) {
            BeanInfo beanInfo = iter.next();
            String mbeanName = beanInfo.getMbeanName();
            String objectName = beanInfo.getObjectName();
            String excludeList = beanInfo.getExcludes();
            // All JMX collected values are per node
            String obj = useMbeanForRrds ? mbeanName : objectName;
            AttributeGroupType attribGroupType = new AttributeGroupType(fixGroupName(obj), "all");

            List<String> attribNames = beanInfo.getAttributeNames();
            List<String> compAttribNames = beanInfo.getCompositeAttributeNames();

            for (String compAttribName : compAttribNames) {
              if (attribNames.contains(compAttribName)) {
                attribNames.remove(compAttribName);
                String[] ac = compAttribName.split("\\|", -1);
                String attrName = ac[0];
                if (!attribNames.contains(attrName)) {
                  attribNames.add(attrName);
                }
              }
            }
            // log.debug(" JMXCollector: processed the following attributes: " +
            // attribNames.toString());
            // log.debug(" JMXCollector: processed the following Composite Attributes: " +
            // compAttribNames.toString());

            String[] attrNames = attribNames.toArray(new String[attribNames.size()]);

            if (objectName.indexOf("*") == -1) {
              LogUtils.debugf(
                  this,
                  "%s Collector - getAttributes: %s, # attributes: %d, # composite attribute members: %d",
                  serviceName,
                  objectName,
                  attrNames.length,
                  compAttribNames.size());
              try {
                ObjectName oName = new ObjectName(objectName);
                if (mbeanServer.isRegistered(oName)) {
                  AttributeList attrList = mbeanServer.getAttributes(oName, attrNames);
                  Map<String, JMXDataSource> dsMap = nodeInfo.getDsMap();
                  for (Object attribute : attrList) {
                    List<String> compositeMemberKeys = new ArrayList<String>();
                    Boolean isComposite = false;
                    Attribute attrib = (Attribute) attribute;
                    for (String compAttrName : compAttribNames) {
                      String[] attribKeys = compAttrName.split("\\|", -1);
                      if (attrib.getName().equals(attribKeys[0])) {
                        compositeMemberKeys.add(attribKeys[1]);
                        isComposite = true;
                      }
                    }
                    if (isComposite) {
                      try {
                        CompositeData cd = (CompositeData) attrib.getValue();
                        for (String key : compositeMemberKeys) {
                          /*
                          value = cd.get(key);

                          log.debug(" JMXCollector - got CompositeData: " +
                                    objectName + "|" + attrib.getName() + "|" + key + " |-> " + cd.get(key).toString());
                          */
                          JMXDataSource ds =
                              dsMap.get(objectName + "|" + attrib.getName() + "|" + key);
                          JMXCollectionAttributeType attribType =
                              new JMXCollectionAttributeType(ds, null, null, attribGroupType);
                          collectionResource.setAttributeValue(attribType, cd.get(key).toString());
                        }
                      } catch (final ClassCastException cce) {
                        LogUtils.debugf(
                            this,
                            cce,
                            "%s Collection - getAttributes (try CompositeData) - ERROR: Failed to cast attribute value to type CompositeData!",
                            serviceName);
                      }
                    } else {
                      // this is a normal attribute, so fallback to default handler
                      JMXDataSource ds = dsMap.get(objectName + "|" + attrib.getName());
                      JMXCollectionAttributeType attribType =
                          new JMXCollectionAttributeType(ds, null, null, attribGroupType);
                      collectionResource.setAttributeValue(
                          attribType, attrib.getValue().toString());
                    }
                  }
                }
              } catch (final InstanceNotFoundException e) {
                LogUtils.errorf(this, e, "Unable to retrieve attributes from %s", objectName);
              }
            } else {
              /*
               * This section is for ObjectNames that use the
               * '*' wildcard
               */
              Set<ObjectName> mbeanSet = getObjectNames(mbeanServer, objectName);
              for (Iterator<ObjectName> objectNameIter = mbeanSet.iterator();
                  objectNameIter.hasNext(); ) {
                ObjectName oName = objectNameIter.next();
                LogUtils.debugf(
                    this,
                    "%s Collector - getAttributesWC: %s, # attributes: %d, alias: %s",
                    serviceName,
                    oName,
                    attrNames.length,
                    beanInfo.getKeyAlias());

                try {
                  if (excludeList == null) {
                    // the exclude list doesn't apply
                    if (mbeanServer.isRegistered(oName)) {
                      AttributeList attrList = mbeanServer.getAttributes(oName, attrNames);
                      Map<String, JMXDataSource> dsMap = nodeInfo.getDsMap();

                      for (Object attribute : attrList) {
                        Attribute attrib = (Attribute) attribute;
                        JMXDataSource ds = dsMap.get(objectName + "|" + attrib.getName());
                        JMXCollectionAttributeType attribType =
                            new JMXCollectionAttributeType(
                                ds,
                                oName.getKeyProperty(beanInfo.getKeyField()),
                                beanInfo.getKeyAlias(),
                                attribGroupType);

                        collectionResource.setAttributeValue(
                            attribType, attrib.getValue().toString());
                      }
                    }
                  } else {
                    /*
                     * filter out calls if the key field
                     * matches an entry in the exclude
                     * list
                     */
                    String keyName = oName.getKeyProperty(beanInfo.getKeyField());
                    boolean found = false;
                    StringTokenizer st = new StringTokenizer(excludeList, ",");
                    while (st.hasMoreTokens()) {
                      if (keyName.equals(st.nextToken())) {
                        found = true;
                        break;
                      }
                    }
                    if (!found) {
                      if (mbeanServer.isRegistered(oName)) {
                        AttributeList attrList = mbeanServer.getAttributes(oName, attrNames);
                        Map<String, JMXDataSource> dsMap = nodeInfo.getDsMap();

                        for (Object attribute : attrList) {
                          Attribute attrib = (Attribute) attribute;
                          JMXDataSource ds = dsMap.get(objectName + "|" + attrib.getName());
                          JMXCollectionAttributeType attribType =
                              new JMXCollectionAttributeType(
                                  ds,
                                  oName.getKeyProperty(beanInfo.getKeyField()),
                                  beanInfo.getKeyAlias(),
                                  attribGroupType);

                          collectionResource.setAttributeValue(
                              attribType, attrib.getValue().toString());
                        }
                      }
                    }
                  }
                } catch (final InstanceNotFoundException e) {
                  LogUtils.errorf(this, e, "Error retrieving attributes for %s", oName);
                }
              }
            }
          }
          break;
        } catch (final Exception e) {
          LogUtils.debugf(
              this,
              e,
              "%s Collector.collect: IOException while collecting address: %s",
              serviceName,
              agent.getAddress());
        }
      }
    } catch (final Exception e) {
      LogUtils.errorf(this, e, "Error getting MBeanServer");
    } finally {
      if (connection != null) {
        connection.close();
      }
    }

    collectionSet.setStatus(ServiceCollector.COLLECTION_SUCCEEDED);
    return collectionSet;
  }
Пример #6
0
  private static boolean test(String proto, MBeanServer mbs, ObjectName on) throws Exception {
    System.out.println("Testing for protocol " + proto);

    JMXConnectorServer cs;
    JMXServiceURL url = new JMXServiceURL(proto, null, 0);
    try {
      cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    } catch (MalformedURLException e) {
      System.out.println("System does not recognize URL: " + url + "; ignoring");
      return true;
    }
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector client = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = client.getMBeanServerConnection();
    Object getAttributeExotic = mbsc.getAttribute(on, "Exotic");
    AttributeList getAttrs = mbsc.getAttributes(on, new String[] {"Exotic"});
    AttributeList setAttrs = new AttributeList();
    setAttrs.add(new Attribute("Exotic", new Exotic()));
    setAttrs = mbsc.setAttributes(on, setAttrs);
    Object invokeExotic = mbsc.invoke(on, "anExotic", new Object[] {}, new String[] {});
    MBeanInfo exoticMBI = mbsc.getMBeanInfo(on);

    mbsc.setAttribute(on, new Attribute("Exception", Boolean.TRUE));
    Exception getAttributeException, setAttributeException, invokeException;
    try {
      try {
        mbsc.getAttribute(on, "Exotic");
        throw noException("getAttribute");
      } catch (Exception e) {
        getAttributeException = e;
      }
      try {
        mbsc.setAttribute(on, new Attribute("Exotic", new Exotic()));
        throw noException("setAttribute");
      } catch (Exception e) {
        setAttributeException = e;
      }
      try {
        mbsc.invoke(on, "anExotic", new Object[] {}, new String[] {});
        throw noException("invoke");
      } catch (Exception e) {
        invokeException = e;
      }
    } finally {
      mbsc.setAttribute(on, new Attribute("Exception", Boolean.FALSE));
    }
    client.close();
    cs.stop();

    boolean ok = true;

    ok &= checkAttrs("getAttributes", getAttrs);
    ok &= checkAttrs("setAttributes", setAttrs);

    ok &= checkType("getAttribute", getAttributeExotic, Exotic.class);
    ok &= checkType("getAttributes", attrValue(getAttrs), Exotic.class);
    ok &= checkType("setAttributes", attrValue(setAttrs), Exotic.class);
    ok &= checkType("invoke", invokeExotic, Exotic.class);
    ok &= checkType("getMBeanInfo", exoticMBI, ExoticMBeanInfo.class);

    ok &= checkExceptionType("getAttribute", getAttributeException, ExoticException.class);
    ok &= checkExceptionType("setAttribute", setAttributeException, ExoticException.class);
    ok &= checkExceptionType("invoke", invokeException, ExoticException.class);

    if (ok) System.out.println("Test passes for protocol " + proto);
    return ok;
  }