fixed formatting (tabs to spaces, version from 1.4 to 1.5, general formatting)

added MBean for "LoadAverage"
This commit is contained in:
Bjoern Martin 2007-05-01 20:26:42 +00:00
parent 3471b84099
commit 6662294a7f
8 changed files with 1825 additions and 1569 deletions

View File

@ -43,230 +43,226 @@ import org.hyperic.sigar.SigarProxyCache;
* implement subset of them. * implement subset of them.
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.4 (2007-04) * @since 1.5
*/ */
public abstract class AbstractMBean implements DynamicMBean, MBeanRegistration { public abstract class AbstractMBean implements DynamicMBean, MBeanRegistration {
protected static final String MBEAN_ATTR_TYPE = "type"; protected static final String MBEAN_ATTR_TYPE = "type";
protected static final short CACHED_30SEC = 0; protected static final short CACHED_30SEC = 0;
protected static final short CACHED_5SEC = 1; protected static final short CACHED_5SEC = 1;
protected static final short CACHED_500MS = 2; protected static final short CACHED_500MS = 2;
protected static final short CACHELESS = 3; protected static final short CACHELESS = 3;
protected static final short DEFAULT = CACHED_30SEC; protected static final short DEFAULT = CACHED_30SEC;
/** /**
* The Sigar implementation to be used to fetch information from the system. * The Sigar implementation to be used to fetch information from the system.
*/ */
protected final Sigar sigarImpl; protected final Sigar sigarImpl;
/** /**
* The Sigar proxy cache to be used in case the data does not have to be * The Sigar proxy cache to be used in case the data does not have to be
* fetched during each call. The cache timeout is decided during * fetched during each call. The cache timeout is decided during
* construction. See {@link AbstractMBean#AbstractMBean(Sigar, short)} for * construction. See {@link AbstractMBean#AbstractMBean(Sigar, short)} for
* details. * details.
* *
* @see AbstractMBean#AbstractMBean(Sigar, short) * @see AbstractMBean#AbstractMBean(Sigar, short)
*/ */
protected final SigarProxy sigar; protected final SigarProxy sigar;
/** /**
* The MBean server this MBean is registered to. Set during the MBean's * The MBean server this MBean is registered to. Set during the MBean's
* registration to the MBean server and unset to <code>null</code> when * registration to the MBean server and unset to <code>null</code> when
* the deregistration finished. * the deregistration finished.
* *
* @see #preRegister(MBeanServer, ObjectName) * @see #preRegister(MBeanServer, ObjectName)
* @see #postDeregister() * @see #postDeregister()
*/ */
protected MBeanServer mbeanServer; protected MBeanServer mbeanServer;
/** /**
* <p>Creates a new instance of this class. The Sigar instance is stored (and * <p>Creates a new instance of this class. The Sigar instance is stored (and
* accessible) via the {@link #sigarImpl} member. A second instance is * accessible) via the {@link #sigarImpl} member. A second instance is
* stored within the {@link #sigar} member which is either {@link #sigarImpl} * stored within the {@link #sigar} member which is either {@link #sigarImpl}
* or an instance of {@link SigarProxyCache} with the expiration time set to * or an instance of {@link SigarProxyCache} with the expiration time set to
* whatever the <code>cacheMode</code> parameter specifies.</p> * whatever the <code>cacheMode</code> parameter specifies.</p>
* *
* <p>The following cache modes exist:</p> * <p>The following cache modes exist:</p>
* *
* <table border = "1"> * <table border = "1">
* <tr><td><b>Constant</b></td><td><b>Description</b></td></tr> * <tr><td><b>Constant</b></td><td><b>Description</b></td></tr>
* <tr><td>{@link #CACHELESS}</td><td>No cached instance, {@link #sigar} * <tr><td>{@link #CACHELESS}</td><td>No cached instance, {@link #sigar}
* <code>==</code> {@link #sigarImpl}.</td></tr> * <code>==</code> {@link #sigarImpl}.</td></tr>
* <tr><td>{@link #CACHED_500MS}</td><td>500 millisecond cache, for high * <tr><td>{@link #CACHED_500MS}</td><td>500 millisecond cache, for high
* frequency queries on raw data such as reading out CPU timers each * frequency queries on raw data such as reading out CPU timers each
* second. Avoids reading out multiple data sets when all attributes of * second. Avoids reading out multiple data sets when all attributes of
* an MBean are queried in short sequence.</td></tr> * an MBean are queried in short sequence.</td></tr>
* <tr><td>{@link #CACHED_5SEC}</td><td>5 second cache, for high frequency * <tr><td>{@link #CACHED_5SEC}</td><td>5 second cache, for high frequency
* queries on calculated data such as CPU percentages.</td></tr> * queries on calculated data such as CPU percentages.</td></tr>
* <tr><td>{@link #CACHED_30SEC}</td><td>30 second cache, for normal queries * <tr><td>{@link #CACHED_30SEC}</td><td>30 second cache, for normal queries
* or data readouts such as CPU model / vendor. This is the default if * or data readouts such as CPU model / vendor. This is the default if
* nothing (<code>0</code>) is specified.</td></tr> * nothing (<code>0</code>) is specified.</td></tr>
* <tr><td>{@link #DEFAULT}</td><td>Same as {@link #CACHED_30SEC}.</td></tr> * <tr><td>{@link #DEFAULT}</td><td>Same as {@link #CACHED_30SEC}.</td></tr>
* </table> * </table>
* *
* <p><b>Note:</b> Only make use of the cacheless or half second mode if you * <p><b>Note:</b> Only make use of the cacheless or half second mode if you
* know what you are doing. They may have impact on system performance if * know what you are doing. They may have impact on system performance if
* used excessively.</p> * used excessively.</p>
* *
* @param sigar The Sigar impl to use. Must not be <code>null</code> * @param sigar The Sigar impl to use. Must not be <code>null</code>
* @param cacheMode The cache mode to use for {@link #sigar} or {@link #CACHELESS} * @param cacheMode The cache mode to use for {@link #sigar} or {@link #CACHELESS}
* if no separate, cached instance is to be maintained. * if no separate, cached instance is to be maintained.
*/ */
protected AbstractMBean(Sigar sigar, short cacheMode) { protected AbstractMBean(Sigar sigar, short cacheMode) {
// store Sigar // store Sigar
this.sigarImpl = sigar; this.sigarImpl = sigar;
// create a cached instance as well
if (cacheMode == CACHELESS) {
// no cached version
this.sigar = this.sigarImpl;
} else if (cacheMode == CACHED_500MS) {
// 500ms cached version (for 1/sec queries)
this.sigar = SigarProxyCache.newInstance(this.sigarImpl,
500);
} else if (cacheMode == CACHED_5SEC) {
// 5sec cached version (for avg'd queries)
this.sigar = SigarProxyCache.newInstance(this.sigarImpl,
5000);
} else /* if (cacheMode == CACHED_30SEC) */ {
// 30sec (default) cached version (for info and long term queries)
this.sigar = SigarProxyCache.newInstance(this.sigarImpl,
30000);
}
}
/** // create a cached instance as well
* Returns the object name the MBean is registered with within the if (cacheMode == CACHELESS) {
* MBeanServer. May be <code>null</code> in case the instance is not // no cached version
* registered to an MBeanServer, but used standalone. this.sigar = this.sigarImpl;
*
* @return The object name or <code>null</code> if not registered to an
* MBeanServer
*/
public abstract String getObjectName();
/** } else if (cacheMode == CACHED_500MS) {
* Returns a runtime exception for the type and SigarException specified. // 500ms cached version (for 1/sec queries)
* this.sigar = SigarProxyCache.newInstance(this.sigarImpl, 500);
* @param type
* The type that was called
* @param e
* The exception that was raised
* @return A runtime exception encapsulating the information specified
*/
protected RuntimeException unexpectedError(String type, SigarException e) {
String msg = "Unexected error in Sigar.get" + type + ": "
+ e.getMessage();
return new IllegalArgumentException(msg);
}
/** } else if (cacheMode == CACHED_5SEC) {
* Loops over all attributes and calls // 5sec cached version (for avg'd queries)
* {@link DynamicMBean#getAttribute(java.lang.String)} method for each this.sigar = SigarProxyCache.newInstance(this.sigarImpl, 5000);
* attribute sequentially. Any exception thrown by those methods are ignored
* and simply cause the attribute not being added to the result.
*/
public AttributeList getAttributes(String[] attrs) {
final AttributeList result = new AttributeList();
for (int i = 0; i < attrs.length; i++) {
try {
result.add(new Attribute(attrs[i], getAttribute(attrs[i])));
} catch (AttributeNotFoundException e) {
// ignore, as we cannot throw this exception
} catch (MBeanException e) {
// ignore, as we cannot throw this exception
} catch (ReflectionException e) {
// ignore, as we cannot throw this exception
}
}
return result;
}
/** } else /* if (cacheMode == CACHED_30SEC) */{
* Loops over all attributes and calls // 30sec (default) cached version (for info and long term queries)
* {@link DynamicMBean#setAttribute(Attribute)} for each attribute this.sigar = SigarProxyCache.newInstance(this.sigarImpl, 30000);
* sequentially. Any exception thrown by those methods are ignored and }
* simply cause the attribute not being added to the result. }
*/
public AttributeList setAttributes(AttributeList attrs) {
final AttributeList result = new AttributeList();
for (int i = 0; i < attrs.size(); i++) {
try {
final Attribute next = (Attribute) attrs.get(i);
setAttribute(next);
result.add(next);
} catch (AttributeNotFoundException e) {
// ignore, as we cannot throw this exception
} catch (InvalidAttributeValueException e) {
// ignore, as we cannot throw this exception
} catch (MBeanException e) {
// ignore, as we cannot throw this exception
} catch (ReflectionException e) {
// ignore, as we cannot throw this exception
}
}
return result;
}
/**
* Returns the object name the MBean is registered with within the
* MBeanServer. May be <code>null</code> in case the instance is not
* registered to an MBeanServer, but used standalone.
*
* @return The object name or <code>null</code> if not registered to an
* MBeanServer
*/
public abstract String getObjectName();
/**
// ------- * Returns a runtime exception for the type and SigarException specified.
// Implementation of the MBeanRegistration interface *
// ------- * @param type
* The type that was called
/** * @param e
* <p>Returns <code>new ObjectName(this.getObjectName())</code> to guarantee * The exception that was raised
* a reliable and reproducable object name.</p> * @return A runtime exception encapsulating the information specified
* */
* <p><b>Note:</b> Make sure any subclass does a super call to this method, protected RuntimeException unexpectedError(String type, SigarException e) {
* otherwise the implementation might be broken.</p> String msg = "Unexected error in Sigar.get" + type + ": "
* + e.getMessage();
* @see MBeanRegistration#preRegister(MBeanServer, ObjectName) return new IllegalArgumentException(msg);
*/ }
public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception {
this.mbeanServer = server;
return new ObjectName(getObjectName());
}
/** /**
* Empty implementation, allowing aubclasses to ignore the interface. * Loops over all attributes and calls
* * {@link DynamicMBean#getAttribute(java.lang.String)} method for each
* <p><b>Note:</b> Make sure any subclass does a super call to this method, * attribute sequentially. Any exception thrown by those methods are ignored
* otherwise the implementation might be broken.</p> * and simply cause the attribute not being added to the result.
* */
* @see MBeanRegistration#postRegister(Boolean) public AttributeList getAttributes(String[] attrs) {
*/ final AttributeList result = new AttributeList();
public void postRegister(Boolean success) { for (int i = 0; i < attrs.length; i++) {
} try {
result.add(new Attribute(attrs[i], getAttribute(attrs[i])));
} catch (AttributeNotFoundException e) {
// ignore, as we cannot throw this exception
} catch (MBeanException e) {
// ignore, as we cannot throw this exception
} catch (ReflectionException e) {
// ignore, as we cannot throw this exception
}
}
return result;
}
/** /**
* Empty implementation, allowing aubclasses to ignore the interface. * Loops over all attributes and calls
* * {@link DynamicMBean#setAttribute(Attribute)} for each attribute
* <p><b>Note:</b> Make sure any subclass does a super call to this method, * sequentially. Any exception thrown by those methods are ignored and
* otherwise the implementation might be broken.</p> * simply cause the attribute not being added to the result.
* */
* @see MBeanRegistration#preDeregister() public AttributeList setAttributes(AttributeList attrs) {
*/ final AttributeList result = new AttributeList();
public void preDeregister() throws Exception { for (int i = 0; i < attrs.size(); i++) {
} try {
final Attribute next = (Attribute) attrs.get(i);
setAttribute(next);
result.add(next);
} catch (AttributeNotFoundException e) {
// ignore, as we cannot throw this exception
} catch (InvalidAttributeValueException e) {
// ignore, as we cannot throw this exception
} catch (MBeanException e) {
// ignore, as we cannot throw this exception
} catch (ReflectionException e) {
// ignore, as we cannot throw this exception
}
}
return result;
}
/** // -------
* Empty implementation, allowing aubclasses to ignore the interface. // Implementation of the MBeanRegistration interface
* // -------
* <p><b>Note:</b> Make sure any subclass does a super call to this method,
* otherwise the implementation might be broken.</p> /**
* * <p>Returns <code>new ObjectName(this.getObjectName())</code> to guarantee
* @see MBeanRegistration#postDeregister() * a reliable and reproducable object name.</p>
*/ *
public void postDeregister() { * <p><b>Note:</b> Make sure any subclass does a super call to this method,
this.mbeanServer = null; * otherwise the implementation might be broken.</p>
} *
* @see MBeanRegistration#preRegister(MBeanServer, ObjectName)
*/
public ObjectName preRegister(MBeanServer server, ObjectName name)
throws Exception {
this.mbeanServer = server;
return new ObjectName(getObjectName());
}
/**
* Empty implementation, allowing aubclasses to ignore the interface.
*
* <p><b>Note:</b> Make sure any subclass does a super call to this method,
* otherwise the implementation might be broken.</p>
*
* @see MBeanRegistration#postRegister(Boolean)
*/
public void postRegister(Boolean success) {
}
/**
* Empty implementation, allowing aubclasses to ignore the interface.
*
* <p><b>Note:</b> Make sure any subclass does a super call to this method,
* otherwise the implementation might be broken.</p>
*
* @see MBeanRegistration#preDeregister()
*/
public void preDeregister() throws Exception {
}
/**
* Empty implementation, allowing aubclasses to ignore the interface.
*
* <p><b>Note:</b> Make sure any subclass does a super call to this method,
* otherwise the implementation might be broken.</p>
*
* @see MBeanRegistration#postDeregister()
*/
public void postDeregister() {
this.mbeanServer = null;
}
} }

