fix up use of new
[torrus-plus.git] / src / bin / configsnapshot.in
blobae4f7caa025e75ff92ebe2f751ccf4f4f02390c7
1 #!@PERL@
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$
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
21 use strict;
22 use warnings;
23 BEGIN { require '@torrus_config_pl@'; }
25 use Getopt::Long;
27 use Torrus::Log;
28 use Torrus::ConfigTree;
29 use Torrus::SiteConfig;
30 use Torrus::ConfigBuilder;
32 exit(1) unless Torrus::SiteConfig::verify();
34 my $tree;
35 my $help_needed;
36 my $verbose = 0;
38 my $outfile = 'snapshot.xml';
40 my $filter_param;
41 my $filter_value;
42 my $filter_op = '=';
44 my %filterTokens;
46 my $creator = "Torrus version @VERSION@\n" .
47     "This file was generated by command:\n" .
48     $0 . " \\\n";
49 for my $arg ( @ARGV )
51     if( $arg =~ m/^--/ )
52     {
53         $creator .= ' ' . $arg . ' ';
54     }
55     else
56     {
57         $creator .= "\'" . $arg . "\'\\\n";
58     }
60 $creator .= "\nOn " . scalar(localtime(time));
62 my $ok = GetOptions('tree=s'   => \$tree,
63                     'out=s'    => \$outfile,
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
73     scalar(@ARGV) > 0 )
75     print STDERR "Usage: $0 --tree=NAME [options...]\n",
76     "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";
84     exit 1;
87 if( $verbose )
89     Torrus::Log::setLevel('verbose');
92 if( not Torrus::SiteConfig::treeExists( $tree ) )
94     Error('Tree ' . $tree . ' does not exist');
95     exit 1;
98 &Torrus::DB::setSafeSignalHandlers();
100 my $filter_match = sub {return $_[0] == $filter_value};
102 if(defined($filter_param))
104     if( $filter_op eq 'eq' )
105     {
106         $filter_match = sub {return $_[0] eq $filter_value};
107     }
108     elsif( $filter_op eq 're' )
109     {
110         $filter_match = sub {return $_[0] =~ $filter_value};
111     }
115     my $config_tree = Torrus::ConfigTree->new( -TreeName => $tree, -Wait => 1 );
116     if( not defined( $config_tree ) )
117     {
118         exit 1;
119     }
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
126     # included
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 );
134     if( $ok )
135     {
136         Verbose('Wrote ' . $outfile);
137     }
138     else
139     {
140         Error('Cannot write ' . $outfile . ': ' . $!);
141     }
144 exit($ok ? 0:1);
146 sub collect_monitors
148     my $config_tree = shift;
149     my $cb = shift;
151     my $monitorsNode = $cb->startMonitors();
153     for my $action ( $config_tree->getActionNames() )
154     {
155         &Torrus::DB::checkInterrupted();
157         my $params = $config_tree->getParams( $action );
158         $cb->addMonitorAction( $monitorsNode, $action, $params );
159     }
161     for my $monitor ( $config_tree->getMonitorNames() )
162     {
163         &Torrus::DB::checkInterrupted();
165         my $params = $config_tree->getParams( $monitor );
166         $cb->addMonitor( $monitorsNode, $monitor, $params );
167     }
168     return;
171 sub collect_tokensets
173     my $config_tree = shift;
174     my $cb = shift;
176     my $tsetsNode = $cb->startTokensets();
178     for my $tset ( $config_tree->getTsets() )
179     {
180         &Torrus::DB::checkInterrupted();
182         my $params = $config_tree->getParams( $tset );
183         my $name = $tset;
184         $name =~ s/^S//;
185         $cb->addTokenset( $tsetsNode, $name, $params );
186     }
187     return;
191 sub collect_definitions
193     my $config_tree = shift;
194     my $cb = shift;
196     my $definitionsNode = $cb->startDefinitions();
198     for my $defName ( sort $config_tree->getDefinitionNames() )
199     {
200         &Torrus::DB::checkInterrupted();
202         my $value = $config_tree->getDefinition( $defName );
203         $cb->addDefinition( $definitionsNode, $defName, $value );
204     }
206     my $propsNode = $cb->startParamProps();
207     my $props = $config_tree->getParamProperties();
209     &Torrus::DB::checkInterrupted();
211     for my $prop ( sort keys %{$props} )
212     {
213         for my $param ( sort keys %{$props->{$prop}} )
214         {
215             $cb->addParamProp( $propsNode, $param, $prop,
216                                $props->{$prop}{$param} );
217         }
218     }
219     return;
225 sub collect_datasources
227     my $config_tree = shift;
228     my $cb = 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) )
237     {
238         $filterTokens{$topToken} = apply_filter( $config_tree, $topToken );
239     }
241     collect_subtrees( $config_tree, $cb, $topToken, $topNode );
242     return;
247 sub apply_filter
249     my $config_tree = shift;
250     my $token = shift;
252     $filterTokens{$token} = 0;
254     for my $ctoken ( $config_tree->getChildren( $token ) )
255     {
256         &Torrus::DB::checkInterrupted();
258         if( $config_tree->isSubtree( $ctoken ) )
259         {
260             $filterTokens{$token} += apply_filter( $config_tree, $ctoken );
261         }
262         elsif( $config_tree->isLeaf( $ctoken ) )
263         {
264             my $val = $config_tree->getNodeParam( $ctoken, $filter_param );
265             if( defined($val) and &{$filter_match}($val) )
266             {
267                 $filterTokens{$ctoken} = 1;
268                 $filterTokens{$token}++;
269             }
270         }
271     }
273     return $filterTokens{$token};
278 sub collect_subtrees
280     my $config_tree = shift;
281     my $cb = shift;
282     my $token = shift;
283     my $parentNode = shift;
285     for my $ctoken ( $config_tree->getChildren( $token ) )
286     {
287         &Torrus::DB::checkInterrupted();
289         if( not defined($filter_param) or $filterTokens{$ctoken} )
290         {
291             my $childName =
292                 $config_tree->nodeName( $config_tree->path($ctoken) );
293             my $params = prepare_params( $config_tree, $ctoken );
295             if( $config_tree->isSubtree( $ctoken ) )
296             {
297                 my $subtreeNode =
298                     $cb->addSubtree( $parentNode, $childName, $params );
299                 collect_subtrees( $config_tree, $cb, $ctoken, $subtreeNode );
300             }
301             elsif( $config_tree->isLeaf( $ctoken ) )
302             {
303                 $cb->addLeaf( $parentNode, $childName, $params );
304             }
306             for my $aliasToken ( $config_tree->getAliases( $ctoken ) )
307             {
308                 $cb->addAlias( $parentNode,
309                                $config_tree->path( $aliasToken ) );
310             }
311         }
312     }
313     return;
317 sub prepare_params
319     my $config_tree = shift;
320     my $token = shift;
322     my $params = $config_tree->getParams( $token, 1 );
324     # Remove linebreaks
325     while( my( $param, $value ) = each %{$params} )
326     {
327         $value =~ s/\s+/ /gm;
328         $params->{$param} = $value;
329     }
331     return $params;
334 # Local Variables:
335 # mode: perl
336 # indent-tabs-mode: nil
337 # perl-indent-level: 4
338 # End: