[SNMP4J] How to put OID's for getting different interfaces

Chinmay Kant chinmay.kant at gmail.com
Mon Dec 13 09:53:18 CET 2010


Hi,

I have a problem in detecting SNMP interfaces on a machine.
 I am able to get SNMP system information for that machine.
 When i try to discover interfaces, then i get information of First
interfaces only.
 Whereas there are around 70 interfaces.

 My problem is that when i am not able to iterate through the ifTable.
 How should i provide the OID's so that i can iterate through ifTable and
get all different enteries.?

My code is as follows:-

TO create PDU, i use the following method:-

    private static final String OIDifIndex = "1.3.6.1.2.1.2.2.1.1.";
    private static final String OIDifDescr = "1.3.6.1.2.1.2.2.1.2.";
    private static final String OIDifType = "1.3.6.1.2.1.2.2.1.3.";
    private static final String OIDifSpeed = "1.3.6.1.2.1.2.2.1.5.";
    private static final String OIDifPhysAddress = "1.3.6.1.2.1.2.2.1.6.";
    private static final String OIDifAdminStatus = "1.3.6.1.2.1.2.2.1.7.";
    private static final String OIDifOperStatus = "1.3.6.1.2.1.2.2.1.8.";
    private static final String OIDifName = "1.3.6.1.2.1.31.1.1.1.1.";
    private static final String OIDifAlias = "1.3.6.1.2.1.31.1.1.1.18.";
    private static final String OIDifHighSpeed = "1.3.6.1.2.1.31.1.1.1.15.";

    private PDU createInterfacePDU(int count) {
        PDU requestPDU;
        requestPDU = new PDU();
        requestPDU.add(new VariableBinding(new OID(OIDifIndex + count)));
        requestPDU.add(new VariableBinding(new OID(OIDifDescr + count)));
        requestPDU.add(new VariableBinding(new OID(OIDifType + count)));
        requestPDU.add(new VariableBinding(new OID(OIDifSpeed + count)));
        requestPDU.add(new VariableBinding(new OID(OIDifPhysAddress +
count)));
        requestPDU.add(new VariableBinding(new OID(OIDifAdminStatus +
count)));
        requestPDU.add(new VariableBinding(new OID(OIDifOperStatus +
count)));
        requestPDU.add(new VariableBinding(new OID(OIDifName + count)));
        requestPDU.add(new VariableBinding(new OID(OIDifAlias + count)));
        requestPDU.add(new VariableBinding(new OID(OIDifHighSpeed +
count)));

        requestPDU.setType(PDU.GETNEXT);
        return requestPDU;
    }

 TO get interface information i use following code:-

 private void performSNMPInterfaceCheck(Target target, Snmp snmp, int
interfaceCount) {
        try {

            for (int i = 0; i < interfaceCount; i++) {
                PDU requestPDU = createInterfacePDU(i);

                ResponseEvent response = snmp.send(requestPDU, target);
                if (response.getResponse() == null) {
                    continue;
                } else {
                    VariableBinding[] array =
response.getResponse().toArray();
                    for (VariableBinding variableBinding : array) {
                        String value =
variableBinding.getVariable().toString();
                        String key = variableBinding.getOid().toString();

                        if (key.startsWith(OIDifIndex)) {
                            int index = Integer.parseInt(value);
                            System.out.println ("ifIndex -" + index);
                        }
                        if (key.startsWith(OIDifDescr)) {
                            String ifDescription = value;
                            String[] split = value.split(":");
                            if (split.length > 1) {
                                StringBuffer buffer = new StringBuffer();
                                try
                                {
                                    for (String string : split) {
                                        int parseInt =
Integer.parseInt(string, 16);

buffer.append(Character.toChars(parseInt));

                                    }
                                }
                                catch(NumberFormatException exception)
                                {
                                    System.out.println("Exception for if
description - error - " + exception.getMessage());
                                    buffer = new StringBuffer (value);
                                }
                                ifDescription = buffer.toString();
                            }
                           System.out.println("ifDescription - " +
ifDescription);
                        } else if (key.startsWith(OIDifType)) {
                            if (value != null) {
                                int type = Integer.parseInt(value);
                                System.out.println ("ifType - " + type);
                            }
                        } else if (key.startsWith(OIDifSpeed)) {
                            if (value != null) {
                                long speedValue = Long.parseLong(value);
                                speedValue = speedValue / (1000 * 1000);
                                 System.out.println ("ifSpeed -" +
speedValue);
                            }
                        } else if (key.startsWith(OIDifPhysAddress)) {

                            if (value != null) {
                                String[] split = value.trim().split(" ");
                                if (split.length < 1) {
                                    StringBuffer buffer = new
StringBuffer();
                                    for (i = 0; i < split.length; i++) {
                                        buffer.append(split[i].equals(" ") ?
"-" : split[i]);
                                    }
                                    value = buffer.toString();
                                }
                            }
                            value = value.toUpperCase();
                             System.out.println ("ifPhysAddress - " +
value);
                        } else if (key.startsWith(OIDifAdminStatus)) {
                            if (value != null) {
                                int adminStatus = Integer.parseInt(value);
                                System.out.println ("ifAdminStatus - "+
adminStatus);
                            }
                        } else if (key.startsWith(OIDifOperStatus)) {
                            if (value != null) {
                                int operStatus = Integer.parseInt(value);
                                System.out.println ("ifOperStatus -"+
operStatus);
                            }
                        } else if (key.startsWith(OIDifHighSpeed)) {
                            if (value != null) {
                                 System.out.println ("highSpeed - " +
value);
                            }
                        } else if (key.startsWith(OIDifName)) {
                            if (value != null) {
                                 System.out.println ("ifName - "+ value);
                            }
                        } else if (key.startsWith(OIDifAlias)) {
                            if (value != null) {
                                System.out.println ( "ifAlias - "+ value);
                            }
                        }
                    }

                }
            }
        } catch (IOException exception) {
             System.out.println ("Unable to perform snmp interface search  -
error - " + exception.getMessage());
        }
    }

-- 
Thanks and Regards,
Chinmay



More information about the SNMP4J mailing list