PageRenderTime 25ms CodeModel.GetById 8ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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