import torrus 1.0.9
[freeside.git] / torrus / perllib / Torrus / Collector / CDef.pm
1 #
2 #  Copyright (C) 2004-2005  Christian Schnidrig
3 #  Copyright (C) 2007  Stanislav Sinyagin
4 #
5 #  This program is free software; you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation; either version 2 of the License, or
8 #  (at your option) any later version.
9 #
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program; if not, write to the Free Software
17 #  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 # $Id: CDef.pm,v 1.1 2010-12-27 00:03:57 ivan Exp $
20 # Christian Schnidrig <christian.schnidrig@bluewin.ch>
21
22
23 # Torrus collector module for combining multiple datasources into one
24
25 package Torrus::Collector::CDef;
26
27 use strict;
28
29 use Torrus::Collector::CDef_Params;
30 use Torrus::ConfigTree;
31 use Torrus::Log;
32 use Torrus::RPN;
33 use Torrus::DataAccess;
34 use Torrus::Collector::RRDStorage;
35
36 # Register the collector type
37 $Torrus::Collector::collectorTypes{'cdef'} = 1;
38
39 # List of needed parameters and default values
40 $Torrus::Collector::params{'cdef'} = \%Torrus::Collector::CDef_Params::params;
41 $Torrus::Collector::initTarget{'cdef'} = \&Torrus::Collector::CDef::initTarget;
42
43
44 # get access to the configTree;
45 $Torrus::Collector::needsConfigTree{'cdef'}{'runCollector'} = 1;
46
47 sub initTarget
48 {
49     my $collector = shift;
50     my $token = shift;
51     
52     my $cref = $collector->collectorData( 'cdef' );
53     if( not defined( $cref->{'crefTokens'} ) )
54     {
55         $cref->{'crefTokens'} = [];
56     }
57
58     push( @{$cref->{'crefTokens'}}, $token );
59     
60     return 1;
61 }
62
63 # This is first executed per target
64 $Torrus::Collector::runCollector{'cdef'} =
65     \&Torrus::Collector::CDef::runCollector;
66
67 sub runCollector
68 {
69     my $collector = shift;
70     my $cref = shift;
71     my $config_tree = $collector->configTree();
72
73     my $now = time();
74     my $da = new Torrus::DataAccess;
75
76     # By default, try to get the data from one period behind
77     my $defaultAccessTime = $now -
78         ( $now % $collector->period() ) + $collector->offset();
79     
80     foreach my $token ( @{$cref->{'crefTokens'}} )
81     {
82         &Torrus::DB::checkInterrupted();
83         
84         my $accessTime = $defaultAccessTime -
85             ( $collector->period() *
86               $collector->param( $token, 'cdef-collector-delay' ) );
87
88         # The RRDtool is non-reentrant, and we need to be careful
89         # when running multiple threads
90         Torrus::Collector::RRDStorage::semaphoreDown();
91         
92         my ($value, $timestamp) =
93             $da->read_RPN( $config_tree, $token,
94                            $collector->param( $token, 'rpn-expr' ),
95                            $accessTime );
96
97         Torrus::Collector::RRDStorage::semaphoreUp();
98
99         if( defined( $value ) )
100         {
101             if ( $timestamp <
102                  ( $accessTime -
103                    ( $collector->period() *
104                      $collector->param( $token, 'cdef-collector-tolerance' ))))
105             {
106                 Error( "CDEF: Data is " . ($accessTime-$timestamp) .
107                        " seconds too old for " . $collector->path($token) );
108             }
109             else
110             {
111                 $collector->setValue( $token, $value, $timestamp );
112             }
113         }
114     }
115 }
116
117
118
119 1;
120