2 # Copyright (C) 2002 Stanislav Sinyagin
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.
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.
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.
18 # $Id: action_snmptrap.in,v 1.1 2010-12-27 00:04:01 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
22 # See Torrus-MIB.txt for reference
25 use Net::SNMP qw(:ALL);
28 require '@snmptrap_siteconfig_pl@';
30 if( not $ENV{'TORRUS_TOKEN'} )
32 print STDERR ("Torrus environment variables missing. This program ",
33 "must be run from Torrus Monitor\n");
40 my $ok = GetOptions( 'host=s' => \@hosts,
41 'community=s' => \$Torrus::Snmptrap::community,
42 'port=i' => \$Torrus::Snmptrap::port,
43 'severity=i' => \$severity );
47 print STDERR ("Error parsing options\n");
51 if( scalar(@hosts) > 0 )
53 @Torrus::Snmptrap::hosts = @hosts;
56 my $oid_prefix = '.1.3.6.1.4.1.14697.1.1.1';
58 my %event_type = ( 'set' => 1,
64 my @varbindlist = ( $oid_prefix . '.1',
68 OCTET_STRING, $ENV{'TORRUS_TOKEN'},
71 OCTET_STRING, $ENV{'TORRUS_MONITOR'},
74 INTEGER, $event_type{$ENV{'TORRUS_EVENT'}},
77 OCTET_STRING, $ENV{'TORRUS_NODEPATH'},
80 OCTET_STRING, snmp_dateandtime( $ENV{'TORRUS_TSTAMP'} ),
83 OCTET_STRING, $ENV{'TORRUS_TREE'},
86 OCTET_STRING, $ENV{'TORRUS_MCOMMENT'}
89 if( defined( $severity ) )
93 INTEGER32, $severity );
97 foreach my $host ( @Torrus::Snmptrap::hosts )
99 my( $session, $error ) =
100 Net::SNMP->session( -hostname => $host,
101 -community => $Torrus::Snmptrap::community,
102 -port => $Torrus::Snmptrap::port,
106 if( not defined($session) )
108 printf STDERR ("Error opening SNMP trap session: %s.\n", $error);
114 $session->snmpv2_trap( -varbindlist => \@varbindlist );
118 printf STDERR ("Error sending SNMP trap: %s.\n", $session->error());
124 # Converts UNIX time to DateAndTime from SNMPv2-TC
125 # Currently timezone is not handled.
127 # DateAndTime ::= TEXTUAL-CONVENTION
128 # DISPLAY-HINT "2d-1d-1d,1d:1d:1d.1d,1a1d:1d"
131 # "A date-time specification.
133 # field octets contents range
134 # ----- ------ -------- -----
135 # 1 1-2 year* 0..65536
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
148 # - the value of year is in network-byte order
149 # - daylight saving time in New Zealand is +13
151 # For example, Tuesday May 26, 1992 at 1:30:15 PM EDT would be
154 # 1992-5-26,13:30:15.0,-4:0
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))
164 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
165 localtime( $thetime );
167 my $result = pack('nC6',
181 # indent-tabs-mode: nil
182 # perl-indent-level: 4