This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / torrus / bin / action_snmptrap.in
1 #!@PERL@
2 #  Copyright (C) 2002  Stanislav Sinyagin
3 #
4 #  This program is free software; you can redistribute it and/or modify
5 #  it under the terms of the GNU General Public License as published by
6 #  the Free Software Foundation; either version 2 of the License, or
7 #  (at your option) any later version.
8 #
9 #  This program is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #  GNU General Public License for more details.
13 #
14 #  You should have received a copy of the GNU General Public License
15 #  along with this program; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
18 # $Id: action_snmptrap.in,v 1.1 2010-12-27 00:04:01 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20
21 # SNMP v2c trap
22 # See Torrus-MIB.txt for reference
23
24 use strict;
25 use Net::SNMP qw(:ALL);
26 use Getopt::Long;
27
28 require '@snmptrap_siteconfig_pl@';
29
30 if( not $ENV{'TORRUS_TOKEN'} )
31 {
32     print STDERR ("Torrus environment variables missing. This program ",
33                   "must be run from Torrus Monitor\n");
34     exit 1;
35 }
36
37 my @hosts;
38 my $severity;
39
40 my $ok = GetOptions( 'host=s'     => \@hosts,
41                      'community=s' => \$Torrus::Snmptrap::community,
42                      'port=i'      => \$Torrus::Snmptrap::port,
43                      'severity=i'  => \$severity );
44
45 if( not $ok )
46 {
47     print STDERR ("Error parsing options\n");
48     exit 1;
49 }
50
51 if( scalar(@hosts) > 0 )
52 {
53     @Torrus::Snmptrap::hosts = @hosts;
54 }
55
56 my $oid_prefix = '.1.3.6.1.4.1.14697.1.1.1';
57
58 my %event_type = ( 'set'    => 1,
59                    'repeat' => 2,
60                    'clear'  => 3,
61                    'forget' => 4
62                    );
63
64 my @varbindlist = ( $oid_prefix . '.1',
65                     INTEGER32, 1,
66
67                     $oid_prefix . '.2',
68                     OCTET_STRING, $ENV{'TORRUS_TOKEN'},
69
70                     $oid_prefix . '.3',
71                     OCTET_STRING, $ENV{'TORRUS_MONITOR'},
72
73                     $oid_prefix . '.4',
74                     INTEGER, $event_type{$ENV{'TORRUS_EVENT'}},
75
76                     $oid_prefix . '.5',
77                     OCTET_STRING, $ENV{'TORRUS_NODEPATH'},
78
79                     $oid_prefix . '.6',
80                     OCTET_STRING, snmp_dateandtime( $ENV{'TORRUS_TSTAMP'} ),
81
82                     $oid_prefix . '.7',
83                     OCTET_STRING, $ENV{'TORRUS_TREE'},
84                     
85                     $oid_prefix . '.9',
86                     OCTET_STRING, $ENV{'TORRUS_MCOMMENT'}
87                     );
88
89 if( defined( $severity ) )
90 {
91     push( @varbindlist,
92           $oid_prefix . '.8',
93           INTEGER32, $severity );
94 }
95           
96
97 foreach my $host ( @Torrus::Snmptrap::hosts )
98 {
99     my( $session, $error ) =
100         Net::SNMP->session( -hostname  => $host,
101                             -community => $Torrus::Snmptrap::community,
102                             -port      => $Torrus::Snmptrap::port,
103                             -version   => 2
104                             );
105
106     if( not defined($session) )
107     {
108         printf STDERR ("Error opening SNMP trap session: %s.\n", $error);
109         exit 1;
110     }
111
112
113     my $result =
114         $session->snmpv2_trap( -varbindlist  => \@varbindlist );
115
116     if( not $result )
117     {
118         printf STDERR ("Error sending SNMP trap: %s.\n", $session->error());
119     }
120
121     $session->close();
122 }
123
124 # Converts UNIX time to DateAndTime from SNMPv2-TC
125 # Currently timezone is not handled.
126
127 # DateAndTime ::= TEXTUAL-CONVENTION
128 #     DISPLAY-HINT "2d-1d-1d,1d:1d:1d.1d,1a1d:1d"
129 #     STATUS       current
130 #     DESCRIPTION
131 #             "A date-time specification.
132 #
133 #             field  octets  contents                  range
134 #             -----  ------  --------                  -----
135 #               1      1-2   year*                     0..65536
136 #               2       3    month                     1..12
137 #               3       4    day                       1..31
138 #               4       5    hour                      0..23
139 #               5       6    minutes                   0..59
140 #               6       7    seconds                   0..60
141 #                            (use 60 for leap-second)
142 #               7       8    deci-seconds              0..9
143 #               8       9    direction from UTC        '+' / '-'
144 #               9      10    hours from UTC*           0..13
145 #              10      11    minutes from UTC          0..59
146 #
147 #             * Notes:
148 #             - the value of year is in network-byte order
149 #             - daylight saving time in New Zealand is +13
150 #
151 #             For example, Tuesday May 26, 1992 at 1:30:15 PM EDT would be
152 #             displayed as:
153 #
154 #                              1992-5-26,13:30:15.0,-4:0
155 #
156 #             Note that if only local time is known, then timezone
157 #             information (fields 8-10) is not present."
158 #     SYNTAX       OCTET STRING (SIZE (8 | 11))
159
160 sub snmp_dateandtime
161 {
162     my $thetime = shift;
163
164     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
165         localtime( $thetime );
166
167     my $result = pack('nC6',
168                       $year + 1900,
169                       $mon + 1,
170                       $mday,
171                       $hour,
172                       $min,
173                       $sec,
174                       0);
175     return $result;
176 }
177
178
179 # Local Variables:
180 # mode: perl
181 # indent-tabs-mode: nil
182 # perl-indent-level: 4
183 # End: