forked from arogarth/nagios-check_bareos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_bareos.pl
169 lines (125 loc) · 3.9 KB
/
check_bareos.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
#! /usr/bin/perl -w
#
# See INSTALL document
#
use strict;
use FindBin;
FindBin::again();
use lib $FindBin::Bin;
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use vars qw($PROGNAME $PROGVER);
use Getopt::Long;
use vars qw($opt_V $opt_h $verbose $job);
$PROGNAME = "check_bareos";
$PROGVER = "0.2";
my @okStatus = ('C', 'R', 'T');
my @warningStatus = ('F', 'S', 'm', 'M', 's', 'j', 'c', 'd', 't', 'p', 'i', 'a'); # All Waiting status
# add a directive to exclude buffers:
my $DONT_INCLUDE_BUFFERS = 0;
sub print_help ();
sub print_usage ();
#sub sys_status ();
my $bareosDir = "/etc/bareos";
my $baculaDir = "/etc/bacula";
my $workingDir = "";
my $exitcode = $ERRORS{'OK'};
if(-d $bareosDir) {
$workingDir = $bareosDir;
} elsif (-d $baculaDir ) {
$workingDir = $baculaDir;
} else {
print "UNKNOWN: CAN'T DETECT BAREOS/BACULA";
exit $ERRORS{'UNKNOWN'};
}
Getopt::Long::Configure('bundling');
GetOptions (
"V" => \$opt_V, "version" => \$opt_V,
"h" => \$opt_h, "help" => \$opt_h,
"v" => \$verbose, "verbose" => \$verbose,
"j=s" => \$job, "job=s" => \$job);
if ($opt_V) {
print_revision($PROGNAME,'$Revision: '.$PROGVER.' $');
exit $ERRORS{'UNKNOWN'};
}
if ($opt_h) {
print_help();
exit $ERRORS{'UNKNOWN'};
}
my $cmd_sched = "echo \"status dir days=10\" | bconsole -c ${workingDir}/nagios-console.conf";
# split jobs by line to array
my @jobs_sched = split('\n', `$cmd_sched`);
my $jobCount_sched = $#jobs_sched;
my @jobRow_sched = ();
my $start_sched = 0;
my $end_sched = 0;
for(my $j=0; $j<$jobCount_sched; $j++) {
if($jobs_sched[$j] =~ /Scheduled Jobs:/) {
$start_sched=$j+3;
}
if($jobs_sched[$j] =~ /Running Jobs:/) {
$end_sched=$j-3;
last;
}
}
for(my $k=$start_sched; $k<=$end_sched; $k++) {
@jobRow_sched = split(/\s*\ \s*/, $jobs_sched[$k]);
#print $jobRow_sched[5];
# Command to fetch jobs
my $cmd = "echo \"list job=$jobRow_sched[5]\" | bconsole -c ${workingDir}/nagios-console.conf";
# split jobs by line to array
my @jobs = split('\n', `$cmd`);
my $jobCount = $#jobs;
my @jobRow = ();
# search for last valid job entry from bottom of the table
for(my $i=$jobCount; $i>0; $i--) {
if($jobs[$i] =~ /\s*\|\s*/) {
@jobRow = split(/\s*\|\s*/, $jobs[$i]);
last;
}
}
# No job found? exit with unknown state
if(!@jobRow) {
print "UNKNOWN: JOB NOT FOUND!\n" . join("\n", @jobs);
if ($exitcode == $ERRORS{'OK'}) {
$exitcode = $ERRORS{'UNKNOWN'};
}
# exit $ERRORS{'UNKNOWN'};
}
# Field 8 is status-field
my $jobStatus = $jobRow[8];
if( grep{ /$jobStatus/i } @okStatus ) {
print "";
# print "OK: " . $jobRow[2] . " " . $jobRow[3] . " --- ";
# exit $ERRORS{'OK'};
} elsif( grep{ /$jobStatus/i } @warningStatus ) {
print "WARNING: " . $jobRow[2] . " " . $jobRow[3] . " --- ";
if (($exitcode == $ERRORS{'OK'}) || ($exitcode == $ERRORS{'UNKNOWN'})) {
$exitcode = $ERRORS{'WARNING'};
}
# exit $ERRORS{'WARNING'};
} else {
print "CRITICAL: " . $jobRow[2] . " " . $jobRow[3] . " --- ";
if (($exitcode == $ERRORS{'OK'}) || ($exitcode == $ERRORS{'UNKNOWN'}) || ($exitcode == $ERRORS{'WARNING'})) {
$exitcode = $ERRORS{'CRITICAL'};
}
# exit $ERRORS{'CRITICAL'};
}
}
if ($exitcode == $ERRORS{'OK'}) {
print "All backups are OK"
}
exit $exitcode;
sub print_usage () {
print "Usage: $PROGNAME -j <JobName>\n";
exit $ERRORS{'UNKNOWN'} unless ($opt_h);
}
sub print_help () {
print_revision($PROGNAME,'$Revision: '.$PROGVER.' $');
print "Copyright (c) 2013 arogarth\n";
print "\n";
print_usage();
print "\n";
print "-j <JobName> = Bareos/Bacula Job-Name\n";
print "-h = This screen \n\n";
support();
}