OID-----利用SNMP获取、走访节点值【程序代码+分析】

来源:互联网 发布:北斗星通是大数据吗 编辑:程序博客网 时间:2024/05/16 12:05


如何使用snmp协议获取局域网中的IP地址,根据IP地址获取设备的OID,通过oid获取设备的信息?

如何使用snmp协议获取局域网中的IP地址,根据IP地址获取设备的OID,通过oid获取设备的信息,比如是:内存利用率,cpu利用率,磁盘的大小及使用情况。 
忘各位能给点思路,谢谢。如果能有实例代码的话,那就感激不尽了。
问题补充:
谢谢 lovewhzlq  提供的资料代码。 
但是还有问题,就是没有解决怎么获取对应IP中所有oid 
能给点思路么? 
如果有好的snmp资料系统能共享下,谢谢! 

问题补充:
现在的问题是,如果我想对一个局域网内的所有设备进行监控,我知道的只有共同体名称和ip地址,我想通过这两个信息还获取OID,能获取到么?

=====================================================================================================================

文章来源:http://www.iteye.com/topic/292931【文中用到的jar包可以到此网址的最下方下载

  • java-snmp-1.4.2.jar (95.7 KB)
  • 下载次数: 1134
  • SNMP4J.jar (361.5 KB)
  • 下载次数: 716

 本例用到两个开源包,snmpget使用SNMP4J框架,snmpwalk使用Java SNMP Package开源包,下载地址分别为:

http://www.snmp4j.org/html/download.html

http://gicl.cs.drexel.edu/people/sevy/snmp/

Java代码  收藏代码
  1. /** 
  2.  * SNMP管理类 
  3.  */  
  4. public class SnmpManager {  
  5.   
  6.     private static final Log log = LogFactory.getLog(SnmpManager.class);  
  7.       
  8.     private static int version = 0// SNMP版本, 0表示版本1  
  9.       
  10.     private static String protocol = "udp"// 监控时使用的协议  
  11.       
  12.     private static String port = "161"// 监控时使用的端口  
  13.           
  14.     /** 
  15.      * 获取SNMP节点值 
  16.      *  
  17.      * @param ipAddress 目标IP地址 
  18.      * @param community 公同体 
  19.      * @param oid 对象ID 
  20.      * @return String 监控结果代号 
  21.      * @throws AppException 
  22.      */  
  23.     @SuppressWarnings("unchecked")  
  24.     public static String snmpGet(String ipAddress, String community, String oid) throws AppException {  
  25.         String resultStat = null// 监控结果状态  
  26.           
  27.         StringBuffer address = new StringBuffer();  
  28.         address.append(protocol);  
  29.         address.append(":");  
  30.         address.append(ipAddress);  
  31.         address.append("/");  
  32.         address.append(port);  
  33.           
  34. //      Address targetAddress = GenericAddress.parse(protocol + ":" + ipAddress + "/" + port);  
  35.         Address targetAddress = GenericAddress.parse(address.toString());  
  36.         PDU pdu = new PDU();  
  37.         pdu.add(new VariableBinding(new OID(oid)));  
  38.         pdu.setType(PDU.GET);  
  39.   
  40.         // 创建共同体对象CommunityTarget  
  41.         CommunityTarget target = new CommunityTarget();  
  42.         target.setCommunity(new OctetString(community));  
  43.         target.setAddress(targetAddress);  
  44.         target.setVersion(SnmpConstants.version1);  
  45.         target.setTimeout(2000);  
  46.         target.setRetries(1);  
  47.   
  48.         DefaultUdpTransportMapping udpTransportMap = null;  
  49.         Snmp snmp = null;  
  50.         try {  
  51.             // 发送同步消息  
  52.             udpTransportMap = new DefaultUdpTransportMapping();  
  53.             udpTransportMap.listen();  
  54.             snmp = new Snmp(udpTransportMap);  
  55.             ResponseEvent response = snmp.send(pdu, target);  
  56.             PDU resposePdu = response.getResponse();  
  57.   
  58.             if (resposePdu == null) {  
  59.                 log.info(ipAddress + ": Request timed out.");  
  60.             } else {  
  61.                 //errorStatus = resposePdu.getErrorStatus();  
  62.                 Object obj = resposePdu.getVariableBindings().firstElement();  
  63.                 VariableBinding variable = (VariableBinding) obj;  
  64.                 resultStat = variable.getVariable().toString();  
  65.             }  
  66.         } catch (Exception e) {  
  67.             throw new AppException("获取SNMP节点状态时发生错误!", e);  
  68.         } finally {  
  69.             if (snmp != null) {  
  70.                 try {  
  71.                     snmp.close();  
  72.                 } catch (IOException e) {  
  73.                     snmp = null;  
  74.                 }  
  75.             }  
  76.             if (udpTransportMap != null) {  
  77.                 try {  
  78.                     udpTransportMap.close();  
  79.                 } catch (IOException e) {  
  80.                     udpTransportMap = null;  
  81.                 }  
  82.             }  
  83.         }  
  84.           
  85.         if (log.isInfoEnabled()) {  
  86.             log.info("IP:" + ipAddress + " resultStat:" + resultStat);  
  87.         }  
  88.           
  89.         return resultStat;  
  90.     }  
  91.       
  92.       
  93.     /** 
  94.      * 走访SNMP节点 
  95.      *  
  96.      * @param ipAddress 目标IP地址 
  97.      * @param community 共同体 
  98.      * @param oid 节点起始对象标志符 
  99.      * @return String[] 走方结果 
  100.      * @throws AppException 
  101.      */  
  102.     public static String[] snmpWalk(String ipAddress, String community, String oid) throws AppException {  
  103.         String[] returnValueString = null// oid走访结果数组  
  104.           
  105.         SNMPv1CommunicationInterface comInterface = null;  
  106.         try {  
  107.             InetAddress hostAddress = InetAddress.getByName(ipAddress);  
  108.             comInterface = new SNMPv1CommunicationInterface(  
  109.                     version, hostAddress, community);  
  110.             comInterface.setSocketTimeout(2000);  
  111.               
  112.             // 返回所有以oid开始的管理信息库变量值  
  113.             SNMPVarBindList tableVars = comInterface.retrieveMIBTable(oid);  
  114.             returnValueString = new String[tableVars.size()];  
  115.               
  116.             // 循环处理所有以oid开始的节点的返回值  
  117.             for (int i = 0; i < tableVars.size(); i++) {  
  118.                 SNMPSequence pair = (SNMPSequence) tableVars.getSNMPObjectAt(i); // 获取SNMP序列对象, 即(OID,value)对  
  119.                 SNMPObject snmpValue = pair.getSNMPObjectAt(1); // 获取某个节点的返回值  
  120.                 String typeString = snmpValue.getClass().getName(); // 获取SNMP值类型名  
  121.                 // 设置返回值  
  122.                 if (typeString.equals("snmp.SNMPOctetString")) {  
  123.                     String snmpString = snmpValue.toString();  
  124.                     int nullLocation = snmpString.indexOf('\0');  
  125.                     if (nullLocation >= 0)  
  126.                         snmpString = snmpString.substring(0, nullLocation);  
  127.                     returnValueString[i] = snmpString;  
  128.                 } else {  
  129.                     returnValueString[i] = snmpValue.toString();  
  130.                 }  
  131.             }  
  132.         } catch (SocketTimeoutException ste) {  
  133.             if (log.isErrorEnabled()) {  
  134.                 log.error("走访IP为" + ipAddress + ", OID为" + oid + " 时超时!");  
  135.             }  
  136.             returnValueString = null;  
  137.         } catch (Exception e) {  
  138.             throw new AppException("SNMP走访节点时发生错误!", e);  
  139.         } finally {  
  140.             if (comInterface != null) {  
  141.                 try {  
  142.                     comInterface.closeConnection();  
  143.                 } catch (SocketException e) {  
  144.                     comInterface = null;  
  145.                 }  
  146.             }  
  147.         }  
  148.           
  149.         return returnValueString;  
  150.     }  
  151. }  

其他相关文章可以在此查看







0 0