summaryrefslogtreecommitdiff
path: root/torrus/bin/configsnapshot.in
blob: dc79e5bdb7c1aa071dc0d66e18deffb89532e78e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!@PERL@
#  Copyright (C) 2002  Stanislav Sinyagin
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

# $Id: configsnapshot.in,v 1.1 2010-12-27 00:04:01 ivan Exp $
# Stanislav Sinyagin <ssinyagin@yahoo.com>

BEGIN { require '@torrus_config_pl@'; }

use strict;
use Getopt::Long;

use Torrus::Log;
use Torrus::ConfigTree;
use Torrus::SiteConfig;
use Torrus::ConfigBuilder;

exit(1) if not Torrus::SiteConfig::verify();

my $tree;
my $help_needed;
my $verbose = 0;

my $outfile = 'snapshot.xml';

my $filter_param;
my $filter_value;
my $filter_op = '=';

my $creator = "Torrus version @VERSION@\n" .
    "This file was generated by command:\n" .
    $0 . " \\\n";
foreach my $arg ( @ARGV )
{
    if( $arg =~ /^--/ )
    {
        $creator .= ' ' . $arg . ' ';
    }
    else
    {
        $creator .= "\'" . $arg . "\'\\\n";
    }
}
$creator .= "\nOn " . scalar(localtime(time));

my $ok = GetOptions('tree=s'   => \$tree,
                    'out=s'    => \$outfile,
                    'param=s'  => \$filter_param,
                    'value=s'  => \$filter_value,
                    'op=s'     => \$filter_op,
                    'verbose'  => \$verbose,
                    'help'     => \$help_needed);

if( not $ok or not $tree or $help_needed or
    ( defined($filter_param) + defined($filter_value) == 1 ) or
    ( $filter_op ne '=' and $filter_op ne 'eq' and $filter_op ne 're' ) or
    scalar(@ARGV) > 0 )
{
    print STDERR "Usage: $0 --tree=NAME [options...]\n",
    "Options:\n",
    "  --tree=NAME     tree name\n",
    "  --out=filename  output file [".$outfile."]\n",
    "  --param=PARAM --value=VALUE \n",
    "                  filter the output by leaves with specified value\n",
    "  --op=OP         filter operation [=|eq|re], default: [=]\n",
    "  --verbose       print extra information\n",
    "  --help          this help message\n";
    exit 1;
}

if( $verbose )
{
    Torrus::Log::setLevel('verbose');
}

if( not Torrus::SiteConfig::treeExists( $tree ) )
{
    Error('Tree ' . $tree . ' does not exist');
    exit 1;
}

&Torrus::DB::setSafeSignalHandlers();

my $config_tree = new Torrus::ConfigTree( -TreeName => $tree, -Wait => 1 );
if( not defined( $config_tree ) )
{
    exit 1;
}


my $filter_match = sub {return $_[0] == $filter_value};

if(defined($filter_param))
{
    if( $filter_op eq 'eq' )
    {
        $filter_match = sub {return $_[0] eq $filter_value};
    }
    elsif( $filter_op eq 're' )
    {
        $filter_match = sub {return $_[0] =~ $filter_value};
    }
}


        
my $cb = new Torrus::ConfigBuilder;

$cb->addCreatorInfo( $creator );

# We don't collect views, since they are in defaults.xml which is always
# included

collect_monitors( $config_tree, $cb );
collect_tokensets( $config_tree, $cb );
collect_definitions( $config_tree, $cb );
collect_datasources( $config_tree, $cb );

my $ok = $cb->toFile( $outfile );
if( $ok )
{
    Verbose('Wrote ' . $outfile);
}
else
{
    Error('Cannot write ' . $outfile . ': ' . $!);
}

exit($ok ? 0:1);

sub collect_monitors
{
    my $config_tree = shift;
    my $cb = shift;

    my $monitorsNode = $cb->startMonitors();

    foreach my $action ( $config_tree->getActionNames() )
    {
        &Torrus::DB::checkInterrupted();
        
        my $params = $config_tree->getParams( $action );
        $cb->addMonitorAction( $monitorsNode, $action, $params );
    }

    foreach my $monitor ( $config_tree->getMonitorNames() )
    {
        &Torrus::DB::checkInterrupted();
        
        my $params = $config_tree->getParams( $monitor );
        $cb->addMonitor( $monitorsNode, $monitor, $params );
    }
}

