3 # Check the status of da-backup backups
4 # Copyright 2007 Stephen Gran <sgran@debian.org>
5 # Copyright 2008 Peter Palfrader
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 my $DABACKUP_CONF = '/etc/da-backup.conf';
33 my $MAX_AGE = 30*60*60;
42 $SIG{__DIE__ } = sub() {
44 exit $CODE{'UNKNOWN'};
48 my $EXITCODE = 'UNDEF';
52 my ($code, $msg, $resource) = @_;
53 $MESSAGE{$msg} = [] unless defined $MESSAGE{$msg};
54 push @{$MESSAGE{$msg}}, $resource;
55 $EXITCODE = ($CODE{$code} > $CODE{$EXITCODE}) ? $code : $EXITCODE;
60 my ($exitcode, $fd) = @_;
62 print $fd "Usage: $PROGRAM_NAME --help\n";
63 print $fd "Usage: $PROGRAM_NAME";
69 Getopt::Long::config('bundling');
71 'h|help' => \$params->{'help'},
73 die ("$PROGRAM_NAME: Usage: $PROGRAM_NAME [-fwhv]\n");
76 help(0, *STDOUT) if $params->{'help'};
77 help(1, *STDERR) if scalar @ARGV > 0;
80 unless (-e $DABACKUP_CONF) {
81 if (-e '/etc/da-backup') {
82 print "WARNING: No $DABACKUP_CONF, but we have /etc/da-backup/\n";
83 exit $CODE{'WARNING'};
85 if (-e '/var/log/da-backup') {
86 print "WARNING: No $DABACKUP_CONF, but we have /var/log/da-backup/\n";
87 exit $CODE{'WARNING'};
89 print "OK: da-backup not installed\n";
96 open (FH, "< $DABACKUP_CONF") or die ("Cannot open $DABACKUP_CONF: $!\n");
100 } elsif (/logdir=(.*)/) {
105 die ("No confdir found in $DABACKUP_CONF") unless defined $confdir;
106 die ("No logdir found in $DABACKUP_CONF") unless defined $logdir;
108 opendir(DIR, $confdir) or die ("Cannot opendir $confdir: $!\n");
109 my %conffiles = map {$_ => 1} grep { !/^\./ && !/\.bak$/} readdir(DIR);
112 opendir(DIR, $logdir) or die ("Cannot opendir $logdir: $!\n");
113 my %logfiles = map {$_ => 1} grep { !/^\./ && !/\.[0-9]+(\.gz)?$/} readdir(DIR);
116 if (scalar keys %conffiles == 0) {
117 print "WARNING: da-backup installed but no backups configured\n";
118 exit $CODE{'WARNING'};
121 for my $f (keys %conffiles) {
122 unless (exists $logfiles{$f}) {
123 problem('WARNING', 'no log', $f);
128 for my $f (sort {$a cmp $b} keys %logfiles) {
129 unless (exists $conffiles{$f}) {
130 problem('WARNING', 'no config', $f);
134 my @stat = stat("$logdir/$f") or die ("Cannot stat $logdir/$f: $!\n");
135 my $age = time - $stat[10];
137 if ($age < $TOO_FRESH) { # File is too new, let's use the old one
138 if (-e "$logdir/$f.0") {
140 my @stat = stat("$logdir/$f") or die ("Cannot stat $logdir/$f: $!\n");
141 $age = time - $stat[10];
146 problem('WARNING', 'future timestamp', $f);
148 } elsif ($age > $MAX_AGE) {
151 if ($age > 48 * 3600) {
152 $hage = sprintf("%d days", $age / 24 / 3600);
154 $hage = sprintf("%d hours", $age / 3600);
156 problem('WARNING', 'old', "$f ($hage)");
160 open(FH, "< $logdir/$f") or die ("Cannot open $logdir/$f: $!\n");
161 sysseek(FH, -1024, SEEK_END); # just try it - doesn't matter if it fails
166 if (/^sent\s+\d+\s+bytes\s+received\s+\d+\s+bytes\s+[\d\.]+\s+bytes\/sec$/) {
167 problem('OK', 'probably ok', $f);
174 problem('CRITICAL', 'FAILED', "$f ($last2 $last)");
177 my $msg = join("; ", map {"$_: ".join(', ', @{$MESSAGE{$_}}) } (sort {$a cmp $b} keys %MESSAGE));
178 print $EXITCODE, ": ", $msg, "\n";
179 exit $CODE{$EXITCODE};