root/src/scripts/noittrap

Revision 9857d4bbab175a52fd9271eecee034bf267d7f72, 2.9 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

fixes #37

  • Property mode set to 100755
Line 
1 #!/usr/bin/perl
2
3 use Getopt::Long;
4 my $community = 'public';
5 my $version = '2c';
6 my $status;
7 my $available;
8 my $state;
9 my $duration;
10
11 GetOptions("c=s", \$community,
12            "v=s", \$version,
13            "m=s", \$status,
14            "a=s", \$available,
15            "s=s", \$state,
16            "d=i", \$duration);
17
18 sub usage {
19   print STDERR "$0 [flags] [args]
20
21   This program is a wrapper around the snmptrap command from Net-SNMP.
22   It aids in constructing the (somewhat complex) incantation required to
23   affect a trap against a specific check with all of the meta information
24   set.  Multiple (zero or more) metrics may be set via a single invocation.
25
26 FLAGS:
27         -c <community>
28         [-v <version>]    # default 2c
29         [-s <G|B>]        # sets check state to Good/Bad
30         [-a <A|U>]        # sets check availability to Available/Unavailable
31         [-m <status>]     # sets check status message
32         [-d <#ms>]        # number of milliseconds the check took to complete
33
34 ARGS:
35         <targetnoitd> <uuid> [<metric> <type> <value>]*
36
37         <type>: s -> string
38                 x -> hex string
39                 i -> integer
40                 u -> unsigned integer
41                 t -> timeticks
42                 U -> unsigned 64bit integer
43                 I -> 64bit integer
44                 F -> floating point number
45                 D -> double
46 ";
47 }
48 if(@ARGV < 2) {
49   usage();
50   exit -1;
51 }
52 my $host = shift;
53 my $check = shift;
54 if(@ARGV % 3) {
55   usage();
56   exit -1;
57 }
58
59 my @check_oid = qw/1 3 6 1 4 1 32863 1 1 1/;
60 my @metric_oid = qw/1 3 6 1 4 1 32863 1 1 2/;
61 my @status_oid = qw/1 3 6 1 4 1 32863 1 1 3/;
62 my @state_oid = (@status_oid, 1);
63 my @available_oid = (@status_oid, 2);
64 my @duration_oid = (@status_oid, 3);
65 my @mesg_oid = (@status_oid, 4);
66
67 my $hp = qr/[0-9a-fA-F]{4}/;
68 if($check =~ /^($hp)($hp)-($hp)-($hp)-($hp)-($hp)($hp)($hp)$/) {
69   push @check_oid, hex($1);
70   push @check_oid, hex($2);
71   push @check_oid, hex($3);
72   push @check_oid, hex($4);
73   push @check_oid, hex($5);
74   push @check_oid, hex($6);
75   push @check_oid, hex($7);
76   push @check_oid, hex($8);
77 }
78 else {
79   die "Invalid check: $check\n";
80 }
81
82
83 my @cmd = qw/snmptrap/;
84 push @cmd, ('-c', $community);
85 push @cmd, ('-v', $version);
86 push @cmd, $host;
87 push @cmd, "0";
88 push @cmd, join('.', @check_oid);
89 if($state) {
90   push @cmd, join('.', @state_oid);
91   push @cmd, "object";
92   push @cmd, join('.', (@state_oid, ($state eq 'G') ? 1 : 2));
93 }
94 if($available) {
95   push @cmd, join('.', @available_oid);
96   push @cmd, "object";
97   push @cmd, join('.', (@available_oid, ($available eq 'A') ? 1 : 2));
98 }
99 if($available) {
100   push @cmd, join('.', @available_oid);
101   push @cmd, "object";
102   push @cmd, join('.', (@available_oid, ($available eq 'A') ? 1 : 2));
103 }
104 if($duration) {
105   push @cmd, (join('.', @duration_oid), "unsigned", $duration);
106 }
107 if($status) {
108   push @cmd, (join('.', @mesg_oid), "string", $status);
109 }
110 while(@ARGV) {
111   my $metric = shift;
112   my @moid = @metric_oid;
113   push @moid, length($metric);
114   foreach (split //, $metric) {
115     push @moid, ord($_);
116   }
117   push @cmd, join('.', @moid);
118   push @cmd, shift; #type
119   push @cmd, shift; #value
120 }
121
122 exec { "snmptrap" } @cmd;
Note: See TracBrowser for help on using the browser.