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