Search in sources :

Example 36 with AttributeList

use of javax.management.AttributeList in project hadoop by apache.

the class MetricsLoggerTask method run.

/**
   * Write metrics to the metrics appender when invoked.
   */
@Override
public void run() {
    // Skip querying metrics if there are no known appenders.
    if (!metricsLog.isInfoEnabled() || !hasAppenders(metricsLog) || objectName == null) {
        return;
    }
    metricsLog.info(" >> Begin " + nodeName + " metrics dump");
    final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    // Iterate over each MBean.
    for (final ObjectName mbeanName : server.queryNames(objectName, null)) {
        try {
            MBeanInfo mBeanInfo = server.getMBeanInfo(mbeanName);
            final String mBeanNameName = MBeans.getMbeanNameName(mbeanName);
            final Set<String> attributeNames = getFilteredAttributes(mBeanInfo);
            final AttributeList attributes = server.getAttributes(mbeanName, attributeNames.toArray(new String[attributeNames.size()]));
            for (Object o : attributes) {
                final Attribute attribute = (Attribute) o;
                final Object value = attribute.getValue();
                final String valueStr = (value != null) ? value.toString() : "null";
                // Truncate the value if it is too long
                metricsLog.info(mBeanNameName + ":" + attribute.getName() + "=" + trimLine(valueStr));
            }
        } catch (Exception e) {
            metricsLog.error("Failed to get " + nodeName + " metrics for mbean " + mbeanName.toString(), e);
        }
    }
    metricsLog.info(" << End " + nodeName + " metrics dump");
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 37 with AttributeList

use of javax.management.AttributeList in project meteo by pierre.

the class JMXSubscriber method subscribe.

@Override
public void subscribe() {
    try {
        connect();
    } catch (IOException e) {
        // ignored
        return;
    }
    this.worker = Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(new Runnable() {

        @Override
        public void run() {
            final AttributeList attrList;
            try {
                attrList = mbeanConn.getAttributes(mbeanName, attrNames);
                final LinkedHashMap<String, Object> data = new LinkedHashMap<String, Object>();
                log.debug(String.format("Found %d data points", attrList.size()));
                for (Object attrObj : attrList) {
                    Attribute attr = (Attribute) attrObj;
                    data.put(attr.getName(), attr.getValue());
                }
                log.debug("Received a message, yay!\n" + data);
                esperSink.getEPRuntime().sendEvent(data, jmxConfig.getEventOutputName());
            } catch (InstanceNotFoundException ex) {
                log.error("Could not fetch from JMX", ex);
            } catch (ReflectionException ex) {
                log.error("Could not fetch from JMX", ex);
            } catch (IOException ex) {
                log.error("Could not fetch from JMX", ex);
            }
        }
    }, 0, jmxConfig.getPollIntervalInMillis(), TimeUnit.MILLISECONDS);
}
Also used : ReflectionException(javax.management.ReflectionException) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

Example 38 with AttributeList

use of javax.management.AttributeList in project CloudStack-archive by CloudStack-extras.

the class PropertyMapDynamicBean method setAttributes.

@Override
public synchronized AttributeList setAttributes(AttributeList list) {
    Attribute[] attrs = (Attribute[]) list.toArray(new Attribute[0]);
    AttributeList retList = new AttributeList();
    for (Attribute attr : attrs) {
        String name = attr.getName();
        Object value = attr.getValue();
        _propMap.put(name, value);
        retList.add(new Attribute(name, value));
    }
    return retList;
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList)

Example 39 with AttributeList

use of javax.management.AttributeList in project opennms by OpenNMS.

the class DefaultJmxCollector method getAttributes.

/**
     * Get a list of jmx Attributes from the JMX Server.
     * The returned list only contains available attributes.
     * The input list contains all attributes we would like to fetch.
     *
     * @return the list of attributes available at the JMX Server.
     */
private List<Attribute> getAttributes(MBeanServerConnection concreteConnection, ObjectName eachObjectName, List<String> attributes) throws InstanceNotFoundException, IOException, ReflectionException {
    AttributeList attributeList = concreteConnection.getAttributes(eachObjectName, attributes.toArray(new String[attributes.size()]));
    List<Attribute> newList = new ArrayList<>();
    for (Object eachObject : attributeList) {
        if (eachObject instanceof Attribute) {
            newList.add((Attribute) eachObject);
        }
    }
    return Collections.checkedList(newList, Attribute.class);
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) ArrayList(java.util.ArrayList)

Example 40 with AttributeList

use of javax.management.AttributeList in project intellij-community by JetBrains.

the class CpuTimings method getSystemCpuLoad.

public static double getSystemCpuLoad() {
    try {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
        AttributeList list = mbs.getAttributes(name, new String[] { "SystemCpuLoad" });
        if (list.isEmpty())
            return Double.NaN;
        Attribute att = (Attribute) list.get(0);
        Double value = (Double) att.getValue();
        // usually takes a couple of seconds before we get real values
        if (value == -1.0)
            return Double.NaN;
        // returns a percentage value with 1 decimal point precision
        return ((int) (value * 1000) / 10.0);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Aggregations

AttributeList (javax.management.AttributeList)56 Attribute (javax.management.Attribute)46 ReflectionException (javax.management.ReflectionException)22 AttributeNotFoundException (javax.management.AttributeNotFoundException)16 InstanceNotFoundException (javax.management.InstanceNotFoundException)16 MBeanException (javax.management.MBeanException)15 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)14 ObjectName (javax.management.ObjectName)14 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)10 MBeanServer (javax.management.MBeanServer)8 IOException (java.io.IOException)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 MBeanInfo (javax.management.MBeanInfo)7 RuntimeOperationsException (javax.management.RuntimeOperationsException)7 HashMap (java.util.HashMap)6 MalformedObjectNameException (javax.management.MalformedObjectNameException)6 ListenerNotFoundException (javax.management.ListenerNotFoundException)5 MBeanRegistrationException (javax.management.MBeanRegistrationException)5 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)5 ArrayList (java.util.ArrayList)4