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
/
count_backup.pl
270 lines (210 loc) · 6.62 KB
/
count_backup.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
#!/usr/bin/perl
#
# nagios: -epn
#
# ------------------------------------------------------------------------------
# check_bacula_count.pl - checks the size of Bacula backups
# Copyright (C) 2005 NETWAYS GmbH, www.netways.de
# Author: NETWAYS GmbH <[email protected]>
# Version: $Id: 90ffc4432e7ea3086cd5407b7538a22990148853 $
#
# 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: 90ffc4432e7ea3086cd5407b7538a22990148853 $
# ------------------------------------------------------------------------------
use strict;
use POSIX;
use File::Basename;
use DBI;
use Getopt::Long;
use Pod::Usage;
# predeclared vars
use vars qw(
$opt_help
$opt_month
$month1
$month2
$opt_pool
$opt_cron
$opt_warning
$opt_critical
$rc
$sec
$min
$hour
$mday
$mon
$opt_year
$wday
$yday
$isdst
$sql
);
# database credentials
my $host = "localhost";
my $db = "bacula";
my $user = "bacula";
my $pass = "bacula";
my $pool = "Pool1";
my $rc = 0;
# main values
my $PROGNAME = basename($0);
my $VERSION = "0.0.1";
# NAGIOS states
my %ERRORS = (
'UNKNOWN' => '-1',
'OK' => '0',
'WARNING' => '1',
'CRITICAL' => '2'
);
# processing options
Getopt::Long::Configure('bundling');
GetOptions(
"month=s" => \$opt_month,
"year=s" => \$opt_year,
"pool=s" => \$opt_pool,
"h" => \$opt_help,
"help" => \$opt_help,
"cron" => \$opt_cron,
"warning=s" => \$opt_warning,
"critical=s" => \$opt_critical,
"w=s" => \$opt_warning,
"c=s" => \$opt_critical,
)
|| die "Try '$PROGNAME --help' for more information.\n";
# somebody wants help
if ($opt_help) {
print_help(1);
}
if ( ($opt_year && $opt_month) || $opt_cron ) {
$opt_month = "0".$opt_month if ($opt_month =~ /^\d$/);
if (defined $opt_cron) {
($sec,$min,$hour,$mday,$mon,$opt_year,$wday,$yday,$isdst) = localtime(time - 24*60*60);
if ($mon =~ /^\d$/) {
$opt_month = "0" . ($mon + 1);
} else {
$opt_month = $mon + 1;
}
$opt_year += 1900;
}
$month1 = $opt_month;
$month2 = $opt_month + 1;
my $dsn = "DBI:mysql:database=$db;host=$host";
my $dbh = DBI->connect("$dsn","$user","$pass") || die "Database connection not made: $DBI::errstr";
$sql = qq{select Name from Pool};
if (defined $opt_pool) {
$sql = qq{select sum(JobBytes)/1024/1024/1024 from Job where PoolID IN (select PoolID from Pool where Name Like "$opt_pool") AND StartTime >= (select Date_Format(SchedTime, '%Y-%m-%d 00:00:00') AS Date FROM Job where PoolID IN (select PoolID from Pool where Name Like "$opt_pool") AND LEVEL = 'F' AND JobStatus = 'T' GROUP BY Date ORDER BY Date DESC LIMIT 1,1);};
my $bytes = $dbh->selectrow_array($sql);
unless ($bytes) {
$sql = qq{select sum(JobBytes)/1024/1024/1024 from Job where PoolID IN (select PoolID from Pool where Name Like "$opt_pool");};
$bytes = $dbh->selectrow_array($sql);
}
$sql = qq{select (MaxVols * MaxVolBytes / 1024 / 1024 / 1024) from Pool where Name = "$opt_pool";};
my $poolsize = $dbh->selectrow_array($sql);
$bytes =~ s/(\d+\.\d{2})\d*/$1/;
$poolsize =~ s/(\d+\.\d{2})\d*/$1/;
unless ($bytes) {
$bytes = "0.00";
}
my $used_percentage = ($bytes * 100 / $poolsize);
if ( $used_percentage > $opt_critical and defined $opt_critical) {
print "CRITICAL - ";
$rc = 'CRITICAL';;
} elsif ( $used_percentage > $opt_warning and defined $opt_warning) {
print "WARNING - ";
$rc = 'WARNING';
} else {
print "OK - ";
}
printf("%.2f", $used_percentage);
print "% used - ";
print "Backupvolumen ";
print "$bytes GB von $poolsize GB|";
print "transferred=$bytes max=$poolsize\n";
} else {
my $pools = $dbh->selectall_arrayref($sql);
foreach (@$pools) {
$pool = "@$_";
$sql = qq{select sum(JobBytes)/1024/1024/1024 from Job where PoolID IN (select PoolID from Pool where Name Like "$pool") AND StartTime >= (select Date_Format(SchedTime, '%Y-%m-%d 00:00:00') AS Date FROM Job where PoolID IN (select PoolID from Pool where Name Like "$pool") AND LEVEL = 'F' AND JobStatus = 'T' GROUP BY Date ORDER BY Date DESC LIMIT 1,1);};
my $bytes = $dbh->selectrow_array($sql);
unless ($bytes) {
$sql = qq{select sum(JobBytes)/1024/1024/1024 from Job where PoolID IN (select PoolID from Pool where Name Like "$pool");};
$bytes = $dbh->selectrow_array($sql);
}
$bytes =~ s/(\d+\.\d{2})\d*/$1/;
unless ($bytes) {
$bytes = "0.00";
}
print "Backupvolumen $pool Monat $month1/$opt_year\n";
print "=========================================\n";
print "$bytes GB\n\n";
}
}
$dbh->disconnect();
exit $ERRORS{$rc};
} 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} );
}
__END__
=head1 NAME
check_bacula_count.pl - checks for Bacula pools for usage
=head1 SYNOPSIS
check_bacula_count.pl -h
check_bacula_count.pl
check_bacula_count.pl --month <month to check> --year <year to check> | --cron
[ --pool <pool to check> ]
[ --warning <warning threshold in %> ]
[ --critical <critical threshold in %> ]
=head1 DESCRIPTION
B<check_bacula_count.pl> is checking for backup pool size in Bacula database
=head1 OPTIONS
=over 8
=item B<-h>
Display this helpmessage.
=item B<--month>
month to check, in this case year is also required
=item B<--year>
year to check
=item B<--cron>
cron is the automated way to check for pool size periodicaly for the last month
=item B<--pool>
the pool name to check
=item B<-w | --warning>
warning threshold in %
=item B<-c | --critical>
warning threshold in %
=back
=cut
=head1 VERSION
$Id: 90ffc4432e7ea3086cd5407b7538a22990148853 $
=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