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