/* $Id: snmpv2cinformreq.src,v 1.4.2.5 2009/10/27 07:07:11 prathika Exp $ */ /* * @(#)snmpv2cinformreq.java * Copyright (c) 1996-2009 ZOHO Corp. All Rights Reserved. * Please read the associated COPYRIGHTS file for more details. */ /** * This is an example program to explain how to write an application to send a * SNMP v2c INFORM_REQ message using com.adventnet.snmp.snmp2 package of * WebNMS SNMP2 api. The user could run this application by giving any one of * the following usage. * * java snmpv2cinformreq [options] hostname timeTicks snmpTrapOid-value [OID type * value] ... * * java snmpv2cinformreq [-d] [-c community] [-p port] host TimeTicksvalue * OIDvalue [OID {INTEGER | STRING | GAUGE | TIMETICKS | OPAQUE | IPADDRESS | * COUNTER | COUNTER64 | UNSIGNED32} value] ... * e.g. * java snmpv2cinformreq -p 162 -c public adventnet 1000 1.2.0 1.5.0 STRING advent * * If the oid is not starting with a dot (.) it will be prefixed by .1.3.6.1.2.1 . * So the entire OID of 1.1.0 will become .1.3.6.1.2.1.1.1.0 . You can also * give the entire OID . * * Options: * [-d] - Debug output. By default off. * [-c] - community String. By default "public". * [-p] - remote port no. By default 162. * Mandatory - The RemoteHost (agent).Format (string without double * qoutes/IpAddress). * Mandatory - the value of object sysUpTime when the event occurred * Mandatory - Object Identifier * - Give multiple no. of Object Identifiers with value. * - Type of the object * - object-instance value * * Note: Thi uses synchronous send mechanism for sending the PDU. */ import java.lang.*; import java.util.*; import java.net.*; import com.adventnet.snmp.snmp2.*; public class snmpv2cinformreq { public static void main(String args[]) { SnmpAPI api; // Take care of getting options String usage = "snmpv2cinformreq [-d] [-c community] [-p port] \n" + "host TimeTicksvalue OIDvalue [OID {INTEGER | \n" + "STRING | GAUGE | TIMETICKS | OPAQUE | IPADDRESS | \n" + "COUNTER | COUNTER64 | UNSIGNED32} value] ..."; String options[] = { "-d", "-c", "-p"}; String values[] = { "None", null, null}; // Parse the input for correct format. ParseOptions opt = new ParseOptions(args,options,values, usage); if (opt.remArgs.length<3) { opt.usage_error(); } // Start the SNMP API api = new SnmpAPI(); // Check if the debug flag is set or not. if (values[0].equals("Set")) { api.setDebug (true); } // Create an SNMP session SnmpSession session = new SnmpSession(api); // Set v2c version session.setVersion(SnmpAPI.SNMP_VERSION_2C) ; // Set remote Host UDPProtocolOptions ses_opt = new UDPProtocolOptions(opt.remArgs[0]); // Set community if (values[1] != null) session.setCommunity( values[1] ); // Set default remote port to 162 if port is not specified by the user try { if (values[2] != null) { ses_opt.setRemotePort( Integer.parseInt(values[2]) ); } else { ses_opt.setRemotePort(162); } } catch (NumberFormatException ex) { System.err.println("Invalid Integer Arg"); } session.setProtocolOptions(ses_opt); // Build v2c SNMP INFORM_REQ PDU SnmpPDU pdu = new SnmpPDU(); pdu.setCommand( api.INFORM_REQ_MSG ); // Add the sysUpTime variable binding SnmpOID oid = new SnmpOID(".1.3.6.1.2.1.1.3.0"); if (oid.toValue() == null) { System.err.println("Invalid OID argument: .1.3.6.1.2.1.1.3.0"); } else { SnmpVar var = null ; try { var = SnmpVar.createVariable(opt.remArgs[1], SnmpAPI.TIMETICKS); } catch (SnmpException e) { System.err.println("Cannot create variable: " + oid +" with value: "+opt.remArgs[1]); } SnmpVarBind varbind = new SnmpVarBind(oid, var); pdu.addVariableBinding(varbind); } //Add snmpTrapOid variable bindings oid = new SnmpOID(".1.3.6.1.6.3.1.1.4.1.0"); if (oid.toValue() == null) { System.err.println("Invalid OID argument: .1.3.6.1.6.3.1.1.4.1.0"); } else { SnmpVar var = null ; try { var = SnmpVar.createVariable(opt.remArgs[2], SnmpAPI.OBJID); } catch (SnmpException e) { System.err.println("Cannot create variable: " + oid +" with value: "+opt.remArgs[2]); } SnmpVarBind varbind = new SnmpVarBind(oid, var); pdu.addVariableBinding(varbind); } // Add the Variable Bindings for (int i=3;i