KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SNMPTrapSendTest


1 /*
2  * SNMP Trap Test
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
32 import java.util.*;
33 import java.net.*;
34 import java.awt.*;
35 import javax.swing.*;
36 import javax.swing.border.*;
37 import javax.swing.plaf.*;
38 import java.awt.event.*;
39 import java.io.*;
40 import snmp.*;
41
42
43
44
45 public class SNMPTrapSendTest extends JFrame
46                             implements ActionListener
47 {
48     
49     JButton sendv1TrapButton, sendv2TrapButton, sendv2InformRequestButton;
50     JButton clearButton;
51     JTextArea messagesArea;
52     JScrollPane messagesScroll;
53     JTextField hostIDField, communityField, OIDField, valueField, enterpriseField, agentField;
54     JLabel authorLabel, hostIDLabel, communityLabel, OIDLabel, valueLabel, enterpriseLabel, agentLabel, genericTrapLabel, specificTrapLabel;
55     JComboBox valueTypeBox, genericTrapBox, specificTrapBox;
56     
57     MenuBar theMenubar;
58     Menu fileMenu;
59     MenuItem aboutItem, quitItem;
60     
61     
62     SNMPTrapSenderInterface trapSenderInterface;
63     SNMPInformRequestSenderInterface informRequestSenderInterface;
64     
65     PipedReader errorReader;
66     PipedWriter errorWriter;
67     
68     
69     
70     
71     // WindowCloseAdapter to catch window close-box closings
72
private class WindowCloseAdapter extends WindowAdapter
73     {
74         public void windowClosing(WindowEvent e)
75         {
76             System.exit(0);
77         }
78     };
79             
80     
81     public SNMPTrapSendTest()
82     {
83         setUpDisplay();
84             
85         try
86         {
87             errorReader = new PipedReader();
88             errorWriter = new PipedWriter(errorReader);
89             
90             trapSenderInterface = new SNMPTrapSenderInterface();
91             informRequestSenderInterface = new SNMPInformRequestSenderInterface();
92             
93         }
94         catch(Exception JavaDoc e)
95         {
96             messagesArea.append("Problem starting Trap Test: " + e.toString() + "\n");
97         }
98     }
99     
100     
101     
102     private void setUpDisplay()
103     {
104         
105         this.setTitle("SNMP Trap Send Test");
106             
107         this.getRootPane().setBorder(new BevelBorder(BevelBorder.RAISED));
108         
109         // set fonts to smaller-than-normal size, for compaction!
110
/*
111         FontUIResource appFont = new FontUIResource("SansSerif", Font.PLAIN, 10);
112         UIDefaults defaults = UIManager.getLookAndFeelDefaults();
113         Enumeration keys = defaults.keys();
114         
115         while (keys.hasMoreElements())
116         {
117             String nextKey = (String)(keys.nextElement());
118             if ((nextKey.indexOf("font") > -1) || (nextKey.indexOf("Font") > -1))
119             {
120                 UIManager.put(nextKey, appFont);
121             }
122         }
123         */

