snmp4j讀取數據


win7下開啟snmpv2:

打開控制面板-->程序-->程序和功能-->打開或者關閉windows功能,選擇簡單網絡管理協議(snmp),點擊確定,打開服務,找到snmp service,右鍵屬性,找到安全選項,在接受的社區名稱中,添加以后程序要訪問的團體名,在接受來自一下主機的snmp數據包中添加本機IP或者localhost(取決於程序使用localhost還是本機IP訪問,如果使用本機IP訪問,但是這里填localhost,則是無法訪問的)。

 

 以下都是同步實現的:

snmp getBulk方式獲取數據:

public static void syncGetBulk() {
        try {
            Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); //構造一個UDP
            snmp.listen(); //開始監聽snmp消息
            
            CommunityTarget target = new CommunityTarget(); 
            target.setCommunity(new OctetString("public"));//snmpv2的團體名
            target.setVersion(SnmpConstants.version2c);  //snmp版本
            target.setAddress(new UdpAddress("192.168.1.104/161"));
            target.setTimeout(60000); //時延
            target.setRetries(1); //重傳
            
            PDU pdu = new PDU();  //a SNMP protocol data unit
            //使用GETBULK方式獲取數據,無論什么方式取oid,都是取葉子節點,葉子節點的父節點都不會取到
            pdu.setType(PDU.GETBULK);  
            
            //snmp getBulk獨有
            pdu.setMaxRepetitions(3000); //每個OID通過GETBULK方式獲取多少個數據
            /*偏移量,假設有兩個oid,0代表兩個oid都取3000個葉子oid,1代表第一個取它最近的第一個oid,第二個取3000個oid,
             * 大於1的數代表兩個oid都是取他們最近的第一個oid
             */
            pdu.setNonRepeaters(1); 
            
            //添加oid,可以多個
            pdu.add(new VariableBinding(new OID("1.3.6.1.2.1")));
            ResponseEvent responseEvent = snmp.send(pdu, target);
            PDU response = responseEvent.getResponse();
            
            if (response == null) {
                System.out.println("TimeOut...");
            } else {
                if (response.getErrorStatus() == PDU.noError) {
                    //讀取數據
                    Vector<? extends VariableBinding> vbs = response
                            .getVariableBindings();
                    List<SnmpResult> result = new ArrayList<SnmpResult>(vbs.size());
                    for (VariableBinding vb : vbs) {
                        result.add(new SnmpResult(vb.getOid().toString(),vb.getVariable().toString()));
                    }
                    System.out.println(JSONArray.fromObject(result).toString());
                } else {
                    System.out
                            .println("Error:" + response.getErrorStatusText());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

 

snmp walk:PDU中並未提供walk,snmp4j提供了TableUtils,實際實現也是通過snmp getBulk或者getNext,將某個oid下的所有葉子節點,只是TableUtils處理了多次獲取結果,省的自己寫程序去處理。

public static void walk() {
        try {
            Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); // 構造一個UDP
            snmp.listen(); // 開始監聽snmp消息

            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString("public"));// snmpv2的團體名
            target.setVersion(SnmpConstants.version2c); // snmp版本
            target.setAddress(new UdpAddress("192.168.1.104/161"));
            target.setTimeout(60000); // 時延
            target.setRetries(1); // 重傳

            TableUtils utils = new TableUtils(snmp, new DefaultPDUFactory(
                    PDU.GETBULK));// GETNEXT or GETBULK
            utils.setMaxNumRowsPerPDU(5); // only for GETBULK, set max-repetitions, default is 10
            OID[] columnOids = new OID[] {new OID("1.3.6.1.2.1")};
            // If not null, all returned rows have an index in a range (lowerBoundIndex, upperBoundIndex]
            //lowerBoundIndex,upperBoundIndex都為null時返回所有的葉子節點。 必須具體到某個OID,,否則返回的結果不會在(lowerBoundIndex, upperBoundIndex)之間
            List<TableEvent> l = utils.getTable(target, columnOids,new OID("1.3.6.1.2.1.25.5.1.1.1.3324")
                    ,null); //
            for (TableEvent e : l) {
                System.out.println(e.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

 異步實現getNext:對於異步而言,不能在異步執行完前關閉虛擬機,否則出不了數據。

public static void asyncGetNext() {
        try {
            Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); // 構造一個UDP
            snmp.listen(); // 開始監聽snmp消息

            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString("public"));// snmpv2的團體名
            target.setVersion(SnmpConstants.version2c); // snmp版本
            target.setAddress(new UdpAddress("192.168.1.104/161"));
            target.setTimeout(60000); // 時延
            target.setRetries(1); // 重傳

            PDU pdu = new PDU(); // a SNMP protocol data unit
            pdu.setType(PDU.GETNEXT);

            // 添加oid,可以多個
            pdu.add(new VariableBinding(new OID("1.3.6.1.2.1")));
            
             // 設置監聽對象  
            ResponseListener listener = new ResponseListener() {  
                public void onResponse(ResponseEvent event) {  
                    try {
                    PDU response = event.getResponse();
                    
                    if (response == null) {
                        System.out.println("TimeOut...");
                    } else {
                        if (response.getErrorStatus() == PDU.noError) {
                            // 讀取數據
                            Vector<? extends VariableBinding> vbs = response
                                    .getVariableBindings();
                            List<SnmpResult> result = new ArrayList<SnmpResult>(
                                    vbs.size());
                            for (VariableBinding vb : vbs) {
                                result.add(new SnmpResult(vb.getOid().toString(), vb
                                        .getVariable().toString()));
                            }
                            System.out.println(JSONArray.fromObject(result).toString());
                        } else {
                            System.out
                                    .println("Error:" + response.getErrorStatusText());
                        }
                    }
                    }catch(Exception e) {
                        e.printStackTrace();
                    }
                }  
            };  
            snmp.send(pdu, target,null, listener);  //設置異步處理
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM