IpAddrTable.java 3.09 KB
package com.sitech.ismp.coll.net.table;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import org.snmp4j.smi.VariableBinding;

import com.sitech.ismp.snmp.CommSnmpGet;

public class IpAddrTable {

	public static final String ROOT_OID = "1.3.6.1.2.1.4.20.1.1";

	public static final int ATTRIBCOUNT = 5;

	private String IP;

	Vector rowVector = new Vector();

	private void addRow(IpAddrTableRow row) {
		rowVector.add(row);
	}

	public void FillTable(String ip, int port, String community) {
		this.IP = ip;
		CommSnmpGet snmpGet = new CommSnmpGet(ip, port, community);
		snmpGet.init();
		snmpGet.setRootOid(ROOT_OID);
		try {
			snmpGet.workTable(ROOT_OID);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("[IP: " + IP + " 采集AddressTable失败]");
		}
		HashMap hm = snmpGet.getWalkHashMap();
		Set set = hm.keySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			String value = (String) hm.get(key);
			// 填充IP地址
			IpAddrTableRow row = new IpAddrTableRow();
			row.setRowIndex(key);
			row.setIpAddr(value);
			this.addRow(row);
		}

		try {
			for (int i = 0; i < rowVector.size(); i++) {
				IpAddrTableRow row = (IpAddrTableRow) rowVector.elementAt(i);
				String rowIndex = row.getRowIndex();
				Vector oidsVector = getOtherOids(rowIndex, ATTRIBCOUNT);
				Vector resultVector = snmpGet.getPDU(oidsVector);

				for (int j = 0; j < resultVector.size(); j++) {
					VariableBinding vb = (VariableBinding) resultVector.get(j);
					String oid = vb.getOid().toString();
					String var = vb.getVariable().toString();
					if (oid.startsWith("1.3.6.1.2.1.4.20.1.2.")) {
						row.setIfIndex(var);
					} else if (oid.startsWith("1.3.6.1.2.1.4.20.1.3.")) {
						row.setNetMask(var);
					} else if (oid.startsWith("1.3.6.1.2.1.4.20.1.4.")) {
						row.setBcastAddr(var);
					} else if (oid.startsWith("1.3.6.1.2.1.4.20.1.5.")) {
						row.setReasmMaxSize(var);
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("[IP: " + ip + " 采集AddressTable失败]");
		}
		snmpGet.release();
		System.out.println("[IP: " + ip + " AddressTable采集完毕]");
	}

	private Vector getOtherOids(String rowIndex, int attribCount) {
		Vector vector = new Vector();
		for (int i = 2; i <= attribCount; i++) {
			String oid = releaseRootOidLast(ROOT_OID, rowIndex, i);
			vector.add(oid);
		}
		return vector;
	}

	private String releaseRootOidLast(String rootOid, String rowIndex, int int_i) {
		String result = "";
		String rowIndexLast = rowIndex.substring(rootOid.length(), rowIndex
				.length());
		String rootOidStart = rootOid.substring(0, rootOid.length() - 1);

		result = rootOidStart + int_i + rowIndexLast;
		return result;
	}

	public Vector getRrows() {
		return rowVector;
	}

	public static void main(String args[]) throws Exception {
		String ip = "130.30.2.8";
		int port = 161;
		String community = "ahnms2008";

		IpAddrTable ipAddrTable = new IpAddrTable();
		ipAddrTable.FillTable(ip, port, community);
	}
}