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