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.
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
23 BEGIN { require '@torrus_config_pl@'; }
28 use Torrus::ConfigTree;
29 use Torrus::SiteConfig;
30 use Torrus::ConfigBuilder;
32 exit(1) unless Torrus::SiteConfig::verify();
38 my $outfile = 'snapshot.xml';
46 my $creator = "Torrus version @VERSION@\n" .
47 "This file was generated by command:\n" .
53 $creator .= ' ' . $arg . ' ';
57 $creator .= "\'" . $arg . "\'\\\n";
60 $creator .= "\nOn " . scalar(localtime(time));
62 my $ok = GetOptions('tree=s' => \$tree,
64 'param=s' => \$filter_param,
65 'value=s' => \$filter_value,
66 'op=s' => \$filter_op,
67 'verbose' => \$verbose,
68 'help' => \$help_needed);
70 if( not $ok or not $tree or $help_needed or
71 ( defined($filter_param) + defined($filter_value) == 1 ) or
72 ( $filter_op ne '=' and $filter_op ne 'eq' and $filter_op ne 're' ) or
75 print STDERR "Usage: $0 --tree=NAME [options...]\n",
77 " --tree=NAME tree name\n",
78 " --out=filename output file [".$outfile."]\n",
79 " --param=PARAM --value=VALUE \n",
80 " filter the output by leaves with specified value\n",
81 " --op=OP filter operation [=|eq|re], default: [=]\n",
82 " --verbose print extra information\n",
83 " --help this help message\n";
89 Torrus::Log::setLevel('verbose');
92 if( not Torrus::SiteConfig::treeExists( $tree ) )
94 Error('Tree ' . $tree . ' does not exist');
98 &Torrus::DB::setSafeSignalHandlers();
100 my $filter_match = sub {return $_[0] == $filter_value};
102 if(defined($filter_param))
104 if( $filter_op eq 'eq' )
106 $filter_match = sub {return $_[0] eq $filter_value};
108 elsif( $filter_op eq 're' )
110 $filter_match = sub {return $_[0] =~ $filter_value};
115 my $config_tree = Torrus::ConfigTree->new( -TreeName => $tree, -Wait => 1 );
116 if( not defined( $config_tree ) )
121 my $cb = Torrus::ConfigBuilder->new();
123 $cb->addCreatorInfo( $creator );
125 # We don't collect views, since they are in defaults.xml which is always
128 collect_monitors( $config_tree, $cb );
129 collect_tokensets( $config_tree, $cb );
130 collect_definitions( $config_tree, $cb );
131 collect_datasources( $config_tree, $cb );
133 $ok = $cb->toFile( $outfile );
136 Verbose('Wrote ' . $outfile);
140 Error('Cannot write ' . $outfile . ': ' . $!);
148 my $config_tree = shift;
151 my $monitorsNode = $cb->startMonitors();
153 for my $action ( $config_tree->getActionNames() )
155 &Torrus::DB::checkInterrupted();
157 my $params = $config_tree->getParams( $action );
158 $cb->addMonitorAction( $monitorsNode, $action, $params );
161 for my $monitor ( $config_tree->getMonitorNames() )
163 &Torrus::DB::checkInterrupted();
165 my $params = $config_tree->getParams( $monitor );
166 $cb->addMonitor( $monitorsNode, $monitor, $params );
171 sub collect_tokensets
173 my $config_tree = shift;
176 my $tsetsNode = $cb->startTokensets();
178 for my $tset ( $config_tree->getTsets() )
180 &Torrus::DB::checkInterrupted();
182 my $params = $config_tree->getParams( $tset );
185 $cb->addTokenset( $tsetsNode, $name, $params );
191 sub collect_definitions
193 my $config_tree = shift;
196 my $definitionsNode = $cb->startDefinitions();
198 for my $defName ( sort $config_tree->getDefinitionNames() )
200 &Torrus::DB::checkInterrupted();
202 my $value = $config_tree->getDefinition( $defName );
203 $cb->addDefinition( $definitionsNode, $defName, $value );
206 my $propsNode = $cb->startParamProps();
207 my $props = $config_tree->getParamProperties();
209 &Torrus::DB::checkInterrupted();
211 for my $prop ( sort keys %{$props} )
213 for my $param ( sort keys %{$props->{$prop}} )
215 $cb->addParamProp( $propsNode, $param, $prop,
216 $props->{$prop}{$param} );
225 sub collect_datasources
227 my $config_tree = shift;
230 my $topNode = $cb->getTopSubtree();
231 my $topToken = $config_tree->token('/');
233 my $params = prepare_params( $config_tree, $topToken );
234 $cb->addParams( $topNode, $params );
236 if( defined($filter_param) )
238 $filterTokens{$topToken} = apply_filter( $config_tree, $topToken );
241 collect_subtrees( $config_tree, $cb, $topToken, $topNode );
249 my $config_tree = shift;
252 $filterTokens{$token} = 0;
254 for my $ctoken ( $config_tree->getChildren( $token ) )
256 &Torrus::DB::checkInterrupted();
258 if( $config_tree->isSubtree( $ctoken ) )
260 $filterTokens{$token} += apply_filter( $config_tree, $ctoken );
262 elsif( $config_tree->isLeaf( $ctoken ) )
264 my $val = $config_tree->getNodeParam( $ctoken, $filter_param );
265 if( defined($val) and &{$filter_match}($val) )
267 $filterTokens{$ctoken} = 1;
268 $filterTokens{$token}++;
273 return $filterTokens{$token};
280 my $config_tree = shift;
283 my $parentNode = shift;
285 for my $ctoken ( $config_tree->getChildren( $token ) )
287 &Torrus::DB::checkInterrupted();
289 if( not defined($filter_param) or $filterTokens{$ctoken} )
292 $config_tree->nodeName( $config_tree->path($ctoken) );
293 my $params = prepare_params( $config_tree, $ctoken );
295 if( $config_tree->isSubtree( $ctoken ) )
298 $cb->addSubtree( $parentNode, $childName, $params );
299 collect_subtrees( $config_tree, $cb, $ctoken, $subtreeNode );
301 elsif( $config_tree->isLeaf( $ctoken ) )
303 $cb->addLeaf( $parentNode, $childName, $params );
306 for my $aliasToken ( $config_tree->getAliases( $ctoken ) )
308 $cb->addAlias( $parentNode,
309 $config_tree->path( $aliasToken ) );
319 my $config_tree = shift;
322 my $params = $config_tree->getParams( $token, 1 );
325 while( my( $param, $value ) = each %{$params} )
327 $value =~ s/\s+/ /gm;
328 $params->{$param} = $value;
336 # indent-tabs-mode: nil
337 # perl-indent-level: 4