KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPTrapSenderInterface


1 /*
2  * SNMP Package
3  *
4  * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
5  *
6  * This is free software. Redistribution and use in source and binary forms, with
7  * or without modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */

29
30
31 package snmp;
32
33 import java.io.*;
34 import java.net.*;
35
36
37 /**
38 * The class SNMPTrapSenderInterface implements an interface for sending SNMPv1 and SNMPv2 trap messages to a
39 * remote SNMP manager. The approach is that from version 1 of SNMP, using no encryption of data.
40 * Communication occurs via UDP, using port 162, the standard SNMP trap port, as the destination port.
41 */

42
43 public class SNMPTrapSenderInterface
44 {
45     public static final int SNMP_TRAP_PORT = 162;
46     
47     private DatagramSocket dSocket;
48     
49     
50     /**
51     * Construct a new trap sender object to send traps to remote SNMP hosts.
52     */

53     
54     public SNMPTrapSenderInterface()
55         throws SocketException
56     {
57         dSocket = new DatagramSocket();
58     }
59     
60
61     
62     /**
63     * Construct a new trap sender object to send traps to remote SNMP hosts, binding to
64     * the specified local port.
65     */

66     
67     public SNMPTrapSenderInterface(int localPort)
68         throws SocketException
69     {
70         dSocket = new DatagramSocket(localPort);
71     }
72     
73
74     
75     /**
76     * Send the supplied SNMPv1 trap pdu to the specified host, using the supplied version number
77     * and community name. Use version = 0 for SNMP version 1, or version = 1 for enhanced
78     * capabilities provided through RFC 1157.
79     */

80     
81     public void sendTrap(int version, InetAddress hostAddress, String JavaDoc community, SNMPv1TrapPDU pdu)
82         throws IOException
83     {
84         SNMPMessage message = new SNMPMessage(version, community, pdu);
85         
86         byte[] messageEncoding = message.getBEREncoding();
87         
88         /*
89         System.out.println("Request Message bytes:");
90         
91         for (int i = 0; i < messageEncoding.length; ++i)
92             System.out.print(hexByte(messageEncoding[i]) + " ");
93         */

94         
95         DatagramPacket outPacket = new DatagramPacket(messageEncoding, messageEncoding.length, hostAddress, SNMP_TRAP_PORT);
96         
97         /*
98         System.out.println("Message bytes length (out): " + outPacket.getLength());
99         
100         System.out.println("Message bytes (out):");
101         for (int i = 0; i < messageEncoding.length; ++i)
102         {
103             System.out.print(hexByte(messageEncoding[i]) + " ");
104         }
105         System.out.println("\n");
106         */

107         
108         dSocket.send(outPacket);
109         
110     }
111     
112     
113     
114     /**
115     * Send the supplied trap pdu to the specified host, using the supplied community name and
116     * using 0 for the version field in the SNMP message (corresponding to SNMP version 1).
117     */

118     
119     public void sendTrap(InetAddress hostAddress, String JavaDoc community, SNMPv1TrapPDU pdu)
120         throws IOException
121     {
122         int version = 0;
123     
124         sendTrap(version, hostAddress, community, pdu);
125     }
126     
127     
128     
129     /**
130     * Send the supplied SNMPv2 trap pdu to the specified host, using the supplied version number
131     * and community name.
132     */

133     
134     public void sendTrap(int version, InetAddress hostAddress, String JavaDoc community, SNMPv2TrapPDU pdu)
135         throws IOException
136     {
137         SNMPMessage message = new SNMPMessage(version, community, pdu);
138         
139         byte[] messageEncoding = message.getBEREncoding();
140         
141         /*
142         System.out.println("Request Message bytes:");
143         
144         for (int i = 0; i < messageEncoding.length; ++i)
145             System.out.print(hexByte(messageEncoding[i]) + " ");
146         */

147         
148         DatagramPacket outPacket = new DatagramPacket(messageEncoding, messageEncoding.length, hostAddress, SNMP_TRAP_PORT);
149         
150         /*
151         System.out.println("Message bytes length (out): " + outPacket.getLength());
152         
153         System.out.println("Message bytes (out):");
154         for (int i = 0; i < messageEncoding.length; ++i)
155         {
156             System.out.print(hexByte(messageEncoding[i]) + " ");
157         }
158         System.out.println("\n");
159         */

160         
161         dSocket.send(outPacket);
162         
163     }
164     
165     
166     
167     /**
168     * Send the supplied trap pdu to the specified host, using the supplied community name and
169     * using 1 for the version field in the SNMP message.
170     */

171     
172     public void sendTrap(InetAddress hostAddress, String JavaDoc community, SNMPv2TrapPDU pdu)
173         throws IOException
174     {
175         int version = 1;
176     
177         sendTrap(version, hostAddress, community, pdu);
178     }
179     
180     
181     
182     private String JavaDoc hexByte(byte b)
183     {
184         int pos = b;
185         if (pos < 0)
186             pos += 256;
187         String JavaDoc returnString = new String JavaDoc();
188         returnString += Integer.toHexString(pos/16);
189         returnString += Integer.toHexString(pos%16);
190         return returnString;
191     }
192     
193     
194     
195     
196     
197     
198     private String JavaDoc getHex(byte theByte)
199     {
200         int b = theByte;
201         
202         if (b < 0)
203             b += 256;
204         
205         String JavaDoc returnString = new String JavaDoc(Integer.toHexString(b));
206         
207         // add leading 0 if needed
208
if (returnString.length()%2 == 1)
209             returnString = "0" + returnString;
210             
211         return returnString;
212     }
213     
214     
215     
216 }
Popular Tags