import torrus 1.0.9
[freeside.git] / torrus / perllib / Torrus / Renderer / AdmInfo.pm
1 #  Copyright (C) 2002  Stanislav Sinyagin
2 #
3 #  This program is free software; you can redistribute it and/or modify
4 #  it under the terms of the GNU General Public License as published by
5 #  the Free Software Foundation; either version 2 of the License, or
6 #  (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software
15 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16
17 # $Id: AdmInfo.pm,v 1.1 2010-12-27 00:03:44 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
19
20 package Torrus::Renderer::AdmInfo;
21
22 use strict;
23
24 use Torrus::ConfigTree;
25 use Torrus::Log;
26 use Torrus::ACL;
27
28 use Template;
29
30 my %rrd_params =
31     (
32      'leaf-type' => {'rrd-def' => {'rrd-ds' => undef,
33                                    'rrd-cf' => undef,
34                                    'data-file' => undef,
35                                    'data-dir'  => undef},
36                      'rrd-cdef' => {'rpn-expr' => undef}},
37      );
38
39 my %rrdmulti_params = ( 'ds-names' => undef );
40
41 my %collector_params =
42     (
43      'storage-type'   => {'rrd' => {
44          'data-file'              => undef,
45          'data-dir'               => undef,
46          'leaf-type'              => {
47              'rrd-def'  => {'rrd-ds' => undef,
48                             'rrd-cf' => undef,
49                             'rrd-create-dstype' => undef,
50                             'rrd-create-rra'         => undef,
51                             'rrd-create-heartbeat'   => undef,
52                             'rrd-hwpredict'         => {
53                                 'enabled' => {'rrd-create-hw-rralen' => undef},
54                                 'disabled' => undef
55                                 }}}}},
56      'collector-type'        => undef,
57      'collector-period'      => undef,
58      'collector-timeoffset'  => undef,
59      'collector-instance'    => undef,
60      'collector-instance-hashstring' => undef,
61      'collector-scale'      => undef,
62      'collector-dispersed-timeoffset' => {
63          'no' => undef,
64          'yes' => {'collector-timeoffset-min' => undef,
65                    'collector-timeoffset-max' => undef,
66                    'collector-timeoffset-step' => undef,
67                    'collector-timeoffset-hashstring' => undef}}
68      );
69
70
71 my %leaf_params =
72     ('ds-type' => {'rrd-file' => \%rrd_params,
73                    'rrd-multigraph' => \%rrdmulti_params,
74                    'collector' => \%collector_params},
75      'rrgraph-views'             => undef,
76      'rrd-scaling-base'         => undef,
77      'graph-logarithmic'        => undef,
78      'graph-rigid-boundaries'   => undef,
79      'graph-ignore-decorations' => undef,
80      'nodeid'                   => undef);
81
82
83 my %param_categories =
84     (
85      'collector-dispersed-timeoffset'    => 'Collector',
86      'collector-period'                  => 'Collector',
87      'collector-scale'                   => 'Collector',
88      'collector-timeoffset'              => 'Collector',
89      'collector-timeoffset-hashstring'   => 'Collector',
90      'collector-timeoffset-max'          => 'Collector',
91      'collector-timeoffset-min'          => 'Collector',
92      'collector-timeoffset-step'         => 'Collector',
93      'collector-type'                    => 'Collector',
94      'collector-instance'                => 'Collector',
95      'collector-instance-hashstring'     => 'Collector',
96      'data-dir'                          => 'Storage',
97      'data-file'                         => 'Storage',
98      'ds-names'                          => 'Multigraph',
99      'ds-type'                           => 'Common Parameters',
100      'graph-ignore-decorations'          => 'Display',
101      'graph-logarithmic'                 => 'Display',
102      'graph-rigid-boundaries'            => 'Display',
103      'leaf-type'                         => 'Common Parameters',
104      'nodeid'                            => 'Common Parameters',
105      'rpn-expr'                          => 'RRD CDEF Paramters',
106      'rrd-cf'                            => 'RRD',
107      'rrd-create-dstype'                 => 'RRD',
108      'rrd-create-heartbeat'              => 'RRD',
109      'rrd-create-hw-rralen'              => 'RRD',
110      'rrd-create-rra'                    => 'RRD',
111      'rrd-ds'                            => 'RRD',
112      'rrd-hwpredict'                     => 'RRD',
113      'rrd-scaling-base'                  => 'RRD',
114      'rrgraph-views'                     => 'Display',
115      'storage-type'                      => 'Storage'
116      );
117      
118
119 # Load additional validation, configurable from
120 # torrus-config.pl and torrus-siteconfig.pl
121
122 foreach my $mod ( @Torrus::Renderer::loadAdmInfo )
123 {
124     eval( 'require ' . $mod );
125     die( $@ ) if $@;
126     eval( '&' . $mod . '::initAdmInfo( \%leaf_params, \%param_categories )' );
127     die( $@ ) if $@;
128 }
129
130
131 # All our methods are imported by Torrus::Renderer;
132
133 sub render_adminfo
134 {
135     my $self = shift;
136     my $config_tree = shift;
137     my $token = shift;
138     my $view = shift;
139     my $outfile = shift;
140
141     if( $self->may_display_adminfo( $config_tree, $token ) )
142     {
143         $self->{'adminfo'} = $self->retrieve_adminfo( $config_tree, $token );
144         my @ret = $self->render_html( $config_tree, $token, $view, $outfile );
145         delete $self->{'adminfo'};
146         return @ret;
147     }
148     else
149     {
150         if( not open(OUT, ">$outfile") )
151         {
152             Error("Cannot open $outfile for writing: $!");
153             return undef;
154         }
155         else
156         {
157             print OUT "Cannot display admin information\n";
158             close OUT;
159         }
160
161         return (300+time(), 'text/plain');
162     }
163 }
164
165
166 sub may_display_adminfo
167 {
168     my $self = shift;
169     my $config_tree = shift;
170     my $token = shift;
171
172     if( $config_tree->isLeaf( $token ) )
173     {
174         # hasPrivilege is imported from Torrus::Renderer::HTML
175         if( $self->hasPrivilege( $config_tree->treeName(),
176                                  'DisplayAdmInfo' ) )
177         {
178             return 1;
179         }
180     }
181     
182     return 0;
183 }
184
185
186 sub retrieve_adminfo
187 {
188     my $self = shift;
189     my $config_tree = shift;
190     my $token = shift;
191
192     my $ret = {};
193     my @namemaps = ( \%leaf_params );
194
195     while( scalar( @namemaps ) > 0 )
196     {
197         my @next_namemaps = ();
198
199         foreach my $namemap ( @namemaps )
200         {
201             foreach my $paramkey ( keys %{$namemap} )
202             {
203                 my $pname = $paramkey;
204
205                 my $pval = $config_tree->getNodeParam( $token, $pname );
206                 if( defined( $pval ) )
207                 {
208                     if( ref( $namemap->{$paramkey} ) )
209                     {
210                         if( exists $namemap->{$paramkey}->{$pval} )
211                         {
212                             if( defined $namemap->{$paramkey}->{$pval} )
213                             {
214                                 push( @next_namemaps,
215                                       $namemap->{$paramkey}->{$pval} );
216                             }
217                         }
218                     }
219
220                     my $category = $param_categories{$pname};
221                     if( not defined( $category ) )
222                     {
223                         $category = 'Other';
224                     }                    
225                     $ret->{$category}{$pname} = $pval;
226                 }
227             }
228         }
229         @namemaps = @next_namemaps;
230     }
231
232     return $ret;
233 }
234
235 1;
236
237
238 # Local Variables:
239 # mode: perl
240 # indent-tabs-mode: nil
241 # perl-indent-level: 4
242 # End: