This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
check_bacula.pl
301 lines (231 loc) · 7.16 KB
/
check_bacula.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/perl -w
# ------------------------------------------------------------------------------
# check_bacula.pl - checks the status of bacula
# Copyright (C) 2005 NETWAYS GmbH, www.netways.de
# Author: NETWAYS GmbH <[email protected]>
# Version: $Id: ec31a70a082a410a68e991b1af185bcde76c4c99 $
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# $Id: ec31a70a082a410a68e991b1af185bcde76c4c99 $
# ------------------------------------------------------------------------------
# basic requirements
use strict;
use POSIX;
use File::Basename;
use DBI;
use Getopt::Long;
use Pod::Usage;
# predeclared vars
use vars qw(
$opt_help
$opt_usage
$opt_host
$opt_job
$opt_critical
$opt_warning
$opt_hours
$opt_dbhost
$opt_dbname
$opt_dbuser
$opt_dbpass
$out
$sql
$date_start
$date_stop
$state
$hint
);
my $count = 0;
my $jobfiles = 0;
my $jobbytes = 0;
my $joberrors = 0;
my $jobmissingfiles = 0;
my $opt_dbdriver = 'mysql';
# predeclared subs
sub print_help;
sub get_now;
sub get_date;
# main values
my $PROGNAME = basename($0);
my $VERSION = "0.0.1";
# NAGIOS states
my %ERRORS = (
'UNKNOWN' => '-1',
'OK' => '0',
'WARNING' => '1',
'CRITICAL' => '2'
);
Getopt::Long::Configure('bundling');
GetOptions(
"c=s" => \$opt_critical,
"critical=s" => \$opt_critical,
"w=s" => \$opt_warning,
"warning=s" => \$opt_warning,
"hours=s" => \$opt_hours,
"H=s" => \$opt_host,
"host=s" => \$opt_host,
"j=s" => \$opt_job,
"job=s" => \$opt_job,
"dbhost=s" => \$opt_dbhost,
"db=s" => \$opt_dbname,
"dbuser=s" => \$opt_dbuser,
"dbpass=s" => \$opt_dbpass,
"dbdriver=s" => \$opt_dbdriver,
"h" => \$opt_help,
"help" => \$opt_help,
"usage" => \$opt_usage,
)
|| die "Try '$PROGNAME --help' for more information.\n";
# somebody wants help
if ($opt_help) {
print_help(99);
} elsif ($opt_usage) {
print_help(1);
}
if ( $opt_host && $opt_warning && $opt_critical ) {
# setting up db connection
my $dsn = "DBI:$opt_dbdriver:database=$opt_dbname;host=$opt_dbhost";
my $dbh = DBI->connect( $dsn, $opt_dbuser, $opt_dbpass ) or die "Error connecting to: '$dsn': $DBI::errstr\n";
# setting backup age
if ($opt_hours) {
$date_stop = get_date($opt_hours);
} else {
$date_stop = '1970-01-01 01:00:00';
}
$date_start = get_now();
$opt_host .= " ".$opt_job if(defined $opt_job);
# Use correct statement depending on dbdriver
if($opt_dbdriver eq 'Pg') {
$sql = "SELECT count(*) as count,sum(JobFiles),sum(JobBytes),sum(JobErrors),sum(JobMissingFiles) from Job where Name='" .
$opt_host."' and JobStatus='T' and EndTime IS NOT NULL and EndTime <= '".$date_start."' and EndTime >= '".$date_stop."';";
} else {
$sql = "SELECT count(*) as 'count',sum(JobFiles),sum(JobBytes),sum(JobErrors),sum(JobMissingFiles) from Job where Name='" .
$opt_host."' and JobStatus='T' and EndTime <> '' and EndTime <= '".$date_start."' and EndTime >= '".$date_stop."';";
}
# getting backups from db
my $sth = $dbh->prepare($sql) or die "Error preparing statemment", $dbh->errstr;
$sth->execute;
# processing db results
while ( my @row = $sth->fetchrow_array() ) {
( $count, $jobfiles, $jobbytes, $joberrors, $jobmissingfiles ) = @row;
$count = 0 if ( !$count );
$jobfiles = 0 if ( !$jobfiles );
$jobbytes = 0 if ( !$jobbytes );
$joberrors = 0 if ( !$joberrors );
$jobmissingfiles = 0 if ( !$jobmissingfiles );
}
$state = 'OK';
$hint = '';
# checking results and setting error states
if ( $count < $opt_warning ) { $state = 'WARNING' }
if ( $count < $opt_critical ) { $state = 'CRITICAL' }
if ( $state eq 'OK' ) {
if ( $jobfiles == 0 || $jobbytes == 0 ) {
# nothing backuped
$hint = " but no files nor bytes were backuped at all.";
$state = 'WARNING';
} elsif ( $jobfiles < $joberrors ) {
# many errors
$hint = " but more errors than backuped files.";
$state = 'WARNING';
}
}
print "Bacula $state: Found $count successful jobs " . $hint
. "| jobs=" . $count
. " jobfiles=" . $jobfiles
. " jobbytes=" . $jobbytes
. " joberrors=" . $joberrors
. " jobmissingfiles=" . $jobmissingfiles . "\n";
$dbh->disconnect();
exit $ERRORS{$state};
} else {
print_help(1);
}
# -------------------------
# THE SUBS:
# -------------------------
# print_help($level, $msg);
# prints some message and the POD DOC
sub print_help {
my ( $level, $msg ) = @_;
$level = 0 unless ($level);
pod2usage(
{
-message => $msg,
-verbose => $level
}
);
exit( $ERRORS{UNKNOWN} );
}
sub get_now {
my $now = defined $_[0] ? $_[0] : time;
my $out = strftime( "%Y-%m-%d %X", localtime($now) );
return ($out);
}
sub get_date {
my $day = shift;
my $now = defined $_[0] ? $_[0] : time;
my $new = $now - ( ( 60 * 60 * 1 ) * $day );
my $out = strftime( "%Y-%m-%d %X", localtime($new) );
return ($out);
}
1;
__END__
=head1 NAME
check_bacula.pl - checks for backups per host in Bacula DB
=head1 SYNOPSIS
check_bacula.pl -h
check_bacula.pl --usage
check_bacula.pl
-H <backuped host>
-w <warning backup count> -c <critical backup count>
--dbhost <database host> --db <database name>
--dbuser <database user> --dbpass <database password>
[ -j | --job <name of backup job> ]
[ --hours <maximal age of backup> ]
=head1 DESCRIPTION
B<check_bacula.pl> is checking for backups per host in a specified timeframe, e.g. the last 24 hours.
=head1 OPTIONS
=over 8
=item B<-h>
Display this helpmessage.
=item B<--usage>
Display the usage help
=item B<-H | --host>
The hostname of the backup host
=item B<-w | --warning>
The warning threshold, if number of backups found in db is smaller than this value state WARNING is returned
=item B<-c> | --critical>
The critical threshold, if number of backups found in db is smaller than this value state CRITICAL is returned
=item B<-j> | --job>
The backup job to search for in the database for the specified hostname
=item B<--dbhost>
Bacula database host
=item B<--db>
Bacula database name
=item B<--dbuser>
Bacula database user
=item B<--dbpass>
Bacula database password
=item B<--dbdriver>
DBI driver to use, default mysql
=back
=cut
=head1 VERSION
$Id: ec31a70a082a410a68e991b1af185bcde76c4c99 $
=head1 AUTHOR
NETWAYS GmbH, 2009, http://www.netways.de.
Written by NETWAYS GmbH <[email protected]>
Please report bugs at https://www.netways.org/projects/plugins