snmptrapd.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:4k
- /* $Id: snmptrapd.src,v 1.5 2002/09/09 05:43:43 pushpar Exp $ */
- /*
- * @(#)snmptrapd.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /**
- * This is an example program for receiving traps using the
- * com.adventnet.snmp.snmp2 and com.adventnet.snmp.mibs packages
- * of AdventNetSNMP2 api.
- * The user could run this application by giving any one of the following usage.
- *
- * java snmptrapd [options]
- *
- * java snmptrapd [-d] [-p port] [-m MIB_files]
- * e.g.
- * java snmptrapd -p 162
- *
- * Options:
- * [-d] - Debug output. By default off.
- * [-p] <port> - remote port no. By default 162.
- * [-m] <MIBfile> - MIB files.To load muliple mibs give within double quotes files seperated by a blank space.
- */
-
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- import com.adventnet.snmp.mibs.*;
- public class snmptrapd implements SnmpClient {
- private static final int DEBUG = 0;
- private static final int PORT = 1;
- private static final int MIBS = 2;
-
- static SnmpAPI api;
- MibOperations mibOps = null;
- public snmptrapd(String mibs) throws Exception
- {
- mibOps = new MibOperations();
- if(mibs!=null)
- {
- // To load MIBs from compiled file
- mibOps.setLoadFromCompiledMibs(true);
- // Load MIBs
- mibOps.loadMibModules(mibs);
- }
- }
-
- public static void main(String args[]) {
-
- // Take care of getting options
- String usage = "snmptrapd [-d] [-p port] [-m MIB_files]";
- String options[] = { "-d", "-p", "-m"};
- String values[] = { "None", null, null};
- ParseOptions opt = new ParseOptions(args,options,values, usage);
- if (opt.remArgs.length > 0 ) opt.usage_error();
-
- // Start SNMP API
- api = new SnmpAPI();
-
- if (values[DEBUG].equals("Set")) api.setDebug( true );
-
- // Open session
- SnmpSession session = new SnmpSession(api);
- snmptrapd recvTrap = null;
-
- try
- {
- recvTrap = new snmptrapd(values[MIBS]);
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
-
- session.addSnmpClient(recvTrap);
- // set local port
- try {
- if (values[PORT] != null)
- session.setLocalPort( Integer.parseInt(values[PORT]) );
- else
- session.setLocalPort(162);
- }
- catch (NumberFormatException ex) {
- System.err.println("Invalid Integer Arg");
- }
- // Open the session
- try {
- session.open();
- }
- catch (SnmpException e) {
- System.err.println(e);
- System.exit(1);
- }
- }
- public boolean authenticate(SnmpPDU pdu, String community){
- return (pdu.getCommunity().equals(community));
- }
- public boolean callback(SnmpSession session,SnmpPDU pdu, int requestID){
- if(pdu == null)
- return false;
- // Check for trap version
- if (pdu.getCommand() == api.TRP_REQ_MSG) {
- System.out.println("Trap received from: "
- +pdu.getAddress() +", community: " + pdu.getCommunity());
- System.out.println("Enterprise: " + pdu.getEnterprise());
- System.out.println("Agent: " + pdu.getAgentAddress());
- System.out.println("TRAP_TYPE: " + pdu.getTrapType());
- System.out.println("SPECIFIC NUMBER: " + pdu.getSpecificType());
- System.out.println("Time: " + pdu.getUpTime()+"nVARBINDS:");
- // print varbinds
- System.out.println(mibOps.varBindsToString(pdu));
- }
- else if(pdu.getCommand() == api.TRP2_REQ_MSG)
- {
- System.out.println("Trap received from: "
- +pdu.getAddress() + ", community: " + pdu.getCommunity());
- System.out.println(mibOps.varBindsToString(pdu));
- }
- else
- System.err.println("Non trap PDU received.");
- System.out.println(""); // a blank line between traps
- return true;
- } // end for listen for ever
- public void debugPrint(String debugOutput){
- System.out.println(debugOutput);
- return;
- }
- }