难道只能用JNI实现JMX或者JDMK的instrumentation layer?

最近在用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类 部分代码
  1. // Generated by mibgen version 5.1 (05/20/05) when compiling RFC1213-MIB in standard metadata mode.   
  2. import com.sun.management.snmp.SnmpStatusException;   
  3.   
  4. /**  
  5.  * This interface is used for representing the remote management interface for the "System" MBean.  
  6.  */  
  7. public interface SystemMBean {   
  8.   
  9.     /**  
  10.      * Getter for the "SysLocation" variable.  
  11.      */  
  12.     public String getSysLocation() throws SnmpStatusException;   
  13.   
  14.     /**  
  15.      * Setter for the "SysLocation" variable.  
  16.      */  
  17.     public void setSysLocation(String x) throws SnmpStatusException;   
  18.   
  19.     /**  
  20.      * Checker for the "SysLocation" variable.  
  21.      */  
  22.     public void checkSysLocation(String x) throws SnmpStatusException;   
  23.   
  24.     /**  
  25.      * Getter for the "SysName" variable.  
  26.      */  
  27.     public String getSysName() throws SnmpStatusException;   
  28.   
  29.     /**  
  30.      * Setter for the "SysName" variable.  
  31.      */  
  32.     public void setSysName(String x) throws SnmpStatusException;   
  33.   
  34.     /**  
  35.      * Checker for the "SysName" variable.  
  36.      */  
  37.     public void checkSysName(String x) throws SnmpStatusException;   
  38.   
  39.    ………………   
  40. }  
System类 部分代码
  1.   
  2. // Generated by mibgen version 5.1 (05/20/05) when compiling RFC1213-MIB.   
  3. import java.io.Serializable;   
  4.   
  5. /**  
  6.  * The class is used for implementing the "System" group.  
  7.  * The group is defined with the following oid: 1.3.6.1.2.1.1.  
  8.  */  
  9. public class System implements SystemMBean, Serializable {   
  10.   
  11.     /**  
  12.      * Variable for storing the value of "SysLocation".  
  13.      * The variable is identified by: "1.3.6.1.2.1.1.6".  
  14.      */  
  15.     protected String SysLocation = new String("JDMK 5.1");   
  16.   
  17.     /**  
  18.      * Variable for storing the value of "SysName".  
  19.      * The variable is identified by: "1.3.6.1.2.1.1.5".  
  20.      */  
  21.     protected String SysName = new String("JDMK 5.1");   
  22.   
  23.     /**  
  24.      * Variable for storing the value of "SysContact".  
  25.      * The variable is identified by: "1.3.6.1.2.1.1.4".  
  26.      */  
  27.     protected String SysContact = new String("JDMK 5.1");   
  28.   
  29.     /**  
  30.      * Variable for storing the value of "SysUpTime".  
  31.      * The variable is identified by: "1.3.6.1.2.1.1.3".  
  32.      */  
  33.     protected Long SysUpTime = new Long(1);   
  34.   
  35.     /**  
  36.      * Variable for storing the value of "SysObjectID".  
  37.      * The variable is identified by: "1.3.6.1.2.1.1.2".  
  38.      */  
  39.     protected String SysObjectID = new String("1.3.6.1.4.1.42");   
  40.   
  41.     /**  
  42.      * Variable for storing the value of "SysDescr".  
  43.      * The variable is identified by: "1.3.6.1.2.1.1.1".  
  44.      */  
  45.     protected String SysDescr = new String("JDMK 5.1");   
  46.   
  47.     /**  
  48.      * Variable for storing the value of "SysServices".  
  49.      * The variable is identified by: "1.3.6.1.2.1.1.7".  
  50.      */  
  51.     protected Integer SysServices = new Integer(1);   
  52.   
  53.   
  54.     /**  
  55.      * Constructor for the "System" group.  
  56.      * If the group contains a table, the entries created through an SNMP SET will not be registered in Java DMK.  
  57.      */  
  58.     public System(SnmpMib myMib) {   
  59.     }   
  60.   
  61.   
  62.     /**  
  63.      * Constructor for the "System" group.  
  64.      * If the group contains a table, the entries created through an SNMP SET will be AUTOMATICALLY REGISTERED in Java DMK.  
  65.      */  
  66.     public System(SnmpMib myMib, MBeanServer server) {   
  67.     }   
  68.   
  69.     /**  
  70.      * Getter for the "SysLocation" variable.  
  71.      */  
  72.     public String getSysLocation() throws SnmpStatusException {   
  73.         return SysLocation;   
  74.     }   
  75.   
  76.     /**  
  77.      * Setter for the "SysLocation" variable.  
  78.      */  
  79.     public void setSysLocation(String x) throws SnmpStatusException {   
  80.         SysLocation = x;   
  81.     }   
  82.   
  83.     /**  
  84.      * Checker for the "SysLocation" variable.  
  85.      */  
  86.     public void checkSysLocation(String x) throws SnmpStatusException {   
  87.         //   
  88.         // Add your own checking policy.   
  89.         //   
  90.     }   
  91.   
  92.     /**  
  93.      * Getter for the "SysName" variable.  
  94.      */  
  95.     public String getSysName() throws SnmpStatusException {   
  96.         return SysName;   
  97.     }   
  98.   
  99.     /**  
  100.      * Setter for the "SysName" variable.  
  101.      */  
  102.     public void setSysName(String x) throws SnmpStatusException {   
  103.         SysName = x;   
  104.     }   
  105.   
  106.     /**  
  107.      * Checker for the "SysName" variable.  
  108.      */  
  109.     public void checkSysName(String x) throws SnmpStatusException {   
  110.         //   
  111.         // Add your own checking policy.   
  112.         //   
  113.     }   
  114. ………………   
  115. }   

