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

SNMP编程

开发平台:

C/C++

  1. /* $Id: snmptrapforwarder.src,v 1.4 2002/04/11 13:28:31 tonyjpaul Exp $*/
  2. import com.adventnet.snmp.snmp2.*;
  3. import java.util.*;
  4. import ParseOptions;
  5. import java.net.InetAddress;
  6. public class snmptrapforwarder extends Thread implements SnmpClient {
  7.     SnmpAPI api;
  8.     SnmpSession v3_session;
  9.     Vector v;
  10.     String remoteHost = "localhost";
  11.     int remotePort = 2001;
  12.     int local_port=162;
  13.     private static final int DEBUG_FLAG = 0;
  14.     snmptrapforwarder()
  15.     {
  16.         // Start SNMP API
  17.         api = new SnmpAPI();
  18.         try {
  19.             Thread.sleep(500);
  20.         } catch (Exception x) {} 
  21.         // Open session and set remote host & port if needed,
  22.         v3_session = new SnmpSession(api);
  23.         v = new Vector();
  24.     }
  25.     public static void main(String args[]) {
  26. if (args.length!= 3){
  27. System.out.println("Usage : java snmptrapforwarder [LocalPort] [RemoteHost] [RemotePort]");
  28. System.exit(1);
  29. }
  30.     snmptrapforwarder eserv = new snmptrapforwarder();
  31.     // Take care of getting options
  32. //String[] hostname={""};
  33. //eserv.v3_session.setLocalAddresses(hostname);
  34.     eserv.local_port=Integer.parseInt(args[0]);
  35.     eserv.v3_session.setLocalPort(eserv.local_port);
  36. //     eserv.api.setDebug( true );
  37.     eserv.remotePort = Integer.parseInt(args[2]);
  38. eserv.v3_session.setRemotePort(eserv.remotePort);
  39. eserv.remoteHost=args[1];
  40.     try {
  41.         eserv.v3_session.open();
  42.     } catch (SnmpException e) {
  43. System.out.println("Unable to start session: " + e);
  44. System.exit(1);
  45. }
  46.     eserv.v3_session.addSnmpClient(eserv);
  47.     eserv.start();
  48.      } // end main()
  49.     public boolean authenticate(SnmpPDU pdu, String community)
  50.     {
  51.          return true;
  52.     }
  53.     public boolean callback(SnmpSession sess, SnmpPDU pdu, int reqID)
  54.     {
  55.         if (pdu == null)
  56.         {
  57.             System.err.println("Null PDU received");
  58.         }
  59.         enQ(pdu);
  60.         return true;
  61.     }
  62.         public void debugPrint(String debugOutput)
  63.     {
  64.         return;
  65.     }
  66.     /** Print octet data in a more readable form */
  67.     String printOctets(byte[] data, int length) {
  68.     StringBuffer s = new StringBuffer();
  69.     int j = 0, line = 20; // we'll allow 20 bytes per line
  70.     if (data.length < length) length = data.length;
  71.     for (int i=0;i<length;i++) {
  72.         if (j++ > 19) { j=1; s.append("n"); }
  73.         String bs = Integer.toString(byteToInt(data[i]),16);
  74.         if (bs.length() < 2) bs = "0" + bs;
  75.         s.append(bs+ " ");
  76.     }
  77.     return s.toString();
  78.     }
  79.     public synchronized SnmpPDU deQ()
  80.     {
  81.         for (Enumeration e = v.elements() ; e.hasMoreElements() ;)
  82.         {
  83.                 SnmpPDU pdu = (SnmpPDU) e.nextElement();
  84.             v.removeElement(pdu);
  85.             return pdu;
  86.         }
  87.         return null;
  88.     }
  89.     
  90.     /** Place in specified queue */
  91.     public synchronized void enQ(SnmpPDU pdu)
  92.     {
  93.         v.addElement(pdu);
  94.         notifyAll();
  95.     }
  96.     public void run()
  97.     {
  98.         System.out.println("snmptrapforwarder: Ready to forward traps");
  99.         while (true)
  100.         {
  101.             SnmpPDU pdu = deQ();
  102.             if (pdu == null)
  103.                 wait_for_v3pdus();
  104.             if (pdu == null)
  105.                 pdu = deQ();
  106.             if (pdu == null)
  107.                 continue;
  108.             try {
  109. if ((pdu.getCommand() == SnmpAPI.TRP_REQ_MSG) 
  110. ||(pdu.getCommand() == SnmpAPI.TRP2_REQ_MSG)
  111. )
  112. {
  113. pdu.setRemotePort(remotePort);
  114. pdu.setRemoteHost(remoteHost);
  115. System.out.println("Trap is Forwarded to "+remoteHost+":"+remotePort);
  116.                  v3_session.send(pdu);
  117. }
  118. else
  119. {
  120. System.out.println("Not a Trap PDU -- dropping");
  121. }
  122.             } catch (SnmpException e) {
  123.                     System.err.println("Sending  PDU" + e.getMessage());
  124.                 continue;
  125.             }
  126.         }
  127.     }
  128.     public synchronized void wait_for_v3pdus()
  129.     {
  130.         try {
  131.             if (v.size() > 0)
  132.                 return;
  133.             else
  134.                 wait();
  135.         } catch (InterruptedException i) {}
  136.     }
  137.     
  138.     static int byteToInt(byte b) 
  139.         {
  140.           return (int)b & 0xFF;
  141.         }
  142.     
  143. }