SnmpSendTrapUI.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:3k
- /* $Id: SnmpSendTrapUI.java,v 1.2 2002/06/18 07:19:20 kousalya Exp $ */
- /* SnmpSendTrapUI.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import com.adventnet.snmp.beans.*;
- import com.adventnet.snmp.ui.*;
- public class SnmpSendTrapUI implements TrapListener, ActionListener {
-
- SnmpTarget target = new SnmpTarget();
- SnmpTrapReceiver trapreceiver = new SnmpTrapReceiver();
- JTextArea textfield = new JTextArea();
- JButton clearbutton = new JButton();
- JButton setbutton = new JButton();
- String values [] = {"testing"};
- public static void main(String args[]) {
-
- SnmpSendTrapUI snmpops = new SnmpSendTrapUI();
- JFrame frame = new JFrame();
- frame.setVisible(true);
- frame.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent evt)
- {
- System.exit(0);
- }
- });
- frame.setSize(615,400);
- frame.setTitle( " Tutorial Example for receiving SNMP Traps" );
- frame.getContentPane().setLayout(null);
- snmpops.trapreceiver.setPort(8001);
- frame.getContentPane().add(snmpops.setbutton);
- snmpops.setbutton.setBounds(210,40,105,25);
- snmpops.setbutton.setText("Send Trap");
- frame.getContentPane().add(snmpops.textfield);
- snmpops.textfield.setBounds(30,80,490,120);
- snmpops.textfield.setText("Waiting to receive traps in the port "+snmpops.trapreceiver.getPort() +".......");
- frame.getContentPane().add(snmpops.clearbutton);
- snmpops.clearbutton.setBounds(185,225,105,35);
- snmpops.clearbutton.setText("Clear");
- snmpops.trapreceiver.addTrapListener(snmpops);
- snmpops.clearbutton.addActionListener(snmpops);
- snmpops.setbutton.addActionListener(snmpops);
-
- try {
- snmpops.target.loadMibs("RFC1213-MIB");
- } catch (Exception e) {
- System.err.println("Set Error: "+e.getMessage());
- }
-
- }
-
- public void actionPerformed(ActionEvent e) {
- if (e.getActionCommand().equals("Clear")) {
- textfield.setText("Waiting to receive traps in the port "+trapreceiver.getPort() +".......");
- }
- else if (e.getActionCommand().equals("Send Trap")) {
-
- target.setTargetPort( 8001 );
- target.setObjectID("1.5.0");
- try {
- target.snmpSendTrap("1.2.0", "localhost", 0, 6, 1000, values);
- } catch (Exception ex) {
- System.err.println("Set Error: "+ex.getMessage());
- }
- }
- }
-
- public void receivedTrap(TrapEvent trapEvent) {
-
- textfield.setText("TrapEvent received." +"n"+
- "Received a trap from:"+trapEvent.getRemoteHost()+ " in the port "+ trapreceiver.getPort()+"n"+
- "Community is:"+trapEvent.getCommunity()+"n"+
- "Agent Address is:"+trapEvent.getAgentAddress()+"n"+
- "Enterprise OID:"+trapEvent.getEnterprise()+"n"+
- "Trap Variable OID:"+trapEvent.getObjectID(0)+"n"+
- "Continues waiting to receive traps in the port "+trapreceiver.getPort() +".......");
- }
-
- }
-