在该例子中,给出了继承System的类SystemImpl

SystemImpl 代码
  1.  * @(#)file      SystemImpl.java   
  2. import java.net.InetAddress;   
  3.   
  4.   
  5. /**  
  6.  * The SystemImpl class provides an simple implementation of the "System" group.  
  7.  */  
  8.   
  9. public class SystemImpl extends System {   
  10.   
  11.     // MBean properties.   
  12.     //   
  13.        
  14.     // Start up time of the agent.   
  15.     //   
  16.     private long startUpTime = 0;   
  17.   
  18.     /**  
  19.      * Constructors.  
  20.      */  
  21.     public SystemImpl(SnmpMib myMib) {   
  22.        
  23.         super(myMib);   
  24.         init();   
  25.     }   
  26.        
  27.     public SystemImpl(SnmpMib myMib, MBeanServer server) {   
  28.        
  29.         super(myMib, server);   
  30.       //  java.lang.System.out.println(((SnmpAdaptorServer)myMib.getSnmpAdaptor()).getSnmpInGetNexts()+"adffds");   
  31.            
  32.         init();   
  33.     }   
  34.        
  35.     private void init() {   
  36.            
  37.         // Initialize the system description using some system properties.   
  38.         //   
  39.         try {   
  40.             SysDescr = java.lang.System.getProperty("os.name") + " " +   
  41.                 java.lang.System.getProperty("os.arch") + " " +   
  42.                 java.lang.System.getProperty("os.version");   
  43.         } catch(SecurityException e) {   
  44.             // Do not process the exception   
  45.         }   
  46.        
  47.         // Initialize the system name using the hostname.   
  48.         //   
  49.         try {   
  50.             SysName = (InetAddress.getLocalHost()).getHostName();   
  51.         } catch(UnknownHostException e) {   
  52.             // Do not process the exception   
  53.         }   
  54.        
  55.         // Initialize the system contact using some system properties.   
  56.         //   
  57.         try {   
  58.             SysContact = java.lang.System.getProperty("user.name");   
  59.         } catch(SecurityException e) {   
  60.             // Do not process the exception   
  61.         }   
  62.        
  63.         // Initialize the set of services. Assume the agent only deals   
  64.         // with application layer.   
  65.         //   
  66.         SysServices = new Integer(72);   
  67.   
  68.         // Initialize the location with a dummy string.   
  69.         //   
  70.         SysLocation = "Sample implementation of system group.";   
  71.        
  72.         // For the sysUpTime, use the time the agent started...   
  73.         //   
  74.         startUpTime = java.lang.System.currentTimeMillis();   
  75.     }   
  76.        
  77.     public Long getSysUpTime() throws SnmpStatusException {   
  78.        
  79.         long diff = java.lang.System.currentTimeMillis() - startUpTime;   
  80.         return new Long(diff);   
  81.     }   
  82. }   

在SystemImpl中,是具体的实现instrumentation layer层中的MBean的例子,但是如果不是获得System group上的信息,而是获得CPU利用率,一般情况下,通过在客户端装snmp agent(net-snmp),然后用snmpget命令根据OID可以获得CPU利用率。但是在JDMK中,用java实现的话,是不是只能够通过JNI的方式来建立MBean,能否在JDMK下,利用SNMP的OID获得资源的状态信息呢?有没有其他可行的方法呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值