sub collect_tokensets
{
    my $config_tree = shift;
    my $cb = shift;

    my $tsetsNode = $cb->startTokensets();

    foreach my $tset ( $config_tree->getTsets() )
    {
        &Torrus::DB::checkInterrupted();
        
        my $params = $config_tree->getParams( $tset );
        my $name = $tset;
        $name =~ s/^S//;
        $cb->addTokenset( $tsetsNode, $name, $params );
    }
}


sub collect_definitions
{
    my $config_tree = shift;
    my $cb = shift;

    my $definitionsNode = $cb->startDefinitions();

    foreach my $defName ( sort $config_tree->getDefinitionNames() )
    {
        &Torrus::DB::checkInterrupted();

        my $value = $config_tree->getDefinition( $defName );
        $cb->addDefinition( $definitionsNode, $defName, $value );
    }

    my $propsNode = $cb->startParamProps();
    my $props = $config_tree->getParamProperties();

    &Torrus::DB::checkInterrupted();

    foreach my $prop ( sort keys %{$props} )
    {
        foreach my $param ( sort keys %{$props->{$prop}} )
        {
            $cb->addParamProp( $propsNode, $param, $prop,
                               $props->{$prop}{$param} );
        }
    }
}


my %filterTokens;


sub collect_datasources
{
    my $config_tree = shift;
    my $cb = shift;
    
    my $topNode = $cb->getTopSubtree();
    my $topToken = $config_tree->token('/');
    
    my $params = prepare_params( $config_tree, $topToken );
    $cb->addParams( $topNode, $params );

    if( defined($filter_param) )
    {
        $filterTokens{$topToken} = apply_filter( $config_tree, $topToken );
    }
    
    collect_subtrees( $config_tree, $cb, $topToken, $topNode );
}
        


sub apply_filter
{
    my $config_tree = shift;
    my $token = shift;

    $filterTokens{$token} = 0;
    
    foreach my $ctoken ( $config_tree->getChildren( $token ) )
    {
        &Torrus::DB::checkInterrupted();
        
        if( $config_tree->isSubtree( $ctoken ) )
        {
            $filterTokens{$token} += apply_filter( $config_tree, $ctoken );
        }
        elsif( $config_tree->isLeaf( $ctoken ) )
        {
            my $val = $config_tree->getNodeParam( $ctoken, $filter_param );
            if( defined($val) and &{$filter_match}($val) )
            {
                $filterTokens{$ctoken} = 1;
                $filterTokens{$token}++;
            }                
        }
    }

    return $filterTokens{$token};
}
        


sub collect_subtrees
{
    my $config_tree = shift;
    my $cb = shift;
    my $token = shift;
    my $parentNode = shift;
    
    foreach my $ctoken ( $config_tree->getChildren( $token ) )
    {
        &Torrus::DB::checkInterrupted();
        
        if( not defined($filter_param) or $filterTokens{$ctoken} )
        {
            my $childName =
                $config_tree->nodeName( $config_tree->path($ctoken) );
            my $params = prepare_params( $config_tree, $ctoken );
        
            if( $config_tree->isSubtree( $ctoken ) )
            {
                my $subtreeNode =
                    $cb->addSubtree( $parentNode, $childName, $params );
                collect_subtrees( $config_tree, $cb, $ctoken, $subtreeNode );
            }
            elsif( $config_tree->isLeaf( $ctoken ) )
            {
                $cb->addLeaf( $parentNode, $childName, $params );
            }
            
            foreach my $aliasToken ( $config_tree->getAliases( $ctoken ) )
            {
                $cb->addAlias( $parentNode,
                               $config_tree->path( $aliasToken ) );
            }
        }
    }
}


sub prepare_params
{
    my $config_tree = shift;
    my $token = shift;
    
    my $params = $config_tree->getParams( $token, 1 );

    # Remove linebreaks
    while( my( $param, $value ) = each %{$params} )
    {
        $value =~ s/\s+/ /gm;
        $params->{$param} = $value;
    }

    return $params;
}

# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# perl-indent-level: 4
# End: