forked from inuits/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_bacula
executable file
·184 lines (156 loc) · 5.47 KB
/
check_bacula
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
#!/usr/bin/perl -w
## 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 3 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 check_bacula.pl. If not, see <http://www.gnu.org/licenses/>.
# Submitted:
# Julian Hein NETWAYS GmbH
# Managing Director Deutschherrnstr. 47a
# Fon.0911/92885-0 D-90429 Nürnberg
# Fax.0911/92885-31
# jhein@netways.de www.netways.de
# Modified:
# Silver Salonen <silver@ultrasoft.ee>
# Dave Simons <simonsd@inuits.eu>
# Veros Kaplan <veros@inuits.eu>
# version 0.0.5-2 (16.July.2014)
# * fixed time formatting
# version 0.0.5 (18.March.2013)
# * parameterize sql vars
# version 0.0.4 (23.March.2012)
# * add $sqlHost variable
# version 0.0.3 (07.May.2007)
# * fix typo 'successfull' -> 'successful'
# * add $sqlUsername and $sqlDB variables
# version 0.0.2 (05.May.2006)
# * implement print_usage()
# * implement print_help()
# * add variable $sqlPassword for setting MySQL-password
# * add variable $progVers for showing it in case of -V
use strict;
use POSIX;
use File::Basename;
use DBI;
use Getopt::Long;
use vars qw(
$opt_help
$opt_job
$opt_critical
$opt_warning
$opt_hours
$opt_usage
$opt_version
$out
$sql
$date_start
$date_stop
$state
$count
$opt_sqlDB
$opt_sqlHost
$opt_sqlUser
$opt_sqlPass
);
sub print_help();
sub print_usage();
sub get_now();
sub get_date;
my $progname = basename($0);
my $progVers = "0.0.5";
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,
"H=s" => \$opt_hours, "hours=s" => \$opt_hours,
"j=s" => \$opt_job, "job=s" => \$opt_job,
"n=s" => \$opt_sqlDB, "dbname=s" => \$opt_sqlDB,
"s=s" => \$opt_sqlHost, "dbserver=s" => \$opt_sqlHost,
"u=s" => \$opt_sqlUser, "dbuser=s" => \$opt_sqlUser,
"p=s" => \$opt_sqlPass, "dbpass=s" => \$opt_sqlPass,
"h" => \$opt_help, "help" => \$opt_help,
"usage" => \$opt_usage,
"V" => \$opt_version, "version" => \$opt_version
) || die "Try '$progname --help' for more information.\n";
sub print_help() {
print "\n";
print "Options:\n";
print "H check successful jobs within <hours> period\n";
print "c number of successful jobs for not returning critical\n";
print "w number of successful jobs for not returning warning\n";
print "j name of the job to check (case-sensitive)\n";
print "n name of the bacula database\n",
print "s name of the host running the bacula database\n";
print "u user with which to connect to the bacula database\n";
print "p password with which to connect to the bacula database\n";
print "h show this help\n";
print "V print script version\n";
}
sub print_usage() {
print "Usage: $progname -H <hours> -c <critical> -w <warning> -j <job-name> -n <dbname> -s <dbserver> -u <dbuser> -p <dbpass> [ -h ] [ -V ]\n";
}
sub get_now() {
my $now = defined $_[0] ? $_[0] : time;
my $out = strftime("%Y-%m-%d %H:%M:%I", 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 %H:%M:%I", localtime($new));
return ($out);
}
if ($opt_help) {
print_usage();
print_help();
exit $ERRORS{'UNKNOWN'};
}
if ($opt_usage) {
print_usage();
exit $ERRORS{'UNKNOWN'};
}
if ($opt_version) {
print "$progname $progVers\n";
exit $ERRORS{'UNKNOWN'};
}
if ($opt_job && $opt_warning && $opt_critical) {
my $dsn = "DBI:mysql:database=$opt_sqlDB;host=$opt_sqlHost";
my $dbh = DBI->connect( $dsn,$opt_sqlUser,$opt_sqlPass ) or die "Error connecting to: '$dsn': $DBI::errstr\n";
if ($opt_hours)
{
$date_stop = get_date($opt_hours);
}
else
{
$date_stop = '1970-01-01 01:00:00';
}
$date_start = get_now();
$sql = "SELECT count(*) as 'count' from Job where (Name='$opt_job') and (JobStatus='T') and (EndTime <> '') and ((EndTime <= '$date_start') and (EndTime >= '$date_stop'));";
my $sth = $dbh->prepare($sql) or die "Error preparing statemment",$dbh->errstr;
$sth->execute;
while (my @row = $sth->fetchrow_array()) {
($count) = @row;
}
$state = 'OK';
if ($count<$opt_warning) { $state='WARNING' }
if ($count<$opt_critical) { $state='CRITICAL' }
print "Bacula $state: Found $count successful jobs\n";
exit $ERRORS{$state};
$dbh->disconnect();
}
else {
print_usage();
}