2 # Copyright (C) 2002 Stanislav Sinyagin
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 # $Id: configsnapshot.in,v 1.1 2010-12-27 00:04:01 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
21 BEGIN { require '@torrus_config_pl@'; }
27 use Torrus::ConfigTree;
28 use Torrus::SiteConfig;
29 use Torrus::ConfigBuilder;
31 exit(1) if not Torrus::SiteConfig::verify();
37 my $outfile = 'snapshot.xml';
43 my $creator = "Torrus version @VERSION@\n" .
44 "This file was generated by command:\n" .
46 foreach my $arg ( @ARGV )
50 $creator .= ' ' . $arg . ' ';
54 $creator .= "\'" . $arg . "\'\\\n";
57 $creator .= "\nOn " . scalar(localtime(time));
59 my $ok = GetOptions('tree=s' => \$tree,
61 'param=s' => \$filter_param,
62 'value=s' => \$filter_value,
63 'op=s' => \$filter_op,
64 'verbose' => \$verbose,
65 'help' => \$help_needed);
67 if( not $ok or not $tree or $help_needed or
68 ( defined($filter_param) + defined($filter_value) == 1 ) or
69 ( $filter_op ne '=' and $filter_op ne 'eq' and $filter_op ne 're' ) or
72 print STDERR "Usage: $0 --tree=NAME [options...]\n",
74 " --tree=NAME tree name\n",
75 " --out=filename output file [".$outfile."]\n",
76 " --param=PARAM --value=VALUE \n",
77 " filter the output by leaves with specified value\n",
78 " --op=OP filter operation [=|eq|re], default: [=]\n",
79 " --verbose print extra information\n",
80 " --help this help message\n";
86 Torrus::Log::setLevel('verbose');
89 if( not Torrus::SiteConfig::treeExists( $tree ) )
91 Error('Tree ' . $tree . ' does not exist');
95 &Torrus::DB::setSafeSignalHandlers();
97 my $config_tree = new Torrus::ConfigTree( -TreeName => $tree, -Wait => 1 );
98 if( not defined( $config_tree ) )
104 my $filter_match = sub {return $_[0] == $filter_value};
106 if(defined($filter_param))
108 if( $filter_op eq 'eq' )
110 $filter_match = sub {return $_[0] eq $filter_value};
112 elsif( $filter_op eq 're' )
114 $filter_match = sub {return $_[0] =~ $filter_value};
120 my $cb = new Torrus::ConfigBuilder;
122 $cb->addCreatorInfo( $creator );
124 # We don't collect views, since they are in defaults.xml which is always
127 collect_monitors( $config_tree, $cb );
128 collect_tokensets( $config_tree, $cb );
129 collect_definitions( $config_tree, $cb );
130 collect_datasources( $config_tree, $cb );
132 my $ok = $cb->toFile( $outfile );
135 Verbose('Wrote ' . $outfile);
139 Error('Cannot write ' . $outfile . ': ' . $!);
146 my $config_tree = shift;
149 my $monitorsNode = $cb->startMonitors();
151 foreach my $action ( $config_tree->getActionNames() )
153 &Torrus::DB::checkInterrupted();
155 my $params = $config_tree->getParams( $action );
156 $cb->addMonitorAction( $monitorsNode, $action, $params );
159 foreach my $monitor ( $config_tree->getMonitorNames() )
161 &Torrus::DB::checkInterrupted();
163 my $params = $config_tree->getParams( $monitor );
164 $cb->addMonitor( $monitorsNode, $monitor, $params );
168 sub collect_tokensets
170 my $config_tree = shift;
173 my $tsetsNode = $cb->startTokensets();
175 foreach my $tset ( $config_tree->getTsets() )
177 &Torrus::DB::checkInterrupted();
179 my $params = $config_tree->getParams( $tset );
182 $cb->addTokenset( $tsetsNode, $name, $params );
187 sub collect_definitions
189 my $config_tree = shift;
192 my $definitionsNode = $cb->startDefinitions();
194 foreach my $defName ( sort $config_tree->getDefinitionNames() )
196 &Torrus::DB::checkInterrupted();
198 my $value = $config_tree->getDefinition( $defName );
199 $cb->addDefinition( $definitionsNode, $defName, $value );
202 my $propsNode = $cb->startParamProps();
203 my $props = $config_tree->getParamProperties();
205 &Torrus::DB::checkInterrupted();
207 foreach my $prop ( sort keys %{$props} )
209 foreach my $param ( sort keys %{$props->{$prop}} )
211 $cb->addParamProp( $propsNode, $param, $prop,
212 $props->{$prop}{$param} );
221 sub collect_datasources
223 my $config_tree = shift;
226 my $topNode = $cb->getTopSubtree();
227 my $topToken = $config_tree->token('/');
229 my $params = prepare_params( $config_tree, $topToken );
230 $cb->addParams( $topNode, $params );
232 if( defined($filter_param) )
234 $filterTokens{$topToken} = apply_filter( $config_tree, $topToken );
237 collect_subtrees( $config_tree, $cb, $topToken, $topNode );
244 my $config_tree = shift;
247 $filterTokens{$token} = 0;
249 foreach my $ctoken ( $config_tree->getChildren( $token ) )
251 &Torrus::DB::checkInterrupted();
253 if( $config_tree->isSubtree( $ctoken ) )
255 $filterTokens{$token} += apply_filter( $config_tree, $ctoken );
257 elsif( $config_tree->isLeaf( $ctoken ) )
259 my $val = $config_tree->getNodeParam( $ctoken, $filter_param );
260 if( defined($val) and &{$filter_match}($val) )
262 $filterTokens{$ctoken} = 1;
263 $filterTokens{$token}++;
268 return $filterTokens{$token};
275 my $config_tree = shift;
278 my $parentNode = shift;
280 foreach my $ctoken ( $config_tree->getChildren( $token ) )
282 &Torrus::DB::checkInterrupted();
284 if( not defined($filter_param) or $filterTokens{$ctoken} )
287 $config_tree->nodeName( $config_tree->path($ctoken) );
288 my $params = prepare_params( $config_tree, $ctoken );
290 if( $config_tree->isSubtree( $ctoken ) )
293 $cb->addSubtree( $parentNode, $childName, $params );
294 collect_subtrees( $config_tree, $cb, $ctoken, $subtreeNode );
296 elsif( $config_tree->isLeaf( $ctoken ) )
298 $cb->addLeaf( $parentNode, $childName, $params );
301 foreach my $aliasToken ( $config_tree->getAliases( $ctoken ) )
303 $cb->addAlias( $parentNode,
304 $config_tree->path( $aliasToken ) );
313 my $config_tree = shift;
316 my $params = $config_tree->getParams( $token, 1 );
319 while( my( $param, $value ) = each %{$params} )
321 $value =~ s/\s+/ /gm;
322 $params->{$param} = $value;
330 # indent-tabs-mode: nil
331 # perl-indent-level: 4