[project @ peter@palfrader.org-20080414210537-nxv7xzwb11fw1mzs]
[mirror/dsa-nagios.git] / dsa-nagios-nrpe-config / dsa-check-dabackup
1 #!/usr/bin/perl -w
2
3 # Check the status of da-backup backups
4 # Copyright 2007 Stephen Gran <sgran@debian.org>
5 # Copyright 2008 Peter Palfrader
6 #
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:
14 #
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
17 #
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.
25
26 use strict;
27 use warnings;
28 use English;
29 use Getopt::Long;
30 use Fcntl qw(:seek);
31
32 my $DABACKUP_CONF = '/etc/da-backup.conf';
33 my $MAX_AGE = 24*60*60;
34 my %CODE = (
35         'UNDEF'         => -1,
36         'OK'            => 0,
37         'WARNING'       => 1,
38         'CRITICAL'      => 2,
39         'UNKNOWN'       => 3
40 );
41 $SIG{__DIE__ } = sub() {
42         print shift;
43         exit $CODE{'UNKNOWN'};
44 };
45
46
47 my $EXITCODE = 'UNDEF';
48 my %MESSAGE = ();
49
50 sub problem($$$) {
51         my ($code, $msg, $resource) = @_;
52         $MESSAGE{$msg} = [] unless defined $MESSAGE{$msg};
53         push @{$MESSAGE{$msg}}, $resource;
54         $EXITCODE = ($CODE{$code} > $CODE{$EXITCODE}) ? $code : $EXITCODE;
55 };
56
57
58 sub help($$) {
59         my ($exitcode, $fd) = @_;
60         version ($fd, 0);
61         print $fd "Usage: $PROGRAM_NAME --help\n";
62         print $fd "Usage: $PROGRAM_NAME";
63         exit $exitcode
64 };
65
66 my $params = {};
67
68 Getopt::Long::config('bundling');
69 if (!GetOptions (
70         'h|help'        =>  \$params->{'help'},
71         )) {
72         die ("$PROGRAM_NAME: Usage: $PROGRAM_NAME [-fwhv]\n");
73 };
74
75 help(0, *STDOUT) if $params->{'help'};
76 help(1, *STDERR) if scalar @ARGV > 0;
77
78
79 unless (-e $DABACKUP_CONF) {
80         if (-e '/etc/da-backup') {
81                 print "WARNING: No $DABACKUP_CONF, but we have /etc/da-backup/\n";
82                 exit $CODE{'WARNING'};
83         };
84         if (-e '/var/log/da-backup') {
85                 print "WARNING: No $DABACKUP_CONF, but we have /var/log/da-backup/\n";
86                 exit $CODE{'WARNING'};
87         };
88         print "OK: da-backup not installed\n";
89         exit $CODE{'OK'};
90 };
91
92 my $confdir;
93 my $logdir;
94
95 open (FH, "< $DABACKUP_CONF") or die ("Cannot open $DABACKUP_CONF: $!\n");
96 while (<FH>) {
97         if (/confdir=(.*)/) {
98                 $confdir = $1;
99         } elsif (/logdir=(.*)/) {
100                 $logdir = $1;
101         };
102 };
103
104 die ("No confdir found in $DABACKUP_CONF") unless defined $confdir;
105 die ("No logdir found in $DABACKUP_CONF") unless defined $logdir;
106
107 opendir(DIR, $confdir) or die ("Cannot opendir $confdir: $!\n");
108 my %conffiles = map {$_ => 1} grep { !/^\./ && !/\.bak$/} readdir(DIR);
109 closedir(DIR);
110
111 opendir(DIR, $logdir) or die ("Cannot opendir $logdir: $!\n");
112 my %logfiles = map {$_ => 1} grep { !/^\./ && !/\.[0-9]+(\.gz)?$/} readdir(DIR);
113 closedir(DIR);
114
115 if (scalar keys %conffiles == 0) {
116         print "WARNING: da-backup installed but no backups configured\n";
117         exit $CODE{'WARNING'};
118 };
119
120 for my $f (keys %conffiles) {
121         unless (exists $logfiles{$f}) {
122                 problem('WARNING', 'no log', $f);
123         }
124 }
125
126 FILE:
127 for my $f (sort {$a cmp $b} keys %logfiles) {
128         unless (exists $conffiles{$f}) {
129                 problem('WARNING', 'no config', $f);
130                 next;
131         }
132
133         my @stat = stat("$logdir/$f") or die ("Cannot stat $logdir/$f: $!\n");
134         my $age = time - $stat[10];
135         if ($age < 0) {
136                 problem('WARNING', 'future timestamp', $f);
137                 next;
138         } elsif ($age > $MAX_AGE) {
139                 my $hage;
140
141                 if ($age > 48 * 3600) {
142                         $hage = sprintf("%d days", $age / 24 / 3600);
143                 } else {
144                         $hage = sprintf("%d hours", $age /  3600);
145                 };
146                 problem('WARNING', 'old', "$f ($hage)");
147                 next;
148         };
149
150         open(FH, "< $logdir/$f") or die ("Cannot open $logdir/$f: $!\n");
151         sysseek(FH, -1024, SEEK_END); # just try it - doesn't matter if it fails
152         my $last2 = '';
153         my $last = '';
154         while (<FH>) {
155                 chomp;
156                 if (/^sent\s+\d+\s+bytes\s+received\s+\d+\s+bytes\s+[\d\.]+\s+bytes\/sec$/) {
157                         problem('OK', 'probably ok', $f);
158                         close(FH);
159                         next FILE;
160                 };
161                 $last2 = $last;
162                 $last = $_;
163         };
164         problem('CRITICAL', 'FAILED', "$f ($last2 $last)");
165 };
166
167 my $msg = join("; ", map {"$_: ".join(', ', @{$MESSAGE{$_}}) } (sort {$a cmp $b} keys %MESSAGE));
168 print $EXITCODE, ": ", $msg, "\n";
169 exit $CODE{$EXITCODE};