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

SNMP编程

开发平台:

C/C++

  1. /* $Id: snmptrapd.src,v 1.5.2.3 2009/01/28 13:01:35 prathika Exp $ */
  2. /*
  3.  * @(#)snmptrapd.java
  4.  * Copyright (c) 1996-2009 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. {
  32.     private static final int DEBUG = 0;
  33.     private static final int PORT = 1;
  34.     private static final int MIBS = 2;
  35.     
  36.     static SnmpAPI api;
  37.     MibOperations mibOps = null;
  38.     public snmptrapd(String mibs) throws Exception
  39.     {
  40.         mibOps = new MibOperations();
  41.         if(mibs!=null)
  42.         {
  43.             // To load MIBs from compiled file
  44.             mibOps.setLoadFromCompiledMibs(true);
  45.             // Load MIBs
  46.             mibOps.loadMibModules(mibs);
  47.         }   
  48.     }
  49.     
  50.     public static void main(String args[]) 
  51.     {
  52.         // Take care of getting options
  53.         String usage = "snmptrapd [-d] [-p port] [-m MIB_files]";
  54.         
  55.         String options[] = 
  56.             {
  57.                 "-d", "-p", "-m"
  58.             };
  59.             
  60.         String values[] = 
  61.             {
  62.                 "None", null, null
  63.             };
  64.         ParseOptions opt = new ParseOptions(args,options,values, usage);
  65.         if (opt.remArgs.length > 0 ) 
  66.         {
  67.             opt.usage_error();
  68.         }   
  69.     
  70.         // Start SNMP API
  71.         api = new SnmpAPI();
  72.         
  73.         if (values[DEBUG].equals("Set"))
  74.         {
  75.             api.setDebug( true );
  76.         }   
  77.                 
  78.         // Open session         
  79.         SnmpSession session = new SnmpSession(api);
  80.         snmptrapd recvTrap = null;
  81.     
  82.         try
  83.         {
  84.             recvTrap = new snmptrapd(values[MIBS]);
  85.         }
  86.         catch(Exception e)
  87.         {
  88.             System.out.println(e);
  89.         }
  90.         
  91.         session.addSnmpClient(recvTrap);
  92.         // set local port
  93.         try 
  94.         {
  95.             UDPProtocolOptions ses_opt = new UDPProtocolOptions();
  96.             if (values[PORT] != null)
  97.             {
  98.                 ses_opt.setLocalPort( Integer.parseInt(values[PORT]) );        
  99.             }   
  100.             else 
  101.             {
  102.                 ses_opt.setLocalPort(162);
  103.             }
  104.             session.setProtocolOptions(ses_opt);
  105.         }
  106.         catch (NumberFormatException ex) 
  107.         {
  108.             System.err.println("Invalid Integer Arg");
  109.         }
  110.         // Open the session
  111.         try 
  112.         { 
  113.             session.open();
  114.         }
  115.         catch (SnmpException e) 
  116.         {
  117.             System.err.println(e);
  118.             System.exit(1);
  119.         }
  120.     }
  121.     public boolean authenticate(SnmpPDU pdu, String community)
  122.     {
  123.         return (pdu.getCommunity().equals(community));
  124.     }
  125.     public boolean callback(SnmpSession session,SnmpPDU pdu, int requestID)
  126.     {
  127.         if(pdu == null)
  128.         {
  129.             return false;
  130.         }   
  131.         // Check for trap version
  132.         if (pdu.getCommand() == api.TRP_REQ_MSG) 
  133.         {
  134.             System.out.println("Trap received from: ");
  135.             System.out.println(pdu.getProtocolOptions().getSessionId());
  136.             System.out.println(", community: " + pdu.getCommunity());
  137.             System.out.println("Enterprise: " + pdu.getEnterprise());
  138.             System.out.println("Agent: " + pdu.getAgentAddress());
  139.             System.out.println("TRAP_TYPE: " + pdu.getTrapType());
  140.             System.out.println("SPECIFIC NUMBER: " + pdu.getSpecificType());
  141.             System.out.println("Time: " + pdu.getUpTime()+"nVARBINDS:");
  142.             // print varbinds
  143.             System.out.println(mibOps.varBindsToString(pdu));
  144.         }
  145.         else if(pdu.getCommand() == api.TRP2_REQ_MSG)
  146.         {
  147.             System.out.println("Trap received from: ");
  148.             System.out.println(pdu.getProtocolOptions().getSessionId());
  149.             System.out.println(", community: " + pdu.getCommunity());
  150.             System.out.println(mibOps.varBindsToString(pdu));
  151.         }
  152.         else
  153.         {
  154.             System.err.println("Non trap PDU received.");
  155.         }           
  156.         System.out.println(""); // a blank line between traps
  157.         return true;
  158.     } // end for listen for ever
  159.     public void debugPrint(String debugOutput)
  160.     {
  161.         System.out.println(debugOutput);
  162.         return;
  163.     }
  164. }