convert Cpu*List to ReflectedMBeans

This commit is contained in:
Doug MacEachern 2008-10-09 06:13:05 +00:00
parent c409cb469f
commit 1c75927eca
4 changed files with 12 additions and 896 deletions

View File

@ -1,312 +0,0 @@
/*
* 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.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanParameterInfo;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
/**
* Sigar JMX MBean implementation for the <code>Cpu</code> information
* package. Provides an OpenMBean conform implementation.
*
* @author Bjoern Martin
* @since 1.5
*/
public class SigarCpu extends AbstractMBean {
private static final String MBEAN_TYPE = "CpuList";
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_NICE;
private static final MBeanAttributeInfo MBEAN_ATTR_SYS;
private static final MBeanAttributeInfo MBEAN_ATTR_TOTAL;
private static final MBeanAttributeInfo MBEAN_ATTR_USER;
private static final MBeanAttributeInfo MBEAN_ATTR_WAIT;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR;
private static MBeanParameterInfo MBEAN_PARAM_CPUINDEX;
private static MBeanParameterInfo MBEAN_PARAM_SIGAR;
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);
}
/**
* Index of the CPU processed by the instance.
*/
private final int cpuIndex;
/**
* Object name this instance will give itself when being registered to an
* MBeanServer.
*/
private final String objectName;
/**
* 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
* 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 SigarCpu(Sigar sigar, int cpuIndex) throws IllegalArgumentException {
super(sigar, CACHED_500MS);
// check index
if (cpuIndex < 0)
throw new IllegalArgumentException(
"CPU index has to be non-negative: " + cpuIndex);
try {
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
* 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#getMBeanInfo()
*/
public MBeanInfo getMBeanInfo() {
return MBEAN_INFO;
}
}

View File

@ -1,266 +0,0 @@
/*
* 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.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanParameterInfo;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
/**
* Sigar JMX MBean implementation for the <code>CpuInfo</code> information
* package. Provides an OpenMBean conform implementation.
*
* @author Bjoern Martin
* @since 1.5
*/
public class SigarCpuInfo extends AbstractMBean {
private static final String MBEAN_TYPE = "CpuInfoList";
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_MHZ;
private static final MBeanAttributeInfo MBEAN_ATTR_MODEL;
private static final MBeanAttributeInfo MBEAN_ATTR_VENDOR;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR;
private static final MBeanParameterInfo MBEAN_PARAM_CPUINDEX;
private static final MBeanParameterInfo MBEAN_PARAM_SIGAR;
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;
/**
* Object name this instance will give itself when being registered to an
* MBeanServer.
*/
private String objectName;
/**
* 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 SigarCpuInfo(int index) throws IllegalArgumentException {
this(new Sigar(), index);
}
/**
* 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)
throw new IllegalArgumentException(
"CPU index has to be non-negative: " + index);
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
* 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 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#getMBeanInfo()
*/
public MBeanInfo getMBeanInfo() {
return MBEAN_INFO;
}
}

View File

@ -1,313 +0,0 @@
/*
* 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.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanParameterInfo;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
/**
* <p>Sigar JMX MBean implementation for the <code>CpuPerc</code> information
* package. Provides an OpenMBean conform implementation.</p>
*
* @author Bjoern Martin
* @since 1.5
*/
public class SigarCpuPerc extends AbstractMBean {
private static final String MBEAN_TYPE = "CpuPercList";
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_IDLE;
private static final MBeanAttributeInfo MBEAN_ATTR_NICE;
private static final MBeanAttributeInfo MBEAN_ATTR_SYS;
private static final MBeanAttributeInfo MBEAN_ATTR_USER;
private static final MBeanAttributeInfo MBEAN_ATTR_WAIT;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX;
private static final MBeanConstructorInfo MBEAN_CONSTR_CPUINDEX_SIGAR;
private static MBeanParameterInfo MBEAN_PARAM_CPUINDEX;
private static MBeanParameterInfo MBEAN_PARAM_SIGAR;
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);
}
/**
* Index of the CPU processed by the instance.
*/
private int cpuIndex;
/**
* Object name this instance will give itself when being registered to an
* MBeanServer.
*/
private String objectName;
/**
* 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 SigarCpuPerc(int index) {
this(new Sigar(), index);
}
/**
* 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);
// check index
if (index < 0)
throw new IllegalArgumentException(
"CPU index has to be non-negative: " + index);
try {
int cpuCount;
if ((cpuCount = sigar.getCpuPercList().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
+ "=CpuPerc,"
+ 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
* 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 total time of the CPU, as a fraction of 1
*/
public double getCombined() {
try {
return sigar.getCpuPercList()[this.cpuIndex].getCombined();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The idle time of the CPU, as a fraction of 1
*/
public double getIdle() {
try {
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
*/
public double getNice() {
try {
return sigar.getCpuPercList()[this.cpuIndex].getNice();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The time of the CPU used by the system, as a fraction of 1
*/
public double getSys() {
try {
return sigar.getCpuPercList()[this.cpuIndex].getSys();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The time of the CPU used by user processes, as a fraction of 1
*/
public double getUser() {
try {
return sigar.getCpuPercList()[this.cpuIndex].getUser();
} catch (SigarException e) {
throw unexpectedError(MBEAN_TYPE, e);
}
}
/**
* @return The time the CPU had to wait for data to be loaded, as a fraction of 1
*/
public double getWait() {
try {
return sigar.getCpuPercList()[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_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)
* @see DynamicMBean#getMBeanInfo()
*/
public MBeanInfo getMBeanInfo() {
return MBEAN_INFO;
}
}

View File

@ -32,6 +32,7 @@ import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.Sigar;
@ -222,11 +223,17 @@ public class SigarRegistry extends AbstractMBean {
//CPU beans
try {
final int cpuCount = sigar.getCpuInfoList().length;
for (int i = 0; i < cpuCount; i++) {
registerMBean(new SigarCpu(sigarImpl, i));
registerMBean(new SigarCpuPerc(sigarImpl, i));
registerMBean(new SigarCpuInfo(sigarImpl, i));
CpuInfo[] info = sigar.getCpuInfoList();
for (int i=0; i<info.length; i++) {
String idx = String.valueOf(i);
ReflectedMBean mbean =
new ReflectedMBean(sigarImpl, "CpuCoreTime", idx);
mbean.setType("CpuList");
registerMBean(mbean);
mbean =
new ReflectedMBean(sigarImpl, "CpuCoreUsage", idx);
mbean.setType("CpuPercList");
registerMBean(mbean);
}
} catch (SigarException e) {
throw unexpectedError("CpuInfoList", e);