snmptrapd.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:4k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /* $Id: snmptrapd.src,v 1.5 2002/09/09 05:43:43 pushpar Exp $ */
  2. /*
  3.  * @(#)snmptrapd.java
  4.  * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the associated COPYRIGHTS file for more details.
  6.  */
  7. /**
  8.  * This is an example program for receiving traps using the
  9.  * com.adventnet.snmp.snmp2 and com.adventnet.snmp.mibs packages
  10.  * of AdventNetSNMP2 api.
  11.  * The user could run this application by giving any one of the following usage.
  12.  *  
  13.  * java snmptrapd [options]
  14.  *
  15.  * java snmptrapd [-d] [-p port] [-m MIB_files]
  16.  * e.g. 
  17.  * java snmptrapd -p 162
  18.  *
  19.  * Options:
  20.  * [-d]                - Debug output. By default off.
  21.  * [-p] <port>         - remote port no. By default 162.
  22.  * [-m] <MIBfile>      - MIB files.To load muliple mibs give within double quotes files seperated by a blank space. 
  23.  */
  24.         
  25. import java.lang.*;
  26. import java.util.*;
  27. import java.net.*;
  28. import com.adventnet.snmp.snmp2.*;
  29. import com.adventnet.snmp.mibs.*;
  30. public class snmptrapd implements SnmpClient {
  31.     private static final int DEBUG = 0;
  32.     private static final int PORT = 1;
  33.     private static final int MIBS = 2;
  34.     
  35. static SnmpAPI api;
  36. MibOperations mibOps = null;
  37. public snmptrapd(String mibs) throws Exception
  38. {
  39. mibOps = new MibOperations();
  40. if(mibs!=null)
  41. {
  42.          // To load MIBs from compiled file
  43.       mibOps.setLoadFromCompiledMibs(true);
  44. // Load MIBs
  45. mibOps.loadMibModules(mibs);
  46. }
  47. }
  48.     
  49.     public static void main(String args[]) {
  50.         
  51.         // Take care of getting options
  52.         String usage = "snmptrapd [-d] [-p port] [-m MIB_files]";
  53.         String options[] = { "-d", "-p", "-m"};
  54.         String values[] = { "None", null, null};
  55.         ParseOptions opt = new ParseOptions(args,options,values, usage);
  56.         if (opt.remArgs.length > 0 ) opt.usage_error();
  57.     
  58.         // Start SNMP API
  59.         api = new SnmpAPI();
  60.         
  61.         if (values[DEBUG].equals("Set")) api.setDebug( true );
  62.                 
  63.         // Open session         
  64.         SnmpSession session = new SnmpSession(api);
  65. snmptrapd recvTrap = null;
  66. try
  67. {
  68. recvTrap = new snmptrapd(values[MIBS]);
  69. }
  70. catch(Exception e)
  71. {
  72. System.out.println(e);
  73. }
  74. session.addSnmpClient(recvTrap);
  75.         // set local port
  76.         try {
  77.             if (values[PORT] != null) 
  78.                 session.setLocalPort( Integer.parseInt(values[PORT]) );        
  79.             else 
  80.                 session.setLocalPort(162);
  81.         }
  82.         catch (NumberFormatException ex) {
  83.             System.err.println("Invalid Integer Arg");
  84.         }
  85.         // Open the session
  86.         try { 
  87.             session.open();
  88.         }
  89.         catch (SnmpException e) {
  90.             System.err.println(e);
  91.             System.exit(1);
  92.         }
  93. }
  94.     public boolean authenticate(SnmpPDU pdu, String community){
  95.             return (pdu.getCommunity().equals(community));
  96. }
  97. public boolean callback(SnmpSession session,SnmpPDU pdu, int requestID){
  98. if(pdu == null)
  99. return false;
  100.          // Check for trap version
  101.             if (pdu.getCommand() == api.TRP_REQ_MSG) {
  102.                 System.out.println("Trap received from: "
  103.                        +pdu.getAddress() +", community: " + pdu.getCommunity());
  104.                 System.out.println("Enterprise: " + pdu.getEnterprise());
  105.                 System.out.println("Agent: " + pdu.getAgentAddress());
  106.                 System.out.println("TRAP_TYPE: " + pdu.getTrapType());
  107.                 System.out.println("SPECIFIC NUMBER: " + pdu.getSpecificType());
  108.                 System.out.println("Time: " + pdu.getUpTime()+"nVARBINDS:");
  109.                 // print varbinds
  110.                 System.out.println(mibOps.varBindsToString(pdu));
  111.             }
  112.             else if(pdu.getCommand() == api.TRP2_REQ_MSG)
  113.             {
  114.                 System.out.println("Trap received from: "
  115.                        +pdu.getAddress() + ", community: " + pdu.getCommunity());
  116.                 System.out.println(mibOps.varBindsToString(pdu));
  117.             }
  118.             else
  119.                 System.err.println("Non trap PDU received.");
  120.             System.out.println(""); // a blank line between traps
  121. return true;
  122.         } // end for listen for ever
  123. public void debugPrint(String debugOutput){
  124. System.out.println(debugOutput);
  125. return;
  126. }
  127. }