This commit is contained in:
Doug MacEachern 2009-02-09 14:21:00 -08:00
parent 7a5288278c
commit 7ef48da327
5 changed files with 1403 additions and 1403 deletions

View File

@ -1,334 +1,334 @@
/* /*
* Copyright (C) [2004, 2005, 2006, 2007], Hyperic, Inc. * Copyright (C) [2004, 2005, 2006, 2007], Hyperic, Inc.
* This file is part of SIGAR. * This file is part of SIGAR.
* *
* SIGAR is free software; you can redistribute it and/or modify * SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as * it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed * published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more * PARTICULAR PURPOSE. See the GNU General Public License for more
* details. * details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA. * USA.
*/ */
package org.hyperic.sigar.jmx; package org.hyperic.sigar.jmx;
import javax.management.Attribute; import javax.management.Attribute;
import javax.management.AttributeNotFoundException; import javax.management.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo; import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo; import javax.management.MBeanConstructorInfo;
import javax.management.MBeanInfo; import javax.management.MBeanInfo;
import javax.management.MBeanParameterInfo; import javax.management.MBeanParameterInfo;
import javax.management.ReflectionException; import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar; 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 * Sigar JMX MBean implementation for the <code>Cpu</code> information
* package. Provides an OpenMBean conform implementation. * package. Provides an OpenMBean conform implementation.
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.5 * @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_CPUINDEX;
private static final MBeanAttributeInfo MBEAN_ATTR_IDLE; private static final MBeanAttributeInfo MBEAN_ATTR_IDLE;
private static final MBeanAttributeInfo MBEAN_ATTR_NICE; private static final MBeanAttributeInfo MBEAN_ATTR_NICE;
private static final MBeanAttributeInfo MBEAN_ATTR_SYS; private static final MBeanAttributeInfo MBEAN_ATTR_SYS;
private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL; private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL;
private static final MBeanAttributeInfo MBEAN_ATTR_USER; private static final MBeanAttributeInfo MBEAN_ATTR_USER;
private static final MBeanAttributeInfo MBEAN_ATTR_WAIT; private static final MBeanAttributeInfo MBEAN_ATTR_WAIT;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR;
private static MBeanParameterInfo MBEAN_PARAM_CPUINDEX; private static MBeanParameterInfo MBEAN_PARAM_CPUINDEX;
private static MBeanParameterInfo MBEAN_PARAM_SIGAR; private static MBeanParameterInfo MBEAN_PARAM_SIGAR;
static { static {
MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo("CpuIndex", "int", MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo("CpuIndex", "int",
"The index of the CPU, typically starting at 0", true, false, "The index of the CPU, typically starting at 0", true, false,
false); false);
MBEAN_ATTR_IDLE = new MBeanAttributeInfo("Idle", "long", MBEAN_ATTR_IDLE = new MBeanAttributeInfo("Idle", "long",
"The idle time of the CPU, in [ms]", true, false, false); "The idle time of the CPU, in [ms]", true, false, false);
MBEAN_ATTR_NICE = new MBeanAttributeInfo("Nice", "long", MBEAN_ATTR_NICE = new MBeanAttributeInfo("Nice", "long",
"The time of the CPU spent on nice priority, in [ms]", true, "The time of the CPU spent on nice priority, in [ms]", true,
false, false); false, false);
MBEAN_ATTR_SYS = new MBeanAttributeInfo("Sys", "long", MBEAN_ATTR_SYS = new MBeanAttributeInfo("Sys", "long",
"The time of the CPU used by the system, in [ms]", true, false, "The time of the CPU used by the system, in [ms]", true, false,
false); false);
MBEAN_ATTR_TOTAL = new MBeanAttributeInfo("Total", "long", MBEAN_ATTR_TOTAL = new MBeanAttributeInfo("Total", "long",
"The total time of the CPU, in [ms]", true, false, false); "The total time of the CPU, in [ms]", true, false, false);
MBEAN_ATTR_USER = new MBeanAttributeInfo("User", "long", MBEAN_ATTR_USER = new MBeanAttributeInfo("User", "long",
"The time of the CPU used by user processes, in [ms]", true, "The time of the CPU used by user processes, in [ms]", true,
false, false); false, false);
MBEAN_ATTR_WAIT = new MBeanAttributeInfo("Wait", "long", MBEAN_ATTR_WAIT = new MBeanAttributeInfo("Wait", "long",
"The time the CPU had to wait for data to be loaded, in [ms]", "The time the CPU had to wait for data to be loaded, in [ms]",
true, false, false); true, false, false);
MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo("cpuIndex", "int", MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo("cpuIndex", "int",
"The index of the CPU to read data for. Must be >= 0 " "The index of the CPU to read data for. Must be >= 0 "
+ "and not exceed the CPU count of the system"); + "and not exceed the CPU count of the system");
MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class
.getName(), "The Sigar instance to use to fetch data from"); .getName(), "The Sigar instance to use to fetch data from");
MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(SigarCpu.class MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(SigarCpu.class
.getName(), .getName(),
"Creates a new instance for the CPU index specified, " "Creates a new instance for the CPU index specified, "
+ "using a new Sigar instance to fetch the data. " + "using a new Sigar instance to fetch the data. "
+ "Fails if the CPU index is out of range.", + "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_CPUINDEX }); new MBeanParameterInfo[] { MBEAN_PARAM_CPUINDEX });
MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo( MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo(
SigarCpu.class.getName(), SigarCpu.class.getName(),
"Creates a new instance for the CPU index specified, " "Creates a new instance for the CPU index specified, "
+ "using the Sigar instance specified to fetch the data. " + "using the Sigar instance specified to fetch the data. "
+ "Fails if the CPU index is out of range.", + "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR, new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR,
MBEAN_PARAM_CPUINDEX }); MBEAN_PARAM_CPUINDEX });
MBEAN_INFO = new MBeanInfo( MBEAN_INFO = new MBeanInfo(
SigarCpu.class.getName(), SigarCpu.class.getName(),
"Sigar CPU MBean. Provides raw timing data for a single " "Sigar CPU MBean. Provides raw timing data for a single "
+ "CPU. The data is cached for 500ms, meaning each request " + "CPU. The data is cached for 500ms, meaning each request "
+ "(and as a result each block request to all parameters) " + "(and as a result each block request to all parameters) "
+ "within half a second is satisfied from the same dataset.", + "within half a second is satisfied from the same dataset.",
new MBeanAttributeInfo[] { MBEAN_ATTR_CPUINDEX, new MBeanAttributeInfo[] { MBEAN_ATTR_CPUINDEX,
MBEAN_ATTR_IDLE, MBEAN_ATTR_NICE, MBEAN_ATTR_SYS, MBEAN_ATTR_IDLE, MBEAN_ATTR_NICE, MBEAN_ATTR_SYS,
MBEAN_ATTR_TOTAL, MBEAN_ATTR_USER, MBEAN_ATTR_WAIT }, MBEAN_ATTR_TOTAL, MBEAN_ATTR_USER, MBEAN_ATTR_WAIT },
new MBeanConstructorInfo[] { MBEAN_CONSTR_CPUINDEX, new MBeanConstructorInfo[] { MBEAN_CONSTR_CPUINDEX,
MBEAN_CONSTR_CPUINDEX_SIGAR }, null, null); MBEAN_CONSTR_CPUINDEX_SIGAR }, null, null);
} }
/** /**
* Index of the CPU processed by the instance. * Index of the CPU processed by the instance.
*/ */
private final int cpuIndex; private final int 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.
*/ */
private final String objectName; private final String objectName;
/** /**
* Creates a new instance for the CPU index specified, using a new Sigar * 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. * instance to fetch the data. Fails if the CPU index is out of range.
* *
* @param cpuIndex * @param cpuIndex
* The index of the CPU to read data for. Must be <code>&gt;= 0</code> * The index of the CPU to read data for. Must be <code>&gt;= 0</code>
* and not exceed the CPU count of the system. * and not exceed the CPU count of the system.
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If the CPU index is out of range or an unexpected Sigar error * If the CPU index is out of range or an unexpected Sigar error
* occurs. * occurs.
*/ */
public SigarCpu(int cpuIndex) throws IllegalArgumentException { public SigarCpu(int cpuIndex) throws IllegalArgumentException {
this(new Sigar(), cpuIndex); this(new Sigar(), cpuIndex);
} }
/** /**
* Creates a new instance for the CPU index specified, using the Sigar * 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 * instance specified to fetch the data. Fails if the CPU index is out of
* range. * range.
* *
* @param sigar * @param sigar
* The Sigar instance to use to fetch data from * The Sigar instance to use to fetch data from
* @param cpuIndex * @param cpuIndex
* The index of the CPU to read data for. Must be * The index of the CPU to read data for. Must be
* <code>&gt;= 0</code> and not exceed the CPU count of the * <code>&gt;= 0</code> and not exceed the CPU count of the
* system. * system.
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If the CPU index is out of range or an unexpected Sigar error * If the CPU index is out of range or an unexpected Sigar error
* occurs * occurs
*/ */
public SigarCpu(Sigar sigar, int cpuIndex) throws IllegalArgumentException { public SigarCpu(Sigar sigar, int cpuIndex) throws IllegalArgumentException {
super(sigar, CACHED_500MS); super(sigar, CACHED_500MS);
// check index // check index
if (cpuIndex < 0) if (cpuIndex < 0)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"CPU index has to be non-negative: " + cpuIndex); "CPU index has to be non-negative: " + cpuIndex);
try { try {
int cpuCount; int cpuCount;
if ((cpuCount = sigar.getCpuList().length) < cpuIndex) if ((cpuCount = sigar.getCpuList().length) < cpuIndex)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"CPU index out of range (found " + cpuCount "CPU index out of range (found " + cpuCount
+ " CPU(s)): " + cpuIndex); + " CPU(s)): " + cpuIndex);
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
// all fine // all fine
this.cpuIndex = cpuIndex; this.cpuIndex = cpuIndex;
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
+ "=Cpu," + "=Cpu,"
+ MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase() + MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase()
+ MBEAN_ATTR_CPUINDEX.getName().substring(1) + "=" + cpuIndex; + 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() { public String getObjectName() {
return this.objectName; return this.objectName;
} }
/** /**
* @return The index of the CPU, typically starting at 0 * @return The index of the CPU, typically starting at 0
*/ */
public int getCpuIndex() { public int getCpuIndex() {
return this.cpuIndex; return this.cpuIndex;
} }
/** /**
* @return The idle time of the CPU, in [ms] * @return The idle time of the CPU, in [ms]
*/ */
public long getIdle() { public long getIdle() {
try { try {
return sigar.getCpuList()[this.cpuIndex].getIdle(); return sigar.getCpuList()[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 spent on nice priority, in [ms] * @return The time of the CPU spent on nice priority, in [ms]
*/ */
public long getNice() { public long getNice() {
try { try {
return sigar.getCpuList()[this.cpuIndex].getNice(); return sigar.getCpuList()[this.cpuIndex].getNice();
} 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, in [ms] * @return The time of the CPU used by the system, in [ms]
*/ */
public long getSys() { public long getSys() {
try { try {
return sigar.getCpuList()[this.cpuIndex].getSys(); return sigar.getCpuList()[this.cpuIndex].getSys();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The total time of the CPU, in [ms] * @return The total time of the CPU, in [ms]
*/ */
public long getTotal() { public long getTotal() {
try { try {
return sigar.getCpuList()[this.cpuIndex].getTotal(); return sigar.getCpuList()[this.cpuIndex].getTotal();
} 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, in [ms] * @return The time of the CPU used by user processes, in [ms]
*/ */
public long getUser() { public long getUser() {
try { try {
return sigar.getCpuList()[this.cpuIndex].getUser(); return sigar.getCpuList()[this.cpuIndex].getUser();
} 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, in [ms] * @return The time the CPU had to wait for data to be loaded, in [ms]
*/ */
public long getWait() { public long getWait() {
try { try {
return sigar.getCpuList()[this.cpuIndex].getWait(); return sigar.getCpuList()[this.cpuIndex].getWait();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
// ------- // -------
// Implementation of the DynamicMBean interface // Implementation of the DynamicMBean interface
// ------- // -------
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see DynamicMBean#getAttribute(String) * @see DynamicMBean#getAttribute(String)
*/ */
public Object getAttribute(String attr) throws AttributeNotFoundException { public Object getAttribute(String attr) throws AttributeNotFoundException {
if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) { if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) {
return new Integer(getCpuIndex()); return new Integer(getCpuIndex());
} else if (MBEAN_ATTR_IDLE.getName().equals(attr)) { } else if (MBEAN_ATTR_IDLE.getName().equals(attr)) {
return new Long(getIdle()); return new Long(getIdle());
} else if (MBEAN_ATTR_NICE.getName().equals(attr)) { } else if (MBEAN_ATTR_NICE.getName().equals(attr)) {
return new Long(getNice()); return new Long(getNice());
} else if (MBEAN_ATTR_SYS.getName().equals(attr)) { } else if (MBEAN_ATTR_SYS.getName().equals(attr)) {
return new Long(getSys()); return new Long(getSys());
} else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) { } else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) {
return new Long(getTotal()); return new Long(getTotal());
} else if (MBEAN_ATTR_USER.getName().equals(attr)) { } else if (MBEAN_ATTR_USER.getName().equals(attr)) {
return new Long(getUser()); return new Long(getUser());
} else if (MBEAN_ATTR_WAIT.getName().equals(attr)) { } else if (MBEAN_ATTR_WAIT.getName().equals(attr)) {
return new Long(getWait()); return new Long(getWait());
} else { } else {
throw new AttributeNotFoundException(attr); throw new AttributeNotFoundException(attr);
} }
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see DynamicMBean#setAttribute(Attribute) * @see DynamicMBean#setAttribute(Attribute)
*/ */
public void setAttribute(Attribute attr) throws AttributeNotFoundException { public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName()); throw new AttributeNotFoundException(attr.getName());
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see DynamicMBean#invoke(String, Object[], String[]) * @see DynamicMBean#invoke(String, Object[], String[])
*/ */
public Object invoke(String actionName, Object[] params, String[] signature) public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException { throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName), throw new ReflectionException(new NoSuchMethodException(actionName),
actionName); actionName);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see DynamicMBean#getMBeanInfo() * @see DynamicMBean#getMBeanInfo()
*/ */
public MBeanInfo getMBeanInfo() { public MBeanInfo getMBeanInfo() {
return MBEAN_INFO; return MBEAN_INFO;
} }
} }

View File

@ -1,286 +1,286 @@
/* /*
* Copyright (C) [2004, 2005, 2006, 2007], Hyperic, Inc. * Copyright (C) [2004, 2005, 2006, 2007], Hyperic, Inc.
* This file is part of SIGAR. * This file is part of SIGAR.
* *
* SIGAR is free software; you can redistribute it and/or modify * SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as * it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed * published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more * PARTICULAR PURPOSE. See the GNU General Public License for more
* details. * details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA. * USA.
*/ */
package org.hyperic.sigar.jmx; package org.hyperic.sigar.jmx;
import javax.management.Attribute; import javax.management.Attribute;
import javax.management.AttributeNotFoundException; import javax.management.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo; import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo; import javax.management.MBeanConstructorInfo;
import javax.management.MBeanInfo; import javax.management.MBeanInfo;
import javax.management.MBeanParameterInfo; import javax.management.MBeanParameterInfo;
import javax.management.ReflectionException; import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
/** /**
* Sigar JMX MBean implementation for the <code>CpuInfo</code> information * Sigar JMX MBean implementation for the <code>CpuInfo</code> information
* package. Provides an OpenMBean conform implementation. * package. Provides an OpenMBean conform implementation.
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.5 * @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_CPUINDEX;
private static final MBeanAttributeInfo MBEAN_ATTR_CACHESIZE; private static final MBeanAttributeInfo MBEAN_ATTR_CACHESIZE;
private static final MBeanAttributeInfo MBEAN_ATTR_MHZ; private static final MBeanAttributeInfo MBEAN_ATTR_MHZ;
private static final MBeanAttributeInfo MBEAN_ATTR_MODEL; private static final MBeanAttributeInfo MBEAN_ATTR_MODEL;
private static final MBeanAttributeInfo MBEAN_ATTR_VENDOR; private static final MBeanAttributeInfo MBEAN_ATTR_VENDOR;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR;
private static final MBeanParameterInfo MBEAN_PARAM_CPUINDEX; private static final MBeanParameterInfo MBEAN_PARAM_CPUINDEX;
private static final MBeanParameterInfo MBEAN_PARAM_SIGAR; private static final MBeanParameterInfo MBEAN_PARAM_SIGAR;
static { static {
MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo("CpuIndex", "int", MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo("CpuIndex", "int",
"The index of the CPU, typically starting at 0", true, false, "The index of the CPU, typically starting at 0", true, false,
false); false);
MBEAN_ATTR_CACHESIZE = new MBeanAttributeInfo("CacheSize", "long", MBEAN_ATTR_CACHESIZE = new MBeanAttributeInfo("CacheSize", "long",
"The cache size of the CPU, in [byte]", true, false, false); "The cache size of the CPU, in [byte]", true, false, false);
MBEAN_ATTR_MHZ = new MBeanAttributeInfo("Mhz", "int", MBEAN_ATTR_MHZ = new MBeanAttributeInfo("Mhz", "int",
"The clock speed of the CPU, in [MHz]", true, false, false); "The clock speed of the CPU, in [MHz]", true, false, false);
MBEAN_ATTR_MODEL = new MBeanAttributeInfo("Model", "java.lang.String", MBEAN_ATTR_MODEL = new MBeanAttributeInfo("Model", "java.lang.String",
"The CPU model reported", true, false, false); "The CPU model reported", true, false, false);
MBEAN_ATTR_VENDOR = new MBeanAttributeInfo("Vendor", MBEAN_ATTR_VENDOR = new MBeanAttributeInfo("Vendor",
"java.lang.String", "The CPU vendor reported", true, false, "java.lang.String", "The CPU vendor reported", true, false,
false); false);
MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo("cpuIndex", "int", MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo("cpuIndex", "int",
"The index of the CPU to read data for. Must be >= 0 " "The index of the CPU to read data for. Must be >= 0 "
+ "and not exceed the CPU count of the system"); + "and not exceed the CPU count of the system");
MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class
.getName(), "The Sigar instance to use to fetch data from"); .getName(), "The Sigar instance to use to fetch data from");
MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(SigarCpuInfo.class MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(SigarCpuInfo.class
.getName(), .getName(),
"Creates a new instance for the CPU index specified, " "Creates a new instance for the CPU index specified, "
+ "using a new Sigar instance to fetch the data. " + "using a new Sigar instance to fetch the data. "
+ "Fails if the CPU index is out of range.", + "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_CPUINDEX }); new MBeanParameterInfo[] { MBEAN_PARAM_CPUINDEX });
MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo( MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo(
SigarCpuInfo.class.getName(), SigarCpuInfo.class.getName(),
"Creates a new instance for the CPU index specified, " "Creates a new instance for the CPU index specified, "
+ "using the Sigar instance specified to fetch the data. " + "using the Sigar instance specified to fetch the data. "
+ "Fails if the CPU index is out of range.", + "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR, new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR,
MBEAN_PARAM_CPUINDEX }); MBEAN_PARAM_CPUINDEX });
MBEAN_INFO = new MBeanInfo( MBEAN_INFO = new MBeanInfo(
SigarCpuInfo.class.getName(), SigarCpuInfo.class.getName(),
"Sigar CPU Info MBean, provides overall information for a " "Sigar CPU Info MBean, provides overall information for a "
+ "single CPU. This information only changes if, for example, " + "single CPU. This information only changes if, for example, "
+ "a CPU is reducing its clock frequency or shutting down " + "a CPU is reducing its clock frequency or shutting down "
+ "part of its cache. Subsequent requests are satisfied from " + "part of its cache. Subsequent requests are satisfied from "
+ "within a cache that invalidates after 30 seconds.", + "within a cache that invalidates after 30 seconds.",
new MBeanAttributeInfo[] { MBEAN_ATTR_CPUINDEX, new MBeanAttributeInfo[] { MBEAN_ATTR_CPUINDEX,
MBEAN_ATTR_CACHESIZE, MBEAN_ATTR_MHZ, MBEAN_ATTR_MODEL, MBEAN_ATTR_CACHESIZE, MBEAN_ATTR_MHZ, MBEAN_ATTR_MODEL,
MBEAN_ATTR_VENDOR }, new MBeanConstructorInfo[] { MBEAN_ATTR_VENDOR }, new MBeanConstructorInfo[] {
MBEAN_CONSTR_CPUINDEX, MBEAN_CONSTR_CPUINDEX_SIGAR }, MBEAN_CONSTR_CPUINDEX, MBEAN_CONSTR_CPUINDEX_SIGAR },
null, null); null, null);
} }
/** /**
* Index of the CPU processed by the instance. * Index of the CPU processed by the instance.
*/ */
private int cpuIndex; private int 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.
*/ */
private String objectName; private String objectName;
/** /**
* Creates a new instance for the CPU index specified, using a new Sigar * 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. * 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 * @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. * <code>&gt;= 0</code> and not exceed the CPU count of the system.
* *
* @throws IllegalArgumentException If the CPU index is out of range or * @throws IllegalArgumentException If the CPU index is out of range or
* an unexpected Sigar error occurs * an unexpected Sigar error occurs
*/ */
public SigarCpuInfo(int index) throws IllegalArgumentException { public SigarCpuInfo(int index) throws IllegalArgumentException {
this(new Sigar(), index); this(new Sigar(), index);
} }
/** /**
* Creates a new instance for the CPU index specified, using the Sigar * 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 * instance specified to fetch the data. Fails if the CPU index is out
* of range. * of range.
* *
* @param sigar The Sigar instance to use to fetch data from * @param sigar The Sigar instance to use to fetch data from
* @param cpuIndex The index of the CPU to read data for. Must be * @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. * <code>&gt;= 0</code> and not exceed the CPU count of the system.
* *
* @throws IllegalArgumentException If the CPU index is out of range or * @throws IllegalArgumentException If the CPU index is out of range or
* an unexpected Sigar error occurs * an unexpected Sigar error occurs
*/ */
public SigarCpuInfo(Sigar sigar, int index) { public SigarCpuInfo(Sigar sigar, int index) {
super(sigar, DEFAULT); super(sigar, DEFAULT);
// check index // check index
if (index < 0) if (index < 0)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"CPU index has to be non-negative: " + index); "CPU index has to be non-negative: " + index);
try { try {
int cpuCount; int cpuCount;
if ((cpuCount = sigar.getCpuInfoList().length) < index) if ((cpuCount = sigar.getCpuInfoList().length) < index)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"CPU index out of range (found " + cpuCount "CPU index out of range (found " + cpuCount
+ " CPU(s)): " + index); + " CPU(s)): " + index);
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
// all fine // all fine
this.cpuIndex = index; this.cpuIndex = index;
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
+ "=CpuInfo," + "=CpuInfo,"
+ MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase() + MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase()
+ MBEAN_ATTR_CPUINDEX.getName().substring(1) + "=" + cpuIndex; + 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() { public String getObjectName() {
return this.objectName; return this.objectName;
} }
/** /**
* @return The index of the CPU, typically starting at 0 * @return The index of the CPU, typically starting at 0
*/ */
public int getCpuIndex() { public int getCpuIndex() {
return this.cpuIndex; return this.cpuIndex;
} }
/** /**
* @return The cache size of the CPU, in [byte] * @return The cache size of the CPU, in [byte]
*/ */
public long getCacheSize() { public long getCacheSize() {
try { try {
return sigar.getCpuInfoList()[this.cpuIndex].getCacheSize(); return sigar.getCpuInfoList()[this.cpuIndex].getCacheSize();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The clock speed of the CPU, in [MHz] * @return The clock speed of the CPU, in [MHz]
*/ */
public int getMhz() { public int getMhz() {
try { try {
return sigar.getCpuInfoList()[this.cpuIndex].getMhz(); return sigar.getCpuInfoList()[this.cpuIndex].getMhz();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The CPU model reported * @return The CPU model reported
*/ */
public String getModel() { public String getModel() {
try { try {
return sigar.getCpuInfoList()[this.cpuIndex].getModel(); return sigar.getCpuInfoList()[this.cpuIndex].getModel();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The CPU vendor reported * @return The CPU vendor reported
*/ */
public String getVendor() { public String getVendor() {
try { try {
return sigar.getCpuInfoList()[this.cpuIndex].getVendor(); return sigar.getCpuInfoList()[this.cpuIndex].getVendor();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
// ------- // -------
// Implementation of the DynamicMBean interface // Implementation of the DynamicMBean interface
// ------- // -------
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see DynamicMBean#getAttribute(String) * @see DynamicMBean#getAttribute(String)
*/ */
public Object getAttribute(String attr) throws AttributeNotFoundException { public Object getAttribute(String attr) throws AttributeNotFoundException {
if (MBEAN_ATTR_CACHESIZE.getName().equals(attr)) { if (MBEAN_ATTR_CACHESIZE.getName().equals(attr)) {
return new Long(getCacheSize()); return new Long(getCacheSize());
} else if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) { } else if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) {
return new Integer(getCpuIndex()); return new Integer(getCpuIndex());
} else if (MBEAN_ATTR_MHZ.getName().equals(attr)) { } else if (MBEAN_ATTR_MHZ.getName().equals(attr)) {
return new Integer(getMhz()); return new Integer(getMhz());
} else if (MBEAN_ATTR_MODEL.getName().equals(attr)) { } else if (MBEAN_ATTR_MODEL.getName().equals(attr)) {
return getModel(); return getModel();
} else if (MBEAN_ATTR_VENDOR.getName().equals(attr)) { } else if (MBEAN_ATTR_VENDOR.getName().equals(attr)) {
return getVendor(); return getVendor();
} else { } else {
throw new AttributeNotFoundException(attr); throw new AttributeNotFoundException(attr);
} }
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see DynamicMBean#setAttribute(Attribute) * @see DynamicMBean#setAttribute(Attribute)
*/ */
public void setAttribute(Attribute attr) throws AttributeNotFoundException { public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName()); throw new AttributeNotFoundException(attr.getName());
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see DynamicMBean#invoke(String, Object[], String[]) * @see DynamicMBean#invoke(String, Object[], String[])
*/ */
public Object invoke(String actionName, Object[] params, String[] signature) public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException { throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName), throw new ReflectionException(new NoSuchMethodException(actionName),
actionName); actionName);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see DynamicMBean#getMBeanInfo() * @see DynamicMBean#getMBeanInfo()
*/ */
public MBeanInfo getMBeanInfo() { public MBeanInfo getMBeanInfo() {
return MBEAN_INFO; return MBEAN_INFO;
} }
} }

View File

@ -1,333 +1,333 @@
/* /*
* Copyright (C) [2004, 2005, 2006, 2007], Hyperic, Inc. * Copyright (C) [2004, 2005, 2006, 2007], Hyperic, Inc.
* This file is part of SIGAR. * This file is part of SIGAR.
* *
* SIGAR is free software; you can redistribute it and/or modify * SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as * it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed * published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more * PARTICULAR PURPOSE. See the GNU General Public License for more
* details. * details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA. * USA.
*/ */
package org.hyperic.sigar.jmx; package org.hyperic.sigar.jmx;
import javax.management.Attribute; import javax.management.Attribute;
import javax.management.AttributeNotFoundException; import javax.management.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo; import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo; import javax.management.MBeanConstructorInfo;
import javax.management.MBeanInfo; import javax.management.MBeanInfo;
import javax.management.MBeanParameterInfo; import javax.management.MBeanParameterInfo;
import javax.management.ReflectionException; import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
/** /**
* <p>Sigar JMX MBean implementation for the <code>CpuPerc</code> information * <p>Sigar JMX MBean implementation for the <code>CpuPerc</code> information
* package. Provides an OpenMBean conform implementation.</p> * package. Provides an OpenMBean conform implementation.</p>
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.5 * @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_CPUINDEX;
private static final MBeanAttributeInfo MBEAN_ATTR_COMBINED; private static final MBeanAttributeInfo MBEAN_ATTR_COMBINED;
private static final MBeanAttributeInfo MBEAN_ATTR_IDLE; private static final MBeanAttributeInfo MBEAN_ATTR_IDLE;
private static final MBeanAttributeInfo MBEAN_ATTR_NICE; private static final MBeanAttributeInfo MBEAN_ATTR_NICE;
private static final MBeanAttributeInfo MBEAN_ATTR_SYS; private static final MBeanAttributeInfo MBEAN_ATTR_SYS;
private static final MBeanAttributeInfo MBEAN_ATTR_USER; private static final MBeanAttributeInfo MBEAN_ATTR_USER;
private static final MBeanAttributeInfo MBEAN_ATTR_WAIT; private static final MBeanAttributeInfo MBEAN_ATTR_WAIT;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR; private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR;
private static MBeanParameterInfo MBEAN_PARAM_CPUINDEX; private static MBeanParameterInfo MBEAN_PARAM_CPUINDEX;
private static MBeanParameterInfo MBEAN_PARAM_SIGAR; private static MBeanParameterInfo MBEAN_PARAM_SIGAR;
static { static {
MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo("CpuIndex", "int", MBEAN_ATTR_CPUINDEX = new MBeanAttributeInfo("CpuIndex", "int",
"The index of the CPU, typically starting at 0", true, false, "The index of the CPU, typically starting at 0", true, false,
false); false);
MBEAN_ATTR_COMBINED = new MBeanAttributeInfo("Combined", "double", MBEAN_ATTR_COMBINED = new MBeanAttributeInfo("Combined", "double",
"The total time of the CPU, as a fraction of 1", true, false, "The total time of the CPU, as a fraction of 1", true, false,
false); false);
MBEAN_ATTR_IDLE = new MBeanAttributeInfo("Idle", "double", MBEAN_ATTR_IDLE = new MBeanAttributeInfo("Idle", "double",
"The idle time of the CPU, as a fraction of 1", true, false, "The idle time of the CPU, as a fraction of 1", true, false,
false); false);
MBEAN_ATTR_NICE = new MBeanAttributeInfo( MBEAN_ATTR_NICE = new MBeanAttributeInfo(
"Nice", "Nice",
"double", "double",
"The time of the CPU spent on nice priority, as a fraction of 1", "The time of the CPU spent on nice priority, as a fraction of 1",
true, false, false); true, false, false);
MBEAN_ATTR_SYS = new MBeanAttributeInfo("Sys", "double", MBEAN_ATTR_SYS = new MBeanAttributeInfo("Sys", "double",
"The time of the CPU used by the system, as a fraction of 1", "The time of the CPU used by the system, as a fraction of 1",
true, false, false); true, false, false);
MBEAN_ATTR_USER = new MBeanAttributeInfo( MBEAN_ATTR_USER = new MBeanAttributeInfo(
"User", "User",
"double", "double",
"The time of the CPU used by user processes, as a fraction of 1", "The time of the CPU used by user processes, as a fraction of 1",
true, false, false); true, false, false);
MBEAN_ATTR_WAIT = new MBeanAttributeInfo( MBEAN_ATTR_WAIT = new MBeanAttributeInfo(
"Wait", "Wait",
"double", "double",
"The time the CPU had to wait for data to be loaded, as a fraction of 1", "The time the CPU had to wait for data to be loaded, as a fraction of 1",
true, false, false); true, false, false);
MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo("cpuIndex", "int", MBEAN_PARAM_CPUINDEX = new MBeanParameterInfo("cpuIndex", "int",
"The index of the CPU to read data for. Must be >= 0 " "The index of the CPU to read data for. Must be >= 0 "
+ "and not exceed the CPU count of the system"); + "and not exceed the CPU count of the system");
MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class
.getName(), "The Sigar instance to use to fetch data from"); .getName(), "The Sigar instance to use to fetch data from");
MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(SigarCpuPerc.class MBEAN_CONSTR_CPUINDEX = new MBeanConstructorInfo(SigarCpuPerc.class
.getName(), .getName(),
"Creates a new instance for the CPU index specified, " "Creates a new instance for the CPU index specified, "
+ "using a new Sigar instance to fetch the data. " + "using a new Sigar instance to fetch the data. "
+ "Fails if the CPU index is out of range.", + "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_CPUINDEX }); new MBeanParameterInfo[] { MBEAN_PARAM_CPUINDEX });
MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo( MBEAN_CONSTR_CPUINDEX_SIGAR = new MBeanConstructorInfo(
SigarCpuPerc.class.getName(), SigarCpuPerc.class.getName(),
"Creates a new instance for the CPU index specified, " "Creates a new instance for the CPU index specified, "
+ "using the Sigar instance specified to fetch the data. " + "using the Sigar instance specified to fetch the data. "
+ "Fails if the CPU index is out of range.", + "Fails if the CPU index is out of range.",
new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR, new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR,
MBEAN_PARAM_CPUINDEX }); MBEAN_PARAM_CPUINDEX });
MBEAN_INFO = new MBeanInfo( MBEAN_INFO = new MBeanInfo(
SigarCpuPerc.class.getName(), SigarCpuPerc.class.getName(),
"Sigar CPU MBean. Provides percentage data for a single " "Sigar CPU MBean. Provides percentage data for a single "
+ "CPU, averaged over the timeframe between the last and " + "CPU, averaged over the timeframe between the last and "
+ "the current measurement point. Two measurement points " + "the current measurement point. Two measurement points "
+ "can be as close as 5 seconds, meaning subsequent requests " + "can be as close as 5 seconds, meaning subsequent requests "
+ "for data within 5 seconds after the last executed call " + "for data within 5 seconds after the last executed call "
+ "will be satisfied from cached data.", + "will be satisfied from cached data.",
new MBeanAttributeInfo[] { MBEAN_ATTR_CPUINDEX, new MBeanAttributeInfo[] { MBEAN_ATTR_CPUINDEX,
MBEAN_ATTR_COMBINED, MBEAN_ATTR_IDLE, MBEAN_ATTR_NICE, MBEAN_ATTR_COMBINED, MBEAN_ATTR_IDLE, MBEAN_ATTR_NICE,
MBEAN_ATTR_SYS, MBEAN_ATTR_USER, MBEAN_ATTR_WAIT }, MBEAN_ATTR_SYS, MBEAN_ATTR_USER, MBEAN_ATTR_WAIT },
new MBeanConstructorInfo[] { MBEAN_CONSTR_CPUINDEX, new MBeanConstructorInfo[] { MBEAN_CONSTR_CPUINDEX,
MBEAN_CONSTR_CPUINDEX_SIGAR }, null, null); MBEAN_CONSTR_CPUINDEX_SIGAR }, null, null);
} }
/** /**
* Index of the CPU processed by the instance. * Index of the CPU processed by the instance.
*/ */
private int cpuIndex; private int 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.
*/ */
private String objectName; private String objectName;
/** /**
* Creates a new instance for the CPU index specified, using a new Sigar * 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. * 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 * @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. * <code>&gt;= 0</code> and not exceed the CPU count of the system.
* *
* @throws IllegalArgumentException If the CPU index is out of range or * @throws IllegalArgumentException If the CPU index is out of range or
* an unexpected Sigar error occurs * an unexpected Sigar error occurs
*/ */
public SigarCpuPerc(int index) { public SigarCpuPerc(int index) {
this(new Sigar(), index); this(new Sigar(), index);
} }
/** /**
* Creates a new instance for the CPU index specified, using the Sigar * 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 * instance specified to fetch the data. Fails if the CPU index is out
* of range. * of range.
* *
* @param sigar The Sigar instance to use to fetch data from * @param sigar The Sigar instance to use to fetch data from
* @param cpuIndex The index of the CPU to read data for. Must be * @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. * <code>&gt;= 0</code> and not exceed the CPU count of the system.
* *
* @throws IllegalArgumentException If the CPU index is out of range or * @throws IllegalArgumentException If the CPU index is out of range or
* an unexpected Sigar error occurs * an unexpected Sigar error occurs
*/ */
public SigarCpuPerc(Sigar sigar, int index) { public SigarCpuPerc(Sigar sigar, int index) {
super(sigar, CACHED_5SEC); super(sigar, CACHED_5SEC);
// check index // check index
if (index < 0) if (index < 0)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"CPU index has to be non-negative: " + index); "CPU index has to be non-negative: " + index);
try { try {
int cpuCount; int cpuCount;
if ((cpuCount = sigar.getCpuPercList().length) < index) if ((cpuCount = sigar.getCpuPercList().length) < index)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"CPU index out of range (found " + cpuCount "CPU index out of range (found " + cpuCount
+ " CPU(s)): " + index); + " CPU(s)): " + index);
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
// all fine // all fine
this.cpuIndex = index; this.cpuIndex = index;
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
+ "=CpuPerc," + "=CpuPerc,"
+ MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase() + MBEAN_ATTR_CPUINDEX.getName().substring(0, 1).toLowerCase()
+ MBEAN_ATTR_CPUINDEX.getName().substring(1) + "=" + cpuIndex; + 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() { public String getObjectName() {
return this.objectName; return this.objectName;
} }
/** /**
* @return The index of the CPU, typically starting at 0 * @return The index of the CPU, typically starting at 0
*/ */
public int getCpuIndex() { public int getCpuIndex() {
return this.cpuIndex; return this.cpuIndex;
} }
/** /**
* @return The total time of the CPU, as a fraction of 1 * @return The total time of the CPU, as a fraction of 1
*/ */
public double getCombined() { public double getCombined() {
try { try {
return sigar.getCpuPercList()[this.cpuIndex].getCombined(); return sigar.getCpuPercList()[this.cpuIndex].getCombined();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The idle time of the CPU, as a fraction of 1 * @return The idle time of the CPU, as a fraction of 1
*/ */
public double getIdle() { public double getIdle() {
try { try {
return sigar.getCpuPercList()[this.cpuIndex].getIdle(); 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 spent on nice priority, as a fraction of 1 * @return The time of the CPU spent on nice priority, as a fraction of 1
*/ */
public double getNice() { public double getNice() {
try { try {
return sigar.getCpuPercList()[this.cpuIndex].getNice(); 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 of the CPU used by the system, as a fraction of 1 * @return The time of the CPU used by the system, as a fraction of 1
*/ */
public double getSys() { public double getSys() {
try { try {
return sigar.getCpuPercList()[this.cpuIndex].getSys(); 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 * @return The time of the CPU used by user processes, as a fraction of 1
*/ */
public double getUser() { public double getUser() {
try { try {
return sigar.getCpuPercList()[this.cpuIndex].getUser(); return sigar.getCpuPercList()[this.cpuIndex].getUser();
} 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 the CPU had to wait for data to be loaded, as a fraction of 1
*/ */
public double getWait() { public double getWait() {
try { try {
return sigar.getCpuPercList()[this.cpuIndex].getWait(); return sigar.getCpuPercList()[this.cpuIndex].getWait();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
// ------- // -------
// Implementation of the DynamicMBean interface // Implementation of the DynamicMBean interface
// ------- // -------
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see DynamicMBean#getAttribute(String) * @see DynamicMBean#getAttribute(String)
*/ */
public Object getAttribute(String attr) throws AttributeNotFoundException { public Object getAttribute(String attr) throws AttributeNotFoundException {
if (MBEAN_ATTR_COMBINED.getName().equals(attr)) { if (MBEAN_ATTR_COMBINED.getName().equals(attr)) {
return new Double(getCombined()); return new Double(getCombined());
} else if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) { } else if (MBEAN_ATTR_CPUINDEX.getName().equals(attr)) {
return new Integer(getCpuIndex()); return new Integer(getCpuIndex());
} else if (MBEAN_ATTR_IDLE.getName().equals(attr)) { } else if (MBEAN_ATTR_IDLE.getName().equals(attr)) {
return new Double(getIdle()); return new Double(getIdle());
} else if (MBEAN_ATTR_NICE.getName().equals(attr)) { } else if (MBEAN_ATTR_NICE.getName().equals(attr)) {
return new Double(getNice()); return new Double(getNice());
} else if (MBEAN_ATTR_SYS.getName().equals(attr)) { } else if (MBEAN_ATTR_SYS.getName().equals(attr)) {
return new Double(getSys()); return new Double(getSys());
} else if (MBEAN_ATTR_USER.getName().equals(attr)) { } else if (MBEAN_ATTR_USER.getName().equals(attr)) {
return new Double(getUser()); return new Double(getUser());
} else if (MBEAN_ATTR_WAIT.getName().equals(attr)) { } else if (MBEAN_ATTR_WAIT.getName().equals(attr)) {
return new Double(getWait()); return new Double(getWait());
} else { } else {
throw new AttributeNotFoundException(attr); throw new AttributeNotFoundException(attr);
} }
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see DynamicMBean#setAttribute(Attribute) * @see DynamicMBean#setAttribute(Attribute)
*/ */
public void setAttribute(Attribute attr) throws AttributeNotFoundException { public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName()); throw new AttributeNotFoundException(attr.getName());
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see DynamicMBean#invoke(String, Object[], String[]) * @see DynamicMBean#invoke(String, Object[], String[])
*/ */
public Object invoke(String actionName, Object[] params, String[] signature) public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException { throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName), throw new ReflectionException(new NoSuchMethodException(actionName),
actionName); actionName);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see DynamicMBean#getMBeanInfo() * @see DynamicMBean#getMBeanInfo()
*/ */
public MBeanInfo getMBeanInfo() { public MBeanInfo getMBeanInfo() {
return MBEAN_INFO; return MBEAN_INFO;
} }
} }

View File

@ -1,251 +1,251 @@
/* /*
* Copyright (C) [2004, 2005, 2006, 2007], Hyperic, Inc. * Copyright (C) [2004, 2005, 2006, 2007], Hyperic, Inc.
* This file is part of SIGAR. * This file is part of SIGAR.
* *
* SIGAR is free software; you can redistribute it and/or modify * SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as * it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed * published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more * PARTICULAR PURPOSE. See the GNU General Public License for more
* details. * details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA. * USA.
*/ */
package org.hyperic.sigar.jmx; package org.hyperic.sigar.jmx;
import javax.management.Attribute; import javax.management.Attribute;
import javax.management.AttributeNotFoundException; import javax.management.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo; import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo; import javax.management.MBeanConstructorInfo;
import javax.management.MBeanException; import javax.management.MBeanException;
import javax.management.MBeanInfo; import javax.management.MBeanInfo;
import javax.management.MBeanParameterInfo; import javax.management.MBeanParameterInfo;
import javax.management.ReflectionException; import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
/** /**
* Sigar JMX MBean implementation for the <code>Mem</code> information * Sigar JMX MBean implementation for the <code>Mem</code> information
* package. Provides an OpenMBean conform implementation. * package. Provides an OpenMBean conform implementation.
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.5 * @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_FREE;
private static final MBeanAttributeInfo MBEAN_ATTR_ACTUAL_USED; private static final MBeanAttributeInfo MBEAN_ATTR_ACTUAL_USED;
private static final MBeanAttributeInfo MBEAN_ATTR_FREE; private static final MBeanAttributeInfo MBEAN_ATTR_FREE;
private static final MBeanAttributeInfo MBEAN_ATTR_RAM; private static final MBeanAttributeInfo MBEAN_ATTR_RAM;
private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL; private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL;
private static final MBeanAttributeInfo MBEAN_ATTR_USED; private static final MBeanAttributeInfo MBEAN_ATTR_USED;
private static final MBeanConstructorInfo MBEAN_CONSTR_SIGAR; private static final MBeanConstructorInfo MBEAN_CONSTR_SIGAR;
private static MBeanParameterInfo MBEAN_PARAM_SIGAR; private static MBeanParameterInfo MBEAN_PARAM_SIGAR;
static { static {
MBEAN_ATTR_ACTUAL_FREE = new MBeanAttributeInfo("ActualFree", "long", MBEAN_ATTR_ACTUAL_FREE = new MBeanAttributeInfo("ActualFree", "long",
"TODO add proper description here", true, false, false); "TODO add proper description here", true, false, false);
MBEAN_ATTR_ACTUAL_USED = new MBeanAttributeInfo("ActualUsed", "long", MBEAN_ATTR_ACTUAL_USED = new MBeanAttributeInfo("ActualUsed", "long",
"TODO add proper description here", true, false, false); "TODO add proper description here", true, false, false);
MBEAN_ATTR_FREE = new MBeanAttributeInfo("Free", "long", MBEAN_ATTR_FREE = new MBeanAttributeInfo("Free", "long",
"TODO add proper description here", true, false, false); "TODO add proper description here", true, false, false);
MBEAN_ATTR_RAM = new MBeanAttributeInfo("Ram", "long", MBEAN_ATTR_RAM = new MBeanAttributeInfo("Ram", "long",
"TODO add proper description here", true, false, false); "TODO add proper description here", true, false, false);
MBEAN_ATTR_TOTAL = new MBeanAttributeInfo("Total", "long", MBEAN_ATTR_TOTAL = new MBeanAttributeInfo("Total", "long",
"TODO add proper description here", true, false, false); "TODO add proper description here", true, false, false);
MBEAN_ATTR_USED = new MBeanAttributeInfo("Used", "long", MBEAN_ATTR_USED = new MBeanAttributeInfo("Used", "long",
"TODO add proper description here", true, false, false); "TODO add proper description here", true, false, false);
MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class
.getName(), "The Sigar instance to use to fetch data from"); .getName(), "The Sigar instance to use to fetch data from");
MBEAN_CONSTR_SIGAR = new MBeanConstructorInfo(SigarMem.class.getName(), MBEAN_CONSTR_SIGAR = new MBeanConstructorInfo(SigarMem.class.getName(),
"Creates a new instance, using the Sigar instance " "Creates a new instance, using the Sigar instance "
+ "specified to fetch the data.", + "specified to fetch the data.",
new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR }); new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR });
MBEAN_INFO = new MBeanInfo( MBEAN_INFO = new MBeanInfo(
SigarMem.class.getName(), SigarMem.class.getName(),
"Sigar Memory MBean, provides raw data for the physical " "Sigar Memory MBean, provides raw data for the physical "
+ "memory installed on the system. Uses an internal cache " + "memory installed on the system. Uses an internal cache "
+ "that invalidates within 500ms, allowing for bulk request " + "that invalidates within 500ms, allowing for bulk request "
+ "being satisfied with a single dataset fetch.", + "being satisfied with a single dataset fetch.",
new MBeanAttributeInfo[] { MBEAN_ATTR_ACTUAL_FREE, new MBeanAttributeInfo[] { MBEAN_ATTR_ACTUAL_FREE,
MBEAN_ATTR_ACTUAL_USED, MBEAN_ATTR_FREE, MBEAN_ATTR_ACTUAL_USED, MBEAN_ATTR_FREE,
MBEAN_ATTR_RAM, MBEAN_ATTR_TOTAL, MBEAN_ATTR_USED }, MBEAN_ATTR_RAM, MBEAN_ATTR_TOTAL, MBEAN_ATTR_USED },
new MBeanConstructorInfo[] { MBEAN_CONSTR_SIGAR }, null, null); new MBeanConstructorInfo[] { MBEAN_CONSTR_SIGAR }, null, null);
} }
/** /**
* 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.
*/ */
private final String objectName; private final String objectName;
/** /**
* Creates a new instance, using the Sigar instance specified to fetch the * Creates a new instance, using the Sigar instance specified to fetch the
* data. * data.
* *
* @param sigar * @param sigar
* The Sigar instance to use to fetch data from * The Sigar instance to use to fetch data from
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If an unexpected Sigar error occurs * If an unexpected Sigar error occurs
*/ */
public SigarMem(Sigar sigar) throws IllegalArgumentException { public SigarMem(Sigar sigar) throws IllegalArgumentException {
super(sigar, CACHED_500MS); super(sigar, CACHED_500MS);
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
+ "=Memory"; + "=Memory";
} }
/** /**
* 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() { public String getObjectName() {
return this.objectName; return this.objectName;
} }
/** /**
* @return The actual amount of free physical memory, in [bytes] * @return The actual amount of free physical memory, in [bytes]
*/ */
public long getActualFree() { public long getActualFree() {
try { try {
return sigar.getMem().getActualFree(); return sigar.getMem().getActualFree();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The actual amount of physical memory used, in [bytes] * @return The actual amount of physical memory used, in [bytes]
*/ */
public long getActualUsed() { public long getActualUsed() {
try { try {
return sigar.getMem().getActualUsed(); return sigar.getMem().getActualUsed();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The amount of free physical memory, in [bytes] * @return The amount of free physical memory, in [bytes]
*/ */
public long getFree() { public long getFree() {
try { try {
return sigar.getMem().getFree(); return sigar.getMem().getFree();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The amount of physical memory, in [bytes] * @return The amount of physical memory, in [bytes]
*/ */
public long getRam() { public long getRam() {
try { try {
return sigar.getMem().getRam(); return sigar.getMem().getRam();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The total amount of physical memory, in [bytes] * @return The total amount of physical memory, in [bytes]
*/ */
public long getTotal() { public long getTotal() {
try { try {
return sigar.getMem().getTotal(); return sigar.getMem().getTotal();
} 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 amount of physical memory in use, in [bytes]
*/ */
public long getUsed() { public long getUsed() {
try { try {
return sigar.getMem().getUsed(); return sigar.getMem().getUsed();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
// ------- // -------
// Implementation of the DynamicMBean interface // Implementation of the DynamicMBean interface
// ------- // -------
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see javax.management.DynamicMBean#getAttribute(java.lang.String) * @see javax.management.DynamicMBean#getAttribute(java.lang.String)
*/ */
public Object getAttribute(String attr) throws AttributeNotFoundException, public Object getAttribute(String attr) throws AttributeNotFoundException,
MBeanException, ReflectionException { MBeanException, ReflectionException {
if (MBEAN_ATTR_ACTUAL_FREE.getName().equals(attr)) { if (MBEAN_ATTR_ACTUAL_FREE.getName().equals(attr)) {
return new Long(getActualFree()); return new Long(getActualFree());
} else if (MBEAN_ATTR_ACTUAL_USED.getName().equals(attr)) { } else if (MBEAN_ATTR_ACTUAL_USED.getName().equals(attr)) {
return new Long(getActualUsed()); return new Long(getActualUsed());
} else if (MBEAN_ATTR_FREE.getName().equals(attr)) { } else if (MBEAN_ATTR_FREE.getName().equals(attr)) {
return new Long(getFree()); return new Long(getFree());
} else if (MBEAN_ATTR_RAM.getName().equals(attr)) { } else if (MBEAN_ATTR_RAM.getName().equals(attr)) {
return new Long(getRam()); return new Long(getRam());
} else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) { } else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) {
return new Long(getTotal()); return new Long(getTotal());
} else if (MBEAN_ATTR_USED.getName().equals(attr)) { } else if (MBEAN_ATTR_USED.getName().equals(attr)) {
return new Long(getUsed()); return new Long(getUsed());
} else { } else {
throw new AttributeNotFoundException(attr); throw new AttributeNotFoundException(attr);
} }
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute) * @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
*/ */
public void setAttribute(Attribute attr) throws AttributeNotFoundException { public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName()); throw new AttributeNotFoundException(attr.getName());
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see javax.management.DynamicMBean#invoke(java.lang.String, * @see javax.management.DynamicMBean#invoke(java.lang.String,
* java.lang.Object[], java.lang.String[]) * java.lang.Object[], java.lang.String[])
*/ */
public Object invoke(String actionName, Object[] params, String[] signature) public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException { throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName), throw new ReflectionException(new NoSuchMethodException(actionName),
actionName); actionName);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see javax.management.DynamicMBean#getMBeanInfo() * @see javax.management.DynamicMBean#getMBeanInfo()
*/ */
public MBeanInfo getMBeanInfo() { public MBeanInfo getMBeanInfo() {
return MBEAN_INFO; return MBEAN_INFO;
} }
} }

View File

@ -1,199 +1,199 @@
/* /*
* Copyright (C) [2004, 2005, 2006, 2007], Hyperic, Inc. * Copyright (C) [2004, 2005, 2006, 2007], Hyperic, Inc.
* This file is part of SIGAR. * This file is part of SIGAR.
* *
* SIGAR is free software; you can redistribute it and/or modify * SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as * it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed * published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more * PARTICULAR PURPOSE. See the GNU General Public License for more
* details. * details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA. * USA.
*/ */
package org.hyperic.sigar.jmx; package org.hyperic.sigar.jmx;
import javax.management.Attribute; import javax.management.Attribute;
import javax.management.AttributeNotFoundException; import javax.management.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo; import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo; import javax.management.MBeanConstructorInfo;
import javax.management.MBeanException; import javax.management.MBeanException;
import javax.management.MBeanInfo; import javax.management.MBeanInfo;
import javax.management.MBeanParameterInfo; import javax.management.MBeanParameterInfo;
import javax.management.ReflectionException; import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
/** /**
* Sigar JMX MBean implementation for the <code>Swap</code> information * Sigar JMX MBean implementation for the <code>Swap</code> information
* package. Provides an OpenMBean conform implementation. * package. Provides an OpenMBean conform implementation.
* *
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.5 * @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_FREE;
private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL; private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL;
private static final MBeanAttributeInfo MBEAN_ATTR_USED; private static final MBeanAttributeInfo MBEAN_ATTR_USED;
private static final MBeanConstructorInfo MBEAN_CONSTR_SIGAR; private static final MBeanConstructorInfo MBEAN_CONSTR_SIGAR;
private static MBeanParameterInfo MBEAN_PARAM_SIGAR; private static MBeanParameterInfo MBEAN_PARAM_SIGAR;
static { static {
MBEAN_ATTR_FREE = new MBeanAttributeInfo("Free", "long", MBEAN_ATTR_FREE = new MBeanAttributeInfo("Free", "long",
"The amount of free swap memory, in [bytes]", true, false, "The amount of free swap memory, in [bytes]", true, false,
false); false);
MBEAN_ATTR_TOTAL = new MBeanAttributeInfo("Total", "long", MBEAN_ATTR_TOTAL = new MBeanAttributeInfo("Total", "long",
"The total amount of swap memory, in [bytes]", true, false, "The total amount of swap memory, in [bytes]", true, false,
false); false);
MBEAN_ATTR_USED = new MBeanAttributeInfo("Used", "long", MBEAN_ATTR_USED = new MBeanAttributeInfo("Used", "long",
"The amount of swap memory in use, in [bytes]", true, false, "The amount of swap memory in use, in [bytes]", true, false,
false); false);
MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class MBEAN_PARAM_SIGAR = new MBeanParameterInfo("sigar", Sigar.class
.getName(), "The Sigar instance to use to fetch data from"); .getName(), "The Sigar instance to use to fetch data from");
MBEAN_CONSTR_SIGAR = new MBeanConstructorInfo( MBEAN_CONSTR_SIGAR = new MBeanConstructorInfo(
SigarSwap.class.getName(), SigarSwap.class.getName(),
"Creates a new instance, using the Sigar instance " "Creates a new instance, using the Sigar instance "
+ "specified to fetch the data.", + "specified to fetch the data.",
new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR }); new MBeanParameterInfo[] { MBEAN_PARAM_SIGAR });
MBEAN_INFO = new MBeanInfo( MBEAN_INFO = new MBeanInfo(
SigarSwap.class.getName(), SigarSwap.class.getName(),
"Sigar Swap MBean, provides raw data for the swap memory " "Sigar Swap MBean, provides raw data for the swap memory "
+ "configured on the system. Uses an internal cache that " + "configured on the system. Uses an internal cache that "
+ "invalidates within 5 seconds.", + "invalidates within 5 seconds.",
new MBeanAttributeInfo[] { MBEAN_ATTR_FREE, MBEAN_ATTR_TOTAL, new MBeanAttributeInfo[] { MBEAN_ATTR_FREE, MBEAN_ATTR_TOTAL,
MBEAN_ATTR_USED }, MBEAN_ATTR_USED },
new MBeanConstructorInfo[] { MBEAN_CONSTR_SIGAR }, null, null); new MBeanConstructorInfo[] { MBEAN_CONSTR_SIGAR }, null, null);
} }
/** /**
* 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.
*/ */
private final String objectName; private final String objectName;
/** /**
* Creates a new instance, using the Sigar instance specified to fetch the * Creates a new instance, using the Sigar instance specified to fetch the
* data. * data.
* *
* @param sigar * @param sigar
* The Sigar instance to use to fetch data from * The Sigar instance to use to fetch data from
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If an unexpected Sigar error occurs * If an unexpected Sigar error occurs
*/ */
public SigarSwap(Sigar sigar) throws IllegalArgumentException { public SigarSwap(Sigar sigar) throws IllegalArgumentException {
super(sigar, CACHED_5SEC); super(sigar, CACHED_5SEC);
this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE this.objectName = SigarInvokerJMX.DOMAIN_NAME + ":" + MBEAN_ATTR_TYPE
+ "=Swap"; + "=Swap";
} }
/** /**
* 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() { public String getObjectName() {
return this.objectName; return this.objectName;
} }
/** /**
* @return The amount of free swap memory, in [bytes] * @return The amount of free swap memory, in [bytes]
*/ */
public long getFree() { public long getFree() {
try { try {
return sigar.getSwap().getFree(); return sigar.getSwap().getFree();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The total amount of swap memory, in [bytes] * @return The total amount of swap memory, in [bytes]
*/ */
public long getTotal() { public long getTotal() {
try { try {
return sigar.getSwap().getTotal(); return sigar.getSwap().getTotal();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
/** /**
* @return The amount of swap memory in use, in [bytes] * @return The amount of swap memory in use, in [bytes]
*/ */
public long getUsed() { public long getUsed() {
try { try {
return sigar.getSwap().getUsed(); return sigar.getSwap().getUsed();
} catch (SigarException e) { } catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e); throw unexpectedError(MBEAN_TYPE, e);
} }
} }
// ------- // -------
// Implementation of the DynamicMBean interface // Implementation of the DynamicMBean interface
// ------- // -------
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see javax.management.DynamicMBean#getAttribute(java.lang.String) * @see javax.management.DynamicMBean#getAttribute(java.lang.String)
*/ */
public Object getAttribute(String attr) throws AttributeNotFoundException, public Object getAttribute(String attr) throws AttributeNotFoundException,
MBeanException, ReflectionException { MBeanException, ReflectionException {
if (MBEAN_ATTR_FREE.getName().equals(attr)) { if (MBEAN_ATTR_FREE.getName().equals(attr)) {
return new Long(getFree()); return new Long(getFree());
} else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) { } else if (MBEAN_ATTR_TOTAL.getName().equals(attr)) {
return new Long(getTotal()); return new Long(getTotal());
} else if (MBEAN_ATTR_USED.getName().equals(attr)) { } else if (MBEAN_ATTR_USED.getName().equals(attr)) {
return new Long(getUsed()); return new Long(getUsed());
} else { } else {
throw new AttributeNotFoundException(attr); throw new AttributeNotFoundException(attr);
} }
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute) * @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
*/ */
public void setAttribute(Attribute attr) throws AttributeNotFoundException { public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName()); throw new AttributeNotFoundException(attr.getName());
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see javax.management.DynamicMBean#invoke(java.lang.String, * @see javax.management.DynamicMBean#invoke(java.lang.String,
* java.lang.Object[], java.lang.String[]) * java.lang.Object[], java.lang.String[])
*/ */
public Object invoke(String actionName, Object[] params, String[] signature) public Object invoke(String actionName, Object[] params, String[] signature)
throws ReflectionException { throws ReflectionException {
throw new ReflectionException(new NoSuchMethodException(actionName), throw new ReflectionException(new NoSuchMethodException(actionName),
actionName); actionName);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see javax.management.DynamicMBean#getMBeanInfo() * @see javax.management.DynamicMBean#getMBeanInfo()
*/ */
public MBeanInfo getMBeanInfo() { public MBeanInfo getMBeanInfo() {
return MBEAN_INFO; return MBEAN_INFO;
} }
} }