質問

i'm writting a java code that retrieves some information from several servers. everything is working fine, except ifInOctets and ifOutOctets that return "noSuchInstance" instead of their value. i can retrieve ifInOctets and ifOutOctets of the same servers by using iReasoning MIB Browser.

public Map<String, Object> getStatus() throws IOException{
    Snmp snmp4j = new Snmp(new DefaultUdpTransportMapping());
    snmp4j.listen();
    CommunityTarget target = new CommunityTarget();
    Map<String, Object> result = new HashMap<>();
    List<Map<String, Object>> servers = new LinkedList<>();
    for(SnmpServer snmpServer : snmpServers){
        Map<String, Object> server = new HashMap<>();
        server.put("name", snmpServer.getName());
        List<Map<String, Object>> parameters = new LinkedList<>();
        target.setAddress(new UdpAddress(snmpServer.getAddress()));
        target.setTimeout(500);
        target.setRetries(3);
        target.setCommunity(new OctetString("public"));
        target.setVersion(SnmpConstants.version2c);
        List<String> keys = new ArrayList<>();
        PDU request = new PDU();
        request.setType(PDU.GET);
        request.add(new VariableBinding(new OID(".1.3.6.1.2.1.2.2.1.10")));
        keys.add("ifInOctets");
        request.add(new VariableBinding(new OID(".1.3.6.1.2.1.2.2.1.16")));
        keys.add("ifOutOctets");
        request.add(new VariableBinding(new OID(".1.3.6.1.4.1.2021.9.1.9.1")));
        keys.add("diskPercentage");
        request.add(new VariableBinding(new OID(".1.3.6.1.4.1.2021.4.6.0")));
        keys.add("usedMemory");
        request.add(new VariableBinding(new OID(".1.3.6.1.4.1.2021.4.5.0")));
        keys.add("totalMemory");
        request.add(new VariableBinding(new OID(".1.3.6.1.4.1.2021.11.11.0")));
        keys.add("idleCpuPercentage");
        request.add(new VariableBinding(new OID(".1.3.6.1.4.1.2021.9.1.6.1")));
        keys.add("totalDisk");
        request.add(new VariableBinding(new OID(".1.3.6.1.4.1.2021.9.1.8.1")));
        keys.add("usedDisk");
        request.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3.0")));
        keys.add("systemUpTime");
        ResponseEvent responseEvent;
        responseEvent = snmp4j.send(request, target);
        PDU responsePDU = responseEvent.getResponse();
        try{
            Vector<VariableBinding> variableBindings = responsePDU.getVariableBindings();
            for(int j = 0; j < variableBindings.size(); j++){
                Map<String, Object> parameter = new HashMap<>();
                parameter.put("name", keys.get(j));
                parameter.put("value", variableBindings.get(j).getVariable().toString());
                parameters.add(parameter);
            }
            server.put("parameters", parameters);
        }catch(NullPointerException e){
            server.put("offline", true);
        }
        servers.add(server);
    }
    result.put("servers", servers);
    return result;
}

so any ideas on what might be the problem?

役に立ちましたか?

解決

You need to specify the interface index after .1.3.6.1.2.1.2.2.1.10 and .1.3.6.1.2.1.2.2.1.16. (Eg. if the index is 17, append .17 to both OIDs.) Otherwise, the target server doesn't know which interface you want to return the traffic counter values for.

Servers and routers can have multiple interfaces, and they count the traffic on each interface separately. I can't tell you what the correct interface index should be; you'll have to look that up on the server/router yourself. Alternatively, you could use a GETNEXT request instead of GET, if you just want to return the traffic on the interface with lowest index. However, that might not necessarily be the right interface.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top