1 # Copyright (C) 2002 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: SiteConfig.pm,v 1.1 2010-12-27 00:03:39 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20 ## %Torrus::Global::treeConfig manipulation
22 package Torrus::SiteConfig;
27 our %validDaemonNames = ('collector' => 1,
30 our %mandatoryGraphStyles =
32 'SingleGraph' => {'color' => 1, 'line' => 1},
33 'HWBoundary' => {'color' => 1, 'line' => 1},
34 'HWFailure' => {'color' => 1},
35 'HruleMin' => {'color' => 1},
36 'HruleNormal' => {'color' => 1},
37 'HruleMax' => {'color' => 1},
38 'BpsIn' => {'color' => 1, 'line' => 1},
39 'BpsOut' => {'color' => 1, 'line' => 1}
42 %Torrus::SiteConfig::validLineStyles =
51 ## Verify the correctness of %Torrus::Global::treeConfig contents
56 if( not (scalar( keys %Torrus::Global::treeConfig )) )
58 Error('%Torrus::Global::treeConfig is not defined or empty');
62 foreach my $tree ( keys %Torrus::Global::treeConfig )
64 if( $tree !~ /^[a-zA-Z][a-zA-Z0-9_\-]*$/o )
66 Error("Invalid tree name: " . $tree);
71 if( not $Torrus::Global::treeConfig{$tree}{'description'} )
73 Error("Missing description for the tree named \"" . $tree . "\"");
77 my $xmlfiles = $Torrus::Global::treeConfig{$tree}{'xmlfiles'};
78 if( not ref( $xmlfiles ) or not scalar( @{$xmlfiles} ) )
80 Error("'xmlfiles' array is not defined for the tree named \"" .
86 foreach my $file ( @{$xmlfiles} )
88 $ok = findXMLFile( $file,
89 "in the tree named \"" . $tree . "\"" ) ?
93 if( ref( $Torrus::Global::treeConfig{$tree}{'run'} ) )
96 ( keys %{$Torrus::Global::treeConfig{$tree}{'run'}} )
98 if( not $validDaemonNames{$daemon} )
100 Error("\"" . $daemon . "\" is not a correct daemon " .
101 "name in the tree named \"" . $tree . "\"");
109 foreach my $file ( @Torrus::Global::xmlAlwaysIncludeFirst )
111 $ok = findXMLFile( $file,
112 'in @Torrus::Global::xmlAlwaysIncludeFirst' ) ?
115 foreach my $file ( @Torrus::Global::xmlAlwaysIncludeLast )
117 $ok = findXMLFile( $file,
118 'in @Torrus::Global::xmlAlwaysIncludeLast' ) ?
122 # Validate the styling profile
124 my $file = $Torrus::Global::stylingDir . '/' .
125 $Torrus::Renderer::stylingProfile . '.pl';
130 #Color names are always there
131 require $Torrus::Global::stylingDir . '/colornames.pl';
133 if( defined($Torrus::Renderer::stylingProfileOverlay) )
135 my $overlay = $Torrus::Renderer::stylingProfileOverlay;
142 Error('Error reading styling profile overlay from ' .
143 $overlay . ': File is not readable');
148 my $profile = \%Torrus::Renderer::graphStyles;
149 # Check if mandatory parameters present
150 foreach my $element ( keys %mandatoryGraphStyles )
152 if( ref( $profile->{$element} ) )
154 if( $mandatoryGraphStyles{$element}{'color'}
155 and not defined( $profile->{$element}{'color'} ) )
157 Error('Mandatory color for ' . $element .
158 ' is not defined in ' . $file);
161 if( $mandatoryGraphStyles{$element}{'line'}
162 and not defined( $profile->{$element}{'line'} ) )
164 Error('Mandatory line style for ' . $element .
165 ' is not defined in ' . $file);
171 Error('Mandatory styling for ' . $element .
172 ' is not defined in ' . $file);
176 # Check validity of all parameters
177 foreach my $element ( keys %{$profile} )
179 if( defined( $profile->{$element}{'color'} ) )
181 my $color = $profile->{$element}{'color'};
182 my $recursionLimit = 100;
184 while( $color =~ /^\#\#(\S+)$/ )
186 if( $recursionLimit-- <= 0 )
188 Error('Color recursion is too deep');
194 $color = $profile->{$colorName}{'color'};
195 if( not defined( $color ) )
197 Error('No color is defined for ' . $colorName);
203 if( $color !~ /^\#[0-9a-fA-F]{6}$/ )
205 Error('Invalid color specification for ' . $element .
210 if( defined( $profile->{$element}{'line'} ) )
212 if( not $Torrus::SiteConfig::validLineStyles{
213 $profile->{$element}{'line'}} )
215 Error('Invalid line specification for ' . $element .
224 Error('Error reading styling profile from ' . $file .
225 ': File is not readable');
239 if( defined( $file ) )
242 foreach my $dir ( @Torrus::Global::xmlDirs )
244 $filename = $dir . '/' . $file;
254 Error("Cannot find file: " . $file);
260 Error("File name undefined " . $msg);
269 return defined( $Torrus::Global::treeConfig{$tree} );
275 return( sort keys %Torrus::Global::treeConfig );
282 my $run = $Torrus::Global::treeConfig{$tree}{'run'}{'collector'};
283 return( defined($run) and $run > 0 );
286 sub collectorInstances
289 my $run = $Torrus::Global::treeConfig{$tree}{'run'}{'collector'};
290 return( (defined($run) and $run > 1) ? int($run) : 1 );
296 return $Torrus::Global::treeConfig{$tree}{'run'}{'monitor'};
303 return @{$Torrus::Global::treeConfig{$tree}{'xmlfiles'}};
310 return $Torrus::Global::treeConfig{$tree}{'description'};
316 require $Torrus::Global::stylingDir . '/' .
317 $Torrus::Renderer::stylingProfile . '.pl';
319 require $Torrus::Global::stylingDir . '/colornames.pl';
321 if( defined($Torrus::Renderer::stylingProfileOverlay) )
323 require $Torrus::Renderer::stylingProfileOverlay;
333 # indent-tabs-mode: nil
334 # perl-indent-level: 4