SupplyEntryWalker.java 3.6 KB
package com.sitech.ismp.coll.net.f5;

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 SupplyEntryWalker {

    public static final String ROOT_OID = "1.3.6.1.4.1.3375.2.1.3.2.2.2.1.1";
    public static final int ATTRIBCOUNT = 2;

    private String IP;

    Vector supplyVecotor = new Vector();

    private void addEntry(SupplyEntry supply) {
        supplyVecotor.add(supply);
    }

    public Vector getEntry() {
        return supplyVecotor;
    }

    public void walker(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 + " 采集RunEntry失败]");
        }

        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);
            // 填充Index
            SupplyEntry supplyEntry = new SupplyEntry();
            supplyEntry.setSupplyIndex(key);
            // temperatrueEntry.setStatusDesc(value);
            this.supplyVecotor.add(supplyEntry);
        }
        // try {
        for (int i = 0; i < supplyVecotor.size(); i++) {
            SupplyEntry supplyEntry = (SupplyEntry) supplyVecotor.elementAt(i);
            String rowIndex = supplyEntry.getSupplyIndex();
            // System.out.println("rowIndex: " + rowIndex);
            Vector oidsVector = getOtherOids(rowIndex, ATTRIBCOUNT);
            Vector resultVector = null;
            try {
                resultVector = snmpGet.getPDU(oidsVector);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if (resultVector != null) {
                for (int j = 0; j < resultVector.size(); j++) {
                    VariableBinding vb = (VariableBinding) resultVector.get(j);
                    String oid = vb.getOid().toString();
                    String var = vb.getVariable().toString();
                    System.out.println(oid + " = " + var);
                    if (oid.startsWith("1.3.6.1.4.1.3375.2.1.3.2.2.2.1.1")) {
                        supplyEntry.setSupplyIndex(rowIndex);
                    } else if (oid.startsWith("1.3.6.1.4.1.3375.2.1.3.2.2.2.1.2")) {
                        supplyEntry.setSupplyStatus(var);
                    }
                }
            }
        }
        snmpGet.release();
        System.out.println("[temperature : " + IP + " temperature采集完毕]");

    }

    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);
            // System.out.println("rowIndex change oid: " + oid);
            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;
    }
}