124         
125         
126         // add WindowCloseAdapter to catch window close-box closings
127
addWindowListener(new WindowCloseAdapter());
128
129         
130         theMenubar = new MenuBar();
131         this.setMenuBar(theMenubar);
132         fileMenu = new Menu("File");
133         
134         aboutItem = new MenuItem("About...");
135         aboutItem.setActionCommand("about");
136         aboutItem.addActionListener(this);
137         fileMenu.add(aboutItem);
138         
139         fileMenu.addSeparator();
140         
141         quitItem = new MenuItem("Quit");
142         quitItem.setActionCommand("quit");
143         quitItem.addActionListener(this);
144         fileMenu.add(quitItem);
145         
146         theMenubar.add(fileMenu);
147         
148         
149         hostIDLabel = new JLabel("Trap receiver address:");
150         hostIDField = new JTextField(20);
151         hostIDField.setText("10.0.1.1");
152         hostIDField.setEditable(true);
153         
154         OIDLabel = new JLabel("additional variable OID:");
155         OIDField = new JTextField(20);
156         OIDField.setEditable(true);
157         
158         valueLabel = new JLabel("Value for additional variable:");
159         valueField = new JTextField(20);
160         valueField.setEditable(true);
161         
162         communityLabel = new JLabel("Community:");
163         communityField = new JTextField(20);
164         communityField.setText("public");
165         communityField.setEditable(true);
166         
167         authorLabel = new JLabel(" Version 1.1 J. Sevy, January 2001 ");
168         authorLabel.setFont(new Font("SansSerif", Font.ITALIC, 8));
169             
170         
171         sendv1TrapButton = new JButton("Send v1 trap");
172         sendv1TrapButton.setActionCommand("send v1 trap");
173         sendv1TrapButton.addActionListener(this);
174         
175         sendv2TrapButton = new JButton("Send v2 trap");
176         sendv2TrapButton.setActionCommand("send v2 trap");
177         sendv2TrapButton.addActionListener(this);
178         
179         sendv2InformRequestButton = new JButton("Send v2 inform request");
180         sendv2InformRequestButton.setActionCommand("send v2 inform request");
181         sendv2InformRequestButton.addActionListener(this);
182         
183         clearButton = new JButton("Clear messages");
184         clearButton.setActionCommand("clear messages");
185         clearButton.addActionListener(this);
186         
187         messagesArea = new JTextArea(10,60);
188         messagesScroll = new JScrollPane(messagesArea);
189         
190         valueTypeBox = new JComboBox();
191         valueTypeBox.addItem("SNMPInteger");
192         valueTypeBox.addItem("SNMPCounter32");
193         valueTypeBox.addItem("SNMPCounter64");
194         valueTypeBox.addItem("SNMPGauge32");
195         valueTypeBox.addItem("SNMPOctetString");
196         valueTypeBox.addItem("SNMPIPAddress");
197         valueTypeBox.addItem("SNMPNSAPAddress");
198         valueTypeBox.addItem("SNMPObjectIdentifier");
199         valueTypeBox.addItem("SNMPTimeTicks");
200         valueTypeBox.addItem("SNMPUInteger32");
201         
202         
203         
204         enterpriseLabel = new JLabel("Enterprise OID:");
205         enterpriseField = new JTextField(20);
206         enterpriseField.setEditable(true);
207         
208         agentLabel = new JLabel("Agent IP address:");
209         agentField = new JTextField(20);
210         agentField.setEditable(true);
211         
212         genericTrapLabel = new JLabel("Generic trap:");
213         genericTrapBox = new JComboBox();
214         genericTrapBox.addItem("Cold start (0)");
215         genericTrapBox.addItem("Warm start (1)");
216         genericTrapBox.addItem("Link down (2)");
217         genericTrapBox.addItem("Link up (3)");
218         genericTrapBox.addItem("Authentication failure (4)");
219         genericTrapBox.addItem("EGP neighbor loss (5)");
220         genericTrapBox.addItem("Enterprise specific (6)");
221         
222         
223         specificTrapLabel = new JLabel("Specific trap:");
224         specificTrapBox = new JComboBox();
225         specificTrapBox.addItem("0");
226         specificTrapBox.addItem("1");
227         specificTrapBox.addItem("2");
228         specificTrapBox.addItem("3");
229         specificTrapBox.addItem("4");
230         specificTrapBox.addItem("4");
231         
232
233         
234         // now set up display
235

