[SNMP4J] Problem with SNMP4J Program
murali.r at netsol.co.in
murali.r at netsol.co.in
Mon Nov 7 05:42:04 CET 2005
Hi,
Here is my SNMP Program for getting the inoctets and outoctets for all
interfaces in a system.Problem here I am facing is I am not able to get the
full list of inoctets ie for more then one interface in a system.Can you
tell me some solution for this.
Here is my code:
package SNMP4JDemo;
import java.io.IOException;
import java.util.Vector;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.MessageProcessingModel;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
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 SNMPDemon {
private static Snmp snmp;
public static void main(String args[]) {
// SNMP access parameters
// final String SNMP_HOST = "localhost";
// final int SNMP_PORT = 161;
// final String SNMP_COMMUNITY = "public";
// final String SNMP_ALL_ENTRIES = ".1.3.6.1.2.1.2.2.1";
// final String SNMP_OUT = ".1.3.6.1.2.1.2.2.1.16";
// final String SNMP_IN = ".1.3.6.1.2.1.2.2.1.10";
final Vector vbs = new Vector();
// creates SNMP communicator object
// InetAddress snmpHost;
// try {
// snmpHost = InetAddress.getByName(SNMP_HOST);
// System.out.println(snmpHost);
// } catch (UnknownHostException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
Address targetAddress =
GenericAddress.parse("10.40.1.113/161");
TransportMapping transport;
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
MPv3 mpv3 = (MPv3)
snmp.getMessageProcessingModel(MessageProcessingModel.MPv3);
USM usm = new
USM(SecurityProtocols.getInstance(), new
OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
transport.listen();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// setting up target
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
System.out.println(target);
// creating PDU
final PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.2.2.1.10")));
pdu.add(new VariableBinding(new
OID("1.3.6.1.2.1.2.2.1.10.16777219")));
pdu.setType(PDU.GETNEXT);
// Interface Numbers
//pdu.add(new VariableBinding(new
OID("1.3.6.1.2.1.2.2.1")));
// InOctets
//pdu.add(new VariableBinding(new
OID("1.3.6.1.2.1.2.2.1.10")));
// outOctets
//pdu.add(new VariableBinding(new
OID("1.3.6.1.2.1.2.2.1.16")));
// Interface Name
//pdu.add(new VariableBinding(new
OID("1.3.6.1.2.1.2.2.1.3")));
// Number of interfaces
//pdu.add(new VariableBinding(new
OID("1.3.6.1.2.1.2.1")));
// All
//pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.2")));
// // Ethernet CSMA/CD
// pdu.add(new VariableBinding(new
// OID("1.3.6.1.2.1.2.2.1.3.16777219")));
// // Software LoopBack
// pdu.add(new VariableBinding(new
OID("1.3.6.1.2.1.2.2.1.3.1")));
System.out.println(pdu);
// All
// pdu.setMaxRepetitions(50);
// Get ifNumber only once
// pdu.setNonRepeaters(1);
// set context non-default context (default context does
not need to be
// set)
// pdu.setContextName(new
OctetString("subSystemContextA"));
// set non-default context engine ID (to use targets
authoritative
// engine ID
// use an empty (size == 0) octet string)
//
pdu.setContextEngineID(OctetString.fromHexString("80:00:13:70:c0:a8:01:0d"))
;
// sending request
ResponseListener listener = new ResponseListener() {
public void onResponse(ResponseEvent event) {
// Always cancel async request
when response has been received
// otherwise a memory leak is
created! Not canceling a request
// immediately can be useful when
sending a request to a
// broadcast
// address.
((Snmp)
event.getSource()).cancel(event.getRequest(), this);
PDU response =
event.getResponse();
PDU request = event.getRequest();
if (response == null) {
System.out.println("Request " + request + " timed out");
} else {
System.out.println("Received response " + response + " on request " +
request);
// String name = null;
// String name1 = null;
// String interfaceNum =
event.getResponse().get(4) + "";
// String inOctets =
event.getResponse().get(1) + "";
// String outOctets =
event.getResponse().get(2) + "";
// String interfaceName
= event.getResponse().get(3) + "";
//
//
System.out.println("-------------->" + interfaceName);
// String interfaceNum1
= interfaceNum.substring(interfaceNum.indexOf("=") + 1);
// String inOctets1 =
inOctets.substring(inOctets.indexOf("=") + 1);
// String outOctets1 =
outOctets.substring(outOctets.indexOf("=") + 1);
// String interfaceName1
= interfaceName.substring(interfaceName.indexOf("=") + 1).trim();
//
// if
("1".equalsIgnoreCase(interfaceName1)) {
// name =
"other";
// }
// if
("2".equalsIgnoreCase(interfaceName1)) {
// name =
"regular1822";
// }
// if
("3".equalsIgnoreCase(interfaceName1)) {
// name =
"hdh1822";
// }
// if
("4".equalsIgnoreCase(interfaceName1)) {
// name =
"ddn-x25";
// }
// if
("5".equalsIgnoreCase(interfaceName1)) {
// name =
"rfc877-x25";
// }
// if
("6".equalsIgnoreCase(interfaceName1)) {
// name =
"ethernet-csmacd";
// }
// if
("7".equalsIgnoreCase(interfaceName1)) {
// name =
"so88023-csmacd ";
// }
// if
("8".equalsIgnoreCase(interfaceName1)) {
// name =
"iso88024-tokenBus";
// }
// if
("9".equalsIgnoreCase(interfaceName1)) {
// name =
"iso88024-tokenRing";
// }
// if
("10".equalsIgnoreCase(interfaceName1)) {
// name =
"iso88026-man";
// }
// if
("11".equalsIgnoreCase(interfaceName1)) {
// name =
"starLan";
// }
// if
("12".equalsIgnoreCase(interfaceName1)) {
// name =
"proteon-10Mbit";
// }
// if
("13".equalsIgnoreCase(interfaceName1)) {
// name =
"proteon-80Mbit";
// }
// if
("14".equalsIgnoreCase(interfaceName1)) {
// name =
"hyperchannel";
// }
// if
("15".equalsIgnoreCase(interfaceName1)) {
// name =
"fddi";
// }
// if
("16".equalsIgnoreCase(interfaceName1)) {
// name =
"lapb";
// }
// if
("17".equalsIgnoreCase(interfaceName1)) {
// name =
"sdlc";
// }
// if
("18".equalsIgnoreCase(interfaceName1)) {
// name =
"ds1";
// }
// if
("19".equalsIgnoreCase(interfaceName1)) {
// name =
"e1";
// }
// if
("20".equalsIgnoreCase(interfaceName1)) {
// name =
"basicISDN";
// }
// if
("21".equalsIgnoreCase(interfaceName1)) {
// name =
"primaryISDN";
// }
// if
("22".equalsIgnoreCase(interfaceName1)) {
// name =
"propPointToPointSerial";
// }
// if
("23".equalsIgnoreCase(interfaceName1)) {
// name =
"ppp";
// }
// if
("24".equalsIgnoreCase(interfaceName1)) {
// name =
"softwareLoopback";
// }
// if
("25".equalsIgnoreCase(interfaceName1)) {
// name =
"eon";
// }
// if
("26".equalsIgnoreCase(interfaceName1)) {
// name =
"ethernet-3Mbit";
// }
// if
("27".equalsIgnoreCase(interfaceName1)) {
// name =
"nsip";
// }
// if
("28".equalsIgnoreCase(interfaceName1)) {
// name =
"slip";
// }
// if
("29".equalsIgnoreCase(interfaceName1)) {
// name =
"ultra";
// }
// if
("30".equalsIgnoreCase(interfaceName1)) {
// name =
"ds3";
// }
// if
("31".equalsIgnoreCase(interfaceName1)) {
// name =
"sip";
// }
// if
("32".equalsIgnoreCase(interfaceName1)) {
// name =
"frame-relay";
// }
//
//
System.out.println("Response From: "
//
+ event.getPeerAddress());
//
System.out.println("Received response PDU is: "
//
+ event.getResponse());
//
System.out.println("InOctets: " + inOctets1);
//
System.out.println("OutOctets: " + outOctets1);
//
System.out.println("No of Interfaces: " + interfaceNum1);
//
System.out.println("Interface Name: " + name);
}
}
};
try {
snmp.send(pdu, target, null, listener);
snmp.notify(pdu, target);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
More information about the SNMP4J
mailing list