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

SNMP编程

开发平台:

C/C++

  1. /* $Id: snmptrapd.src,v 1.4 2002/09/09 05:36:52 parasuraman 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 package of AdventNetSNMP2 api.
  10.  * The user could run this application by giving any one of the following usage.
  11.  *  
  12.  * java snmptrapd [options]
  13.  *
  14.  * java snmptrapd [-d] [-p port][-c community]
  15.  * e.g. 
  16.  * java snmptrapd -p 162 -c public 
  17.  *
  18.  * Options:
  19.  * [-d]                - Debug output. By default off.
  20.  * [-p] <port>         - remote port no. By default 162.
  21.  * [-c] <community>    - community String. By default "public".               
  22.  */
  23. import java.lang.*;
  24. import java.util.*;
  25. import java.net.*;
  26. import com.adventnet.snmp.snmp2.*;
  27. public class snmptrapd implements SnmpClient {
  28.     static SnmpAPI api;
  29.     public static void main(String args[]) {
  30.         
  31.         // Take care of getting options
  32.         String usage = "snmptrapd [-d] [-p port][-c community]";
  33.         String options[] = { "-d", "-p", "-c"};
  34.         String values[] = { "None", null, null};
  35.         ParseOptions opt = new ParseOptions(args,options,values, usage);
  36.         // Start SNMP API
  37.         api = new SnmpAPI();
  38.         if (values[0].equals("Set")) api.setDebug( true );
  39.         
  40.         if (opt.remArgs.length>0) opt.usage_error();
  41.         // Open session 
  42.         SnmpSession session = new SnmpSession(api);
  43.                 session.addSnmpClient(new snmptrapd());
  44.         // set local port
  45.         try {
  46.             if (values[1] != null) 
  47.                 session.setLocalPort( Integer.parseInt(values[1]) );        
  48.             else 
  49.                 session.setLocalPort(162);
  50.         }
  51.         catch (NumberFormatException ex) {
  52.             System.err.println("Invalid Integer Arg");
  53.         }
  54.       
  55.         // set community
  56.         if(values[2] != null)
  57.             session.setCommunity(values[2]);
  58.         
  59.         // Open the session
  60.         try { session.open(); }
  61.         catch (SnmpException e) {
  62.             System.err.println(e);
  63.             System.exit(1);
  64.         }
  65.     }
  66.     
  67.     public boolean authenticate(SnmpPDU pdu, String community){
  68.         return (pdu.getCommunity().equals(community));
  69.     }
  70.   
  71.     public boolean callback(SnmpSession session,SnmpPDU pdu, int requestID){
  72.         // check trap version
  73. if(pdu == null)
  74. return false;
  75.         if (pdu.getCommand() == api.TRP_REQ_MSG) {
  76.             System.out.println("Trap received from: "
  77.                 +pdu.getAddress() +", community: " + pdu.getCommunity());
  78.             System.out.println("Enterprise: " + pdu.getEnterprise());
  79.             System.out.println("Agent: " + pdu.getAgentAddress());
  80.             System.out.println("TRAP_TYPE: " + pdu.getTrapType());
  81.             System.out.println("SPECIFIC NUMBER: " + pdu.getSpecificType());
  82.             System.out.println("Time: " + pdu.getUpTime()+"nVARBINDS:");
  83.             // print varbinds
  84.             for (Enumeration e = pdu.getVariableBindings().elements();e.hasMoreElements();)
  85.                 System.out.println(((SnmpVarBind) e.nextElement()).toTagString());
  86.         }
  87.         else if(pdu.getCommand() == api.TRP2_REQ_MSG)
  88.         {
  89.             System.out.println("Trap received from: "
  90.                 +pdu.getAddress() + ", community: " + pdu.getCommunity());
  91.             for (Enumeration e = pdu.getVariableBindings().elements();e.hasMoreElements();)
  92.                 System.out.println(((SnmpVarBind) e.nextElement()).toTagString());
  93.         }
  94.         else
  95.             System.err.println("Non trap PDU received.");
  96.         System.out.println(""); // a blank line between traps
  97.         return true;
  98.   
  99.     }
  100.   
  101.     public void debugPrint(String debugOutput){
  102.         System.out.println(debugOutput);
  103.         return;    
  104.     }
  105.     
  106. }