1 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 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 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 124 125 126 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 236 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 { 445 String 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 } 466 467 468 if (command == "send v1 trap") 469 { 470 try 471 { 472 473 String community = communityField.getText(); 474 int version = 0; 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 SNMPVarBindList varBindList = new SNMPVarBindList(); 488 489 String itemIDString = OIDField.getText(); 490 491 if (!itemIDString.equals("")) 492 { 493 SNMPObjectIdentifier itemID = new SNMPObjectIdentifier(itemIDString); 494 495 String valueString = valueField.getText(); 496 String valueTypeString = (String )valueTypeBox.getSelectedItem(); 497 valueTypeString = "snmp." + valueTypeString; 498 499 SNMPObject itemValue; 500 Class valueClass = Class.forName(valueTypeString); 501 itemValue = (SNMPObject)valueClass.newInstance(); 502 itemValue.setValue(valueString); 503 504 varBindList.addSNMPObject(new SNMPVariablePair(itemID, itemValue)); 505 } 506 507 SNMPv1TrapPDU pdu = new SNMPv1TrapPDU(enterpriseOID, agentAddress, genericTrap, specificTrap, timestamp, varBindList); 509 510 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 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 community = communityField.getText(); 543 InetAddress hostAddress = InetAddress.getByName(hostIDField.getText()); 544 545 546 SNMPObjectIdentifier snmpTrapOID = new SNMPObjectIdentifier(enterpriseField.getText()); 548 549 SNMPTimeTicks sysUptime = new SNMPTimeTicks((long)(System.currentTimeMillis()/10)); 551 552 SNMPVarBindList varBindList = new SNMPVarBindList(); 555 556 String itemIDString = OIDField.getText(); 557 558 if (!itemIDString.equals("")) 559 { 560 SNMPObjectIdentifier itemID = new SNMPObjectIdentifier(itemIDString); 561 562 String valueString = valueField.getText(); 563 String valueTypeString = (String )valueTypeBox.getSelectedItem(); 564 valueTypeString = "snmp." + valueTypeString; 565 566 SNMPObject itemValue; 567 Class valueClass = Class.forName(valueTypeString); 568 itemValue = (SNMPObject)valueClass.newInstance(); 569 itemValue.setValue(valueString); 570 571 varBindList.addSNMPObject(new SNMPVariablePair(itemID, itemValue)); 572 } 573 574 SNMPv2TrapPDU pdu = new SNMPv2TrapPDU(sysUptime, snmpTrapOID, varBindList); 576 577 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 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 community = communityField.getText(); 607 InetAddress hostAddress = InetAddress.getByName(hostIDField.getText()); 608 609 610 SNMPObjectIdentifier snmpTrapOID = new SNMPObjectIdentifier(enterpriseField.getText()); 612 613 SNMPTimeTicks sysUptime = new SNMPTimeTicks((long)(System.currentTimeMillis()/10)); 615 616 SNMPVarBindList varBindList = new SNMPVarBindList(); 619 620 String itemIDString = OIDField.getText(); 621 622 if (!itemIDString.equals("")) 623 { 624 SNMPObjectIdentifier itemID = new SNMPObjectIdentifier(itemIDString); 625 626 String valueString = valueField.getText(); 627 String valueTypeString = (String )valueTypeBox.getSelectedItem(); 628 valueTypeString = "snmp." + valueTypeString; 629 630 SNMPObject itemValue; 631 Class valueClass = Class.forName(valueTypeString); 632 itemValue = (SNMPObject)valueClass.newInstance(); 633 itemValue.setValue(valueString); 634 635 varBindList.addSNMPObject(new SNMPVariablePair(itemID, itemValue)); 636 } 637 638 SNMPv2InformRequestPDU pdu = new SNMPv2InformRequestPDU(sysUptime, snmpTrapOID, varBindList); 640 641 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 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 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 e) 679 {} 680 } 681 682 683 } | Popular Tags |