236         // set params for layout manager
237
GridBagLayout theLayout = new GridBagLayout();
238         GridBagConstraints c = new GridBagConstraints();
239         
240         c.gridwidth = 1;
241         c.gridheight = 1;
242         c.fill = GridBagConstraints.NONE;
243         c.ipadx = 0;
244         c.ipady = 0;
245         c.insets = new Insets(2,2,2,2);
246         c.anchor = GridBagConstraints.CENTER;
247         c.weightx = 0;
248         c.weighty = 0;
249         
250         
251         JPanel buttonPanel = new JPanel();
252         buttonPanel.setLayout(theLayout);
253         
254         c.gridx = 1;
255         c.gridy = 1;
256         theLayout.setConstraints(sendv1TrapButton, c);
257         buttonPanel.add(sendv1TrapButton);
258         
259         c.gridx = 2;
260         c.gridy = 1;
261         theLayout.setConstraints(sendv2TrapButton, c);
262         buttonPanel.add(sendv2TrapButton);
263         
264         c.gridx = 3;
265         c.gridy = 1;
266         theLayout.setConstraints(sendv2InformRequestButton, c);
267         buttonPanel.add(sendv2InformRequestButton);
268         
269         JPanel hostPanel = new JPanel();
270         hostPanel.setLayout(theLayout);
271         
272         c.gridx = 1;
273         c.gridy = 1;
274         theLayout.setConstraints(hostIDLabel, c);
275         hostPanel.add(hostIDLabel);
276         
277         c.gridx = 2;
278         c.gridy = 1;
279         theLayout.setConstraints(hostIDField, c);
280         hostPanel.add(hostIDField);
281         
282         c.gridx = 1;
283         c.gridy = 2;
284         theLayout.setConstraints(communityLabel, c);
285         hostPanel.add(communityLabel);
286         
287         c.gridx = 2;
288         c.gridy = 2;
289         theLayout.setConstraints(communityField, c);
290         hostPanel.add(communityField);
291         
292         
293         
294         JPanel oidPanel = new JPanel();
295         oidPanel.setLayout(theLayout);
296         
297         c.gridx = 1;
298         c.gridy = 1;
299         theLayout.setConstraints(enterpriseLabel, c);
300         oidPanel.add(enterpriseLabel);
301         
302         c.gridx = 2;
303         c.gridy = 1;
304         theLayout.setConstraints(enterpriseField, c);
305         oidPanel.add(enterpriseField);
306         
307         c.gridx = 1;
308         c.gridy = 2;
309         theLayout.setConstraints(agentLabel, c);
310         oidPanel.add(agentLabel);
311         
312         c.gridx = 2;
313         c.gridy = 2;
314         theLayout.setConstraints(agentField, c);
315         oidPanel.add(agentField);
316         
317         c.gridx = 1;
318         c.gridy = 3;
319         theLayout.setConstraints(genericTrapLabel, c);
320         oidPanel.add(genericTrapLabel);
321         
322         c.gridx = 2;
323         c.gridy = 3;
324         theLayout.setConstraints(genericTrapBox, c);
325         oidPanel.add(genericTrapBox);
326         
327         c.gridx = 1;
328         c.gridy = 4;
329         theLayout.setConstraints(specificTrapLabel, c);
330         oidPanel.add(specificTrapLabel);
331         
332         c.gridx = 2;
333         c.gridy = 4;
334         theLayout.setConstraints(specificTrapBox, c);
335         oidPanel.add(specificTrapBox);
336         
337         c.gridx = 1;
338         c.gridy = 5;
339         theLayout.setConstraints(OIDLabel, c);
340         oidPanel.add(OIDLabel);
341         
342         c.gridx = 2;
343         c.gridy = 5;
344         theLayout.setConstraints(OIDField, c);
345         oidPanel.add(OIDField);
346         
347         c.gridx = 1;
348         c.gridy = 6;
349         theLayout.setConstraints(valueLabel, c);
350         oidPanel.add(valueLabel);
351         
352         c.gridx = 2;
353         c.gridy = 6;
354         theLayout.setConstraints(valueField, c);
355         oidPanel.add(valueField);
356         
357         c.gridx = 3;
358         c.gridy = 6;
359         theLayout.setConstraints(valueTypeBox, c);
360         oidPanel.add(valueTypeBox);
361         
362         
363         c.gridwidth = 1;
364         c.anchor = GridBagConstraints.CENTER;
365         
366         
367         
368         JPanel messagesPanel = new JPanel();
369         messagesPanel.setLayout(theLayout);
370         
371         c.gridx = 1;
372         c.gridy = 1;
373         c.anchor = GridBagConstraints.WEST;
374         JLabel messagesLabel = new JLabel("Sent traps:");
375         theLayout.setConstraints(messagesLabel, c);
376         messagesPanel.add(messagesLabel);
377         
378         c.gridx = 2;
379         c.gridy = 1;
380         c.anchor = GridBagConstraints.EAST;
381         theLayout.setConstraints(clearButton, c);
382         messagesPanel.add(clearButton);
383         
384         c.fill = GridBagConstraints.BOTH;
385         c.gridx = 1;
386         c.gridy = 2;
387         c.gridwidth = 2;
388         c.weightx = .5;
389         c.weighty = .5;
390         c.anchor = GridBagConstraints.CENTER;
391         theLayout.setConstraints(messagesScroll, c);
392         messagesPanel.add(messagesScroll);
393         
394         
395         c.gridwidth = 1;
396         c.weightx = 0;
397         c.weighty = 0;
398         
399         
400         
401         this.getContentPane().setLayout(theLayout);
402         
403         
404         c.gridx = 1;
405         c.gridy = 1;
406         theLayout.setConstraints(hostPanel, c);
407         this.getContentPane().add(hostPanel);
408         
409         c.gridx = 1;
410         c.gridy = 2;
411         theLayout.setConstraints(oidPanel, c);
412         this.getContentPane().add(oidPanel);
413         
414         c.gridx = 1;
415         c.gridy = 3;
416         theLayout.setConstraints(buttonPanel, c);
417         this.getContentPane().add(buttonPanel);
418         
419         c.fill = GridBagConstraints.BOTH;
420         c.gridx = 1;
421         c.gridy = 4;
422         c.weightx = .5;
423         c.weighty = .5;
424         theLayout.setConstraints(messagesPanel, c);
425         this.getContentPane().add(messagesPanel);
426         
427         c.fill = GridBagConstraints.NONE;
428         c.gridx = 1;
429         c.gridy = 5;
430         c.weightx = 0;
431         c.weighty = 0;
432         theLayout.setConstraints(authorLabel, c);
433         this.getContentPane().add(authorLabel);
434         
435         
436     }
437     
438     
439     
440     
441     
442     public void actionPerformed(ActionEvent theEvent)
443     // respond to button pushes, menu selections
444
{
445         String JavaDoc command = theEvent.getActionCommand();
446         
447     
448         if (command == "quit")
449         {
450             System.exit(0);
451         }
452         
453         
454         
455         if (command == "clear messages")
456         {
457             messagesArea.setText("");
458         }
459         
460         
461         
462         if (command == "about")
463         {
464             //AboutDialog aboutDialog = new AboutDialog(this);
465
}
466         
467         
468         if (command == "send v1 trap")
469         {
470             try
471             {
472             
473                 String JavaDoc community = communityField.getText();
474                 int version = 0; // SNMPv1
475
InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
476                 
477                 
478                 
479                 SNMPObjectIdentifier enterpriseOID = new SNMPObjectIdentifier(enterpriseField.getText());
480                 SNMPIPAddress agentAddress = new SNMPIPAddress(agentField.getText());
481                 int genericTrap = genericTrapBox.getSelectedIndex();
482                 int specificTrap = specificTrapBox.getSelectedIndex();
483                 SNMPTimeTicks timestamp = new SNMPTimeTicks((long)(System.currentTimeMillis()/10));
484                 
485                 // see if have any additional variable pairs to send, and add them to
486
// the VarBindList if so
487
SNMPVarBindList varBindList = new SNMPVarBindList();
488                 
489                 String JavaDoc itemIDString = OIDField.getText();
490                 
491                 if (!itemIDString.equals(""))
492                 {
493                     SNMPObjectIdentifier itemID = new SNMPObjectIdentifier(itemIDString);
494                     
495                     String JavaDoc valueString = valueField.getText();
496                     String JavaDoc valueTypeString = (String JavaDoc)valueTypeBox.getSelectedItem();
497                     valueTypeString = "snmp." + valueTypeString;
498                     
499                     SNMPObject itemValue;
500                     Class JavaDoc valueClass = Class.forName(valueTypeString);
501                     itemValue = (SNMPObject)valueClass.newInstance();
502                     itemValue.setValue(valueString);
503                     
504                     varBindList.addSNMPObject(new SNMPVariablePair(itemID, itemValue));
505                 }
506                 
507                 // create trap pdu
508
SNMPv1TrapPDU pdu = new SNMPv1TrapPDU(enterpriseOID, agentAddress, genericTrap, specificTrap, timestamp, varBindList);
509     
510                 // and send it
511
messagesArea.append("Sending trap to " + hostIDField.getText() + ":\n");
512         
513                 messagesArea.append(" enterprise OID: " + pdu.getEnterpriseOID().toString() + "\n");
514                 messagesArea.append(" agent address: " + pdu.getAgentAddress().toString() + "\n");
515                 messagesArea.append(" generic trap: " + pdu.getGenericTrap() + "\n");
516                 messagesArea.append(" specific trap: " + pdu.getSpecificTrap() + "\n");
517                 messagesArea.append(" timestamp: " + pdu.getTimestamp() + "\n");
518                 messagesArea.append(" supplementary vars: " + pdu.getVarBindList().toString() + "\n");
519                 
520                 messagesArea.append("\n");
521                 
522                 
523                 trapSenderInterface.sendTrap(hostAddress, community, pdu);
524             
525             }
526             catch(InterruptedIOException e)
527             {
528                 messagesArea.append("Interrupted during trap send: " + e + "\n");
529             }
530             catch(Exception JavaDoc e)
531             {
532                 messagesArea.append("Exception during trap send: " + e + "\n");
533             }
534         }
535         
536         
537         if (command == "send v2 trap")
538         {
539             try
540             {
541             
542                 String JavaDoc community = communityField.getText();
543                 InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
544                 
545                 
546                 // use the enterprise OID field as the snmp trap OID
547
SNMPObjectIdentifier snmpTrapOID = new SNMPObjectIdentifier(enterpriseField.getText());
548                 
549                 // let uptime just be system time...
550
SNMPTimeTicks sysUptime = new SNMPTimeTicks((long)(System.currentTimeMillis()/10));
551                 
552                 // see if have any additional variable pairs to send, and add them to
553
// the VarBindList if so
554
SNMPVarBindList varBindList = new SNMPVarBindList();
555                 
556                 String JavaDoc itemIDString = OIDField.getText();
557                 
558                 if (!itemIDString.equals(""))
559                 {
560                     SNMPObjectIdentifier itemID = new SNMPObjectIdentifier(itemIDString);
561                     
562                     String JavaDoc valueString = valueField.getText();
563                     String JavaDoc valueTypeString = (String JavaDoc)valueTypeBox.getSelectedItem();
564                     valueTypeString = "snmp." + valueTypeString;
565                     
566                     SNMPObject itemValue;
567                     Class JavaDoc valueClass = Class.forName(valueTypeString);
568                     itemValue = (SNMPObject)valueClass.newInstance();
569                     itemValue.setValue(valueString);
570                     
571                     varBindList.addSNMPObject(new SNMPVariablePair(itemID, itemValue));
572                 }
573                 
574                 // create trap pdu
575
SNMPv2TrapPDU pdu = new SNMPv2TrapPDU(sysUptime, snmpTrapOID, varBindList);
576     
577                 // and send it
578
messagesArea.append("Sending trap to " + hostIDField.getText() + ":\n");
579         
580                 messagesArea.append(" system uptime: " + pdu.getSysUptime().toString() + "\n");
581                 messagesArea.append(" trap OID: " + pdu.getSNMPTrapOID().toString() + "\n");
582                 messagesArea.append(" var bind list: " + pdu.getVarBindList().toString() + "\n");
583                 
584                 messagesArea.append("\n");
585                 
586                 
587                 trapSenderInterface.sendTrap(hostAddress, community, pdu);
588             
589             }
590             catch(InterruptedIOException e)
591             {
592                 messagesArea.append("Interrupted during trap send: " + e + "\n");
593             }
594             catch(Exception JavaDoc e)
595             {
596                 messagesArea.append("Exception during trap send: " + e + "\n");
597             }
598         }
599         
600         
601         if (command == "send v2 inform request")
602         {
603             try
604             {
605             
606                 String JavaDoc community = communityField.getText();
607                 InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
608                 
609                 
610                 // use the enterprise OID field as the snmp trap OID
611
SNMPObjectIdentifier snmpTrapOID = new SNMPObjectIdentifier(enterpriseField.getText());
612                 
613                 // let uptime just be system time...
614
SNMPTimeTicks sysUptime = new SNMPTimeTicks((long)(System.currentTimeMillis()/10));
615                 
616                 // see if have any additional variable pairs to send, and add them to
617
// the VarBindList if so
618
SNMPVarBindList varBindList = new SNMPVarBindList();
619                 
620                 String JavaDoc itemIDString = OIDField.getText();
621                 
622                 if (!itemIDString.equals(""))
623                 {
624                     SNMPObjectIdentifier itemID = new SNMPObjectIdentifier(itemIDString);
625                     
626                     String JavaDoc valueString = valueField.getText();
627                     String JavaDoc valueTypeString = (String JavaDoc)valueTypeBox.getSelectedItem();
628                     valueTypeString = "snmp." + valueTypeString;
629                     
630                     SNMPObject itemValue;
631                     Class JavaDoc valueClass = Class.forName(valueTypeString);
632                     itemValue = (SNMPObject)valueClass.newInstance();
633                     itemValue.setValue(valueString);
634                     
635                     varBindList.addSNMPObject(new SNMPVariablePair(itemID, itemValue));
636                 }
637                 
638                 // create inform request pdu
639
SNMPv2InformRequestPDU pdu = new SNMPv2InformRequestPDU(sysUptime, snmpTrapOID, varBindList);
640     
641                 // and send it
642
messagesArea.append("Sending inform request to " + hostIDField.getText() + ":\n");
643         
644                 messagesArea.append(" system uptime: " + pdu.getSysUptime().toString() + "\n");
645                 messagesArea.append(" trap OID: " + pdu.getSNMPTrapOID().toString() + "\n");
646                 messagesArea.append(" var bind list: " + pdu.getVarBindList().toString() + "\n");
647                 
648                 messagesArea.append("\n");
649                 
650                 
651                 informRequestSenderInterface.sendInformRequest(hostAddress, community, pdu);
652             
653             }
654             catch(InterruptedIOException e)
655             {
656                 messagesArea.append("Interrupted during inform request send: " + e + "\n");
657             }
658             catch(Exception JavaDoc e)
659             {
660                 messagesArea.append("Exception during inform request send: " + e + "\n");
661             }
662         }
663         
664     }
665     
666       
667     
668     
669     public static void main(String JavaDoc args[])
670     {
671         try
672         {
673             SNMPTrapSendTest theApp = new SNMPTrapSendTest();
674             theApp.pack();
675             theApp.setSize(700,500);
676             theApp.setVisible(true);
677         }
678         catch (Exception JavaDoc e)
679         {}
680     }
681     
682
683 }
Popular Tags