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