View File

@ -30,314 +30,305 @@ import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
/** /**
* Sigar JMX MBean implementation for the <code>Cpu</code> information package. * Sigar JMX MBean implementation for the <code>Cpu</code> information
* Provides an OpenMBean conform implementation. * package. Provides an OpenMBean conform implementation.
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.4 (2007-04) * @since 1.5
*/ */
public class SigarCpu extends AbstractMBean { public class SigarCpu extends AbstractMBean {
private static final String MBEAN_TYPE = "CpuList"; private static final String MBEAN_TYPE = "CpuList";
private static final MBeanInfo MBEAN_INFO; private static final MBeanInfo MBEAN_INFO;
private static final MBeanAttributeInfo MBEAN_ATTR_CPUINDEX;
private static final MBeanAttributeInfo MBEAN_ATTR_IDLE; private static final MBeanAttributeInfo MBEAN_ATTR_CPUINDEX;
private static final MBeanAttributeInfo MBEAN_ATTR_NICE; private static final MBeanAttributeInfo MBEAN_ATTR_IDLE;
private static final MBeanAttributeInfo MBEAN_ATTR_SYS; private static final MBeanAttributeInfo MBEAN_ATTR_NICE;
private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL; private static final MBeanAttributeInfo MBEAN_ATTR_SYS;
private static final MBeanAttributeInfo MBEAN_ATTR_USER; private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL;
private static final MBeanAttributeInfo MBEAN_ATTR_WAIT; private static final MBeanAttributeInfo MBEAN_ATTR_USER;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX; private static final MBeanAttributeInfo MBEAN_ATTR_WAIT;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX;
private static MBeanParameterInfo MBEAN_PARAM_CPUINDEX; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR;
private static MBeanParameterInfo MBEAN_PARAM_SIGAR; private static MBeanParameterInfo MBEAN_PARAM_CPUINDEX;
static {
MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo(
"CpuIndex", "int",
"The index of the CPU, typically starting at 0",
true, false, false);
MBEAN_ATTR_IDLE = new MBeanAttributeInfo(
"Idle", "long",
"The idle time of the CPU, in [ms]",
true, false, false);
MBEAN_ATTR_NICE = new MBeanAttributeInfo(
"Nice", "long",
"The time of the CPU spent on nice priority, in [ms]",
true, false, false);
MBEAN_ATTR_SYS = new MBeanAttributeInfo(
"Sys", "long",
"The time of the CPU used by the system, in [ms]",
true, false, false);
MBEAN_ATTR_TOTAL = new MBeanAttributeInfo(
"Total", "long",
"The total time of the CPU, in [ms]",
true, false, false);
MBEAN_ATTR_USER = new MBeanAttributeInfo(
"User", "long",
"The time of the CPU used by user processes, in [ms]",
true, false, false);
MBEAN_ATTR_WAIT = new MBeanAttributeInfo(
"Wait", "long",
"The time the CPU had to wait for data to be loaded, in [ms]",
true, false, false);
MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo(
"cpuIndex", "int",
"The index of the CPU to read data for. Must be >= 0 " +
"and not exceed the CPU count of the system");
MBEAN_PARAM_SIGAR = new MBeanParameterInfo(
"sigar", Sigar.class.getName(),
"The Sigar instance to use to fetch data from");
MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(
SigarCpu.class.getName(),
"Creates a new instance for the CPU index specified, " +
"using a new Sigar instance to fetch the data. " +
"Fails if the CPU index is out of range.",
new MBeanParameterInfo[]{
MBEAN_PARAM_CPUINDEX});
MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo(
SigarCpu.class.getName(),
"Creates a new instance for the CPU index specified, " +
"using the Sigar instance specified to fetch the data. " +
"Fails if the CPU index is out of range.",
new MBeanParameterInfo[]{
MBEAN_PARAM_SIGAR,
MBEAN_PARAM_CPUINDEX});
MBEAN_INFO = new MBeanInfo(
SigarCpu.class.getName(),
"Sigar CPU MBean. Provides raw timing data for a single " +
"CPU. The data is cached for 500ms, meaning each request " +
"(and as a result each block request to all parameters) " +
"within half a second is satisfied from the same dataset.",
new MBeanAttributeInfo[]{
MBEAN_ATTR_CPUINDEX,
MBEAN_ATTR_IDLE,
MBEAN_ATTR_NICE,
MBEAN_ATTR_SYS,
MBEAN_ATTR_TOTAL,
MBEAN_ATTR_USER,
MBEAN_ATTR_WAIT},
new MBeanConstructorInfo[]{
MBEAN_CONSTR_CPUINDEX,
MBEAN_CONSTR_CPUINDEX_SIGAR},
null, null);
}
/** private static MBeanParameterInfo MBEAN_PARAM_SIGAR;
* Index of the CPU processed by the instance.
*/
private final int cpuIndex;
/** static {
* Object name this instance will give itself when being registered to an MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo("CpuIndex", "int",
* MBeanServer. "The index of the CPU, typically starting at 0", true, false,
*/ false);
private final String objectName; MBEAN_ATTR_IDLE = new MBeanAttributeInfo("Idle", "long",
"The idle time of the CPU, in [ms]", true, false, false);
MBEAN_ATTR_NICE = new MBeanAttributeInfo("Nice", "long",
"The time of the CPU spent on nice priority, in [ms]", true,
false, false);
MBEAN_ATTR_SYS = new MBeanAttributeInfo("Sys", "long",
"The time of the CPU used by the system, in [ms]", true, false,
false);
MBEAN_ATTR_TOTAL = new MBeanAttributeInfo("Total", "long",
"The total time of the CPU, in [ms]", true, false, false);
MBEAN_ATTR_USER = new MBeanAttributeInfo("User", "long",
"The time of the CPU used by user processes, in [ms]", true,
false, false);
MBEAN_ATTR_WAIT = new MBeanAttributeInfo("Wait", "long",
"The time the CPU had to wait for data to be loaded, in [ms]",
true, false, false);
MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo("cpuIndex", "int",
"The index of the CPU to read data for. Must be >= 0 "
+ "and not exceed the CPU count of the system");
MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class
.getName(), "The Sigar instance to use to fetch data from");
MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(SigarCpu.class
.getName(),
"Creates a new instance for the CPU index specified, "
+ "using a new Sigar instance to fetch the data. "
+ "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_CPUINDEX });
MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo(
SigarCpu.class.getName(),
"Creates a new instance for the CPU index specified, "
+ "using the Sigar instance specified to fetch the data. "
+ "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR,
MBEAN_PARAM_CPUINDEX });
MBEAN_INFO = new MBeanInfo(
SigarCpu.class.getName(),
"Sigar CPU MBean. Provides raw timing data for a single "
+ "CPU. The data is cached for 500ms, meaning each request "
+ "(and as a result each block request to all parameters) "
+ "within half a second is satisfied from the same dataset.",
new MBeanAttributeInfo[] { MBEAN_ATTR_CPUINDEX,
MBEAN_ATTR_IDLE, MBEAN_ATTR_NICE, MBEAN_ATTR_SYS,
MBEAN_ATTR_TOTAL, MBEAN_ATTR_USER, MBEAN_ATTR_WAIT },
new MBeanConstructorInfo[] { MBEAN_CONSTR_CPUINDEX,
MBEAN_CONSTR_CPUINDEX_SIGAR }, null, null);
/** }
* Creates a new instance for the CPU index specified, using a new Sigar
* instance to fetch the data. Fails if the CPU index is out of range.
*
* @param cpuIndex The index of the CPU to read data for. Must be
* <code>&gt;= 0</code> and not exceed the CPU count of the system.
*
* @throws IllegalArgumentException If the CPU index is out of range or
* an unexpected Sigar error occurs
*/
public SigarCpu(int cpuIndex) throws IllegalArgumentException {
this(new Sigar(), cpuIndex);
}
/** /**
* Creates a new instance for the CPU index specified, using the Sigar * Index of the CPU processed by the instance.
* instance specified to fetch the data. Fails if the CPU index is out */
* of range. private final int cpuIndex;
*
* @param sigar The Sigar instance to use to fetch data from
* @param cpuIndex The index of the CPU to read data for. Must be
* <code>&gt;= 0</code> and not exceed the CPU count of the system.
*
* @throws IllegalArgumentException If the CPU index is out of range or
* an unexpected Sigar error occurs
*/
public SigarCpu(Sigar sigar, int cpuIndex) throws IllegalArgumentException {
super(sigar, CACHED_500MS);
// check index /**
if (cpuIndex < 0) * Object name this instance will give itself when being registered to an
throw new IllegalArgumentException( * MBeanServer.
"CPU index has to be non-negative: " + cpuIndex); */
try { private final String objectName;
int cpuCount;
if ((cpuCount = sigar.getCpuList().length) < cpuIndex)
throw new IllegalArgumentException(
"CPU index out of range (found " + cpuCount + " CPU(s)): " + cpuIndex);
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
// all fine
this.cpuIndex = cpuIndex;
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":"
+ MBEAN_ATTR_TYPE + "=Cpu,"
+ MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase()
+ MBEAN_ATTR_CPUINDEX.getName().substring(1) + "=" + cpuIndex;
}
/** /**
* Object name this instance will give itself when being registered to an * Creates a new instance for the CPU index specified, using a new Sigar
* MBeanServer. * instance to fetch the data. Fails if the CPU index is out of range.
*/ *
public String getObjectName() { * @param cpuIndex
return this.objectName; * The index of the CPU to read data for. Must be <code>&gt;= 0</code>
} * and not exceed the CPU count of the system.
*
/** * @throws IllegalArgumentException
* @return The index of the CPU, typically starting at 0 * If the CPU index is out of range or an unexpected Sigar error
*/ * occurs.
public int getCpuIndex() { */
return this.cpuIndex; public SigarCpu(int cpuIndex) throws IllegalArgumentException {
} this(new Sigar(), cpuIndex);
}
/**
* @return The idle time of the CPU, in [ms]
*/
public long getIdle() {
try {
return sigar.getCpuList()[this.cpuIndex].getIdle();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The time of the CPU spent on nice priority, in [ms]
*/
public long getNice() {
try {
return sigar.getCpuList()[this.cpuIndex].getNice();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The time of the CPU used by the system, in [ms]
*/
public long getSys() {
try {
return sigar.getCpuList()[this.cpuIndex].getSys();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The total time of the CPU, in [ms]
*/
public long getTotal() {
try {
return sigar.getCpuList()[this.cpuIndex].getTotal();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The time of the CPU used by user processes, in [ms]
*/
public long getUser() {
try {
return sigar.getCpuList()[this.cpuIndex].getUser();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The time the CPU had to wait for data to be loaded, in [ms]
*/
public long getWait() {
try {
return sigar.getCpuList()[this.cpuIndex].getWait();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* Creates a new instance for the CPU index specified, using the Sigar
// ------- * instance specified to fetch the data. Fails if the CPU index is out of
// Implementation of the DynamicMBean interface * range.
// ------- *
* @param sigar
/* * The Sigar instance to use to fetch data from
* (non-Javadoc) * @param cpuIndex
* @see DynamicMBean#getAttribute(String) * The index of the CPU to read data for. Must be
*/ * <code>&gt;= 0</code> and not exceed the CPU count of the
public Object getAttribute(String attr) throws AttributeNotFoundException { * system.
*
if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) { * @throws IllegalArgumentException
return new Integer(getCpuIndex()); * If the CPU index is out of range or an unexpected Sigar error
* occurs
} else if (MBEAN_ATTR_IDLE.getName().equals(attr)) { */
return new Long(getIdle()); public SigarCpu(Sigar sigar, int cpuIndex) throws IllegalArgumentException {
super(sigar, CACHED_500MS);
} else if (MBEAN_ATTR_NICE.getName().equals(attr)) {
return new Long(getNice());
} else if (MBEAN_ATTR_SYS.getName().equals(attr)) {
return new Long(getSys());
} else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) {
return new Long(getTotal());
} else if (MBEAN_ATTR_USER.getName().equals(attr)) {
return new Long(getUser());
} else if (MBEAN_ATTR_WAIT.getName().equals(attr)) {
return new Long(getWait());
} else {
throw new AttributeNotFoundException(attr);
}
}
/* // check index
* (non-Javadoc) if (cpuIndex < 0)
* @see DynamicMBean#setAttribute(Attribute) throw new IllegalArgumentException(
*/ "CPU index has to be non-negative: " + cpuIndex);
public void setAttribute(Attribute attr) throws AttributeNotFoundException { try {
throw new AttributeNotFoundException(attr.getName()); int cpuCount;
} if ((cpuCount = sigar.getCpuList().length) < cpuIndex)
throw new IllegalArgumentException(
"CPU index out of range (found " + cpuCount
+ " CPU(s)): " + cpuIndex);
/* } catch (SigarException e) {
* (non-Javadoc) throw unexpectedError(MBEAN_TYPE, e);
* @see DynamicMBean#invoke(String, Object[], String[]) }
*/
public Object invoke(String actionName, Object[] params, String[] signature) throws ReflectionException { // all fine
throw new ReflectionException(new NoSuchMethodException(actionName), actionName); this.cpuIndex = cpuIndex;
} this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
+ "=Cpu,"
/* + MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase()
* (non-Javadoc) + MBEAN_ATTR_CPUINDEX.getName().substring(1) + "=" + cpuIndex;
* @see DynamicMBean#getMBeanInfo() }
*/
public MBeanInfo getMBeanInfo() { /**
return MBEAN_INFO; * Object name this instance will give itself when being registered to an
} * MBeanServer.
*/
public String getObjectName() {
return this.objectName;
}
/**
* @return The index of the CPU, typically starting at 0
*/
public int getCpuIndex() {
return this.cpuIndex;
}
/**
* @return The idle time of the CPU, in [ms]
*/
public long getIdle() {
try {
return sigar.getCpuList()[this.cpuIndex].getIdle();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The time of the CPU spent on nice priority, in [ms]
*/
public long getNice() {
try {
return sigar.getCpuList()[this.cpuIndex].getNice();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The time of the CPU used by the system, in [ms]
*/
public long getSys() {
try {
return sigar.getCpuList()[this.cpuIndex].getSys();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The total time of the CPU, in [ms]
*/
public long getTotal() {
try {
return sigar.getCpuList()[this.cpuIndex].getTotal();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The time of the CPU used by user processes, in [ms]
*/
public long getUser() {
try {
return sigar.getCpuList()[this.cpuIndex].getUser();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The time the CPU had to wait for data to be loaded, in [ms]
*/
public long getWait() {
try {
return sigar.getCpuList()[this.cpuIndex].getWait();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
// -------
// Implementation of the DynamicMBean interface
// -------
/*
* (non-Javadoc)
*
* @see DynamicMBean#getAttribute(String)
*/
public Object getAttribute(String attr) throws AttributeNotFoundException {
if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) {
return new Integer(getCpuIndex());
} else if (MBEAN_ATTR_IDLE.getName().equals(attr)) {
return new Long(getIdle());
} else if (MBEAN_ATTR_NICE.getName().equals(attr)) {
return new Long(getNice());
} else if (MBEAN_ATTR_SYS.getName().equals(attr)) {
return new Long(getSys());
} else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) {
return new Long(getTotal());
} else if (MBEAN_ATTR_USER.getName().equals(attr)) {
return new Long(getUser());
} else if (MBEAN_ATTR_WAIT.getName().equals(attr)) {
return new Long(getWait());
} else {
throw new AttributeNotFoundException(attr);
}
}
/*
* (non-Javadoc)
*
* @see DynamicMBean#setAttribute(Attribute)
*/
public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName());
}
/*
* (non-Javadoc)
*
* @see DynamicMBean#invoke(String, Object[], String[])
*/
public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName),
actionName);
}
/*
* (non-Javadoc)
*
* @see DynamicMBean#getMBeanInfo()
*/
public MBeanInfo getMBeanInfo() {
return MBEAN_INFO;
}
} }

View File

@ -34,269 +34,253 @@ import org.hyperic.sigar.SigarException;
* package. Provides an OpenMBean conform implementation. * package. Provides an OpenMBean conform implementation.
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.4 (2007-04) * @since 1.5
*/ */
public class SigarCpuInfo extends AbstractMBean { public class SigarCpuInfo extends AbstractMBean {
private static final String MBEAN_TYPE = "CpuInfoList"; private static final String MBEAN_TYPE = "CpuInfoList";
private static final MBeanInfo MBEAN_INFO; private static final MBeanInfo MBEAN_INFO;
private static final MBeanAttributeInfo MBEAN_ATTR_CPUINDEX;
private static final MBeanAttributeInfo MBEAN_ATTR_CACHESIZE; private static final MBeanAttributeInfo MBEAN_ATTR_CPUINDEX;
private static final MBeanAttributeInfo MBEAN_ATTR_MHZ; private static final MBeanAttributeInfo MBEAN_ATTR_CACHESIZE;
private static final MBeanAttributeInfo MBEAN_ATTR_MODEL; private static final MBeanAttributeInfo MBEAN_ATTR_MHZ;
private static final MBeanAttributeInfo MBEAN_ATTR_VENDOR; private static final MBeanAttributeInfo MBEAN_ATTR_MODEL;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX; private static final MBeanAttributeInfo MBEAN_ATTR_VENDOR;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX;
private static final MBeanParameterInfo MBEAN_PARAM_CPUINDEX; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR;
private static final MBeanParameterInfo MBEAN_PARAM_SIGAR; private static final MBeanParameterInfo MBEAN_PARAM_CPUINDEX;
static {
MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo(
"CpuIndex", "int",
"The index of the CPU, typically starting at 0",
true, false, false);
MBEAN_ATTR_CACHESIZE = new MBeanAttributeInfo(
"CacheSize", "long",
"The cache size of the CPU, in [byte]",
true, false, false);
MBEAN_ATTR_MHZ = new MBeanAttributeInfo(
"Mhz", "int",
"The clock speed of the CPU, in [MHz]",
true, false, false);
MBEAN_ATTR_MODEL = new MBeanAttributeInfo(
"Model", "java.lang.String",
"The CPU model reported",
true, false, false);
MBEAN_ATTR_VENDOR = new MBeanAttributeInfo(
"Vendor", "java.lang.String",
"The CPU vendor reported",
true, false, false);
MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo(
"cpuIndex", "int",
"The index of the CPU to read data for. Must be >= 0 " +
"and not exceed the CPU count of the system");
MBEAN_PARAM_SIGAR = new MBeanParameterInfo(
"sigar", Sigar.class.getName(),
"The Sigar instance to use to fetch data from");
MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(
SigarCpuInfo.class.getName(),
"Creates a new instance for the CPU index specified, " +
"using a new Sigar instance to fetch the data. " +
"Fails if the CPU index is out of range.",
new MBeanParameterInfo[]{
MBEAN_PARAM_CPUINDEX});
MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo(
SigarCpuInfo.class.getName(),
"Creates a new instance for the CPU index specified, " +
"using the Sigar instance specified to fetch the data. " +
"Fails if the CPU index is out of range.",
new MBeanParameterInfo[]{
MBEAN_PARAM_SIGAR,
MBEAN_PARAM_CPUINDEX});
MBEAN_INFO = new MBeanInfo(
SigarCpuInfo.class.getName(),
"Sigar CPU Info MBean, provides overall information for a " +
"single CPU. This information only changes if, for example, " +
"a CPU is reducing its clock frequency or shutting down " +
"part of its cache. Subsequent requests are satisfied from " +
"within a cache that invalidates after 30 seconds.",
new MBeanAttributeInfo[]{
MBEAN_ATTR_CPUINDEX,
MBEAN_ATTR_CACHESIZE,
MBEAN_ATTR_MHZ,
MBEAN_ATTR_MODEL,
MBEAN_ATTR_VENDOR},
new MBeanConstructorInfo[]{
MBEAN_CONSTR_CPUINDEX,
MBEAN_CONSTR_CPUINDEX_SIGAR},
null, null);
}
/**
* Index of the CPU processed by the instance.
*/
private int cpuIndex;
/** private static final MBeanParameterInfo MBEAN_PARAM_SIGAR;
* Object name this instance will give itself when being registered to an
* MBeanServer.
*/
private String objectName;
/** static {
* Creates a new instance for the CPU index specified, using a new Sigar MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo("CpuIndex", "int",
* instance to fetch the data. Fails if the CPU index is out of range. "The index of the CPU, typically starting at 0", true, false,
* false);
* @param cpuIndex The index of the CPU to read data for. Must be MBEAN_ATTR_CACHESIZE = new MBeanAttributeInfo("CacheSize", "long",
* <code>&gt;= 0</code> and not exceed the CPU count of the system. "The cache size of the CPU, in [byte]", true, false, false);
* MBEAN_ATTR_MHZ = new MBeanAttributeInfo("Mhz", "int",
* @throws IllegalArgumentException If the CPU index is out of range or "The clock speed of the CPU, in [MHz]", true, false, false);
* an unexpected Sigar error occurs MBEAN_ATTR_MODEL = new MBeanAttributeInfo("Model", "java.lang.String",
*/ "The CPU model reported", true, false, false);
public SigarCpuInfo(int index) throws IllegalArgumentException { MBEAN_ATTR_VENDOR = new MBeanAttributeInfo("Vendor",
this(new Sigar(), index); "java.lang.String", "The CPU vendor reported", true, false,
} false);
MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo("cpuIndex", "int",
"The index of the CPU to read data for. Must be >= 0 "
+ "and not exceed the CPU count of the system");
MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class
.getName(), "The Sigar instance to use to fetch data from");
MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(SigarCpuInfo.class
.getName(),
"Creates a new instance for the CPU index specified, "
+ "using a new Sigar instance to fetch the data. "
+ "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_CPUINDEX });
MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo(
SigarCpuInfo.class.getName(),
"Creates a new instance for the CPU index specified, "
+ "using the Sigar instance specified to fetch the data. "
+ "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR,
MBEAN_PARAM_CPUINDEX });
MBEAN_INFO = new MBeanInfo(
SigarCpuInfo.class.getName(),
"Sigar CPU Info MBean, provides overall information for a "
+ "single CPU. This information only changes if, for example, "
+ "a CPU is reducing its clock frequency or shutting down "
+ "part of its cache. Subsequent requests are satisfied from "
+ "within a cache that invalidates after 30 seconds.",
new MBeanAttributeInfo[] { MBEAN_ATTR_CPUINDEX,
MBEAN_ATTR_CACHESIZE, MBEAN_ATTR_MHZ, MBEAN_ATTR_MODEL,
MBEAN_ATTR_VENDOR }, new MBeanConstructorInfo[] {
MBEAN_CONSTR_CPUINDEX, MBEAN_CONSTR_CPUINDEX_SIGAR },
null, null);
/** }
* Creates a new instance for the CPU index specified, using the Sigar
* instance specified to fetch the data. Fails if the CPU index is out
* of range.
*
* @param sigar The Sigar instance to use to fetch data from
* @param cpuIndex The index of the CPU to read data for. Must be
* <code>&gt;= 0</code> and not exceed the CPU count of the system.
*
* @throws IllegalArgumentException If the CPU index is out of range or
* an unexpected Sigar error occurs
*/
public SigarCpuInfo(Sigar sigar, int index) {
super(sigar, DEFAULT);
// check index /**
if (index < 0) * Index of the CPU processed by the instance.
throw new IllegalArgumentException( */
"CPU index has to be non-negative: " + index); private int cpuIndex;
try {
int cpuCount;
if ((cpuCount = sigar.getCpuInfoList().length) < index)
throw new IllegalArgumentException(
"CPU index out of range (found " + cpuCount + " CPU(s)): " + index);
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
// all fine
this.cpuIndex = index;
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":"
+ MBEAN_ATTR_TYPE + "=CpuInfo,"
+ MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase()
+ MBEAN_ATTR_CPUINDEX.getName().substring(1) + "=" + cpuIndex;
}
/** /**
* Object name this instance will give itself when being registered to an * Object name this instance will give itself when being registered to an
* MBeanServer. * MBeanServer.
*/ */
public String getObjectName() { private String objectName;
return this.objectName;
}
/**
* @return The index of the CPU, typically starting at 0
*/
public int getCpuIndex() {
return this.cpuIndex;
}
/**
* @return The cache size of the CPU, in [byte]
*/
public long getCacheSize() {
try {
return sigar.getCpuInfoList()[this.cpuIndex].getCacheSize();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/** /**
* @return The clock speed of the CPU, in [MHz] * Creates a new instance for the CPU index specified, using a new Sigar
*/ * instance to fetch the data. Fails if the CPU index is out of range.
public int getMhz() { *
try { * @param cpuIndex The index of the CPU to read data for. Must be
return sigar.getCpuInfoList()[this.cpuIndex].getMhz(); * <code>&gt;= 0</code> and not exceed the CPU count of the system.
} catch (SigarException e) { *
throw unexpectedError(MBEAN_TYPE, e); * @throws IllegalArgumentException If the CPU index is out of range or
} * an unexpected Sigar error occurs
} */
public SigarCpuInfo(int index) throws IllegalArgumentException {
this(new Sigar(), index);
}
/** /**
* @return The CPU model reported * Creates a new instance for the CPU index specified, using the Sigar
*/ * instance specified to fetch the data. Fails if the CPU index is out
public String getModel() { * of range.
try { *
return sigar.getCpuInfoList()[this.cpuIndex].getModel(); * @param sigar The Sigar instance to use to fetch data from
} catch (SigarException e) { * @param cpuIndex The index of the CPU to read data for. Must be
throw unexpectedError(MBEAN_TYPE, e); * <code>&gt;= 0</code> and not exceed the CPU count of the system.
} *
} * @throws IllegalArgumentException If the CPU index is out of range or
* an unexpected Sigar error occurs
*/
public SigarCpuInfo(Sigar sigar, int index) {
super(sigar, DEFAULT);
/** // check index
* @return The CPU vendor reported if (index < 0)
*/ throw new IllegalArgumentException(
public String getVendor() { "CPU index has to be non-negative: " + index);
try { try {
return sigar.getCpuInfoList()[this.cpuIndex].getVendor(); int cpuCount;
} catch (SigarException e) { if ((cpuCount = sigar.getCpuInfoList().length) < index)
throw unexpectedError(MBEAN_TYPE, e); throw new IllegalArgumentException(
} "CPU index out of range (found " + cpuCount
} + " CPU(s)): " + index);
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
// ------- }
// Implementation of the DynamicMBean interface
// -------
/*
* (non-Javadoc)
* @see DynamicMBean#getAttribute(String)
*/
public Object getAttribute(String attr) throws AttributeNotFoundException {
if (MBEAN_ATTR_CACHESIZE.getName().equals(attr)) {
return new Long(getCacheSize());
} else if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) {
return new Integer(getCpuIndex());
} else if (MBEAN_ATTR_MHZ.getName().equals(attr)) {
return new Integer(getMhz());
} else if (MBEAN_ATTR_MODEL.getName().equals(attr)) {
return getModel();
} else if (MBEAN_ATTR_VENDOR.getName().equals(attr)) {
return getVendor();
} else {
throw new AttributeNotFoundException(attr);
}
}
/* // all fine
* (non-Javadoc) this.cpuIndex = index;
* @see DynamicMBean#setAttribute(Attribute) this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
*/ + "=CpuInfo,"
public void setAttribute(Attribute attr) throws AttributeNotFoundException { + MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase()
throw new AttributeNotFoundException(attr.getName()); + MBEAN_ATTR_CPUINDEX.getName().substring(1) + "=" + cpuIndex;
} }
/* /**
* (non-Javadoc) * Object name this instance will give itself when being registered to an
* @see DynamicMBean#invoke(String, Object[], String[]) * MBeanServer.
*/ */
public Object invoke(String actionName, Object[] params, String[] signature) throws ReflectionException { public String getObjectName() {
throw new ReflectionException(new NoSuchMethodException(actionName), actionName); return this.objectName;
} }
/* /**
* (non-Javadoc) * @return The index of the CPU, typically starting at 0
* @see DynamicMBean#getMBeanInfo() */
*/ public int getCpuIndex() {
public MBeanInfo getMBeanInfo() { return this.cpuIndex;
return MBEAN_INFO; }
}
/**
* @return The cache size of the CPU, in [byte]
*/
public long getCacheSize() {
try {
return sigar.getCpuInfoList()[this.cpuIndex].getCacheSize();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The clock speed of the CPU, in [MHz]
*/
public int getMhz() {
try {
return sigar.getCpuInfoList()[this.cpuIndex].getMhz();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The CPU model reported
*/
public String getModel() {
try {
return sigar.getCpuInfoList()[this.cpuIndex].getModel();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The CPU vendor reported
*/
public String getVendor() {
try {
return sigar.getCpuInfoList()[this.cpuIndex].getVendor();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
// -------
// Implementation of the DynamicMBean interface
// -------
/*
* (non-Javadoc)
* @see DynamicMBean#getAttribute(String)
*/
public Object getAttribute(String attr) throws AttributeNotFoundException {
if (MBEAN_ATTR_CACHESIZE.getName().equals(attr)) {
return new Long(getCacheSize());
} else if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) {
return new Integer(getCpuIndex());
} else if (MBEAN_ATTR_MHZ.getName().equals(attr)) {
return new Integer(getMhz());
} else if (MBEAN_ATTR_MODEL.getName().equals(attr)) {
return getModel();
} else if (MBEAN_ATTR_VENDOR.getName().equals(attr)) {
return getVendor();
} else {
throw new AttributeNotFoundException(attr);
}
}
/*
* (non-Javadoc)
* @see DynamicMBean#setAttribute(Attribute)
*/
public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName());
}
/*
* (non-Javadoc)
* @see DynamicMBean#invoke(String, Object[], String[])
*/
public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName),
actionName);
}
/*
* (non-Javadoc)
* @see DynamicMBean#getMBeanInfo()
*/
public MBeanInfo getMBeanInfo() {
return MBEAN_INFO;
}
} }

View File

@ -34,312 +34,300 @@ import org.hyperic.sigar.SigarException;
* package. Provides an OpenMBean conform implementation.</p> * package. Provides an OpenMBean conform implementation.</p>
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.4 (2007-04) * @since 1.5
*/ */
public class SigarCpuPerc extends AbstractMBean { public class SigarCpuPerc extends AbstractMBean {
private static final String MBEAN_TYPE = "CpuPercList"; private static final String MBEAN_TYPE = "CpuPercList";
private static final MBeanInfo MBEAN_INFO; private static final MBeanInfo MBEAN_INFO;
private static final MBeanAttributeInfo MBEAN_ATTR_CPUINDEX;
private static final MBeanAttributeInfo MBEAN_ATTR_COMBINED; private static final MBeanAttributeInfo MBEAN_ATTR_CPUINDEX;
private static final MBeanAttributeInfo MBEAN_ATTR_IDLE; private static final MBeanAttributeInfo MBEAN_ATTR_COMBINED;
private static final MBeanAttributeInfo MBEAN_ATTR_NICE; private static final MBeanAttributeInfo MBEAN_ATTR_IDLE;
private static final MBeanAttributeInfo MBEAN_ATTR_SYS; private static final MBeanAttributeInfo MBEAN_ATTR_NICE;
private static final MBeanAttributeInfo MBEAN_ATTR_USER; private static final MBeanAttributeInfo MBEAN_ATTR_SYS;
private static final MBeanAttributeInfo MBEAN_ATTR_WAIT; private static final MBeanAttributeInfo MBEAN_ATTR_USER;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX; private static final MBeanAttributeInfo MBEAN_ATTR_WAIT;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX;
private static MBeanParameterInfo MBEAN_PARAM_CPUINDEX; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR;
private static MBeanParameterInfo MBEAN_PARAM_SIGAR; private static MBeanParameterInfo MBEAN_PARAM_CPUINDEX;
static {
MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo(
"CpuIndex", "int",
"The index of the CPU, typically starting at 0",
true, false, false);
MBEAN_ATTR_COMBINED = new MBeanAttributeInfo(
"Combined", "double",
"The total time of the CPU, as a fraction of 1",
true, false, false);
MBEAN_ATTR_IDLE = new MBeanAttributeInfo(
"Idle", "double",
"The idle time of the CPU, as a fraction of 1",
true, false, false);
MBEAN_ATTR_NICE = new MBeanAttributeInfo(
"Nice", "double",
"The time of the CPU spent on nice priority, as a fraction of 1",
true, false, false);
MBEAN_ATTR_SYS = new MBeanAttributeInfo(
"Sys", "double",
"The time of the CPU used by the system, as a fraction of 1",
true, false, false);
MBEAN_ATTR_USER = new MBeanAttributeInfo(
"User", "double",
"The time of the CPU used by user processes, as a fraction of 1",
true, false, false);
MBEAN_ATTR_WAIT = new MBeanAttributeInfo(
"Wait", "double",
"The time the CPU had to wait for data to be loaded, as a fraction of 1",
true, false, false);
MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo(
"cpuIndex", "int",
"The index of the CPU to read data for. Must be >= 0 " +
"and not exceed the CPU count of the system");
MBEAN_PARAM_SIGAR = new MBeanParameterInfo(
"sigar", Sigar.class.getName(),
"The Sigar instance to use to fetch data from");
MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(
SigarCpuPerc.class.getName(),
"Creates a new instance for the CPU index specified, " +
"using a new Sigar instance to fetch the data. " +
"Fails if the CPU index is out of range.",
new MBeanParameterInfo[]{
MBEAN_PARAM_CPUINDEX});
MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo(
SigarCpuPerc.class.getName(),
"Creates a new instance for the CPU index specified, " +
"using the Sigar instance specified to fetch the data. " +
"Fails if the CPU index is out of range.",
new MBeanParameterInfo[]{
MBEAN_PARAM_SIGAR,
MBEAN_PARAM_CPUINDEX});
MBEAN_INFO = new MBeanInfo(
SigarCpuPerc.class.getName(),
"Sigar CPU MBean. Provides percentage data for a single " +
"CPU, averaged over the timeframe between the last and " +
"the current measurement point. Two measurement points " +
"can be as close as 5 seconds, meaning subsequent requests " +
"for data within 5 seconds after the last executed call " +
"will be satisfied from cached data.",
new MBeanAttributeInfo[]{
MBEAN_ATTR_CPUINDEX,
MBEAN_ATTR_COMBINED,
MBEAN_ATTR_IDLE,
MBEAN_ATTR_NICE,
MBEAN_ATTR_SYS,
MBEAN_ATTR_USER,
MBEAN_ATTR_WAIT},
new MBeanConstructorInfo[]{
MBEAN_CONSTR_CPUINDEX,
MBEAN_CONSTR_CPUINDEX_SIGAR},
null, null);
}
/** private static MBeanParameterInfo MBEAN_PARAM_SIGAR;
* Index of the CPU processed by the instance.
*/
private int cpuIndex;
/** static {
* Object name this instance will give itself when being registered to an MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo("CpuIndex", "int",
* MBeanServer. "The index of the CPU, typically starting at 0", true, false,
*/ false);
private String objectName; MBEAN_ATTR_COMBINED = new MBeanAttributeInfo("Combined", "double",
"The total time of the CPU, as a fraction of 1", true, false,
false);
MBEAN_ATTR_IDLE = new MBeanAttributeInfo("Idle", "double",
"The idle time of the CPU, as a fraction of 1", true, false,
false);
MBEAN_ATTR_NICE = new MBeanAttributeInfo(
"Nice",
"double",
"The time of the CPU spent on nice priority, as a fraction of 1",
true, false, false);
MBEAN_ATTR_SYS = new MBeanAttributeInfo("Sys", "double",
"The time of the CPU used by the system, as a fraction of 1",
true, false, false);
MBEAN_ATTR_USER = new MBeanAttributeInfo(
"User",
"double",
"The time of the CPU used by user processes, as a fraction of 1",
true, false, false);
MBEAN_ATTR_WAIT = new MBeanAttributeInfo(
"Wait",
"double",
"The time the CPU had to wait for data to be loaded, as a fraction of 1",
true, false, false);
MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo("cpuIndex", "int",
"The index of the CPU to read data for. Must be >= 0 "
+ "and not exceed the CPU count of the system");
MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class
.getName(), "The Sigar instance to use to fetch data from");
MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(SigarCpuPerc.class
.getName(),
"Creates a new instance for the CPU index specified, "
+ "using a new Sigar instance to fetch the data. "
+ "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_CPUINDEX });
MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo(
SigarCpuPerc.class.getName(),
"Creates a new instance for the CPU index specified, "
+ "using the Sigar instance specified to fetch the data. "
+ "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR,
MBEAN_PARAM_CPUINDEX });
MBEAN_INFO = new MBeanInfo(
SigarCpuPerc.class.getName(),
"Sigar CPU MBean. Provides percentage data for a single "
+ "CPU, averaged over the timeframe between the last and "
+ "the current measurement point. Two measurement points "
+ "can be as close as 5 seconds, meaning subsequent requests "
+ "for data within 5 seconds after the last executed call "
+ "will be satisfied from cached data.",
new MBeanAttributeInfo[] { MBEAN_ATTR_CPUINDEX,
MBEAN_ATTR_COMBINED, MBEAN_ATTR_IDLE, MBEAN_ATTR_NICE,
MBEAN_ATTR_SYS, MBEAN_ATTR_USER, MBEAN_ATTR_WAIT },
new MBeanConstructorInfo[] { MBEAN_CONSTR_CPUINDEX,
MBEAN_CONSTR_CPUINDEX_SIGAR }, null, null);
}
/** /**
* Creates a new instance for the CPU index specified, using a new Sigar * Index of the CPU processed by the instance.
* instance to fetch the data. Fails if the CPU index is out of range. */
* private int cpuIndex;
* @param cpuIndex The index of the CPU to read data for. Must be
* <code>&gt;= 0</code> and not exceed the CPU count of the system.
*
* @throws IllegalArgumentException If the CPU index is out of range or
* an unexpected Sigar error occurs
*/
public SigarCpuPerc(int index) {
this(new Sigar(), index);
}
/** /**
* Creates a new instance for the CPU index specified, using the Sigar * Object name this instance will give itself when being registered to an
* instance specified to fetch the data. Fails if the CPU index is out * MBeanServer.
* of range. */
* private String objectName;
* @param sigar The Sigar instance to use to fetch data from
* @param cpuIndex The index of the CPU to read data for. Must be
* <code>&gt;= 0</code> and not exceed the CPU count of the system.
*
* @throws IllegalArgumentException If the CPU index is out of range or
* an unexpected Sigar error occurs
*/
public SigarCpuPerc(Sigar sigar, int index) {
super(sigar, CACHED_5SEC);
// check index /**
if (index < 0) * Creates a new instance for the CPU index specified, using a new Sigar
throw new IllegalArgumentException( * instance to fetch the data. Fails if the CPU index is out of range.
"CPU index has to be non-negative: " + index); *
try { * @param cpuIndex The index of the CPU to read data for. Must be
int cpuCount; * <code>&gt;= 0</code> and not exceed the CPU count of the system.
if ((cpuCount = sigar.getCpuPercList().length) < index) *
throw new IllegalArgumentException( * @throws IllegalArgumentException If the CPU index is out of range or
"CPU index out of range (found " + cpuCount * an unexpected Sigar error occurs
+ " CPU(s)): " + index); */
public SigarCpuPerc(int index) {
this(new Sigar(), index);
}
} catch (SigarException e) { /**
throw unexpectedError(MBEAN_TYPE, e); * Creates a new instance for the CPU index specified, using the Sigar
} * instance specified to fetch the data. Fails if the CPU index is out
* of range.
*
* @param sigar The Sigar instance to use to fetch data from
* @param cpuIndex The index of the CPU to read data for. Must be
* <code>&gt;= 0</code> and not exceed the CPU count of the system.
*
* @throws IllegalArgumentException If the CPU index is out of range or
* an unexpected Sigar error occurs
*/
public SigarCpuPerc(Sigar sigar, int index) {
super(sigar, CACHED_5SEC);
// all fine // check index
this.cpuIndex = index; if (index < 0)
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" throw new IllegalArgumentException(
+ MBEAN_ATTR_TYPE + "=CpuPerc," "CPU index has to be non-negative: " + index);
+ MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase() try {
+ MBEAN_ATTR_CPUINDEX.getName().substring(1) + "=" + cpuIndex; int cpuCount;
} if ((cpuCount = sigar.getCpuPercList().length) < index)
throw new IllegalArgumentException(
"CPU index out of range (found " + cpuCount
+ " CPU(s)): " + index);
/** } catch (SigarException e) {
* Object name this instance will give itself when being registered to an throw unexpectedError(MBEAN_TYPE, e);
* MBeanServer. }
*/
public String getObjectName() {
return this.objectName;
}
/** // all fine
* @return The index of the CPU, typically starting at 0 this.cpuIndex = index;
*/ this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
public int getCpuIndex() { + "=CpuPerc,"
return this.cpuIndex; + MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase()
} + MBEAN_ATTR_CPUINDEX.getName().substring(1) + "=" + cpuIndex;
}
/** /**
* @return The total time of the CPU, as a fraction of 1 * Object name this instance will give itself when being registered to an
*/ * MBeanServer.
public double getCombined() { */
try { public String getObjectName() {
return sigar.getCpuPercList()[this.cpuIndex].getCombined(); return this.objectName;
} catch (SigarException e) { }
throw unexpectedError(MBEAN_TYPE, e);
}
}
/** /**
* @return The idle time of the CPU, as a fraction of 1 * @return The index of the CPU, typically starting at 0
*/ */
public double getIdle() { public int getCpuIndex() {
try { return this.cpuIndex;
return sigar.getCpuPercList()[this.cpuIndex].getIdle(); }
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/** /**
* @return The time of the CPU spent on nice priority, as a fraction of 1 * @return The total time of the CPU, as a fraction of 1
*/ */
public double getNice() { public double getCombined() {
try { try {
return sigar.getCpuPercList()[this.cpuIndex].getNice(); return sigar.getCpuPercList()[this.cpuIndex].getCombined();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The time of the CPU used by the system, as a fraction of 1 * @return The idle time of the CPU, as a fraction of 1
*/ */
public double getSys() { public double getIdle() {
try { try {
return sigar.getCpuPercList()[this.cpuIndex].getSys(); return sigar.getCpuPercList()[this.cpuIndex].getIdle();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The time of the CPU used by user processes, as a fraction of 1 * @return The time of the CPU spent on nice priority, as a fraction of 1
*/ */
public double getUser() { public double getNice() {
try { try {
return sigar.getCpuPercList()[this.cpuIndex].getUser(); return sigar.getCpuPercList()[this.cpuIndex].getNice();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The time the CPU had to wait for data to be loaded, as a fraction of 1 * @return The time of the CPU used by the system, as a fraction of 1
*/ */
public double getWait() { public double getSys() {
try { try {
return sigar.getCpuPercList()[this.cpuIndex].getWait(); return sigar.getCpuPercList()[this.cpuIndex].getSys();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/**
* @return The time of the CPU used by user processes, as a fraction of 1
// ------- */
// Implementation of the DynamicMBean interface public double getUser() {
// ------- try {
return sigar.getCpuPercList()[this.cpuIndex].getUser();
/* } catch (SigarException e) {
* (non-Javadoc) throw unexpectedError(MBEAN_TYPE, e);
* @see DynamicMBean#getAttribute(String) }
*/ }
public Object getAttribute(String attr) throws AttributeNotFoundException {
if (MBEAN_ATTR_COMBINED.getName().equals(attr)) {
return new Double(getCombined());
} else if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) {
return new Integer(getCpuIndex());
} else if (MBEAN_ATTR_IDLE.getName().equals(attr)) {
return new Double(getIdle());
} else if (MBEAN_ATTR_NICE.getName().equals(attr)) {
return new Double(getNice());
} else if (MBEAN_ATTR_SYS.getName().equals(attr)) {
return new Double(getSys());
} else if (MBEAN_ATTR_USER.getName().equals(attr)) {
return new Double(getUser());
} else if (MBEAN_ATTR_WAIT.getName().equals(attr)) {
return new Double(getWait());
} else {
throw new AttributeNotFoundException(attr);
}
}
/* /**
* (non-Javadoc) * @return The time the CPU had to wait for data to be loaded, as a fraction of 1
* @see DynamicMBean#setAttribute(Attribute) */
*/ public double getWait() {
public void setAttribute(Attribute attr) throws AttributeNotFoundException { try {
throw new AttributeNotFoundException(attr.getName()); return sigar.getCpuPercList()[this.cpuIndex].getWait();
} } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/* // -------
* (non-Javadoc) // Implementation of the DynamicMBean interface
* @see DynamicMBean#invoke(String, Object[], String[]) // -------
*/
public Object invoke(String actionName, Object[] params, String[] signature) throws ReflectionException { /*
throw new ReflectionException(new NoSuchMethodException(actionName), actionName); * (non-Javadoc)
} * @see DynamicMBean#getAttribute(String)
*/
/* public Object getAttribute(String attr) throws AttributeNotFoundException {
* (non-Javadoc)
* @see DynamicMBean#getMBeanInfo() if (MBEAN_ATTR_COMBINED.getName().equals(attr)) {
*/ return new Double(getCombined());
public MBeanInfo getMBeanInfo() {
return MBEAN_INFO; } else if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) {
} return new Integer(getCpuIndex());
} else if (MBEAN_ATTR_IDLE.getName().equals(attr)) {
return new Double(getIdle());
} else if (MBEAN_ATTR_NICE.getName().equals(attr)) {
return new Double(getNice());
} else if (MBEAN_ATTR_SYS.getName().equals(attr)) {
return new Double(getSys());
} else if (MBEAN_ATTR_USER.getName().equals(attr)) {
return new Double(getUser());
} else if (MBEAN_ATTR_WAIT.getName().equals(attr)) {
return new Double(getWait());
} else {
throw new AttributeNotFoundException(attr);
}
}
/*
* (non-Javadoc)
* @see DynamicMBean#setAttribute(Attribute)
*/
public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName());
}
/*
* (non-Javadoc)
* @see DynamicMBean#invoke(String, Object[], String[])
*/
public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName),
actionName);
}
/*
* (non-Javadoc)
* @see DynamicMBean#getMBeanInfo()
*/
public MBeanInfo getMBeanInfo() {
return MBEAN_INFO;
}
} }

View File

@ -0,0 +1,263 @@
/*
* Copyright (C) [2004, 2005, 2006, 2007], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.sigar.jmx;
import javax.management.Attribute;
import javax.management.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanParameterInfo;
import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarNotImplementedException;
/**
* Sigar JMX MBean implementation for the <code>LoadAverage</code> information
* package. Provides an OpenMBean conform implementation.
*
* @author Bjoern Martin
* @since 1.5
*/
public class SigarLoadAverage extends AbstractMBean {
private static final String MBEAN_TYPE = "LoadAverage";
/**
* Returned if {@link Sigar#getLoadAverage()}} is detected to be not
* implemented on the platform.
*
* @see #notImplemented
*/
private static final double NOT_IMPLEMENTED_LOAD_VALUE = -1.0d;
private static final MBeanInfo MBEAN_INFO;
private static final MBeanAttributeInfo MBEAN_ATTR_LAST1MIN;
private static final MBeanAttributeInfo MBEAN_ATTR_LAST5MIN;
private static final MBeanAttributeInfo MBEAN_ATTR_LAST15MIN;
private static final MBeanConstructorInfo MBEAN_CONSTR_SIGAR;
private static MBeanParameterInfo MBEAN_PARAM_SIGAR;
static {
MBEAN_ATTR_LAST1MIN = new MBeanAttributeInfo(
"LastMinute",
"double",
"The load average in the last minute, as a fraction of 1, or "
+ "-1.0 if the load cannot be determined on this platform",
true, false, false);
MBEAN_ATTR_LAST5MIN = new MBeanAttributeInfo(
"LastFiveMinutes",
"double",
"The load average over the last five minutes, as a fraction "
+ "of 1, or -1.0 if the load cannot be determined on this platform",
true, false, false);
MBEAN_ATTR_LAST15MIN = new MBeanAttributeInfo(
"Last15Minutes",
"double",
"The load average over the last 15 minutes, as a fraction of "
+ "1, or -1.0 if the load cannot be determined on this platform",
true, false, false);
MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class
.getName(), "The Sigar instance to use to fetch data from");
MBEAN_CONSTR_SIGAR = new MBeanConstructorInfo(
SigarLoadAverage.class.getName(),
"Creates a new instance, using the Sigar instance specified "
+ "to fetch the data. Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR });
MBEAN_INFO = new MBeanInfo(
SigarLoadAverage.class.getName(),
"Sigar load average MBean. Provides load averages of the "
+ "system over the last one, five and 15 minutes. Due to the "
+ "long term character of that information, the fetch is done "
+ "using a Sigar proxy cache with a timeout of 30 seconds.",
new MBeanAttributeInfo[] { MBEAN_ATTR_LAST1MIN,
MBEAN_ATTR_LAST5MIN, MBEAN_ATTR_LAST15MIN },
new MBeanConstructorInfo[] { MBEAN_CONSTR_SIGAR }, null, null);
}
/**
* Object name this instance will give itself when being registered to an
* MBeanServer.
*/
private final String objectName;
/**
* <p>Set <code>true</code> when the load average fetch failed with a
* <code>SigarException</code> that indicates the method is not implemented.
* Any subsequent call to this instance will then be answered with
* {@link #NOT_IMPLEMENTED_LOAD_VALUE}.
* </p>
*
* <p><b>FIXME</b> : This is a workaround and should be replaced by something
* more stable, as the code setting this member <code>true</code> relies on
* a substring being present within the exception. A proposal was made at
* <a href="http://jira.hyperic.com/browse/SIGAR-52">issue SIGAR-52</a>.
* </p>
*/
private boolean notImplemented;
/**
* Creates a new instance, using a new Sigar instance to fetch the data.
*
* @throws IllegalArgumentException
* If an unexpected Sigar error occurs.
*/
public SigarLoadAverage() throws IllegalArgumentException {
this(new Sigar());
}
/**
* Creates a new instance, using the Sigar instance specified to fetch the
* data.
*
* @param sigar
* The Sigar instance to use to fetch data from
*
* @throws IllegalArgumentException
* If an unexpected Sigar error occurs
*/
public SigarLoadAverage(Sigar sigar) throws IllegalArgumentException {
super(sigar, CACHED_30SEC);
// all fine
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
+ "=" + MBEAN_TYPE;
}
/**
* Object name this instance will give itself when being registered to an
* MBeanServer.
*/
public String getObjectName() {
return this.objectName;
}
/**
* @return The load average in the last minute, as a fraction of 1, or
* <code>-1.0d</code> if the load cannot be determined on this platform
*/
public double getLastMinute() {
try {
return sigarImpl.getLoadAverage()[0];
} catch (SigarNotImplementedException e) {
return NOT_IMPLEMENTED_LOAD_VALUE;
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The load average over the last five minutes, as a fraction of 1,
* or <code>-1.0d</code> if the load cannot be determined on this
* platform
*/
public double getLastFiveMinutes() {
try {
return sigarImpl.getLoadAverage()[1];
} catch (SigarNotImplementedException e) {
return NOT_IMPLEMENTED_LOAD_VALUE;
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The load average over the last 15 minutes, as a fraction of 1, or
* <code>-1.0d</code> if the load cannot be determined on this platform
*/
public double getLast15Minutes() {
try {
return sigarImpl.getLoadAverage()[2];
} catch (SigarNotImplementedException e) {
return NOT_IMPLEMENTED_LOAD_VALUE;
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
// -------
// Implementation of the DynamicMBean interface
// -------
/*
* (non-Javadoc)
*
* @see DynamicMBean#getAttribute(String)
*/
public Object getAttribute(String attr) throws AttributeNotFoundException {
if (MBEAN_ATTR_LAST1MIN.getName().equals(attr)) {
return new Double(getLastMinute());
} else if (MBEAN_ATTR_LAST5MIN.getName().equals(attr)) {
return new Double(getLastFiveMinutes());
} else if (MBEAN_ATTR_LAST15MIN.getName().equals(attr)) {
return new Double(getLast15Minutes());
} else {
throw new AttributeNotFoundException(attr);
}
}
/*
* (non-Javadoc)
*
* @see DynamicMBean#setAttribute(Attribute)
*/
public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName());
}
/*
* (non-Javadoc)
*
* @see DynamicMBean#invoke(String, Object[], String[])
*/
public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName),
actionName);
}
/*
* (non-Javadoc)
*
* @see DynamicMBean#getMBeanInfo()
*/
public MBeanInfo getMBeanInfo() {
return MBEAN_INFO;
}
}

View File

@ -35,238 +35,217 @@ import org.hyperic.sigar.SigarException;
* package. Provides an OpenMBean conform implementation. * package. Provides an OpenMBean conform implementation.
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.4 (2007-04) * @since 1.5
*/ */
public class SigarMem extends AbstractMBean { public class SigarMem extends AbstractMBean {
private static final String MBEAN_TYPE = "Mem"; private static final String MBEAN_TYPE = "Mem";
private static final MBeanInfo MBEAN_INFO; private static final MBeanInfo MBEAN_INFO;
private static final MBeanAttributeInfo MBEAN_ATTR_ACTUAL_FREE;
private static final MBeanAttributeInfo MBEAN_ATTR_ACTUAL_USED; private static final MBeanAttributeInfo MBEAN_ATTR_ACTUAL_FREE;
private static final MBeanAttributeInfo MBEAN_ATTR_FREE; private static final MBeanAttributeInfo MBEAN_ATTR_ACTUAL_USED;
private static final MBeanAttributeInfo MBEAN_ATTR_RAM; private static final MBeanAttributeInfo MBEAN_ATTR_FREE;
private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL; private static final MBeanAttributeInfo MBEAN_ATTR_RAM;
private static final MBeanAttributeInfo MBEAN_ATTR_USED; private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL;
private static final MBeanConstructorInfo MBEAN_CONSTR_SIGAR; private static final MBeanAttributeInfo MBEAN_ATTR_USED;
private static MBeanParameterInfo MBEAN_PARAM_SIGAR; private static final MBeanConstructorInfo MBEAN_CONSTR_SIGAR;
static {
MBEAN_ATTR_ACTUAL_FREE = new MBeanAttributeInfo(
"ActualFree", "long",
"TODO add proper description here",
true, false, false);
MBEAN_ATTR_ACTUAL_USED = new MBeanAttributeInfo(
"ActualUsed", "long",
"TODO add proper description here",
true, false, false);
MBEAN_ATTR_FREE = new MBeanAttributeInfo(
"Free", "long",
"TODO add proper description here",
true, false, false);
MBEAN_ATTR_RAM = new MBeanAttributeInfo(
"Ram", "long",
"TODO add proper description here",
true, false, false);
MBEAN_ATTR_TOTAL = new MBeanAttributeInfo(
"Total", "long",
"TODO add proper description here",
true, false, false);
MBEAN_ATTR_USED = new MBeanAttributeInfo(
"Used", "long",
"TODO add proper description here",
true, false, false);
MBEAN_PARAM_SIGAR = new MBeanParameterInfo(
"sigar", Sigar.class.getName(),
"The Sigar instance to use to fetch data from");
MBEAN_CONSTR_SIGAR = new MBeanConstructorInfo(
SigarMem.class.getName(),
"Creates a new instance, using the Sigar instance " +
"specified to fetch the data.",
new MBeanParameterInfo[]{
MBEAN_PARAM_SIGAR});
MBEAN_INFO = new MBeanInfo(
SigarMem.class.getName(),
"Sigar Memory MBean, provides raw data for the physical " +
"memory installed on the system. Uses an internal cache " +
"that invalidates within 500ms, allowing for bulk request " +
"being satisfied with a single dataset fetch.",
new MBeanAttributeInfo[]{
MBEAN_ATTR_ACTUAL_FREE,
MBEAN_ATTR_ACTUAL_USED,
MBEAN_ATTR_FREE,
MBEAN_ATTR_RAM,
MBEAN_ATTR_TOTAL,
MBEAN_ATTR_USED},
new MBeanConstructorInfo[]{
MBEAN_CONSTR_SIGAR},
null, null);
}
/**
* Object name this instance will give itself when being registered to an
* MBeanServer.
*/
private final String objectName;
/** private static MBeanParameterInfo MBEAN_PARAM_SIGAR;
* Creates a new instance, using the Sigar instance specified to fetch the
* data.
*
* @param sigar
* The Sigar instance to use to fetch data from
*
* @throws IllegalArgumentException
* If an unexpected Sigar error occurs
*/
public SigarMem(Sigar sigar) throws IllegalArgumentException {
super(sigar, CACHED_500MS);
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE static {
+ "=Memory"; MBEAN_ATTR_ACTUAL_FREE = new MBeanAttributeInfo("ActualFree", "long",
} "TODO add proper description here", true, false, false);
MBEAN_ATTR_ACTUAL_USED = new MBeanAttributeInfo("ActualUsed", "long",
"TODO add proper description here", true, false, false);
MBEAN_ATTR_FREE = new MBeanAttributeInfo("Free", "long",
"TODO add proper description here", true, false, false);
MBEAN_ATTR_RAM = new MBeanAttributeInfo("Ram", "long",
"TODO add proper description here", true, false, false);
MBEAN_ATTR_TOTAL = new MBeanAttributeInfo("Total", "long",
"TODO add proper description here", true, false, false);
MBEAN_ATTR_USED = new MBeanAttributeInfo("Used", "long",
"TODO add proper description here", true, false, false);
MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class
.getName(), "The Sigar instance to use to fetch data from");
MBEAN_CONSTR_SIGAR = new MBeanConstructorInfo(SigarMem.class.getName(),
"Creates a new instance, using the Sigar instance "
+ "specified to fetch the data.",
new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR });
MBEAN_INFO = new MBeanInfo(
SigarMem.class.getName(),
"Sigar Memory MBean, provides raw data for the physical "
+ "memory installed on the system. Uses an internal cache "
+ "that invalidates within 500ms, allowing for bulk request "
+ "being satisfied with a single dataset fetch.",
new MBeanAttributeInfo[] { MBEAN_ATTR_ACTUAL_FREE,
MBEAN_ATTR_ACTUAL_USED, MBEAN_ATTR_FREE,
MBEAN_ATTR_RAM, MBEAN_ATTR_TOTAL, MBEAN_ATTR_USED },
new MBeanConstructorInfo[] { MBEAN_CONSTR_SIGAR }, null, null);
/** }
* Object name this instance will give itself when being registered to an
* MBeanServer.
*/
public String getObjectName() {
return this.objectName;
}
/** /**
* @return The actual amount of free physical memory, in [bytes] * Object name this instance will give itself when being registered to an
*/ * MBeanServer.
public long getActualFree() { */
try { private final String objectName;
return sigar.getMem().getActualFree();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/** /**
* @return The actual amount of physical memory used, in [bytes] * Creates a new instance, using the Sigar instance specified to fetch the
*/ * data.
public long getActualUsed() { *
try { * @param sigar
return sigar.getMem().getActualUsed(); * The Sigar instance to use to fetch data from
} catch (SigarException e) { *
throw unexpectedError(MBEAN_TYPE, e); * @throws IllegalArgumentException
} * If an unexpected Sigar error occurs
} */
public SigarMem(Sigar sigar) throws IllegalArgumentException {
super(sigar, CACHED_500MS);
/** this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
* @return The amount of free physical memory, in [bytes] + "=Memory";
*/ }
public long getFree() {
try {
return sigar.getMem().getFree();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/** /**
* @return The amount of physical memory, in [bytes] * Object name this instance will give itself when being registered to an
*/ * MBeanServer.
public long getRam() { */
try { public String getObjectName() {
return sigar.getMem().getRam(); return this.objectName;
} catch (SigarException e) { }
throw unexpectedError(MBEAN_TYPE, e);
}
}
/** /**
* @return The total amount of physical memory, in [bytes] * @return The actual amount of free physical memory, in [bytes]
*/ */
public long getTotal() { public long getActualFree() {
try { try {
return sigar.getMem().getTotal(); return sigar.getMem().getActualFree();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The amount of physical memory in use, in [bytes] * @return The actual amount of physical memory used, in [bytes]
*/ */
public long getUsed() { public long getActualUsed() {
try { try {
return sigar.getMem().getUsed(); return sigar.getMem().getActualUsed();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
// ------- /**
// Implementation of the DynamicMBean interface * @return The amount of free physical memory, in [bytes]
// ------- */
public long getFree() {
try {
return sigar.getMem().getFree();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/* /**
* (non-Javadoc) * @return The amount of physical memory, in [bytes]
* */
* @see javax.management.DynamicMBean#getAttribute(java.lang.String) public long getRam() {
*/ try {
public Object getAttribute(String attr) throws AttributeNotFoundException, return sigar.getMem().getRam();
MBeanException, ReflectionException { } catch (SigarException e) {
if (MBEAN_ATTR_ACTUAL_FREE.getName().equals(attr)) { throw unexpectedError(MBEAN_TYPE, e);
return new Long(getActualFree()); }
}
} else if (MBEAN_ATTR_ACTUAL_USED.getName().equals(attr)) { /**
return new Long(getActualUsed()); * @return The total amount of physical memory, in [bytes]
*/
public long getTotal() {
try {
return sigar.getMem().getTotal();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
} else if (MBEAN_ATTR_FREE.getName().equals(attr)) { /**
return new Long(getFree()); * @return The amount of physical memory in use, in [bytes]
*/
public long getUsed() {
try {
return sigar.getMem().getUsed();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
} else if (MBEAN_ATTR_RAM.getName().equals(attr)) { // -------
return new Long(getRam()); // Implementation of the DynamicMBean interface
// -------
} else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) { /*
return new Long(getTotal()); * (non-Javadoc)
*
* @see javax.management.DynamicMBean#getAttribute(java.lang.String)
*/
public Object getAttribute(String attr) throws AttributeNotFoundException,
MBeanException, ReflectionException {
if (MBEAN_ATTR_ACTUAL_FREE.getName().equals(attr)) {
return new Long(getActualFree());
} else if (MBEAN_ATTR_USED.getName().equals(attr)) { } else if (MBEAN_ATTR_ACTUAL_USED.getName().equals(attr)) {
return new Long(getUsed()); return new Long(getActualUsed());
} else { } else if (MBEAN_ATTR_FREE.getName().equals(attr)) {
throw new AttributeNotFoundException(attr); return new Long(getFree());
}
}
/* } else if (MBEAN_ATTR_RAM.getName().equals(attr)) {
* (non-Javadoc) return new Long(getRam());
* @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
*/
public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName());
}
/* } else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) {
* (non-Javadoc) return new Long(getTotal());
* @see javax.management.DynamicMBean#invoke(java.lang.String,
* java.lang.Object[], java.lang.String[])
*/
public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName),
actionName);
}
/* } else if (MBEAN_ATTR_USED.getName().equals(attr)) {
* (non-Javadoc) return new Long(getUsed());
* @see javax.management.DynamicMBean#getMBeanInfo()
*/ } else {
public MBeanInfo getMBeanInfo() { throw new AttributeNotFoundException(attr);
return MBEAN_INFO; }
} }
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
*/
public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName());
}
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#invoke(java.lang.String,
* java.lang.Object[], java.lang.String[])
*/
public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName),
actionName);
}
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#getMBeanInfo()
*/
public MBeanInfo getMBeanInfo() {
return MBEAN_INFO;
}
} }

View File

@ -41,14 +41,14 @@ import org.hyperic.sigar.SigarException;
* *
* <ul> * <ul>
* <li>This class can be instantiated and registered to the MBean server by * <li>This class can be instantiated and registered to the MBean server by
* simply calling {@link MBeanServer#createMBean(String, ObjectName)}, * simply calling {@link MBeanServer#createMBean(String, ObjectName)},
* resulting in the automatic creation of all known default Sigar MBeans such * resulting in the automatic creation of all known default Sigar MBeans such
* as CPU and memory monitoring beans.</li> * as CPU and memory monitoring beans.</li>
* <li>Any Sigar MBean spawned by an instance of this class will use the same * <li>Any Sigar MBean spawned by an instance of this class will use the same
* {@link org.hyperic.sigar.Sigar} instance, saving resources in the * {@link org.hyperic.sigar.Sigar} instance, saving resources in the
* process.</li> * process.</li>
* <li>When this instance is deregistered from the MBean server, it will * <li>When this instance is deregistered from the MBean server, it will
* automatically deregister all instances it created, cleaning up behind * automatically deregister all instances it created, cleaning up behind
* itself.</li> * itself.</li>
* </ul> * </ul>
* *
@ -57,222 +57,286 @@ import org.hyperic.sigar.SigarException;
* it all down again.</p> * it all down again.</p>
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.4 (2007-04) * @since 1.5
*/ */
public class SigarRegistry extends AbstractMBean { public class SigarRegistry extends AbstractMBean {
private static final String MBEAN_TYPE = "SigarRegistry"; private static final String MBEAN_TYPE = "SigarRegistry";
private static final MBeanInfo MBEAN_INFO; private static final MBeanInfo MBEAN_INFO;
private static final MBeanConstructorInfo MBEAN_CONSTR_DEFAULT;
static {
MBEAN_CONSTR_DEFAULT = new MBeanConstructorInfo(
SigarRegistry.class.getName(),
"Creates a new instance of this class. Will create the Sigar " +
"instance this class uses when constructing other MBeans",
new MBeanParameterInfo[0]);
MBEAN_INFO = new MBeanInfo(
SigarRegistry.class.getName(),
"Sigar MBean registry. Provides a central point for creation " +
"and destruction of Sigar MBeans. Any Sigar MBean created via " +
"this instance will automatically be cleaned up when this " +
"instance is deregistered from the MBean server.",
null /*new MBeanAttributeInfo[0]*/,
new MBeanConstructorInfo[]{
MBEAN_CONSTR_DEFAULT},
null, null);
}
private final String objectName; private static final MBeanConstructorInfo MBEAN_CONSTR_DEFAULT;
private final ArrayList managedBeans; // private static final MBeanOperationInfo MBEAN_OPER_LISTPROCESSES;
/**
* Creates a new instance of this class. Will create the Sigar instance this
* class uses when constructing other MBeans.
*/
public SigarRegistry () {
super(new Sigar(), CACHELESS);
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":"
+ MBEAN_ATTR_TYPE + "=" + MBEAN_TYPE;
this.managedBeans = new ArrayList();
}
/*
* @see AbstractMBean#getObjectName()
*/
public String getObjectName() {
return this.objectName;
}
/* (non-Javadoc)
* @see javax.management.DynamicMBean#getMBeanInfo()
*/
public MBeanInfo getMBeanInfo() {
return MBEAN_INFO;
}
/* (non-Javadoc) static {
* @see javax.management.DynamicMBean#getAttribute(java.lang.String) MBEAN_CONSTR_DEFAULT = new MBeanConstructorInfo(
*/ SigarRegistry.class.getName(),
public Object getAttribute(String attr) throws AttributeNotFoundException { "Creates a new instance of this class. Will create the Sigar "
throw new AttributeNotFoundException(attr); + "instance this class uses when constructing other MBeans",
} new MBeanParameterInfo[0]);
// MBEAN_OPER_LISTPROCESSES = new MBeanOperationInfo("listProcesses",
// "Executes a query returning the process IDs of all processes " +
// "found on the system.",
// null /* new MBeanParameterInfo[0] */,
// String.class.getName(), MBeanOperationInfo.INFO);
/* (non-Javadoc) MBEAN_INFO = new MBeanInfo(
* @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute) SigarRegistry.class.getName(),
*/ "Sigar MBean registry. Provides a central point for creation "
public void setAttribute(Attribute attr) throws AttributeNotFoundException { + "and destruction of Sigar MBeans. Any Sigar MBean created via "
throw new AttributeNotFoundException(attr.getName()); + "this instance will automatically be cleaned up when this "
} + "instance is deregistered from the MBean server.",
null /*new MBeanAttributeInfo[0]*/,
new MBeanConstructorInfo[] { MBEAN_CONSTR_DEFAULT },
null /*new MBeanOperationInfo[0] */,
null /*new MBeanNotificationInfo[0]*/);
}
/* (non-Javadoc) private final String objectName;
* @see javax.management.DynamicMBean#invoke(java.lang.String, java.lang.Object[], java.lang.String[])
*/
public Object invoke(String action, Object[] params, String[] signatures)
throws MBeanException, ReflectionException {
throw new ReflectionException(new NoSuchMethodException(action), action);
}
private final ArrayList managedBeans;
// -------
// Implementation of the MBeanRegistration interface
// -------
/**
* Registers the default set of Sigar MBeans. Before doing so, a super call
* is made to satisfy {@link AbstractMBean}.
*
* @see AbstractMBean#postRegister(Boolean)
*/
public void postRegister(Boolean success) {
super.postRegister(success);
if (!success.booleanValue())
return;
// get CPUs
registerCpuBeans();
// get memory
registerMemoryBeans();
}
/** /**
* Registers MBeans for the Sigar types <code>Cpu</code>, <code>CpuPerc</code> * Creates a new instance of this class. Will create the Sigar instance this
* and <code>CpuInfo</code>. One instance will be registered for each CPU * class uses when constructing other MBeans.
* (core?) found. */
*/ public SigarRegistry() {
private void registerCpuBeans() { super(new Sigar(), CACHELESS);
ObjectInstance nextRegistered = null; this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
+ "=" + MBEAN_TYPE;
try { this.managedBeans = new ArrayList();
final int cpuCount = sigar.getCpuInfoList().length; }
for (int i = 0; i < cpuCount; i++) {
// add CPU bean /* (non-Javadoc)
SigarCpu nextCpu = new SigarCpu(sigarImpl, i); * @see AbstractMBean#getObjectName()
try { */
if (!mbeanServer.isRegistered(new ObjectName(nextCpu.getObjectName()))) public String getObjectName() {
nextRegistered = mbeanServer.registerMBean(nextCpu, null); return this.objectName;
} catch (Exception e) { // ignore }
}
// add MBean to set of managed beans /* public String listProcesses() {
if (nextRegistered != null) try {
managedBeans.add(nextRegistered.getObjectName()); final long start = System.currentTimeMillis();
nextRegistered = null; long[] ids = sigar.getProcList();
StringBuffer procNames = new StringBuffer();
// add CPU percentage bean for (int i = 0; i < ids.length; i++) {
SigarCpuPerc nextCpuPerc = new SigarCpuPerc(sigarImpl, i); try {
try { procNames.append(ids[i] + ":" + sigar.getProcExe(ids[i]).getName()).append('\n');
if (!mbeanServer.isRegistered(new ObjectName(nextCpuPerc.getObjectName()))) } catch (SigarException e) {
nextRegistered = mbeanServer.registerMBean(nextCpuPerc, null); procNames.append(ids[i] + ":" + e.getMessage()).append('\n');
} catch (Exception e) { // ignore }
} }
// add MBean to set of managed beans
if (nextRegistered != null) final long end = System.currentTimeMillis();
managedBeans.add(nextRegistered.getObjectName()); procNames.append("-- Took " + (end-start) + "ms");
nextRegistered = null; return procNames.toString();
// add CPU info bean } catch (SigarException e) {
SigarCpuInfo nextCpuInfo = new SigarCpuInfo(sigarImpl, i); throw unexpectedError("ProcList", e);
try { }
if (!mbeanServer.isRegistered(new ObjectName(nextCpuInfo.getObjectName()))) }
nextRegistered = mbeanServer.registerMBean(nextCpuInfo, null); */
} catch (Exception e) { // ignore /* (non-Javadoc)
} * @see javax.management.DynamicMBean#getAttribute(java.lang.String)
// add MBean to set of managed beans */
if (nextRegistered != null) public Object getAttribute(String attr) throws AttributeNotFoundException {
managedBeans.add(nextRegistered.getObjectName()); throw new AttributeNotFoundException(attr);
nextRegistered = null; }
}
/* (non-Javadoc)
} catch (SigarException e) { * @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
throw unexpectedError("CpuInfoList", e); */
} public void setAttribute(Attribute attr) throws AttributeNotFoundException {
} throw new AttributeNotFoundException(attr.getName());
}
/**
* Registers MBeans for the Sigar types <code>Mem</code> and <code>Swap</code>. /* (non-Javadoc)
*/ * @see javax.management.DynamicMBean#invoke(java.lang.String, java.lang.Object[], java.lang.String[])
private void registerMemoryBeans() { */
public Object invoke(String action, Object[] params, String[] signatures)
ObjectInstance nextRegistered = null; throws MBeanException, ReflectionException {
// add physical memory bean /* if (MBEAN_OPER_LISTPROCESSES.getName().equals(action))
SigarMem mem = new SigarMem(sigarImpl); return listProcesses();
try { else */
if (!mbeanServer.isRegistered(new ObjectName(mem.getObjectName()))) throw new ReflectionException(new NoSuchMethodException(action), action);
nextRegistered = mbeanServer.registerMBean(mem, null); }
} catch (Exception e) { // ignore
} /* (non-Javadoc)
* @see javax.management.DynamicMBean#getMBeanInfo()
// add MBean to set of managed beans */
if (nextRegistered != null) public MBeanInfo getMBeanInfo() {
managedBeans.add(nextRegistered.getObjectName()); return MBEAN_INFO;
nextRegistered = null; }
// add swap memory bean // -------
SigarSwap swap = new SigarSwap(sigarImpl); // Implementation of the MBeanRegistration interface
try { // -------
if (!mbeanServer.isRegistered(new ObjectName(swap.getObjectName())))
nextRegistered = mbeanServer.registerMBean(swap, null); /**
} catch (Exception e) { // ignore * Registers the default set of Sigar MBeans. Before doing so, a super call
nextRegistered = null; * is made to satisfy {@link AbstractMBean}.
} *
* @see AbstractMBean#postRegister(Boolean)
// add MBean to set of managed beans */
if (nextRegistered != null) public void postRegister(Boolean success) {
managedBeans.add(nextRegistered.getObjectName());
nextRegistered = null; super.postRegister(success);
}
if (!success.booleanValue())
/** return;
* Deregisters all Sigar MBeans that were created and registered using this
* instance. After doing so, a super call is made to satisfy {@link AbstractMBean}. // get CPUs
* @throws Exception registerCpuBeans();
*
* @see AbstractMBean#preDeregister() // get memory
*/ registerMemoryBeans();
public void preDeregister() throws Exception {
// get system
// count backwards to remove ONs immediately registerSystemBeans();
for (int i = managedBeans.size() - 1; i >= 0; i--) { }
ObjectName next = (ObjectName) managedBeans.remove(i);
if (mbeanServer.isRegistered(next)) { /**
try { * Registers MBeans for the Sigar types <code>Cpu</code>, <code>CpuPerc</code>
mbeanServer.unregisterMBean(next); * and <code>CpuInfo</code>. One instance will be registered for each CPU
} catch (Exception e) { // ignore * (core?) found.
} */
} private void registerCpuBeans() {
} ObjectInstance nextRegistered = null;
// do the super call try {
super.preDeregister(); final int cpuCount = sigar.getCpuInfoList().length;
} for (int i = 0; i < cpuCount; i++) {
// add CPU bean
SigarCpu nextCpu = new SigarCpu(sigarImpl, i);
try {
if (!mbeanServer.isRegistered(new ObjectName(nextCpu
.getObjectName())))
nextRegistered = mbeanServer.registerMBean(nextCpu,
null);
} catch (Exception e) { // ignore
}
// add MBean to set of managed beans
if (nextRegistered != null)
managedBeans.add(nextRegistered.getObjectName());
nextRegistered = null;
// add CPU percentage bean
SigarCpuPerc nextCpuPerc = new SigarCpuPerc(sigarImpl, i);
try {
if (!mbeanServer.isRegistered(new ObjectName(nextCpuPerc
.getObjectName())))
nextRegistered = mbeanServer.registerMBean(nextCpuPerc,
null);
} catch (Exception e) { // ignore
}
// add MBean to set of managed beans
if (nextRegistered != null)
managedBeans.add(nextRegistered.getObjectName());
nextRegistered = null;
// add CPU info bean
SigarCpuInfo nextCpuInfo = new SigarCpuInfo(sigarImpl, i);
try {
if (!mbeanServer.isRegistered(new ObjectName(nextCpuInfo
.getObjectName())))
nextRegistered = mbeanServer.registerMBean(nextCpuInfo,
null);
} catch (Exception e) { // ignore
}
// add MBean to set of managed beans
if (nextRegistered != null)
managedBeans.add(nextRegistered.getObjectName());
nextRegistered = null;
}
} catch (SigarException e) {
throw unexpectedError("CpuInfoList", e);
}
}
/**
* Registers MBeans for the Sigar types <code>Mem</code> and <code>Swap</code>.
*/
private void registerMemoryBeans() {
ObjectInstance nextRegistered = null;
// add physical memory bean
SigarMem mem = new SigarMem(sigarImpl);
try {
if (!mbeanServer.isRegistered(new ObjectName(mem.getObjectName())))
nextRegistered = mbeanServer.registerMBean(mem, null);
} catch (Exception e) { // ignore
}
// add MBean to set of managed beans
if (nextRegistered != null)
managedBeans.add(nextRegistered.getObjectName());
nextRegistered = null;
// add swap memory bean
SigarSwap swap = new SigarSwap(sigarImpl);
try {
if (!mbeanServer.isRegistered(new ObjectName(swap.getObjectName())))
nextRegistered = mbeanServer.registerMBean(swap, null);
} catch (Exception e) { // ignore
nextRegistered = null;
}
// add MBean to set of managed beans
if (nextRegistered != null)
managedBeans.add(nextRegistered.getObjectName());
nextRegistered = null;
}
/**
* Registers MBeans for the Sigar types <code>LoadAverage</code>...
*/
private void registerSystemBeans() {
ObjectInstance nextRegistered = null;
// add load average bean
SigarLoadAverage loadAvg = new SigarLoadAverage(sigarImpl);
try {
if (!mbeanServer.isRegistered(new ObjectName(loadAvg
.getObjectName())))
nextRegistered = mbeanServer.registerMBean(loadAvg, null);
} catch (Exception e) { // ignore
}
// add MBean to set of managed beans
if (nextRegistered != null)
managedBeans.add(nextRegistered.getObjectName());
nextRegistered = null;
}
/**
* Deregisters all Sigar MBeans that were created and registered using this
* instance. After doing so, a super call is made to satisfy {@link AbstractMBean}.
* @throws Exception
*
* @see AbstractMBean#preDeregister()
*/
public void preDeregister() throws Exception {
// count backwards to remove ONs immediately
for (int i = managedBeans.size() - 1; i >= 0; i--) {
ObjectName next = (ObjectName) managedBeans.remove(i);
if (mbeanServer.isRegistered(next)) {
try {
mbeanServer.unregisterMBean(next);
} catch (Exception e) { // ignore
}
}
}
// do the super call
super.preDeregister();
}
} }

View File

@ -35,174 +35,165 @@ import org.hyperic.sigar.SigarException;
* package. Provides an OpenMBean conform implementation. * package. Provides an OpenMBean conform implementation.
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.4 (2007-04) * @since 1.5
*/ */
public class SigarSwap extends AbstractMBean { public class SigarSwap extends AbstractMBean {
private static final String MBEAN_TYPE = "Swap"; private static final String MBEAN_TYPE = "Swap";
private static final MBeanInfo MBEAN_INFO; private static final MBeanInfo MBEAN_INFO;
private static final MBeanAttributeInfo MBEAN_ATTR_FREE;
private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL; private static final MBeanAttributeInfo MBEAN_ATTR_FREE;
private static final MBeanAttributeInfo MBEAN_ATTR_USED; private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL;
private static final MBeanConstructorInfo MBEAN_CONSTR_SIGAR; private static final MBeanAttributeInfo MBEAN_ATTR_USED;
private static MBeanParameterInfo MBEAN_PARAM_SIGAR; private static final MBeanConstructorInfo MBEAN_CONSTR_SIGAR;
static {
MBEAN_ATTR_FREE = new MBeanAttributeInfo(
"Free", "long",
"The amount of free swap memory, in [bytes]",
true, false, false);
MBEAN_ATTR_TOTAL = new MBeanAttributeInfo(
"Total", "long",
"The total amount of swap memory, in [bytes]",
true, false, false);
MBEAN_ATTR_USED = new MBeanAttributeInfo(
"Used", "long",
"The amount of swap memory in use, in [bytes]",
true, false, false);
MBEAN_PARAM_SIGAR = new MBeanParameterInfo(
"sigar", Sigar.class.getName(),
"The Sigar instance to use to fetch data from");
MBEAN_CONSTR_SIGAR = new MBeanConstructorInfo(
SigarSwap.class.getName(),
"Creates a new instance, using the Sigar instance " +
"specified to fetch the data.",
new MBeanParameterInfo[]{
MBEAN_PARAM_SIGAR});
MBEAN_INFO = new MBeanInfo(
SigarSwap.class.getName(),
"Sigar Swap MBean, provides raw data for the swap memory " +
"configured on the system. Uses an internal cache that " +
"invalidates within 5 seconds.",
new MBeanAttributeInfo[]{
MBEAN_ATTR_FREE,
MBEAN_ATTR_TOTAL,
MBEAN_ATTR_USED},
new MBeanConstructorInfo[]{
MBEAN_CONSTR_SIGAR},
null, null);
}
/**
* Object name this instance will give itself when being registered to an
* MBeanServer.
*/
private final String objectName;
/** private static MBeanParameterInfo MBEAN_PARAM_SIGAR;
* Creates a new instance, using the Sigar instance specified to fetch the
* data.
*
* @param sigar
* The Sigar instance to use to fetch data from
*
* @throws IllegalArgumentException
* If an unexpected Sigar error occurs
*/
public SigarSwap(Sigar sigar) throws IllegalArgumentException {
super(sigar, CACHED_5SEC);
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE static {
+ "=Swap"; MBEAN_ATTR_FREE = new MBeanAttributeInfo("Free", "long",
} "The amount of free swap memory, in [bytes]", true, false,
false);
MBEAN_ATTR_TOTAL = new MBeanAttributeInfo("Total", "long",
"The total amount of swap memory, in [bytes]", true, false,
false);
MBEAN_ATTR_USED = new MBeanAttributeInfo("Used", "long",
"The amount of swap memory in use, in [bytes]", true, false,
false);
MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class
.getName(), "The Sigar instance to use to fetch data from");
MBEAN_CONSTR_SIGAR = new MBeanConstructorInfo(
SigarSwap.class.getName(),
"Creates a new instance, using the Sigar instance "
+ "specified to fetch the data.",
new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR });
MBEAN_INFO = new MBeanInfo(
SigarSwap.class.getName(),
"Sigar Swap MBean, provides raw data for the swap memory "
+ "configured on the system. Uses an internal cache that "
+ "invalidates within 5 seconds.",
new MBeanAttributeInfo[] { MBEAN_ATTR_FREE, MBEAN_ATTR_TOTAL,
MBEAN_ATTR_USED },
new MBeanConstructorInfo[] { MBEAN_CONSTR_SIGAR }, null, null);
/** }
* Object name this instance will give itself when being registered to an
* MBeanServer.
*/
public String getObjectName() {
return this.objectName;
}
/** /**
* @return The amount of free swap memory, in [bytes] * Object name this instance will give itself when being registered to an
*/ * MBeanServer.
public long getFree() { */
try { private final String objectName;
return sigar.getSwap().getFree();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/** /**
* @return The total amount of swap memory, in [bytes] * Creates a new instance, using the Sigar instance specified to fetch the
*/ * data.
public long getTotal() { *
try { * @param sigar
return sigar.getSwap().getTotal(); * The Sigar instance to use to fetch data from
} catch (SigarException e) { *
throw unexpectedError(MBEAN_TYPE, e); * @throws IllegalArgumentException
} * If an unexpected Sigar error occurs
} */
public SigarSwap(Sigar sigar) throws IllegalArgumentException {
super(sigar, CACHED_5SEC);
/** this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
* @return The amount of swap memory in use, in [bytes] + "=Swap";
*/ }
public long getUsed() {
try {
return sigar.getSwap().getUsed();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
// ------- /**
// Implementation of the DynamicMBean interface * Object name this instance will give itself when being registered to an
// ------- * MBeanServer.
*/
public String getObjectName() {
return this.objectName;
}
/* /**
* (non-Javadoc) * @return The amount of free swap memory, in [bytes]
* */
* @see javax.management.DynamicMBean#getAttribute(java.lang.String) public long getFree() {
*/ try {
public Object getAttribute(String attr) throws AttributeNotFoundException, return sigar.getSwap().getFree();
MBeanException, ReflectionException { } catch (SigarException e) {
if (MBEAN_ATTR_FREE.getName().equals(attr)) { throw unexpectedError(MBEAN_TYPE, e);
return new Long(getFree()); }
}
} else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) { /**
return new Long(getTotal()); * @return The total amount of swap memory, in [bytes]
*/
public long getTotal() {
try {
return sigar.getSwap().getTotal();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
} else if (MBEAN_ATTR_USED.getName().equals(attr)) { /**
return new Long(getUsed()); * @return The amount of swap memory in use, in [bytes]
*/
public long getUsed() {
try {
return sigar.getSwap().getUsed();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
} else { // -------
throw new AttributeNotFoundException(attr); // Implementation of the DynamicMBean interface
} // -------
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute) *
*/ * @see javax.management.DynamicMBean#getAttribute(java.lang.String)
public void setAttribute(Attribute attr) throws AttributeNotFoundException { */
throw new AttributeNotFoundException(attr.getName()); public Object getAttribute(String attr) throws AttributeNotFoundException,
} MBeanException, ReflectionException {
if (MBEAN_ATTR_FREE.getName().equals(attr)) {
return new Long(getFree());
/* } else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) {
* (non-Javadoc) return new Long(getTotal());
* @see javax.management.DynamicMBean#invoke(java.lang.String,
* java.lang.Object[], java.lang.String[])
*/
public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName),
actionName);
}
/* } else if (MBEAN_ATTR_USED.getName().equals(attr)) {
* (non-Javadoc) return new Long(getUsed());
* @see javax.management.DynamicMBean#getMBeanInfo()
*/ } else {
public MBeanInfo getMBeanInfo() { throw new AttributeNotFoundException(attr);
return MBEAN_INFO; }
} }
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
*/
public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName());
}
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#invoke(java.lang.String,
* java.lang.Object[], java.lang.String[])
*/
public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName),
actionName);
}
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#getMBeanInfo()
*/
public MBeanInfo getMBeanInfo() {
return MBEAN_INFO;
}
} }