1 29 30 31 32 import java.util.*; 33 import java.awt.*; 34 import javax.swing.*; 35 import javax.swing.border.*; 36 import javax.swing.plaf.*; 37 import java.awt.event.*; 38 import java.io.*; 39 import java.net.*; 40 41 import snmp.*; 42 43 44 45 46 public class SNMPTrapReceiveTest extends JFrame 47 implements ActionListener, SNMPv1TrapListener, SNMPv2TrapListener, SNMPv2InformRequestListener, Runnable  48 { 49 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 SNMPTrapReceiverInterface trapReceiverInterface; 63 64 PipedReader errorReader; 65 PipedWriter errorWriter; 66 Thread readerThread; 67 68 69 70 71 private class WindowCloseAdapter extends WindowAdapter 73 { 74 public void windowClosing(WindowEvent e) 75 { 76 readerThread.interrupt(); 77 System.exit(0); 78 } 79 }; 80 81 82 public SNMPTrapReceiveTest() 83 { 84 setUpDisplay(); 85 86 try 87 { 88 errorReader = new PipedReader(); 89 errorWriter = new PipedWriter(errorReader); 90 91 readerThread = new Thread (this); 92 readerThread.start(); 93 94 trapReceiverInterface = new SNMPTrapReceiverInterface(new PrintWriter(errorWriter)); 95 trapReceiverInterface.addv1TrapListener(this); 96 trapReceiverInterface.addv2TrapListener(this); 97 trapReceiverInterface.addv2InformRequestListener(this); 98 trapReceiverInterface.startReceiving(); 99 100 } 101 catch(Exception e) 102 { 103 messagesArea.append("Problem starting Trap Test: " + e.toString() + "\n"); 104 } 105 } 106 107 108 109 private void setUpDisplay() 110 { 111 112 this.setTitle("SNMP Trap Receive Test"); 113 114 this.getRootPane().setBorder(new BevelBorder(BevelBorder.RAISED)); 115 116 131 132 133 addWindowListener(new WindowCloseAdapter()); 135 136 137 theMenubar = new MenuBar(); 138 this.setMenuBar(theMenubar); 139 fileMenu = new Menu("File"); 140 141 aboutItem = new MenuItem("About..."); 142 aboutItem.setActionCommand("about"); 143 aboutItem.addActionListener(this); 144 fileMenu.add(aboutItem); 145 146 fileMenu.addSeparator(); 147 148 quitItem = new MenuItem("Quit"); 149 quitItem.setActionCommand("quit"); 150 quitItem.addActionListener(this); 151 fileMenu.add(quitItem); 152 153 theMenubar.add(fileMenu); 154 155 156 authorLabel = new JLabel(" Version 1.1 J. Sevy, January 2001 "); 157 authorLabel.setFont(new Font("SansSerif", Font.ITALIC, 8)); 158 159 160 clearButton = new JButton("Clear messages"); 161 clearButton.setActionCommand("clear messages"); 162 clearButton.addActionListener(this); 163 164 messagesArea = new JTextArea(10,60); 165 messagesScroll = new JScrollPane(messagesArea); 166 167 168 170 GridBagLayout theLayout = new GridBagLayout(); 172 GridBagConstraints c = new GridBagConstraints(); 173 174 c.gridwidth = 1; 175 c.gridheight = 1; 176 c.fill = GridBagConstraints.NONE; 177 c.ipadx = 0; 178 c.ipady = 0; 179 c.insets = new Insets(2,2,2,2); 180 c.anchor = GridBagConstraints.CENTER; 181 c.weightx = 0; 182 c.weighty = 0; 183 184 185 JPanel messagesPanel = new JPanel(); 186 messagesPanel.setLayout(theLayout); 187 188 c.gridx = 1; 189 c.gridy = 1; 190 c.anchor = GridBagConstraints.WEST; 191 JLabel messagesLabel = new JLabel("Received traps:"); 192 theLayout.setConstraints(messagesLabel, c); 193 messagesPanel.add(messagesLabel); 194 195 c.gridx = 2; 196 c.gridy = 1; 197 c.anchor = GridBagConstraints.EAST; 198 theLayout.setConstraints(clearButton, c); 199 messagesPanel.add(clearButton); 200 201 c.fill = GridBagConstraints.BOTH; 202 c.gridx = 1; 203 c.gridy = 2; 204 c.gridwidth = 2; 205 c.weightx = .5; 206 c.weighty = .5; 207 c.anchor = GridBagConstraints.CENTER; 208 theLayout.setConstraints(messagesScroll, c); 209 messagesPanel.add(messagesScroll); 210 211 212 c.gridwidth = 1; 213 c.weightx = 0; 214 c.weighty = 0; 215 216 217 218 this.getContentPane().setLayout(theLayout); 219 220 221 c.fill = GridBagConstraints.BOTH; 222 c.gridx = 1; 223 c.gridy = 1; 224 c.weightx = .5; 225 c.weighty = .5; 226 theLayout.setConstraints(messagesPanel, c); 227 this.getContentPane().add(messagesPanel); 228 229 c.fill = GridBagConstraints.NONE; 230 c.gridx = 1; 231 c.gridy = 2; 232 c.weightx = 0; 233 c.weighty = 0; 234 theLayout.setConstraints(authorLabel, c); 235 this.getContentPane().add(authorLabel); 236 237 238 } 239 240 241 242 243 244 public void actionPerformed(ActionEvent theEvent) 245 { 247 String command = theEvent.getActionCommand(); 248 249 250 if (command == "quit") 251 { 252 readerThread.interrupt(); 253 System.exit(0); 254 } 255 256 257 258 if (command == "clear messages") 259 { 260 messagesArea.setText(""); 261 } 262 263 264 265 if (command == "about") 266 { 267 } 269 270 271 } 272 273 274 275 276 277 278 public void processv1Trap(SNMPv1TrapPDU pdu, String communityName) 279 { 280 messagesArea.append("Got v1 trap:\n"); 281 282 messagesArea.append(" community name: " + communityName + "\n"); 283 messagesArea.append(" enterprise OID: " + pdu.getEnterpriseOID().toString() + "\n"); 284 messagesArea.append(" agent address: " + pdu.getAgentAddress().toString() + "\n"); 285 messagesArea.append(" generic trap: " + pdu.getGenericTrap() + "\n"); 286 messagesArea.append(" specific trap: " + pdu.getSpecificTrap() + "\n"); 287 messagesArea.append(" timestamp: " + pdu.getTimestamp() + "\n"); 288 messagesArea.append(" supplementary vars: " + pdu.getVarBindList().toString() + "\n"); 289 290 messagesArea.append("\n"); 291 292 } 293 294 295 296 public void processv2Trap(SNMPv2TrapPDU pdu, String communityName, InetAddress agentIPAddress) 297 { 298 messagesArea.append("Got v2 trap:\n"); 299 300 messagesArea.append(" agent IP address: " + agentIPAddress.getHostAddress() + "\n"); 301 messagesArea.append(" community name: " + communityName + "\n"); 302 messagesArea.append(" system uptime: " + pdu.getSysUptime().toString() + "\n"); 303 messagesArea.append(" trap OID: " + pdu.getSNMPTrapOID().toString() + "\n"); 304 messagesArea.append(" var bind list: " + pdu.getVarBindList().toString() + "\n"); 305 306 messagesArea.append("\n"); 307 308 } 309 310 311 312 public void processv2InformRequest(SNMPv2InformRequestPDU pdu, String communityName, InetAddress agentIPAddress) 313 { 314 messagesArea.append("Got v2 inform request:\n"); 315 316 messagesArea.append(" sender IP address: " + agentIPAddress.getHostAddress() + "\n"); 317 messagesArea.append(" community name: " + communityName + "\n"); 318 messagesArea.append(" system uptime: " + pdu.getSysUptime().toString() + "\n"); 319 messagesArea.append(" trap OID: " + pdu.getSNMPTrapOID().toString() + "\n"); 320 messagesArea.append(" var bind list: " + pdu.getVarBindList().toString() + "\n"); 321 322 messagesArea.append("\n"); 323 324 } 325 326 327 328 329 public void run() 330 { 331 int numChars; 332 char[] charArray = new char[256]; 333 334 try 335 { 336 while (!readerThread.isInterrupted() && ((numChars = errorReader.read(charArray, 0, charArray.length)) != -1)) 337 { 338 messagesArea.append("Problem receiving trap or inform:\n"); 339 messagesArea.append(new String (charArray, 0, numChars)); 340 messagesArea.append("\n\n"); 341 } 342 } 343 catch(IOException e) 344 { 345 messagesArea.append("Problem receiving errors; error reporter exiting!"); 346 } 347 } 348 349 350 351 352 353 354 355 public static void main(String args[]) 356 { 357 try 358 { 359 SNMPTrapReceiveTest theApp = new SNMPTrapReceiveTest(); 360 theApp.pack(); 361 theApp.setSize(700,500); 362 theApp.setVisible(true); 363 } 364 catch (Exception e) 365 {} 366 } 367 368 369 } | Popular Tags |