]> git.sur5r.net Git - bacula/bacula/blob - gui/bweb/script/bcheck.pl
bweb: update location title
[bacula/bacula] / gui / bweb / script / bcheck.pl
1 #!/usr/bin/perl -w
2
3 =head1 DESCRIPTION
4
5
6 =head2 USAGE
7
8     bcheck.pl [client=yes]
9
10 =head1 LICENSE
11
12    Bweb - A Bacula web interface
13    Bacula® - The Network Backup Solution
14
15    Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
16
17    The main author of Bweb is Eric Bollengier.
18    The main author of Bacula is Kern Sibbald, with contributions from
19    many others, a complete list can be found in the file AUTHORS.
20    This program is Free Software; you can redistribute it and/or
21    modify it under the terms of version three of the GNU Affero General Public
22    License as published by the Free Software Foundation and included
23    in the file LICENSE.
24
25    This program is distributed in the hope that it will be useful, but
26    WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28    Affero General Public License for more details.
29
30    You should have received a copy of the GNU Affero General Public License
31    along with this program; if not, write to the Free Software
32    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33    02110-1301, USA.
34
35    Bacula® is a registered trademark of Kern Sibbald.
36    The licensor of Bacula is the Free Software Foundation Europe
37    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
38    Switzerland, email:ftf@fsfeurope.org.
39
40 =cut
41
42 use strict ;
43 use Getopt::Long ;
44 use Data::Dumper ;
45 use Bweb;
46 use CGI;
47 my $verbose;
48
49 my $conf = new Bweb::Config(config_file => $Bweb::config_file);
50 $conf->load();
51 my $bweb = new Bweb(info => $conf);
52
53 my $b = $bweb->get_bconsole();
54 my $ret = $b->director_get_sched(1);
55
56 if (CGI::param('client')) {
57
58     print "
59 Check clients
60 -------------\n";
61
62 # check for next backup that client are online
63     foreach my $elt (@$ret) {
64         next unless ($elt->{name});
65         
66         my $job = $b->send_cmd("show job=\"$elt->{name}\"");
67         my $obj = $bweb->run_parse_job($job);
68         
69 #    print "I: test $obj->{client}\n" if $verbose;
70         next unless ($obj->{client});
71         my $out = $b->send_cmd("st client=\"$obj->{client}\"");
72         if ($out !~ /Daemon started/m or $out !~ /^ Heap:/m) {
73             print "E: Can't connect to $obj->{client}\n";
74         }
75         exit 0;
76     }
77 }
78
79 print "
80 Check for backup size
81 ---------------------\n";
82
83 my $arg = $bweb->get_form('age', 'since');
84 my ($limit, $label) = $bweb->get_limit(age => $arg->{age},
85                                        since => $arg->{since});
86 my $query = "
87 SELECT Job.ClientId AS c0, Job.Name AS c1, 
88        Client.Name AS c2
89   FROM Job JOIN Client USING (ClientId)
90  WHERE Type = 'B'
91    AND JobStatus = 'T'
92    AND Level = 'F'
93    $limit
94  GROUP BY Job.Name,Job.ClientId,Client.Name
95 ";
96
97 my $old = $bweb->dbh_selectall_arrayref($query);
98
99 foreach my $elt (@$old) {
100     $elt->[1] = $bweb->dbh_quote($elt->[1]);
101     $query = "
102 SELECT JobId AS jobid, JobBytes AS jobbytes
103   FROM Job
104  WHERE Type = 'B'
105    AND JobStatus = 'T'
106    AND Level = 'F'
107    AND Job.ClientId = $elt->[0]
108    AND Job.Name = $elt->[1]
109    $limit
110  ORDER BY StartTime DESC
111  LIMIT 1
112 ";
113
114     my $last_bkp = $bweb->dbh_selectrow_hashref($query);
115
116     $query = "
117 SELECT COUNT(1) as nbjobs, AVG(JobBytes) as nbbytes
118   FROM 
119     (SELECT StartTime,JobBytes
120        FROM Job
121       WHERE JobId < $last_bkp->{jobid}
122         AND Type = 'B'
123         AND JobStatus = 'T'
124         AND Level = 'F'
125         AND ClientId = $elt->[0]
126         AND Job.Name = $elt->[1]
127       ORDER BY JobId DESC
128       LIMIT 4
129     ) AS T
130 ";
131
132     my $avg_bkp = $bweb->dbh_selectrow_hashref($query);
133
134     if ($avg_bkp->{nbjobs} > 3) {
135         if ($last_bkp->{jobbytes} > ($avg_bkp->{nbbytes} * 1.2)) {
136             print "W: Last backup $elt->[1] on $elt->[2] is greater than 20% (last=",
137                     &Bweb::human_size($last_bkp->{jobbytes}), " avg=",
138                     &Bweb::human_size($avg_bkp->{nbbytes}), ")\n";
139         }
140         if ($last_bkp->{jobbytes} < ($avg_bkp->{nbbytes} * 0.8)) {
141             print "W: Last backup $elt->[1] on $elt->[2] is lower than 20% (last=",
142                     &Bweb::human_size($last_bkp->{jobbytes}), " avg=",
143                     &Bweb::human_size($avg_bkp->{nbbytes}), ")\n";
144         }
145     }
146 }
147 print "
148 Check missing backup for 2 weeks
149 ---------------------------------\n";
150
151 # we check that each job have been run one time for 2 weeks
152 my @jobs = $b->list_job();
153 ($limit, $label) = $bweb->get_limit(age => 2*7*24*60*60);
154
155 foreach my $j (@jobs) {
156
157     $j = $bweb->dbh_quote($j);
158     $query = "
159 SELECT 1 as run
160   FROM Job
161  WHERE Type = 'B'
162    AND JobStatus = 'T'
163    AND Job.Name = $j
164 $limit
165 LIMIT 1
166 ";
167     my $row = $bweb->dbh_selectrow_hashref($query);
168
169     if (!$row) {
170         $query = "
171 SELECT StartTime AS starttime, Level AS level
172   FROM Job
173  WHERE Type = 'B'
174    AND JobStatus = 'T'
175    AND Job.Name = $j
176 LIMIT 1
177 ";
178         $row = $bweb->dbh_selectrow_hashref($query);
179         print "W: No job for $j ";
180         if ($row) {
181             print "($row->{level} $row->{starttime})";
182         }
183         print "\n";
184     }
185 }
186