This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / torrus / bin / genreport.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: genreport.in,v 1.1 2010-12-27 00:04:01 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20
21 # Collect the router information and create the XML file
22
23 BEGIN { require '@torrus_config_pl@'; }
24
25 use strict;
26 use Getopt::Long;
27
28 use Torrus::Log;
29 use Torrus::ReportOutput::HTML;
30 use Torrus::SiteConfig;
31
32 my $report;
33 my $date;
34 my $time = '00:00';
35 my $genhtml;
36 my @trees;
37 my $all2tree;
38
39 my $debug = 0;
40 my $verbose = 0;
41
42 my $ok = GetOptions(
43                     'report=s'    => \$report,
44                     'date=s'      => \$date,
45                     'time=s'      => \$time,
46                     'genhtml'     => \$genhtml,
47                     'tree=s'      => \@trees,
48                     'all2tree=s'  => \$all2tree,
49                     'verbose'     => \$verbose,
50                     'debug'       => \$debug
51                     );
52
53 if( $report and not defined($Torrus::ReportGenerator::modules{$report}) )
54 {
55     print STDERR "Unknown report name: ", $report, "\n\n";
56     $ok = 0;
57 }
58                                             
59 if( not $ok or (not $report and not $genhtml) or
60     ($report and not $date) or
61     ($genhtml and scalar(@trees) > 0 and $all2tree) or
62     scalar( @ARGV ) > 0 )
63 {
64     print STDERR
65         "Usage: $0 --report=ReportName --date=YYYY-MM-DD | ",
66         "--genhtml options...\n";
67     print STDERR "Options:\n",
68     " --report=ReportName Report name.\n",
69     " --date=YYYY-MM-DD   Report start date. ",
70     "For monthly reports, 1st day in a month.\n",
71     " --time=hh:mm        Report start time. Ignored for monthly reports\n",
72     " --genhtml           Generate HTML output from the database\n",
73     " --tree=TREE         Generate HTML for a given tree only\n",
74     " --all2tree=TREE     Generate reports for all service IDs and place\n",
75     "                     into the given tree (excludes the option --tree)\n",
76     " --verbose           print extra information\n",
77     " --debug             print debugging information\n",   
78     "\n",
79     "Report names supported:\n";
80
81     foreach my $rep ( sort keys %Torrus::ReportGenerator::modules )
82     {
83         print STDERR "  ", $rep, "\n";
84     }
85     print STDERR "\n";    
86     
87     exit 1;
88 }
89
90 if( $debug )
91 {
92     Torrus::Log::setLevel('debug');
93 }
94 elsif( $verbose )
95 {
96     Torrus::Log::setLevel('verbose');
97 }
98
99 &Torrus::DB::setSafeSignalHandlers();
100
101 if( $report )
102 {
103     my $class = $Torrus::ReportGenerator::modules{$report};
104     eval( 'require ' . $class );
105     die( $@ ) if $@;
106     
107     my $generator = $class->new({
108         'Name' => $report,
109         'Date' => $date,
110         'Time' => $time});
111     
112     if( defined( $generator ) )
113     {
114         $generator->generate();
115     }
116     else
117     {
118         $ok = 0;
119     }
120 }
121
122 if( $genhtml )
123 {
124     if( $all2tree )
125     {
126         push( @trees, $all2tree );
127     }
128     
129     if( scalar( @trees ) == 0 )
130     {
131         @trees = Torrus::SiteConfig::listTreeNames();
132     }
133     else
134     {
135         foreach my $tree ( @trees )
136         {
137             if( not Torrus::SiteConfig::treeExists( $tree ) )
138             {
139                 Error('Tree ' . $tree . ' does not exist');
140                 $ok = 0;
141             }
142         }
143     }
144
145     if( $ok )
146     {
147         foreach my $tree ( @trees )
148         {
149             &Torrus::DB::checkInterrupted();
150             
151             Verbose('Generating HTML report for tree ' . $tree);
152
153             my $options = {'Tree' => $tree};
154
155             if( length( $all2tree ) > 0 )
156             {
157                 $options->{'All_Service_IDs'} = 1;
158             }
159             
160             my $out = new Torrus::ReportOutput::HTML( $options );
161             
162             if( $out->init() )
163             {
164                 $ok = $out->generate() ? $ok:0;
165             }
166             else
167             {
168                 $ok = 0;
169             }
170         }
171     }
172 }
173
174 exit($ok ? 0:1);
175
176
177 # Local Variables:
178 # mode: perl
179 # indent-tabs-mode: nil
180 # perl-indent-level: 4
181 # End: