Blame view

src/com/sitech/ismp/util/SnmpHandler.java 3.89 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
package com.sitech.ismp.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

/**
 * ClassName:SnmpHandler
 * Description: SNMP工具类
 *
 * @author   Linxc
 * @version  
 * @since    Ver 1.1
 * @Date	 2012	Feb 28, 2012		5:19:22 PM
 */
public class SnmpHandler {
	private String ipAddr;
	private int port;
	private String community;
	private int version;
	
	
	private Snmp snmp = null;   
	 private Address targetAddress = null;
	private CommunityTarget target = null;

	public SnmpHandler(String ipAddr, int port, String community, int version) throws Exception {		
		this.ipAddr = ipAddr;
		this.port = port;
		this.community = community;
		this.version = version;

		init();
	}

	@SuppressWarnings("unchecked")
	public String get(String oid) {
		PDU pdu = new PDU();
		pdu.add(new VariableBinding(new OID(oid)));
		pdu.setType(PDU.GET);

		try {
			ResponseEvent respEvnt = snmp.send(pdu, target);
			if (respEvnt != null && respEvnt.getResponse() != null) {
				Vector<VariableBinding> recVBs = respEvnt.getResponse().getVariableBindings();
				if (recVBs.size() > 0) {
					VariableBinding recVB = recVBs.elementAt(0);
					return recVB.getVariable().toString();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String getNext(String oid) {
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<String> walk(String sOid) {
		if(sOid.startsWith(".")){
			sOid = sOid.substring(1, sOid.length());
		}
		List<String> result = new ArrayList<String>();
		
		try {
			OID oid = new OID(sOid);
			for(int i=0; i<3000; i++) {
				PDU pdu = new PDU();
				pdu.add(new VariableBinding(oid));
				pdu.setType(PDU.GETNEXT);
				
				ResponseEvent respEvnt = snmp.send(pdu, target);
				if (respEvnt != null && respEvnt.getResponse() != null) {
					Vector<VariableBinding> recVBs = respEvnt.getResponse()
							.getVariableBindings();
					if (recVBs !=null && recVBs.size() > 0) {
						VariableBinding recVB = recVBs.elementAt(0);

						oid = recVB.getOid();
						
						String sRecOid = oid.toString();
						if(sRecOid.substring(0, sRecOid.lastIndexOf(".")).equals(sOid)){
							result.add(recVB.getVariable().toString());
							System.out.println(sRecOid + "\t " + recVB.getVariable().toString());
						}else{
							break;
						}

					}else{
						break;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public void init() throws IOException {
		targetAddress = GenericAddress.parse("udp:" + ipAddr + "/" + port + "");
		;
		TransportMapping transport = new DefaultUdpTransportMapping();
		snmp = new Snmp(transport);
		transport.listen();

		target = new CommunityTarget();
		target.setCommunity(new OctetString(community));
		target.setAddress(targetAddress);
		target.setRetries(2);
		target.setTimeout(1500);
		switch (version) {
		case 1:
			target.setVersion(SnmpConstants.version1);
			break;
		case 2:
			target.setVersion(SnmpConstants.version2c);
			break;
		case 3:
			// 暂时不支持V3版本
			target.setVersion(SnmpConstants.version3);
			break;
		default:
			break;
		}
	}
	
	public void destroy() {
		if (snmp != null) {
			try {
				snmp.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		
//		System.out.println();
//		new SnmpHandler("172.21.0.253",161,"public",1).get(".1.3.6.1.4.1.9.2.1.57.0");
		new SnmpHandler("172.21.0.253",161,"public",1).walk(".1.3.6.1.4.1.9.9.48.1.1.1.5");
	}
}