per-agent configuration of batch processors, #71837
[freeside.git] / torrus / perllib / Torrus / SNMP_Failures.pm
1 #  Copyright (C) 2010  Stanislav Sinyagin
2 #
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.
7 #
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.
12 #
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.
16
17 # $Id: SNMP_Failures.pm,v 1.1 2010-12-27 00:03:39 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
19
20
21 # SNMP failures statistics interface
22
23 package Torrus::SNMP_Failures;
24
25 use Torrus::DB;
26 use Torrus::Log;
27 use strict;
28
29
30 sub new
31 {
32     my $self = {};
33     my $class = shift;
34     my %options = @_;
35     bless $self, $class;
36
37     %{$self->{'options'}} = %options;
38
39     die() if ( not defined($options{'-Tree'}) or
40                not defined($options{'-Instance'}) );
41
42     $self->{'db_failures'} =
43         new Torrus::DB( 'snmp_failures_' . $options{'-Instance'},
44                         -Subdir => $self->{'options'}{'-Tree'},
45                         -Btree => 1,
46                         -WriteAccess => $options{'-WriteAccess'} );
47
48     $self->{'counters'} = ['unreachable', 'deleted', 'mib_errors'];
49     
50     return( defined( $self->{'db_failures'} ) ? $self:undef );
51 }
52
53
54 sub DESTROY
55 {
56     my $self = shift;    
57     $self->{'db_failures'}->closeNow();
58 }
59
60
61
62 sub init
63 {
64     my $self = shift;
65
66     $self->{'db_failures'}->trunc();
67     
68     foreach my $c ( @{$self->{'counters'}} )
69     {
70         $self->{'db_failures'}->put('c:' . $c, 0);
71     }
72 }
73
74
75
76 sub host_failure
77 {
78     my $self = shift;    
79     my $type = shift;
80     my $hosthash = shift;
81
82     $self->{'db_failures'}->put('h:' . $hosthash,
83                                 $type . ':' . time());
84 }
85
86
87 sub set_counter
88 {
89     my $self = shift;    
90     my $type = shift;
91     my $count = shift;
92
93     $self->{'db_failures'}->put('c:' . $type, $count);
94 }
95     
96
97 sub remove_host
98 {
99     my $self = shift;    
100     my $hosthash = shift;
101
102     $self->{'db_failures'}->del('h:' . $hosthash);
103 }
104
105     
106 sub mib_error
107 {
108     my $self = shift;    
109     my $hosthash = shift;
110     my $path = shift;
111
112     my $count = $self->{'db_failures'}->get('M:' . $hosthash);
113     $count = 0 unless defined($count);
114
115     $self->{'db_failures'}->put('m:' . $hosthash, $path . ':' . time());    
116     $self->{'db_failures'}->put('M:' . $hosthash, $count + 1);
117
118     my $global_count = $self->{'db_failures'}->get('c:mib_errors');
119     $self->{'db_failures'}->put('c:mib_errors', $global_count + 1);
120 }
121
122
123
124 sub read
125 {
126     my $self = shift;
127     my $out = shift;
128     my %options = @_;
129
130     foreach my $c ( @{$self->{'counters'}} )
131     {
132         if( not defined( $out->{'total_' . $c} ) )
133         {
134             $out->{'total_' . $c} = 0;
135         }
136         
137         $out->{'total_' . $c} += 
138             $self->{'db_failures'}->get('c:' . $c);
139
140         if( $options{'-details'} and
141             not defined( $out->{'detail_' . $c} ) )
142         {
143             $out->{'detail_' . $c} = {};
144         }
145     }
146
147     &Torrus::DB::checkInterrupted();
148         
149     if( $options{'-details'} )
150     {
151         my $cursor = $self->{'db_failures'}->cursor();
152         while( my ($key, $val) = $self->{'db_failures'}->next($cursor) )
153         {
154             if( $key =~ /^h:(.+)$/o )
155             {
156                 my $hosthash = $1;
157                 my ($counter, $timestamp) = split(/:/o, $val);
158
159                 $out->{'detail_' . $counter}{$hosthash} = {
160                     'timestamp' => 0 + $timestamp,
161                     'time' => scalar(localtime( $timestamp )),
162                 };
163             }
164             elsif( $key =~ /^m:(.+)$/o )
165             {
166                 my $hosthash = $1;
167                 my ($path, $timestamp) = split(/:/o, $val);
168
169                 $out->{'detail_mib_errors'}{$hosthash}{'nodes'}{$path} = {
170                     'timestamp' => 0 + $timestamp,
171                     'time' => scalar(localtime( $timestamp )),
172                 }
173             }
174             elsif( $key =~ /^M:(.+)$/o )
175             {
176                 my $hosthash = $1;
177                 my $count = 0 + $val;
178                 
179                 if( not defined
180                     ( $out->{'detail_mib_errors'}{$hosthash}{'count'}) )
181                 {
182                     $out->{'detail_mib_errors'}{$hosthash}{'count'} = 0;
183                 }
184                 
185                 $out->{'detail_mib_errors'}{$hosthash}{'count'} += $count;
186             }
187             
188             &Torrus::DB::checkInterrupted();
189         }
190         
191         undef $cursor;
192     }
193 }
194
195
196
197
198 1;
199
200
201 # Local Variables:
202 # mode: perl
203 # indent-tabs-mode: nil
204 # perl-indent-level: 4
205 # End: