2 # Copyright (C) 2004-2005 Christian Schnidrig
3 # Copyright (C) 2007 Stanislav Sinyagin
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.
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.
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.
19 # $Id: CDef.pm,v 1.1 2010-12-27 00:03:57 ivan Exp $
20 # Christian Schnidrig <christian.schnidrig@bluewin.ch>
23 # Torrus collector module for combining multiple datasources into one
25 package Torrus::Collector::CDef;
29 use Torrus::Collector::CDef_Params;
30 use Torrus::ConfigTree;
33 use Torrus::DataAccess;
34 use Torrus::Collector::RRDStorage;
36 # Register the collector type
37 $Torrus::Collector::collectorTypes{'cdef'} = 1;
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;
44 # get access to the configTree;
45 $Torrus::Collector::needsConfigTree{'cdef'}{'runCollector'} = 1;
49 my $collector = shift;
52 my $cref = $collector->collectorData( 'cdef' );
53 if( not defined( $cref->{'crefTokens'} ) )
55 $cref->{'crefTokens'} = [];
58 push( @{$cref->{'crefTokens'}}, $token );
63 # This is first executed per target
64 $Torrus::Collector::runCollector{'cdef'} =
65 \&Torrus::Collector::CDef::runCollector;
69 my $collector = shift;
71 my $config_tree = $collector->configTree();
74 my $da = new Torrus::DataAccess;
76 # By default, try to get the data from one period behind
77 my $defaultAccessTime = $now -
78 ( $now % $collector->period() ) + $collector->offset();
80 foreach my $token ( @{$cref->{'crefTokens'}} )
82 &Torrus::DB::checkInterrupted();
84 my $accessTime = $defaultAccessTime -
85 ( $collector->period() *
86 $collector->param( $token, 'cdef-collector-delay' ) );
88 # The RRDtool is non-reentrant, and we need to be careful
89 # when running multiple threads
90 Torrus::Collector::RRDStorage::semaphoreDown();
92 my ($value, $timestamp) =
93 $da->read_RPN( $config_tree, $token,
94 $collector->param( $token, 'rpn-expr' ),
97 Torrus::Collector::RRDStorage::semaphoreUp();
99 if( defined( $value ) )
103 ( $collector->period() *
104 $collector->param( $token, 'cdef-collector-tolerance' ))))
106 Error( "CDEF: Data is " . ($accessTime-$timestamp) .
107 " seconds too old for " . $collector->path($token) );
111 $collector->setValue( $token, $value, $timestamp );