最近在用JDMK做网上硬件资源和软件资源的监控,目的是得到这些资源的状态信息(例如:CPU利用率,内存利率,操作系统名称等),按照sun官方提供的JDMK的一个例子,是这样的:
利用JDMK所提供的mibgen工具,将一个MIB definition文件转换成多个MIB MBean,其中包含:一个主要的MBean类,它能够代表所有的MIB;代表SNMP groups或者entities的多个MBean和与之对应的metadata类;代表SNMP tables的多个类;代表SNMP enumerated type的多个类。
在产生的类中,该例子提供了一些实现,现在举个实现的SNMP的MIB system group分支上的MBean的例子:
SystemMBean类 部分代码
- // Generated by mibgen version 5.1 (05/20/05) when compiling RFC1213-MIB in standard metadata mode.
- import com.sun.management.snmp.SnmpStatusException;
- /**
- * This interface is used for representing the remote management interface for the "System" MBean.
- */
- public interface SystemMBean {
- /**
- * Getter for the "SysLocation" variable.
- */
- public String getSysLocation() throws SnmpStatusException;
- /**
- * Setter for the "SysLocation" variable.
- */
- public void setSysLocation(String x) throws SnmpStatusException;
- /**
- * Checker for the "SysLocation" variable.
- */
- public void checkSysLocation(String x) throws SnmpStatusException;
- /**
- * Getter for the "SysName" variable.
- */
- public String getSysName() throws SnmpStatusException;
- /**
- * Setter for the "SysName" variable.
- */
- public void setSysName(String x) throws SnmpStatusException;
- /**
- * Checker for the "SysName" variable.
- */
- public void checkSysName(String x) throws SnmpStatusException;
- ………………
- }
System类 部分代码
- // Generated by mibgen version 5.1 (05/20/05) when compiling RFC1213-MIB.
- import java.io.Serializable;
- /**
- * The class is used for implementing the "System" group.
- * The group is defined with the following oid: 1.3.6.1.2.1.1.
- */
- public class System implements SystemMBean, Serializable {
- /**
- * Variable for storing the value of "SysLocation".
- * The variable is identified by: "1.3.6.1.2.1.1.6".
- */
- protected String SysLocation = new String("JDMK 5.1");
- /**
- * Variable for storing the value of "SysName".
- * The variable is identified by: "1.3.6.1.2.1.1.5".
- */
- protected String SysName = new String("JDMK 5.1");
- /**
- * Variable for storing the value of "SysContact".
- * The variable is identified by: "1.3.6.1.2.1.1.4".
- */
- protected String SysContact = new String("JDMK 5.1");
- /**
- * Variable for storing the value of "SysUpTime".
- * The variable is identified by: "1.3.6.1.2.1.1.3".
- */
- protected Long SysUpTime = new Long(1);
- /**
- * Variable for storing the value of "SysObjectID".
- * The variable is identified by: "1.3.6.1.2.1.1.2".
- */
- protected String SysObjectID = new String("1.3.6.1.4.1.42");
- /**
- * Variable for storing the value of "SysDescr".
- * The variable is identified by: "1.3.6.1.2.1.1.1".
- */
- protected String SysDescr = new String("JDMK 5.1");
- /**
- * Variable for storing the value of "SysServices".
- * The variable is identified by: "1.3.6.1.2.1.1.7".
- */
- protected Integer SysServices = new Integer(1);
- /**
- * Constructor for the "System" group.
- * If the group contains a table, the entries created through an SNMP SET will not be registered in Java DMK.
- */
- public System(SnmpMib myMib) {
- }
- /**
- * Constructor for the "System" group.
- * If the group contains a table, the entries created through an SNMP SET will be AUTOMATICALLY REGISTERED in Java DMK.
- */
- public System(SnmpMib myMib, MBeanServer server) {
- }
- /**
- * Getter for the "SysLocation" variable.
- */
- public String getSysLocation() throws SnmpStatusException {
- return SysLocation;
- }
- /**
- * Setter for the "SysLocation" variable.
- */
- public void setSysLocation(String x) throws SnmpStatusException {
- SysLocation = x;
- }
- /**
- * Checker for the "SysLocation" variable.
- */
- public void checkSysLocation(String x) throws SnmpStatusException {
- //
- // Add your own checking policy.
- //
- }
- /**
- * Getter for the "SysName" variable.
- */
- public String getSysName() throws SnmpStatusException {
- return SysName;
- }
- /**
- * Setter for the "SysName" variable.
- */
- public void setSysName(String x) throws SnmpStatusException {
- SysName = x;
- }
- /**
- * Checker for the "SysName" variable.
- */
- public void checkSysName(String x) throws SnmpStatusException {
- //
- // Add your own checking policy.
- //
- }
- ………………
- }
在该例子中,给出了继承System的类SystemImpl
SystemImpl 代码
- * @(#)file SystemImpl.java
- import java.net.InetAddress;
- /**
- * The SystemImpl class provides an simple implementation of the "System" group.
- */
- public class SystemImpl extends System {
- // MBean properties.
- //
- // Start up time of the agent.
- //
- private long startUpTime = 0;
- /**
- * Constructors.
- */
- public SystemImpl(SnmpMib myMib) {
- super(myMib);
- init();
- }
- public SystemImpl(SnmpMib myMib, MBeanServer server) {
- super(myMib, server);
- // java.lang.System.out.println(((SnmpAdaptorServer)myMib.getSnmpAdaptor()).getSnmpInGetNexts()+"adffds");
- init();
- }
- private void init() {
- // Initialize the system description using some system properties.
- //
- try {
- SysDescr = java.lang.System.getProperty("os.name") + " " +
- java.lang.System.getProperty("os.arch") + " " +
- java.lang.System.getProperty("os.version");
- } catch(SecurityException e) {
- // Do not process the exception
- }
- // Initialize the system name using the hostname.
- //
- try {
- SysName = (InetAddress.getLocalHost()).getHostName();
- } catch(UnknownHostException e) {
- // Do not process the exception
- }
- // Initialize the system contact using some system properties.
- //
- try {
- SysContact = java.lang.System.getProperty("user.name");
- } catch(SecurityException e) {
- // Do not process the exception
- }
- // Initialize the set of services. Assume the agent only deals
- // with application layer.
- //
- SysServices = new Integer(72);
- // Initialize the location with a dummy string.
- //
- SysLocation = "Sample implementation of system group.";
- // For the sysUpTime, use the time the agent started...
- //
- startUpTime = java.lang.System.currentTimeMillis();
- }
- public Long getSysUpTime() throws SnmpStatusException {
- long diff = java.lang.System.currentTimeMillis() - startUpTime;
- return new Long(diff);
- }
- }
在SystemImpl中,是具体的实现instrumentation layer层中的MBean的例子,但是如果不是获得System group上的信息,而是获得CPU利用率,一般情况下,通过在客户端装snmp agent(net-snmp),然后用snmpget命令根据OID可以获得CPU利用率。但是在JDMK中,用java实现的话,是不是只能够通过JNI的方式来建立MBean,能否在JDMK下,利用SNMP的OID获得资源的状态信息呢?有没有其他可行的方法呢?