KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SNMPTrapTest


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
41 import snmp.*;
42
43
44
45
46 public class SNMPTrapTest extends JFrame
47                             implements ActionListener, SNMPv1TrapListener, SNMPv2TrapListener, SNMPv2InformRequestListener, Runnable JavaDoc
48 {
49     
50     private static final long serialVersionUID = 3046898865193941872L;
51     
52     JButton sendv1TrapButton, sendv2TrapButton, sendv2InformRequestButton, receiveButton;
53     JButton clearButton;
54     JTextArea messagesArea;
55     JScrollPane messagesScroll;
56     JTextField hostIDField, communityField, OIDField, valueField, enterpriseField, agentField;
57     JLabel authorLabel, hostIDLabel, communityLabel, OIDLabel, valueLabel, enterpriseLabel, agentLabel, genericTrapLabel, specificTrapLabel;
58     JComboBox valueTypeBox, genericTrapBox, specificTrapBox;
59     
60     MenuBar theMenubar;
61     Menu fileMenu;
62     MenuItem aboutItem, quitItem;
63     
64     
65     SNMPTrapReceiverInterface trapReceiverInterface;
66     SNMPTrapSenderInterface trapSenderInterface;
67     SNMPInformRequestSenderInterface informRequestSenderInterface;
68     
69     PipedReader errorReader;
70     PipedWriter errorWriter;
71     Thread JavaDoc readerThread;
72     
73     
74     
75     
76     // WindowCloseAdapter to catch window close-box closings
77
private class WindowCloseAdapter extends WindowAdapter
78     {
79         public void windowClosing(WindowEvent e)
80         {
81             readerThread.interrupt();
82             System.exit(0);
83         }
84     };
85             
86     
87     public SNMPTrapTest()
88     {
89         setUpDisplay();
90             
91         try
92         {
93             errorReader = new PipedReader();
94             errorWriter = new PipedWriter(errorReader);
95             
96             readerThread = new Thread JavaDoc(this);
97             readerThread.start();
98             
99             trapReceiverInterface = new SNMPTrapReceiverInterface(new PrintWriter(errorWriter));
100             trapReceiverInterface.addv1TrapListener(this);
101             trapReceiverInterface.addv2TrapListener(this);
102             trapReceiverInterface.addv2InformRequestListener(this);
103             trapReceiverInterface.startReceiving();
104             
105             trapSenderInterface = new SNMPTrapSenderInterface();
106             informRequestSenderInterface = new SNMPInformRequestSenderInterface();
107             
108         }
109         catch(Exception JavaDoc e)
110         {
111             messagesArea.append("Problem starting Trap Test: " + e.toString() + "\n");
112         }
113     }
114     
115     
116     
117     private void setUpDisplay()
118     {
119         
120         this.setTitle("SNMP Trap and Inform Test");
121             
122         this.getRootPane().setBorder(new BevelBorder(BevelBorder.RAISED));
123         
124         // set fonts to smaller-than-normal size, for compaction!
125
/*
126         FontUIResource appFont = new FontUIResource("SansSerif", Font.PLAIN, 10);
127         UIDefaults defaults = UIManager.getLookAndFeelDefaults();
128         Enumeration keys = defaults.keys();
129         
130         while (keys.hasMoreElements())
131         {
132             String nextKey = (String)(keys.nextElement());
133             if ((nextKey.indexOf("font") > -1) || (nextKey.indexOf("Font") > -1))
134             {
135                 UIManager.put(nextKey, appFont);
136             }
137         }
138         */

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

255         // set params for layout manager
256
GridBagLayout theLayout = new GridBagLayout();
257         GridBagConstraints c = new GridBagConstraints();
258         
259         c.gridwidth = 1;
260         c.gridheight = 1;
261         c.fill = GridBagConstraints.NONE;
262         c.ipadx = 0;
263         c.ipady = 0;
264         c.insets = new Insets(2,2,2,2);
265         c.anchor = GridBagConstraints.CENTER;
266         c.weightx = 0;
267         c.weighty = 0;
268         
269         
270         JPanel buttonPanel = new JPanel();
271         buttonPanel.setLayout(theLayout);
272         
273         c.gridx = 1;
274         c.gridy = 1;
275         theLayout.setConstraints(sendv1TrapButton, c);
276         buttonPanel.add(sendv1TrapButton);
277         
278         c.gridx = 2;
279         c.gridy = 1;
280         theLayout.setConstraints(sendv2TrapButton, c);
281         buttonPanel.add(sendv2TrapButton);
282         
283         c.gridx = 3;
284         c.gridy = 1;
285         theLayout.setConstraints(sendv2InformRequestButton, c);
286         buttonPanel.add(sendv2InformRequestButton);
287         
288         c.gridx = 4;
289         c.gridy = 1;
290         theLayout.setConstraints(receiveButton, c);
291         buttonPanel.add(receiveButton);
292         
293         JPanel hostPanel = new JPanel();
294         hostPanel.setLayout(theLayout);
295         
296         c.gridx = 1;
297         c.gridy = 1;
298         theLayout.setConstraints(hostIDLabel, c);
299         hostPanel.add(hostIDLabel);
300         
301         c.gridx = 2;
302         c.gridy = 1;
303         theLayout.setConstraints(hostIDField, c);
304         hostPanel.add(hostIDField);
305         
306         c.gridx = 1;
307         c.gridy = 2;
308         theLayout.setConstraints(communityLabel, c);
309         hostPanel.add(communityLabel);
310         
311         c.gridx = 2;
312         c.gridy = 2;
313         theLayout.setConstraints(communityField, c);
314         hostPanel.add(communityField);
315         
316         
317         
318         JPanel oidPanel = new JPanel();
319         oidPanel.setLayout(theLayout);
320         
321         c.gridx = 1;
322         c.gridy = 1;
323         theLayout.setConstraints(enterpriseLabel, c);
324         oidPanel.add(enterpriseLabel);
325         
326         c.gridx = 2;
327         c.gridy = 1;
328         theLayout.setConstraints(enterpriseField, c);
329         oidPanel.add(enterpriseField);
330         
331         c.gridx = 1;
332         c.gridy = 2;
333         theLayout.setConstraints(agentLabel, c);
334         oidPanel.add(agentLabel);
335         
336         c.gridx = 2;
337         c.gridy = 2;
338         theLayout.setConstraints(agentField, c);
339         oidPanel.add(agentField);
340         
341         c.gridx = 1;
342         c.gridy = 3;
343         theLayout.setConstraints(genericTrapLabel, c);
344         oidPanel.add(genericTrapLabel);
345         
346         c.gridx = 2;
347         c.gridy = 3;
348         theLayout.setConstraints(genericTrapBox, c);
349         oidPanel.add(genericTrapBox);
350         
351         c.gridx = 1;
352         c.gridy = 4;
353         theLayout.setConstraints(specificTrapLabel, c);
354         oidPanel.add(specificTrapLabel);
355         
356         c.gridx = 2;
357         c.gridy = 4;
358         theLayout.setConstraints(specificTrapBox, c);
359         oidPanel.add(specificTrapBox);
360         
361         c.gridx = 1;
362         c.gridy = 5;
363         theLayout.setConstraints(OIDLabel, c);
364         oidPanel.add(OIDLabel);
365         
366         c.gridx = 2;
367         c.gridy = 5;
368         theLayout.setConstraints(OIDField, c);
369         oidPanel.add(OIDField);
370         
371         c.gridx = 1;
372         c.gridy = 6;
373         theLayout.setConstraints(valueLabel, c);
374         oidPanel.add(valueLabel);
375         
376         c.gridx = 2;
377         c.gridy = 6;
378         theLayout.setConstraints(valueField, c);
379         oidPanel.add(valueField);
380         
381         c.gridx = 3;
382         c.gridy = 6;
383         theLayout.setConstraints(valueTypeBox, c);
384         oidPanel.add(valueTypeBox);
385         
386         
387         c.gridwidth = 1;
388         c.anchor = GridBagConstraints.CENTER;
389         
390         
391         
392         JPanel messagesPanel = new JPanel();
393         messagesPanel.setLayout(theLayout);
394         
395         c.gridx = 1;
396         c.gridy = 1;
397         c.anchor = GridBagConstraints.WEST;
398         JLabel messagesLabel = new JLabel("Received traps:");
399         theLayout.setConstraints(messagesLabel, c);
400         messagesPanel.add(messagesLabel);
401         
402         c.gridx = 2;
403         c.gridy = 1;
404         c.anchor = GridBagConstraints.EAST;
405         theLayout.setConstraints(clearButton, c);
406         messagesPanel.add(clearButton);
407         
408         c.fill = GridBagConstraints.BOTH;
409         c.gridx = 1;
410         c.gridy = 2;
411         c.gridwidth = 2;
412         c.weightx = .5;
413         c.weighty = .5;
414         c.anchor = GridBagConstraints.CENTER;
415         theLayout.setConstraints(messagesScroll, c);
416         messagesPanel.add(messagesScroll);
417         
418         
419         c.gridwidth = 1;
420         c.weightx = 0;
421         c.weighty = 0;
422         
423         
424         
425         this.getContentPane().setLayout(theLayout);
426         
427         
428         c.gridx = 1;
429         c.gridy = 1;
430         theLayout.setConstraints(hostPanel, c);
431         this.getContentPane().add(hostPanel);
432         
433         c.gridx = 1;
434         c.gridy = 2;
435         theLayout.setConstraints(oidPanel, c);
436         this.getContentPane().add(oidPanel);
437         
438         c.gridx = 1;
439         c.gridy = 3;
440         theLayout.setConstraints(buttonPanel, c);
441         this.getContentPane().add(buttonPanel);
442         
443         c.fill = GridBagConstraints.BOTH;
444         c.gridx = 1;
445         c.gridy = 4;
446         c.weightx = .5;
447         c.weighty = .5;
448         theLayout.setConstraints(messagesPanel, c);
449         this.getContentPane().add(messagesPanel);
450         
451         c.fill = GridBagConstraints.NONE;
452         c.gridx = 1;
453         c.gridy = 5;
454         c.weightx = 0;
455         c.weighty = 0;
456         theLayout.setConstraints(authorLabel, c);
457         this.getContentPane().add(authorLabel);
458         
459         
460     }
461     
462     
463     
464     
465     
466     public void actionPerformed(ActionEvent theEvent)
467     // respond to button pushes, menu selections
468
{
469         String JavaDoc command = theEvent.getActionCommand();
470         
471     
472         if (command == "quit")
473         {
474             readerThread.interrupt();
475             System.exit(0);
476         }
477         
478         
479         
480         if (command == "clear messages")
481         {
482             messagesArea.setText("");
483         }
484         
485         
486         
487         if (command == "about")
488         {
489             //AboutDialog aboutDialog = new AboutDialog(this);
490
}
491         
492         
493         
494         if (command == "stop receiving")
495         {
496             try
497             {
498                 trapReceiverInterface.stopReceiving();
499             }
500             catch (SocketException e)
501             {
502                 System.out.println("Got socket exception");
503             }
504             receiveButton.setText("Start receiving");
505             receiveButton.setActionCommand("start receiving");
506         }
507         
508         
509         
510         if (command == "start receiving")
511         {
512             trapReceiverInterface.startReceiving();
513             receiveButton.setText("Stop receiving");
514             receiveButton.setActionCommand("stop receiving");
515         }
516         
517         
518         
519         if (command == "send v1 trap")
520         {
521             try
522             {
523             
524                 String JavaDoc community = communityField.getText();
525                 InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
526                 
527                 SNMPObjectIdentifier enterpriseOID = new SNMPObjectIdentifier(enterpriseField.getText());
528                 SNMPIPAddress agentAddress = new SNMPIPAddress(agentField.getText());
529                 int genericTrap = genericTrapBox.getSelectedIndex();
530                 int specificTrap = specificTrapBox.getSelectedIndex();
531                 SNMPTimeTicks timestamp = new SNMPTimeTicks((long)(System.currentTimeMillis()/10));
532                 
533                 // see if have any additional variable pairs to send, and add them to
534
// the VarBindList if so
535
SNMPVarBindList varBindList = new SNMPVarBindList();
536                 
537                 String JavaDoc itemIDString = OIDField.getText();
538                 
539                 if (!itemIDString.equals(""))
540                 {
541                     SNMPObjectIdentifier itemID = new SNMPObjectIdentifier(itemIDString);
542                     
543                     String JavaDoc valueString = valueField.getText();
544                     String JavaDoc valueTypeString = (String JavaDoc)valueTypeBox.getSelectedItem();
545                     valueTypeString = "snmp." + valueTypeString;
546                     
547                     SNMPObject itemValue;
548                     Class JavaDoc valueClass = Class.forName(valueTypeString);
549                     itemValue = (SNMPObject)valueClass.newInstance();
550                     itemValue.setValue(valueString);
551                     
552                     varBindList.addSNMPObject(new SNMPVariablePair(itemID, itemValue));
553                 }
554                 
555                 // create trap pdu
556
SNMPv1TrapPDU pdu = new SNMPv1TrapPDU(enterpriseOID, agentAddress, genericTrap, specificTrap, timestamp, varBindList);
557     
558                 // and send it
559
messagesArea.append("Sending trap to " + hostIDField.getText() + ":\n");
560         
561                 messagesArea.append(" enterprise OID: " + pdu.getEnterpriseOID().toString() + "\n");
562                 messagesArea.append(" agent address: " + pdu.getAgentAddress().toString() + "\n");
563                 messagesArea.append(" generic trap: " + pdu.getGenericTrap() + "\n");
564                 messagesArea.append(" specific trap: " + pdu.getSpecificTrap() + "\n");
565                 messagesArea.append(" timestamp: " + pdu.getTimestamp() + "\n");
566                 messagesArea.append(" supplementary vars: " + pdu.getVarBindList().toString() + "\n");
567                 
568                 messagesArea.append("\n");
569                 
570                 
571                 trapSenderInterface.sendTrap(hostAddress, community, pdu);
572             
573             }
574             catch(InterruptedIOException e)
575             {
576                 messagesArea.append("Interrupted during trap send: " + e + "\n");
577             }
578             catch(Exception JavaDoc e)
579             {
580                 messagesArea.append("Exception during trap send: " + e + "\n");
581             }
582         }
583         
584         
585         if (command == "send v2 trap")
586         {
587             try
588             {
589             
590                 String JavaDoc community = communityField.getText();
591                 InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
592                 
593                 
594                 // use the enterprise OID field as the snmp trap OID
595
SNMPObjectIdentifier snmpTrapOID = new SNMPObjectIdentifier(enterpriseField.getText());
596                 
597                 // let uptime just be system time...
598
SNMPTimeTicks sysUptime = new SNMPTimeTicks((long)(System.currentTimeMillis()/10));
599                 
600                 // see if have any additional variable pairs to send, and add them to
601
// the VarBindList if so
602
SNMPVarBindList varBindList = new SNMPVarBindList();
603                 
604                 String JavaDoc itemIDString = OIDField.getText();
605                 
606                 if (!itemIDString.equals(""))
607                 {
608                     SNMPObjectIdentifier itemID = new SNMPObjectIdentifier(itemIDString);
609                     
610                     String JavaDoc valueString = valueField.getText();
611                     String JavaDoc valueTypeString = (String JavaDoc)valueTypeBox.getSelectedItem();
612                     valueTypeString = "snmp." + valueTypeString;
613                     
614                     SNMPObject itemValue;
615                     Class JavaDoc valueClass = Class.forName(valueTypeString);
616                     itemValue = (SNMPObject)valueClass.newInstance();
617                     itemValue.setValue(valueString);
618                     
619                     varBindList.addSNMPObject(new SNMPVariablePair(itemID, itemValue));
620                 }
621                 
622                 // create trap pdu
623
SNMPv2TrapPDU pdu = new SNMPv2TrapPDU(sysUptime, snmpTrapOID, varBindList);
624     
625                 // and send it
626
messagesArea.append("Sending trap to " + hostIDField.getText() + ":\n");
627         
628                 messagesArea.append(" system uptime: " + pdu.getSysUptime().toString() + "\n");
629                 messagesArea.append(" trap OID: " + pdu.getSNMPTrapOID().toString() + "\n");
630                 messagesArea.append(" var bind list: " + pdu.getVarBindList().toString() + "\n");
631                 
632                 messagesArea.append("\n");
633                 
634                 
635                 trapSenderInterface.sendTrap(hostAddress, community, pdu);
636             
637             }
638             catch(InterruptedIOException e)
639             {
640                 messagesArea.append("Interrupted during trap send: " + e + "\n");
641             }
642             catch(Exception JavaDoc e)
643             {
644                 messagesArea.append("Exception during trap send: " + e + "\n");
645             }
646         }
647         
648         
649         if (command == "send v2 inform request")
650         {
651             try
652             {
653             
654                 String JavaDoc community = communityField.getText();
655                 InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
656                 
657                 
658                 // use the enterprise OID field as the snmp trap OID
659
SNMPObjectIdentifier snmpTrapOID = new SNMPObjectIdentifier(enterpriseField.getText());
660                 
661                 // let uptime just be system time...
662
SNMPTimeTicks sysUptime = new SNMPTimeTicks((long)(System.currentTimeMillis()/10));
663                 
664                 // see if have any additional variable pairs to send, and add them to
665
// the VarBindList if so
666
SNMPVarBindList varBindList = new SNMPVarBindList();
667                 
668                 String JavaDoc itemIDString = OIDField.getText();
669                 
670                 if (!itemIDString.equals(""))
671                 {
672                     SNMPObjectIdentifier itemID = new SNMPObjectIdentifier(itemIDString);
673                     
674                     String JavaDoc valueString = valueField.getText();
675                     String JavaDoc valueTypeString = (String JavaDoc)valueTypeBox.getSelectedItem();
676                     valueTypeString = "snmp." + valueTypeString;
677                     
678                     SNMPObject itemValue;
679                     Class JavaDoc valueClass = Class.forName(valueTypeString);
680                     itemValue = (SNMPObject)valueClass.newInstance();
681                     itemValue.setValue(valueString);
682                     
683                     varBindList.addSNMPObject(new SNMPVariablePair(itemID, itemValue));
684                 }
685                 
686                 // create inform request pdu
687
SNMPv2InformRequestPDU pdu = new SNMPv2InformRequestPDU(sysUptime, snmpTrapOID, varBindList);
688     
689                 // and send it
690
messagesArea.append("Sending inform request to " + hostIDField.getText() + ":\n");
691         
692                 messagesArea.append(" system uptime: " + pdu.getSysUptime().toString() + "\n");
693                 messagesArea.append(" trap OID: " + pdu.getSNMPTrapOID().toString() + "\n");
694                 messagesArea.append(" var bind list: " + pdu.getVarBindList().toString() + "\n");
695                 
696                 messagesArea.append("\n");
697                 
698                 
699                 informRequestSenderInterface.sendInformRequest(hostAddress, community, pdu);
700             
701             }
702             catch(InterruptedIOException e)
703             {
704                 messagesArea.append("Interrupted during inform request send: " + e + "\n");
705             }
706             catch(Exception JavaDoc e)
707             {
708                 messagesArea.append("Exception during inform request send: " + e + "\n");
709             }
710         }
711         
712     }
713     
714     
715     
716             
717     
718     
719     public void processv1Trap(SNMPv1TrapPDU pdu, String JavaDoc communityName)
720     {
721         messagesArea.append("Got v1 trap:\n");
722         
723         messagesArea.append(" community name: " + communityName + "\n");
724         messagesArea.append(" enterprise OID: " + pdu.getEnterpriseOID().toString() + "\n");
725         messagesArea.append(" agent address: " + pdu.getAgentAddress().toString() + "\n");
726         messagesArea.append(" generic trap: " + pdu.getGenericTrap() + "\n");
727         messagesArea.append(" specific trap: " + pdu.getSpecificTrap() + "\n");
728         messagesArea.append(" timestamp: " + pdu.getTimestamp() + "\n");
729         messagesArea.append(" supplementary vars: " + pdu.getVarBindList().toString() + "\n");
730         
731         messagesArea.append("\n");
732         
733     }
734     
735     
736     
737     public void processv2Trap(SNMPv2TrapPDU pdu, String JavaDoc communityName, InetAddress agentIPAddress)
738     {
739         messagesArea.append("Got v2 trap:\n");
740         
741         messagesArea.append(" agent IP address: " + agentIPAddress.getHostAddress() + "\n");
742         messagesArea.append(" community name: " + communityName + "\n");
743         messagesArea.append(" system uptime: " + pdu.getSysUptime().toString() + "\n");
744         messagesArea.append(" trap OID: " + pdu.getSNMPTrapOID().toString() + "\n");
745         messagesArea.append(" var bind list: " + pdu.getVarBindList().toString() + "\n");
746                 
747         messagesArea.append("\n");
748         
749     }
750     
751     
752     
753     public void processv2InformRequest(SNMPv2InformRequestPDU pdu, String JavaDoc communityName, InetAddress agentIPAddress)
754     {
755         messagesArea.append("Got v2 inform request:\n");
756         
757         messagesArea.append(" sender IP address: " + agentIPAddress.getHostAddress() + "\n");
758         messagesArea.append(" community name: " + communityName + "\n");
759         messagesArea.append(" system uptime: " + pdu.getSysUptime().toString() + "\n");
760         messagesArea.append(" trap OID: " + pdu.getSNMPTrapOID().toString() + "\n");
761         messagesArea.append(" var bind list: " + pdu.getVarBindList().toString() + "\n");
762                 
763         messagesArea.append("\n");
764         
765     }
766     
767     
768     
769     
770     public void run()
771     {
772         int numChars;
773         char[] charArray = new char[256];
774         
775         try
776         {
777             while (!readerThread.isInterrupted() && ((numChars = errorReader.read(charArray, 0, charArray.length)) != -1))
778             {
779                 messagesArea.append("Problem receiving trap or inform:\n");
780                 messagesArea.append(new String JavaDoc(charArray, 0, numChars));
781                 messagesArea.append("\n\n");
782             }
783         }
784         catch(IOException e)
785         {
786             messagesArea.append("Problem receiving errors; error reporter exiting!");
787         }
788     }
789     
790     
791     
792     
793     
794     
795     
796     public static void main(String JavaDoc args[])
797     {
798         try
799         {
800             SNMPTrapTest theApp = new SNMPTrapTest();
801             theApp.pack();
802             theApp.setSize(700,500);
803             theApp.setVisible(true);
804         }
805         catch (Exception JavaDoc e)
806         {}
807     }
808     
809
810 }
Popular Tags