This commit was generated by cvs2svn to compensate for changes in r10640,
[freeside.git] / torrus / bin / genlist.in
1 #!@PERL@
2 #  Copyright (C) 2002  Stanislav Sinyagin
3 #
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.
8 #
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.
13 #
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.
17
18 # $Id: genlist.in,v 1.1 2010-12-27 00:04:01 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20
21 BEGIN { require '@torrus_config_pl@'; }
22
23 use strict;
24 use Getopt::Long;
25
26 use Torrus::Log;
27 use Torrus::ConfigTree;
28 use Torrus::SiteConfig;
29
30 exit(1) if not Torrus::SiteConfig::verify();
31
32 my $tree;
33 my $initPath = '/';
34 my $listWhat = 'rrdfiles';
35 my $selectType = 'all';
36
37 my $help_needed;
38
39 my %listingsSupported =
40     (
41      'rrdfiles' => {
42          'collector'  => 1,
43          'readonly'   => 1,
44          'all'        => 1,
45      },
46      
47      'snmphosts' => {
48          'collector'  => 1,
49      },
50      );
51
52 my $ok = GetOptions ('tree=s'   => \$tree,
53                      'path=s'   => \$initPath,
54                      'what=s'   => \$listWhat,
55                      'type=s'   => \$selectType,
56                      'help'     => \$help_needed);
57
58 if( not $ok or not $tree or $help_needed or
59     not $listingsSupported{$listWhat}{$selectType} or scalar(@ARGV) > 0 )
60 {
61     print STDERR "Usage: $0 --tree=NAME [options...]\n",
62     "Options:\n",
63     "  --tree=NAME     tree name\n",
64     "  --path=/PATH    [".$initPath."] subtree name\n",
65     "  --what=WHAT     [".$listWhat."] what to list\n",
66     "    Supported listings:\n",
67     "      rrdfiles      List RRD file paths\n",
68     "      snmphosts     List SNMP hosts\n",
69     "  --type=TYPE     [".$selectType."] selection type\n",
70     "    Supported types:\n",
71     "      collector     Collector leaves\n",
72     "      readonly      Read-only leaves\n",
73     "      all           All of above\n",
74     "  --help          this help message\n";
75     exit 1;
76 }
77
78
79 if( not Torrus::SiteConfig::treeExists( $tree ) )
80 {
81     Error('Tree ' . $tree . ' does not exist');
82     exit 1;
83 }
84
85 &Torrus::DB::setSafeSignalHandlers();
86
87 my $config_tree = new Torrus::ConfigTree( -TreeName => $tree, -Wait => 1 );
88 if( not defined( $config_tree ) )
89 {
90     exit 1;
91 }
92
93 my $initToken = $config_tree->token( $initPath );
94 if( not defined( $initToken ) )
95 {
96     Error('No such subtree: ' . $initPath);
97     exit 1;
98 }
99
100 my $listing = {};
101
102 my $listParams = {};
103 if( $selectType eq 'all' )
104 {
105     foreach my $type ( keys %{$listingsSupported{$listWhat}} )
106     {
107         if( $type ne 'all' )
108         {
109             $listParams->{$type} = 1;
110         }
111     }
112 }
113 else
114 {
115     $listParams->{$selectType} = 1;
116 }
117
118 pickup_data( $config_tree, $initToken, $listing, $listWhat, $listParams );
119
120 foreach my $item ( sort keys %{$listing} )
121 {
122     print $item, "\n";
123 }
124
125 exit 0;
126
127 sub pickup_data
128 {
129     my $config_tree = shift;
130     my $token = shift;
131     my $listing = shift;
132     my $listWhat = shift;
133     my $listParams = shift;
134
135     foreach my $ctoken ( $config_tree->getChildren( $token ) )
136     {
137         &Torrus::DB::checkInterrupted();
138         
139         if( $config_tree->isSubtree( $ctoken ) )
140         {
141             pickup_data( $config_tree, $ctoken,
142                          $listing, $listWhat, $listParams );
143         }
144         elsif( $config_tree->isLeaf( $ctoken ) )
145         {
146             if( $listWhat eq 'rrdfiles' and
147                 (
148                  (
149                   $listParams->{'collector'} and
150                   $config_tree->getNodeParam( $ctoken, 'ds-type' ) eq
151                   'collector' and
152                   $config_tree->getNodeParam( $ctoken, 'storage-type' ) eq
153                   'rrd'
154                   ) or
155                  (
156                   $listParams->{'readonly'} and
157                   $config_tree->getNodeParam( $ctoken, 'ds-type' ) eq
158                   'rrd-file' and
159                   $config_tree->getNodeParam( $ctoken, 'leaf-type' ) eq
160                   'rrd-def'
161                   )
162                  )
163                 )
164             {
165                 my $datafile =
166                     $config_tree->getNodeParam( $ctoken, 'data-file' );
167                 my $datadir =
168                     $config_tree->getNodeParam( $ctoken, 'data-dir' );
169                 $listing->{$datadir . '/' . $datafile} = 1;
170             }
171             elsif( $listWhat eq 'snmphosts' and
172                    $listParams->{'collector'} and
173                    $config_tree->getNodeParam( $ctoken, 'ds-type' ) eq
174                    'collector' )
175             {
176                 my $host =
177                     $config_tree->getNodeParam( $ctoken, 'snmp-host' );
178                 my $oid =
179                     $config_tree->getNodeParam( $ctoken, 'snmp-object' );
180                 
181                 if( defined( $host ) and length( $host ) > 0 and
182                     defined( $oid ) and length( $oid ) > 0 )
183                 {
184                     $listing->{$host} = 1;
185                 }                
186             }
187         }
188     }
189 }
190
191
192
193 # Local Variables:
194 # mode: perl
195 # indent-tabs-mode: nil
196 # perl-indent-level: 4
197 # End: