]> git.sur5r.net Git - bacula/bacula/blob - gui/bweb/cgi/btime.pl
ebl Add new btime module
[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 John Walker.
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 CGI;
42
43 my $conf = new Bweb::Config(config_file => $Bweb::config_file);
44 $conf->load();
45
46 my $bweb = new Bweb(info => $conf);
47 my $arg = $bweb->get_form(qw/qiso_begin qiso_end qusage qpool qnojob
48                              jclient_groups db_client_groups qclient_groups/);
49
50 use Digest::MD5 qw(md5_hex);
51 my $md5_rep = md5_hex("$arg->{qiso_begin}:$arg->{qiso_end}:$arg->{qusage}:" . 
52                       "$arg->{jclient_groups}:$arg->{qpool};$arg->{qnojob}") ;
53
54 print CGI::header('text/html');
55 $bweb->display_begin();
56
57 if ($arg->{qiso_begin} && -f "$conf->{fv_write_path}/$md5_rep.png") {
58     $arg->{result} = "/bweb/fv/$md5_rep.png";
59     $bweb->display($arg, 'btime.tpl');
60     
61     $bweb->display_end();
62     exit 0;
63 }
64
65 my $top = new GTime(
66                     debug => $conf->{debug},
67                     xmarge => 200,
68                     type => {
69                         spool =>  1,
70                         despool =>  2,
71                         commit => 4,
72                         waiting => 3,
73                         init => 5,
74                     }) ;
75 my $sec = $bweb->{sql}->{STARTTIME_SEC};
76 $sec =~ s/Job.StartTime/Log.Time/;
77 my $reg = $bweb->{sql}->{MATCH};
78
79
80 my %regs1 = (
81             end_job => ': Bacula [1-9]',
82             start_job => ': Start Backup',
83             data_despool_time => ': Despooling elapsed time',
84             attr_despool_time => ': Sending spooled attrs',
85             get_drive => ': Using Device',
86             start_spool => ': Spooling',
87             end_spool => ': User specified spool',
88             );
89
90
91 my %regs = (
92             end_job => ': Bacula [1-9]',
93             start_job => ': D.marrage du ',
94             data_despool_time => ': Temps du tran',
95             attr_despool_time => ': Transfert des',
96             get_drive => ': Using Device',
97             start_spool => ': Spooling',
98             end_spool => ': Taille du spool',
99             );
100
101 my $filter = join(" OR ", 
102                   map { "Log.LogText $bweb->{sql}->{MATCH} '$_'" } values %regs);
103
104 my $query = "
105 SELECT " . $bweb->dbh_strcat('Job.Name', "'_'", 'Job.Level') . ",
106        $sec,
107        substring(Log.LogText from 1 for 65),
108        Job.JobId,
109        Pool.Name
110
111 FROM  Log INNER JOIN Job USING (JobId) JOIN Pool USING (PoolId)
112 " . ($arg->{jclient_groups}?
113 "     JOIN Client USING (ClientId) 
114       JOIN client_group_member ON (Client.ClientId = client_group_member.clientid) 
115       JOIN client_group USING (client_group_id)
116       WHERE client_group_name IN ($arg->{jclient_groups})
117         AND
118 ":'WHERE') .
119 "
120       Job.StartTime > $arg->{qiso_begin}
121   AND Job.StartTime < $arg->{qiso_end}
122   AND ( $filter )
123   AND Job.Type = 'B'
124 ORDER BY Job.JobId,Log.Time  
125 ";
126
127
128 print STDERR $query if ($conf->{debug})
129 my $all = $bweb->dbh_selectall_arrayref($query);
130
131 my $lastid = 0;
132 my $lastspool = 0;
133 my $last_name;
134 my $data = [];
135 my $write = {};
136 my $pool = {};
137 my $drive = "";
138 my $end;
139 my $begin;
140 foreach my $elt (@$all)
141 {
142     if ($lastid && $lastid ne $elt->[3]) {
143         if (!$arg->{qnojob}) {
144             $top->add_job(label => $last_name,
145                           data => $data);
146         }
147         $data = [];
148         $lastspool=0;
149     }
150
151     if ($elt->[2] =~ /$regs{start_job}/) {
152         push @$data, {
153             type  => "commit",
154             begin => $begin,
155             end   => $elt->[1],
156         };
157
158     } elsif ($elt->[2] =~ /$regs{attr_despool_time}/) {
159
160         push @$data, {
161             type  => "waiting",
162             begin => $begin,
163             end   => $elt->[1],
164         };
165
166     } elsif ($elt->[2] =~ /$regs{get_drive} "([\w\d]+)"/) {
167         $drive = $1;
168
169     } elsif ($elt->[2] =~ /$regs{data_despool_time}.+? = (\d+):(\d+):(\d+)/) {
170         # on connait le temps de despool
171         my $t = $1*60*60+ $2*60 + $3;
172
173         if ($t > 10) {          # en dessous de 10s on affiche pas
174             push @$data, {      # temps d'attente du drive
175                 type  => "waiting",
176                 begin => $begin,
177                 end   => parsedate($elt->[1]) - $t,
178             };
179
180             push @$data, {
181                 type  => "despool",
182                 begin => parsedate($elt->[1]) - $t,
183                 end   => $elt->[1],
184             };
185
186             push @{$write->{$drive}}, { # display only write time
187                 type  => "despool",
188                 begin => parsedate($elt->[1]) - $t,
189                 end   => $elt->[1],
190             };
191
192             push @{$pool->{"$drive: $elt->[4]"}}, {
193                 type  => "despool",
194                 begin => parsedate($elt->[1]) - $t,
195                 end   => $elt->[1],
196             };
197         } else {
198             push @$data, {
199                 type  => "waiting",
200                 begin => $begin,
201                 end   => $elt->[1],
202             };
203         }
204         
205     } elsif ($elt->[2] =~ /$regs{start_spool}/) {
206
207         if (!$lastspool) {
208             push @$data, {
209                 type  => "init",
210                 begin => $begin,
211                 end   => $elt->[1],
212             };  
213         }
214
215         $lastspool = 1;
216
217     } elsif ($elt->[2] =~ /($regs{end_spool}|$regs{attr_despool_time})/) {
218         push @$data, {
219             type  => "spool",
220             begin => $begin,
221             end   => $elt->[1],
222         };
223
224     } elsif ($elt->[2] =~ /$regs{end_job}/) {
225         1;
226     } else {
227         next;
228     }
229
230     $end = $begin;
231     $begin = $elt->[1];
232     $last_name = $elt->[0];
233     $lastid = $elt->[3];
234 }
235
236 if (!$arg->{qnojob} && $last_name) {
237     $top->add_job(label => $last_name,
238                   data => $data);
239 }
240 if ($arg->{qpool}) {
241     foreach my $d (sort keys %$pool) {
242         $top->add_job(label => $d,
243                       data => $pool->{$d});
244     }
245 }
246
247 if ($arg->{qusage}) {
248     foreach my $d (sort keys %$write) {
249         $top->add_job(label => "drive $d",
250                       data => $write->{$d});
251     }
252 }
253
254
255 $top->finalize();
256
257 open(FP, ">$conf->{fv_write_path}/$md5_rep.png");
258 binmode FP;
259 # Convert the image to PNG and print it on standard output
260 print FP $GTime::gd->png;
261 close(FP);
262
263 $arg->{result} = "/bweb/fv/$md5_rep.png";
264 $bweb->display( $arg, 'btime.tpl');
265 $bweb->display_end();