使用SNMP Java管理器监视Windows变量

问题描述 投票:0回答:1

我需要监视有关网络中某些IP的信息。这些信息将是:设备的名称,它们的位置,每个端口上的带宽以及网络性能指标。我已经使用Java创建了SNMP管理器:

import java.io.IOException;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SNMPManager {

    Snmp snmp = null;
    String Agent = "udp:localhost/161";
    String Oid="1.3.6.1.4.1.9600.1.22.8.5.1.5.";

    public static void main(String[] args) throws IOException {

        SNMPManager client = new SNMPManager();
        client.start();
        int counter=0;
        //while(true){
        try{
        String ODIValue = client.getAsString(new OID(client.Oid));//Decrypt the OID
        System.out.println("OID is "+ client.Oid);
        System.out.println("ODIValue is "+ODIValue);
        System.out.println("---------------------------------------------");
        }catch(Exception e){
            System.out.println("Error "+ counter++);
        }
        //}
    }

    private void start() throws IOException {
        TransportMapping transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        transport.listen();
    }


    public String getAsString(OID oid) throws IOException {
        ResponseEvent event = get(new OID[] { oid });//Get IOD from agent
        return event.getResponse().get(0).getVariable().toString(); //Should respond with the OID
    }

    public ResponseEvent get(OID oids[]) throws IOException {
        PDU pdu = new PDU();
        for (OID oid : oids) {
            pdu.add(new VariableBinding(oid));
        }
        pdu.setType(PDU.GET);
        ResponseEvent event = snmp.send(pdu, getTarget(), null);
        if(event != null) {
            return event;
        }
        throw new RuntimeException("Time out occured");
    }

    private Target getTarget() {
        Address targetAddress = GenericAddress.parse(Agent);
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString("public"));
        target.setAddress(targetAddress);
        target.setRetries(2);
        target.setTimeout(1500);
        target.setVersion(SnmpConstants.version2c);
        return target;
    }

}

我将161端口用于SNMP,将localhost用于我的计算机。我还在Windows 10上启用了SNMP功能。现在,我希望获得操作系统版本,但是什么也没有出现。问题出在哪里?

而且我还需要知道在Windows上的哪里可以找到MIB文件,以了解系统变量的所有代码。

java snmp
1个回答
0
投票

我曾在一个具有类似要求的项目中工作,我们曾经用来监视远程IP的数据。您可以在这里查看源代码github.com/ashish2199/Nework_Bandwidth_Monitor

© www.soinside.com 2019 - 2024. All rights reserved.