CpuEntryWalker.java 3.68 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 CpuEntryWalker {
    public static final String ROOT_OID = "1.3.6.1.4.1.3375.2.1.3.1.2.1.1";

    public static final int ATTRIBCOUNT = 3;

    private String IP;

    Vector cpuVecotor = new Vector();

    private void addEntry(CpuEntry cpu) {
        cpuVecotor.add(cpu);
    }

    public Vector getEntry() {
        return cpuVecotor;
    }

    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
            CpuEntry cpuEntry = new CpuEntry();
            cpuEntry.setCpuIndex(key);
            // temperatrueEntry.setStatusDesc(value);
            this.cpuVecotor.add(cpuEntry);
        }
        // try {
        for (int i = 0; i < cpuVecotor.size(); i++) {
            CpuEntry cpuEntry = (CpuEntry) cpuVecotor.elementAt(i);
            String rowIndex = cpuEntry.getCpuIndex();
            // 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.1.2.1.1")) {
                        cpuEntry.setCpuIndex(rowIndex);
                    } else if (oid.startsWith("1.3.6.1.4.1.3375.2.1.3.1.2.1.2")) {
                        cpuEntry.setCPuTemperature(var);
                    } else if (oid.startsWith("1.3.6.1.4.1.3375.2.1.3.1.2.1.3")) {
                        cpuEntry.setCpuFanSpeed(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;
    }
}