]> git.sur5r.net Git - bacula/bacula/blob - gui/bweb/script/check_bacula.pl
ebl Work on check_bacula.pl
[bacula/bacula] / gui / bweb / script / check_bacula.pl
1 #!/usr/bin/perl -w
2
3 =head1 DESCRIPTION
4
5     check_bacula.pl -- check for bacula status
6
7 =head2 USAGE
8
9     check_bacula.pl ...
10       -C|--Client=ss       List of clients
11       -g|--group=ss        List of groups
12
13       -l|--level=F/I/D     Specify job level
14
15       -a|--age=i           Age in hours (default 10h)
16       -w|--warning=i       warning threshold (jobs)
17       -c|--critical=i      critical threshold (jobs)
18
19       -S|--Storage=ss      List of SDs to test
20
21       -s|--scratch=i       threshold scratch number
22       -m|--mediatype=ss    Media type to check for scratch
23
24       -M|--MaxRun          Test for maximum running jobs
25       -F|--Failed          Test for canceled or failed jobs
26
27 =head3 EXAMPLES
28
29    Check :
30       - if more than 10 jobs are running for client c1 and c2 for 1 hour
31     check_bacula.pl -M -C c1 -C c2 -w 10 -c 15 -a 1
32
33       - if more than 10 jobs are running for group g1 for 2 hours
34     check_bacula.pl -M -g g1 -w 10 -c 15 -a 2
35
36       - if more than 10 jobs are failed of canceled for 2 hours for group g1
37     check_bacula.pl -F -g g1 -w 10 -c 15 -a 2
38
39       - if S1_LTO1 and S1_LTO2 storage deamon are responding to status cmd
40     check_bacula.pl -S S1_LTO1 -S S1_LTO2
41
42       - if the scratch pool contains 5 volumes with mediatype Tape% at minimum
43     check_bacula.pl -s 2 -m Tape%
44
45    You can mix all options
46
47    check_bacula.pl -g g1 -w 10 -c 15 -S S1_LTO1 -s 2 -m Tape%
48
49       - if we have more than 10 jobs in error or already running for 2 hours
50    check_bacula.pl -M -F -w 10 -c 15 -a 2
51
52 =head1 LICENSE
53
54    Bweb - A Bacula web interface
55    Bacula® - The Network Backup Solution
56
57    Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
58
59    The main author of Bweb is Eric Bollengier.
60    The main author of Bacula is Kern Sibbald, with contributions from
61    many others, a complete list can be found in the file AUTHORS.
62
63    This program is Free Software; you can redistribute it and/or
64    modify it under the terms of version two of the GNU General Public
65    License as published by the Free Software Foundation plus additions
66    that are listed in the file LICENSE.
67
68    This program is distributed in the hope that it will be useful, but
69    WITHOUT ANY WARRANTY; without even the implied warranty of
70    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
71    General Public License for more details.
72
73    You should have received a copy of the GNU General Public License
74    along with this program; if not, write to the Free Software
75    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
76    02110-1301, USA.
77
78    Bacula® is a registered trademark of John Walker.
79    The licensor of Bacula is the Free Software Foundation Europe
80    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zurich,
81    Switzerland, email:ftf@fsfeurope.org.
82
83 =cut
84
85 use strict ;
86 use Getopt::Long ;
87 use Bweb;
88 use CGI;
89 use POSIX qw/strftime/;
90 use Getopt::Long qw/:config no_ignore_case/;
91 use Pod::Usage;
92
93 my $config_file = $Bweb::config_file;
94 my (@client, @group, $help, $query, $verbose, @msg, @storage);
95
96 my $max_run;
97 my $test_failed;
98 my $mediatype='%';              # check for all mediatype
99 my $nb_scratch;                 # check for scratch media
100 my $crit = 10;
101 my $warn = 5;
102 my $age;
103 my $res;
104 my $ret=0;
105 my $level;
106 my $timeout=50;                 # timeout for storage status command
107
108 GetOptions("Client=s@"  => \@client,
109            "group=s@"   => \@group,
110            "age=i"     => \$age,
111            "scratch=i" => \$nb_scratch,
112            "warning=i" => \$warn,
113            "critical=i"=> \$crit,
114            "verbose"   => \$verbose,
115            "timeout=i" => \$timeout,
116            "Storage=s@"=> \@storage,
117            "mediatype=s"=> \$mediatype,
118            "MaxRun"    => \$max_run,
119            "Failed"    => \$test_failed,
120            "level=s"   => \$level,
121            "help"      => \$help) 
122     || Pod::Usage::pod2usage(-exitval => 2, -verbose => 1) ;
123
124 Pod::Usage::pod2usage(-verbose => 1) if ( $help ) ;
125
126 if ($age) {
127     $age *= 60*60;
128 } else {
129     $age = 10*60*60;            # set default to 10h
130 }
131
132 my $since = time - $age;
133 my $trig = time - 2*60*60;
134
135 my $conf = new Bweb::Config(config_file => $config_file);
136 $conf->load();
137 my $bweb = new Bweb(info => $conf);
138
139 my $b = $bweb->get_bconsole();
140 $b->{timeout} = $timeout;
141
142 CGI::param(-name=> 'client',-value => \@client);
143 CGI::param(-name=> 'client_group', -value => \@group);
144 CGI::param(-name=> 'level', -value => $level);
145
146 my ($where, undef) = $bweb->get_param(qw/clients client_groups level/);
147
148 my $c_filter ="";
149 my $g_filter = "";
150
151 if (@client) {
152     $c_filter = " JOIN Client USING (ClientId) ";
153 }
154
155 if (@group) {
156     $g_filter = " JOIN client_group_member USING (ClientId) " .
157                 " JOIN client_group USING (client_group_id) ";
158 }
159
160 ################################################################
161 # check if more than X jobs are running or just created
162 # for too long (more than 2 hours) since Y ago
163
164 if ($max_run) {
165     $query = "
166 SELECT count(1) AS nb
167   FROM Job $c_filter $g_filter
168
169  WHERE JobStatus IN ('R', 'C')
170    AND Type = 'B'
171    AND JobTDate > $since
172    AND JobTDate < $trig
173  $where
174 ";
175
176     $res = $bweb->dbh_selectrow_hashref($query);
177     if ($res) {
178         my $nb = $res->{nb};
179         if ($nb >= $crit) {
180             push @msg, "$nb jobs are running";
181             $ret = 2;
182         } elsif ($nb >= $warn) {
183             push @msg, "$nb jobs are running";
184             $ret = ($ret>1)?$ret:1;
185         }
186     }
187 }
188
189 ################################################################
190 # check failed jobs (more than X) since x time ago
191
192 if ($test_failed) {
193     $query = "
194 SELECT count(1) AS nb
195   FROM Job $c_filter $g_filter
196
197  WHERE JobStatus IN ('E','e','f','A')
198    AND Type = 'B'
199    AND JobTDate > $since
200  $where
201 ";
202
203     $res = $bweb->dbh_selectrow_hashref($query);
204     if ($res) {
205         my $nb = $res->{nb};
206         if ($nb >= $crit) {
207             push @msg, "$nb jobs are in error";
208             $ret = 2;
209         } elsif ($nb >= $warn) {
210             push @msg, "$nb jobs are in error";
211             $ret = ($ret>1)?$ret:1;
212         }
213     }
214 }
215
216 ################################################################
217 # check storage status command
218
219 foreach my $st (@storage) {
220
221     my $out = $b->send_cmd("status storage=\"$st\"");
222     if (!$out || $out !~ /Attr spooling|JobId/) {
223         push @msg, "timeout ($timeout s) or bad response on status storage $st";
224         $ret = 2;
225     }
226 }
227
228 ################################################################
229 # check for Scratch volume
230
231 if ($nb_scratch) {
232     $query = "
233   SELECT MediaType AS mediatype, count(MediaId) AS nb
234     FROM Media JOIN Pool USING (PoolId)
235    WHERE Pool.Name = 'Scratch'
236      AND Media.MediaType LIKE '$mediatype'
237          
238   GROUP BY MediaType
239 ";
240
241     $res = $bweb->dbh_selectall_hashref($query, 'mediatype');
242     if ($res && keys %$res) {
243         foreach my $k (keys %$res) {
244             if ($res->{$k}->{nb} < $nb_scratch) {
245                 push @msg, "no more scratch for $k ($res->{$k}->{nb})";
246                 $ret = 2;
247             }
248         }
249     } else { # query doesn't report anything...
250         push @msg, "no more scratch for $mediatype";
251         $ret = 2;
252     }
253 }
254
255 ################################################################
256 # print result
257
258 if (!$ret) {
259     print "OK - All checks ok\n";
260 } elsif ($ret == 1) {
261     print "WARNING - ", join(", ", @msg), "\n";
262 } else {
263     print "CRITICAL - ", join(", ", @msg), "\n";
264 }
265 exit $ret;
266