1 # Copyright (C) 2010 Stanislav Sinyagin
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17 # $Id: SNMP_Failures.pm,v 1.1 2010-12-27 00:03:39 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
21 # SNMP failures statistics interface
23 package Torrus::SNMP_Failures;
37 %{$self->{'options'}} = %options;
39 die() if ( not defined($options{'-Tree'}) or
40 not defined($options{'-Instance'}) );
42 $self->{'db_failures'} =
43 new Torrus::DB( 'snmp_failures_' . $options{'-Instance'},
44 -Subdir => $self->{'options'}{'-Tree'},
46 -WriteAccess => $options{'-WriteAccess'} );
48 $self->{'counters'} = ['unreachable', 'deleted', 'mib_errors'];
50 return( defined( $self->{'db_failures'} ) ? $self:undef );
57 $self->{'db_failures'}->closeNow();
66 $self->{'db_failures'}->trunc();
68 foreach my $c ( @{$self->{'counters'}} )
70 $self->{'db_failures'}->put('c:' . $c, 0);
82 $self->{'db_failures'}->put('h:' . $hosthash,
83 $type . ':' . time());
93 $self->{'db_failures'}->put('c:' . $type, $count);
100 my $hosthash = shift;
102 $self->{'db_failures'}->del('h:' . $hosthash);
109 my $hosthash = shift;
112 my $count = $self->{'db_failures'}->get('M:' . $hosthash);
113 $count = 0 unless defined($count);
115 $self->{'db_failures'}->put('m:' . $hosthash, $path . ':' . time());
116 $self->{'db_failures'}->put('M:' . $hosthash, $count + 1);
118 my $global_count = $self->{'db_failures'}->get('c:mib_errors');
119 $self->{'db_failures'}->put('c:mib_errors', $global_count + 1);
130 foreach my $c ( @{$self->{'counters'}} )
132 if( not defined( $out->{'total_' . $c} ) )
134 $out->{'total_' . $c} = 0;
137 $out->{'total_' . $c} +=
138 $self->{'db_failures'}->get('c:' . $c);
140 if( $options{'-details'} and
141 not defined( $out->{'detail_' . $c} ) )
143 $out->{'detail_' . $c} = {};
147 &Torrus::DB::checkInterrupted();
149 if( $options{'-details'} )
151 my $cursor = $self->{'db_failures'}->cursor();
152 while( my ($key, $val) = $self->{'db_failures'}->next($cursor) )
154 if( $key =~ /^h:(.+)$/o )
157 my ($counter, $timestamp) = split(/:/o, $val);
159 $out->{'detail_' . $counter}{$hosthash} = {
160 'timestamp' => 0 + $timestamp,
161 'time' => scalar(localtime( $timestamp )),
164 elsif( $key =~ /^m:(.+)$/o )
167 my ($path, $timestamp) = split(/:/o, $val);
169 $out->{'detail_mib_errors'}{$hosthash}{'nodes'}{$path} = {
170 'timestamp' => 0 + $timestamp,
171 'time' => scalar(localtime( $timestamp )),
174 elsif( $key =~ /^M:(.+)$/o )
177 my $count = 0 + $val;
180 ( $out->{'detail_mib_errors'}{$hosthash}{'count'}) )
182 $out->{'detail_mib_errors'}{$hosthash}{'count'} = 0;
185 $out->{'detail_mib_errors'}{$hosthash}{'count'} += $count;
188 &Torrus::DB::checkInterrupted();
203 # indent-tabs-mode: nil
204 # perl-indent-level: 4