-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_bacula_lastbackup.pl
executable file
·187 lines (157 loc) · 4.99 KB
/
check_bacula_lastbackup.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
#!/usr/bin/perl
# Author: Michael Wyraz
# Much code copied from check_mail_loop.pl which is GPL licensed. So this code has the same license.
#
# Checks the age of the last successfull backup of a given client and (optional) of a given level.
#
use strict;
use Getopt::Long;
use Time::Local;
use Env qw/NAGIOS_HOSTNAME/;
&Getopt::Long::config('auto_abbrev');
my $bconsoleCommand="/usr/sbin/bconsole";
my $level="*";
my $levelName;
my $client;
my $warningAge=24;
my $criticalAge=48;
my %ERRORS = ('OK' , '0',
'WARNING', '1',
'CRITICAL', '2',
'UNKNOWN' , '3');
# Subs declaration
sub usage;
sub nsexit;
# Evaluate Command Line Parameters
my $status = GetOptions(
"bconsole-command=s",\$bconsoleCommand,
"level=s",\$level,
"client=s",\$client,
"warningAge=i",\$warningAge,
"criticalAge=i",\$criticalAge,
);
# If not specified on command line (which will not be empty, but '$'),
# try to use environment variable
$client = $NAGIOS_HOSTNAME if $client eq '$';
if ($status == 0 || !($client && $bconsoleCommand) ) {
usage();
}
if ($level eq "*") {
$levelName="backup";
}
elsif ($level eq "F" || $level eq "f") {
$level="F";
$levelName="FULL backup";
}
elsif ($level eq "I" || $level eq "i") {
$level="I";
$levelName="INCREMENTAL backup";
}
elsif ($level eq "D" || $level eq "d") {
$level="D";
$levelName="DIFFERENTIAL backup";
}
else {
usage();
}
# restrict client names to a-z,A-Z,0-9,".","_","-" to avoid execution of shell commands
if (!($client=~m/^[a-zA-Z0-9\.\-_]+$/i)) {
nsexit ("INVALID CLIENT NAME","ERROR");
}
open (JOBLIST,"echo 'list jobname=$client' | $bconsoleCommand |");
my $latestBackupAge=-1;
my $jobStatus;
while(<JOBLIST>) {
my($line) = $_;
# split into columns (and remove whitespaces)
my ($_dummy,$_jobId,$_client,$_startTime,$_type,$_level,$_jobFiles,$_jobBytes,$_jobStatus)=split(/\s*\|\s*/,$line);
if ( $_jobStatus ne 'T' and $_jobStatus ne 'R') {
next; # only jobs which terminated correctly
} else {
$jobStatus = $_jobStatus;
}
if ( $_client ne $client ) {
next; # only jobs for this client
}
if (!( $level eq "*" || $_level eq $level )) {
next; # only jobs for the required level (or any if $level="*")
}
if($level eq '*') {
if($_level eq 'I') {
$levelName = 'INCREMENTAL backup';
} elsif($_level eq 'F') {
$levelName = 'FULL backup';
} elsif($_level eq 'D') {
$levelName = 'DIFFERENTIAL backup';
} else {
$levelName = 'backup';
}
}
my ($_y,$_m,$_d,$_H,$_M,$_S);
($_y,$_m,$_d,$_H,$_M,$_S) = ( $_startTime=~/^(\d{4})\-(\d{2})\-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})$/ );
if (! $_y ) {
next; # require valid startTime
}
my $_startTimeAsUnixtime=timelocal($_S, $_M, $_H, $_d, $_m-1, $_y);
my $backupAgeInSeconds=time()-$_startTimeAsUnixtime;
$backupAgeInSeconds = 1 if $backupAgeInSeconds<0;
if ($backupAgeInSeconds>0) {
if ($latestBackupAge < 0 || $latestBackupAge > $backupAgeInSeconds) {
$latestBackupAge=$backupAgeInSeconds;
}
}
}
my $result;
my $status;
if ($latestBackupAge<0) {
$result="Unable to find a valid $levelName for $client";
$status="CRITICAL";
} else {
$result="Last $levelName for $client ";
if($jobStatus eq 'R') {
$result .= "started ";
} else {
$result .= "was ";
}
$result.=sprintf ("%02D:%02D hours", $latestBackupAge/3600,($latestBackupAge/60) %60);
$result.=' ago';
if($jobStatus eq 'R') {
$result.= ' and is still running';
} else {
$result.= '.';
}
if ($latestBackupAge/3600 > $criticalAge ) {
$status="CRITICAL";
} elsif ($latestBackupAge/3600 > $warningAge) {
$status="WARNING";
} else {
$status="OK";
}
}
nsexit($result,$status);
sub usage {
print "check_bacula_lastbackup.pl 1.0 Nagios Plugin\n";
print "\n";
print "=" x 75,"\nERROR: Missing or wrong arguments!\n","=" x 75,"\n";
print "\n";
print "This script checks before how many hours the last successfull\n";
print "backup of a certain client was done.\n";
print "\n";
print "\nThe following options are available:\n";
print " -bconsole-command=path path to the bconsole command ($bconsoleCommand)\n";
print " -client=text bacula client to check\n";
print " -level=[*|F|D|I] level of backup to check (*=any, F=full, D=differential, I=incremental - default: any)\n";
print " -warningAge=hours if the last backup is older than $warningAge hours, status is warning\n";
print " -criticalAge=hours if the last backup is older than $criticalAge hours, status is critical\n";
print "\n";
print " Options may abbreviated!\n";
print "This script comes with ABSOLUTELY NO WARRANTY\n";
print "This programm is licensed under the terms of the ";
print "GNU General Public License\n\n";
exit $ERRORS{"UNKNOWN"};
}
sub nsexit {
my ($msg,$code) = @_;
print "$code: $msg\n" if (defined $msg);
exit $ERRORS{$code};
}