Walk一个snmp的OID

package com.boonya.mecache.store;
import java.io.IOException;
import java.util.ArrayList;
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.Integer32;
import org.snmp4j.smi.Null;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SnmpWalk {
	private static int version = SnmpConstants.version1;
	private static String protocol = "udp";
	private static int port = 161;

	/**
	 * 用来walk一个snmp的OID 并获得相应的值
	 * 1)responsePDU == null<br>
	 * 2)responsePDU.getErrorStatus() != 0<br>
	 * 3)responsePDU.get(0).getOid() == null<br>
	 * 4)responsePDU.get(0).getOid().size() < targetOID.size()<br>
	 * 5)targetOID.leftMostCompare(targetOID.size(),responsePDU.get(0).getOid())
	 * !=0<br>
	 * 6)Null.isExceptionSyntax(responsePDU.get(0).getVariable().getSyntax())<br>
	 * 7)responsePDU.get(0).getOid().compareTo(targetOID) <= 0<br>
	 * 
	 * @param ipAddress
	 * @param community
	 * @param oid
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Object snmpWalk(String ip, String community, String targetOid) {
		String address = protocol + ":" + ip + "/" + port;
		ArrayList arr_v = new ArrayList(10);

		OID targetOID = new OID(targetOid);

		PDU requestPDU = new PDU();
		requestPDU.setType(PDU.GETNEXT);
		requestPDU.add(new VariableBinding(targetOID));

		CommunityTarget target = SnmpUtil.createCommunityTarget(address,
				community, version, 2 * 1000L, 3);
		TransportMapping transport = null;
		Snmp snmp = null;
		try {
			transport = new DefaultUdpTransportMapping();
			snmp = new Snmp(transport);
			transport.listen();

			boolean finished = false;
			while (!finished) {
				VariableBinding vb = null;
				ResponseEvent response = snmp.send(requestPDU, target);
				PDU responsePDU = response.getResponse();

				if (null == responsePDU) {
					System.out.println("responsePDU == null");
					finished = true;
					break;
				} else {
					vb = responsePDU.get(0);
				}
				// check finish
				finished = checkWalkFinished(targetOID, responsePDU, vb);
				if (!finished) {
					// Dump response.
					// System.out.println("----"+vb.toString());
					// System.out.println("----"+vb.getOid().toString());
					// System.out.println(vb.getVariable().toString());
					arr_v.add(vb.getVariable().toString());

					// Set up the variable binding for the next entry.
					requestPDU.setRequestID(new Integer32(0));
					requestPDU.set(0, vb);
				}
			}
		} catch (Exception e) {
			System.out.println("SNMP walk Exception: " + e);
		} finally {
			if (snmp != null) {
				try {
					snmp.close();
				} catch (IOException ex1) {
					snmp = null;
				}
			}
			if (transport != null) {
				try {
					transport.close();
				} catch (IOException ex2) {
					transport = null;
				}
			}
		}
		return arr_v;

	}

	/**
	 * 检查SNMP走查完毕
	 * 
	 * @param resquestPDU
	 * @param targetOID
	 * @param responsePDU
	 * @param vb
	 * @return
	 */
	public boolean checkWalkFinished(OID targetOID, PDU responsePDU,
			VariableBinding vb) {
		boolean finished = false;
		if (responsePDU.getErrorStatus() != 0) {
			System.out.println("responsePDU.getErrorStatus() != 0 ");
			System.out.println(responsePDU.getErrorStatusText());
			finished = true;
		} else if (vb.getOid() == null) {
			System.out.println("vb.getOid() == null");
			finished = true;
		} else if (vb.getOid().size() < targetOID.size()) {
			System.out.println("vb.getOid().size() < targetOID.size()");
			finished = true;
		} else if (targetOID.leftMostCompare(targetOID.size(), vb.getOid()) != 0) {
			finished = true;
		} else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) {
			System.out
					.println("Null.isExceptionSyntax(vb.getVariable().getSyntax())");
			finished = true;
		} else if (vb.getOid().compareTo(targetOID) <= 0) {
			System.out.println("Variable received is not "
					+ "lexicographic successor of requested " + "one:");
			System.out.println(vb.toString() + " <= " + targetOID);
			finished = true;
		}
		return finished;

	}

}

你可能感兴趣的:(网络协议,snmp,oid)