]> git.sur5r.net Git - bacula/bacula/blob - gui/bweb/cgi/btime.pl
e4d77e43eed6f51fb4c2767e321cfdec83ce2dba
[bacula/bacula] / gui / bweb / cgi / btime.pl
1 #!/usr/bin/perl -w
2
3 =head1 LICENSE
4
5    Bweb - A Bacula web interface
6    Bacula® - The Network Backup Solution
7
8    Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
9
10    The main author of Bweb is Eric Bollengier.
11    The main author of Bacula is Kern Sibbald, with contributions from
12    many others, a complete list can be found in the file AUTHORS.
13
14    This program is Free Software; you can redistribute it and/or
15    modify it under the terms of version two of the GNU General Public
16    License as published by the Free Software Foundation plus additions
17    that are listed in the file LICENSE.
18
19    This program is distributed in the hope that it will be useful, but
20    WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27    02110-1301, USA.
28
29    Bacula® is a registered trademark of Kern Sibbald.
30    The licensor of Bacula is the Free Software Foundation Europe
31    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zurich,
32    Switzerland, email:ftf@fsfeurope.org.
33
34 =cut
35
36 use strict;
37 use GTime;
38 use Getopt::Long ;
39 use Bweb;
40 use Time::ParseDate qw/parsedate/;
41 use POSIX qw/strftime/;
42 use CGI;
43
44 my $conf = new Bweb::Config(config_file => $Bweb::config_file);
45 $conf->load();
46
47 my $bweb = new Bweb(info => $conf);
48
49 print CGI::header('text/html');
50 $bweb->display_begin();
51 $bweb->can_do('r_view_stat');
52
53 my $arg = $bweb->get_form(qw/qnocache qiso_begin qiso_end qusage qpools 
54                              qpoolusage qnojob jclient_groups qbypool
55                              db_client_groups qclient_groups/);
56 my ($filter1, undef) = $bweb->get_param('pool');
57
58 if (!$arg->{qiso_begin}) {
59    $arg->{qiso_begin} = strftime('\'%F %H:%M:00\'', localtime(time - 60*60*12));
60    $arg->{qiso_end} = strftime('\'%F %H:%M:00\'', localtime(time));
61 }
62 use Digest::MD5 qw(md5_hex);
63
64 my $md5_rep = md5_hex("$arg->{qiso_begin}:$arg->{qiso_end}:$arg->{qusage}:" . 
65                       "$arg->{jclient_groups}:$arg->{qpoolusage};$arg->{qnojob}") ;
66
67 if (   !$arg->{qnocache} 
68     && $arg->{qiso_begin} 
69     && -f "$conf->{fv_write_path}/$md5_rep.png") 
70 {
71     $arg->{result} = "/bweb/fv/$md5_rep.png";
72     $bweb->display($arg, 'btime.tpl');
73     
74     $bweb->display_end();
75     exit 0;
76 }
77
78 my $top = new GTime(
79                     debug => $conf->{debug},
80                     xmarge => 200,
81                     type => {
82                         spool =>  1,
83                         despool =>  2,
84                         commit => 4,
85                         waiting => 3,
86                         init => 5,
87                     }) ;
88 my $sec = $bweb->{sql}->{STARTTIME_SEC};
89 $sec =~ s/Job.StartTime/Log.Time/;
90 my $reg = $bweb->{sql}->{MATCH};
91
92
93 my %regs = (
94             end_job => ': Bacula',
95             start_job => ': Start Backup|: D.marrage du ',
96             data_despool_time => ': Despooling elapsed time|: Temps du tran',
97             attr_despool_time => ': Sending spooled attrs|: Transfert des attributs',
98             get_drive => ': Using Device',
99             start_spool => ': Spooling',
100             end_spool => ': User specified spool|: Taille du spool',
101             end_spool2 => ': Committing spooled data to|: Transfert des donn',
102     );
103
104 my %regs_en = (
105             end_job => ': Bacula',
106             start_job => ': Start Backup',
107             data_despool_time => ': Despooling elapsed time',
108             attr_despool_time => ': Sending spooled attrs',
109             get_drive => ': Using Device',
110             start_spool => ': Spooling',
111             end_spool => ': User specified spool',
112             end_spool2 => ': Committing spooled data to',
113             );
114
115
116 my %regs_fr = (
117             end_job => ': Bacula',
118             start_job => ': D.marrage du ',
119             data_despool_time => ': Temps du tran',
120             attr_despool_time => ': Transfert des attributs',
121             get_drive => ': Using Device',
122             start_spool => ': Spooling',
123             end_spool => ': Taille du spool',
124             end_spool2 => ': Transfert des donn',
125             );
126
127 my $filter = join(" OR ", 
128                   map { "Log.LogText $bweb->{sql}->{MATCH} '$_'" } values %regs);
129
130 my $query = "
131 SELECT " . $bweb->dbh_strcat('Job.Name', "'_'", 'Job.Level') . ",
132        $sec,
133        substring(Log.LogText from 8 for 70),
134        Job.JobId,
135        Pool.Name
136
137 FROM  Log INNER JOIN Job USING (JobId) JOIN Pool USING (PoolId)
138 " . ($arg->{jclient_groups}?
139 "     JOIN Client USING (ClientId) 
140       JOIN client_group_member ON (Client.ClientId = client_group_member.clientid) 
141       JOIN client_group USING (client_group_id)
142       WHERE client_group_name IN ($arg->{jclient_groups})
143         AND
144 ":'WHERE') .
145 "
146       Job.StartTime > $arg->{qiso_begin}
147   AND Job.StartTime < $arg->{qiso_end}
148   AND ( $filter )
149   AND Job.Type = 'B'
150   $filter1
151  ORDER BY " . ($arg->{qbypool}?"Pool.Name,":""). "Job.JobId,Log.LogId,Log.Time";
152
153
154 $bweb->debug($query);
155 my $all = $bweb->dbh_selectall_arrayref($query);
156
157 my $lastid = 0;
158 my $lastspool = 0;
159 my $last_name;
160 my $data = [];
161 my $write = {};
162 my $pool = {};
163 my $drive = "";
164 my $end;
165 my $begin;
166 my $last_pool='';
167 foreach my $elt (@$all)
168 {
169     # if bypool is used, we display pool usage after each job block
170     if ($arg->{qbypool} && $last_pool ne $elt->[4]) {
171         foreach my $i (sort keys %$pool) {
172             $top->add_job(label => $i,
173                           data => $pool->{$i});
174         }
175         $pool = {};
176     }
177     if ($lastid && $lastid ne $elt->[3]) {
178         if (!$arg->{qnojob}) {
179             $top->add_job(label => $last_name,
180                           data => $data);
181         }
182         $data = [];
183         $lastspool=0;
184     }
185
186     if ($elt->[2] =~ /$regs{end_job}/) {
187         push @$data, {
188             type  => "commit",
189             begin => $begin,
190             end   => $elt->[1],
191         };
192
193 #    } elsif ($elt->[2] =~ /$regs{attr_despool_time}/) {
194 #
195 #       push @$data, {
196 #           l => $elt->[2],
197 #           type  => "waiting",
198 #           begin => $begin,
199 #           end   => $elt->[1],
200 #       };
201 #
202     } elsif ($elt->[2] =~ /(?:$regs{get_drive}) "([\w\d\s-\.]+)"/) {
203         $drive = $1;
204
205     } elsif ($elt->[2] =~ /(?:$regs{data_despool_time}).*? = (\d+):(\d+):(\d+)/) {
206         # on connait le temps de despool
207         my $t = $1*60*60+ $2*60 + $3;
208
209         if ($t > 10) {          # en dessous de 10s on affiche pas
210             push @$data, {      # temps d'attente du drive
211 #               l => $elt->[2],
212                 type  => "waiting",
213                 begin => $begin,
214                 end   => parsedate($elt->[1]) - $t,
215             };
216
217             push @$data, {
218 #               l => $elt->[2],
219                 type  => "despool",
220                 begin => parsedate($elt->[1]) - $t,
221                 end   => $elt->[1],
222             };
223
224             push @{$write->{$drive}}, { # display only write time
225                 type  => "despool",
226                 begin => parsedate($elt->[1]) - $t,
227                 end   => $elt->[1],
228             };
229
230             push @{$pool->{"$drive: $elt->[4]"}}, {
231                 type  => "despool",
232                 begin => parsedate($elt->[1]) - $t,
233                 end   => $elt->[1],
234             };
235         } else {
236             push @$data, {
237 #               t => $t,
238 #               l => $elt->[2],
239                 type  => "waiting",
240                 begin => $begin,
241                 end   => $elt->[1],
242             };
243         }
244         
245     } elsif ($elt->[2] =~ /$regs{start_spool}/) {
246
247         if (!$lastspool) {
248             push @$data, {
249                 type  => "init",
250                 begin => $begin,
251                 end   => $elt->[1],
252             };  
253         }
254
255         $lastspool = 1;
256
257     } elsif ($elt->[2] =~ /($regs{end_spool}|$regs{end_spool2})/) {
258         push @$data, {
259 #           l => $elt->[2],
260             type  => "spool",
261             begin => $begin,
262             end   => $elt->[1],
263         };
264
265     } elsif ($elt->[2] =~ /$regs{start_job}/) {
266         1;
267     } else {
268         next;
269     }
270
271     $end = $begin;
272     $begin = $elt->[1];
273     $last_name = $elt->[0];
274     $lastid = $elt->[3];
275     $last_pool = $elt->[4];
276 }
277
278 if (!$arg->{qnojob} && $last_name) {
279     $top->add_job(label => $last_name,
280                   data => $data);
281 }
282 if ($arg->{qpoolusage} or $arg->{qbypool}) {
283     foreach my $d (sort keys %$pool) {
284         $top->add_job(label => $d,
285                       data => $pool->{$d});
286     }
287 }
288
289 if ($arg->{qusage}) {
290     foreach my $d (sort keys %$write) {
291         $top->add_job(label => "drive $d",
292                       data => $write->{$d});
293     }
294 }
295
296 $bweb->debug($top);
297 $top->finalize();
298
299 open(FP, ">$conf->{fv_write_path}/$md5_rep.png");
300 binmode FP;
301 # Convert the image to PNG and print it on standard output
302 print FP $GTime::gd->png;
303 close(FP);
304
305 $arg->{result} = "/bweb/fv/$md5_rep.png";
306 $bweb->display( $arg, 'btime.tpl');
307 $bweb->display_end();