/opennms-base-assembly/src/main/filtered/bin/send-trap.pl

https://github.com/ajakubo1/opennms · Perl · 138 lines · 107 code · 23 blank · 8 comment · 17 complexity · 07de392cdcad567a5c601096894b6df4 MD5 · raw file

  1. #!${install.perl.bin}
  2. #
  3. # a quick hack of a script to send traps
  4. # by Ben Reed (ben@opennms.org)
  5. #
  6. # whee!
  7. $|++;
  8. use lib '.';
  9. use Net::SNMP;
  10. use Getopt::Mixed qw(nextOption);
  11. use Time::HiRes qw(usleep time);
  12. use vars qw(
  13. $BURST
  14. $COMMUNITY
  15. $DEBUG
  16. $DESTINATION
  17. $INTERVAL
  18. $NUMBER
  19. $OID
  20. $PORT
  21. $STRING
  22. $VERBOSE
  23. );
  24. $BURST = 0;
  25. $COMMUNITY = 'public';
  26. $DEBUG = 0;
  27. $DESTINATION = '127.0.0.1';
  28. $INTERVAL = 1000;
  29. $NUMBER = 10;
  30. $OID = '.1.3.6.1.4.1.7001';
  31. $PORT = 162;
  32. $STRING = 'OpenNMS Rules!';
  33. $VERBOSE = 0;
  34. Getopt::Mixed::init('d=s dest>d i=i interval>i n=i number>n b burst>b c=s community>c o=s oid>o p=i port>p s=s string>s v verbose>v h help>h');
  35. while (my ($option, $value) = nextOption()) {
  36. $BURST++ if ($option eq 'b');
  37. $COMMUNITY = $value if ($option eq 'c');
  38. $DESTINATION = $value if ($option eq 'd');
  39. $INTERVAL = $value if ($option eq 'i');
  40. $NUMBER = $value if ($option eq 'n');
  41. $OID = $value if ($option eq 'o');
  42. $PORT = $value if ($option eq 'p');
  43. $STRING = $value if ($option eq 's');
  44. $VERBOSE++ if ($option eq 'v');
  45. if ($option eq 'h') {
  46. print_help();
  47. exit;
  48. }
  49. }
  50. Getopt::Mixed::cleanup();
  51. if ($VERBOSE > 1) {
  52. $DEBUG = 1;
  53. print <<END_INFO;
  54. burst? $BURST
  55. verbose? $VERBOSE
  56. community: $COMMUNITY
  57. destination: $DESTINATION
  58. interval: $INTERVAL
  59. number: $NUMBER
  60. oid: $OID
  61. port: $PORT
  62. string: $STRING
  63. END_INFO
  64. }
  65. my $session = Net::SNMP->session(
  66. -hostname => $DESTINATION,
  67. -community => $COMMUNITY,
  68. # -nonblocking => 1,
  69. -timeout => 1,
  70. -retries => 1,
  71. -debug => $DEBUG,
  72. -port => $PORT,
  73. );
  74. my $start_time = time();
  75. my $counter = 1;
  76. while (1) {
  77. $session->trap(
  78. # -enterprise => '1.3.6.1.4.1.5813',
  79. -enterprise => $OID,
  80. -generictrap => 6,
  81. -specifictrap => 0,
  82. -varbindlist => [$OID.1, OCTET_STRING, $STRING . " ($counter)"],
  83. ) or die "error: $!";
  84. usleep($INTERVAL * 1000) unless $BURST;
  85. $counter++;
  86. last if ($counter > $NUMBER);
  87. }
  88. my $end_time = time();
  89. my $traps = ($counter / ($end_time - $start_time)) if ($counter != 0);
  90. printf("- sent %0.2f traps per second.\n", $traps);
  91. sub print_help {
  92. print <<END_HELP;
  93. usage:
  94. $0 [-b] [-d <dest_addr>] [-o <oid>] [-i <interval>] [-n <num_traps>] [-h] [-v]
  95. -b / --burst try to flood the SNMP agent (default: no)
  96. -c / --community the community string to use (default: public)
  97. -d / --dest destination address of the agent (default: 127.0.0.1)
  98. -p / --port the port to send to (default: 162)
  99. -i / --interval interval (in milliseconds) between sends (default: 1000)
  100. ignored if -b
  101. -n / --number number of traps to send (default: 10)
  102. -h / --help this help
  103. -v / --verbose verbose output (multiple -v's are allowed)
  104. other options:
  105. -o / --oid the enterprise OID of generated traps
  106. -s / --string the string to send in the varbinds
  107. END_HELP
  108. return 1;
  109. }