check_amanda.pl
changeset 0 2098311fb227
child 1 0990f72db33c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/check_amanda.pl	Tue May 17 10:12:33 2011 +0200
@@ -0,0 +1,197 @@
+#! /usr/bin/perl
+
+#   Copyright (C) 2011  Christian Arnold
+#
+#   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 this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+#   Christian Arnold <arnold@schlittermann.de>
+
+use 5.010;
+use strict;
+use warnings;
+use File::Basename qw(basename);
+use Pod::Usage;
+use Getopt::Long;
+use Date::Manip;
+use Date::Calc qw(Delta_Days);
+
+delete @ENV{ grep /^LC_/ => keys %ENV };
+$ENV{LANG}   = "C";
+$ENV{LC_ALL} = "C";
+
+sub version($$);
+sub check_status($$);
+sub report($$);
+
+my %ERRORS = (
+    OK        => 0,
+    WARNING   => 1,
+    CRITICAL  => 2,
+    UNKNOWN   => 3,
+    DEPENDENT => 4
+);
+
+my $ME      = basename $0;
+my $NAME    = 'AMANDA';
+my $VERSION = '1.2';
+
+my %opt = (
+    configfile => 'DailySet1',
+    time       => '1day',
+    binary     => '/usr/sbin/amstatus'
+);
+
+MAIN: {
+    Getopt::Long::Configure('no_ignore_case_always');
+    GetOptions(
+        "C|configfile=s" => \$opt{configfile},
+        "t|time=s"       => \$opt{time},
+        "b|binary=s"     => \$opt{binary},
+        "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
+        "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
+        "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
+    ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
+
+    unless ( -x $opt{binary} ) {
+        say "$NAME CRITICAL: $opt{binary} - not found or not executable";
+        exit $ERRORS{CRITICAL};
+    }
+
+    my ( $rc, $date ) = check_status( $opt{configfile}, $opt{time} );
+    report( $rc, $date );
+
+}
+
+sub check_status($$) {
+    my ( $configfile, $time ) = @_;
+    my $last_backup_date;
+    my $mesg = undef;
+    my $rc   = "CRITICAL";
+
+    foreach (`$opt{binary} --config $configfile --error`) {
+        /^Using.*from\s+(.*)$/ and $last_backup_date = $1 and next;
+        /^\S+/ and $mesg = "some failed backups" and last;
+    }
+
+    # report critical status at any errors
+    if ($mesg) {
+        return ( $rc, $mesg );
+    }
+
+    # check the backup time state
+    if ($last_backup_date) {
+        my $now              = localtime();
+        my @last_backup_date = UnixDate( $last_backup_date, "%Y", "%m", "%d" );
+        my $max_backup_date  = DateCalc( $last_backup_date, "+ $time" );
+        my @max_backup_date  = UnixDate( $max_backup_date, "%Y", "%m", "%d" );
+        my @now              = UnixDate( $now, "%Y", "%m", "%d" );
+        my $allowed_difference_in_days =
+          Delta_Days( @last_backup_date, @max_backup_date );
+        my $difference_in_days = Delta_Days( @last_backup_date, @now );
+        $mesg = $last_backup_date;
+
+        $rc =
+          ( $allowed_difference_in_days > $difference_in_days )
+          ? "OK"
+          : "CRITICAL";
+    }
+    return ( $rc, $mesg );
+}
+
+sub report($$) {
+    my ( $rc, $mesg ) = @_;
+    say "$NAME $rc: Backup ERROR $opt{configfile} - $mesg"
+      and exit $ERRORS{CRITICAL}
+      if ( $rc eq "CRITICAL" );
+    say "$NAME $rc: Backup OK $opt{configfile} - $mesg";
+    exit $ERRORS{OK};
+}
+
+sub version($$) {
+    my ( $progname, $version ) = @_;
+    say <<_VERSION;
+$progname version $version
+Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
+
+$ME comes with ABSOLUTELY NO WARRANTY. This is free software,
+and you are welcome to redistribute it under certain conditions.
+See the GNU General Public Licence for details.
+_VERSION
+}
+
+__END__
+
+=head1 NAME
+
+check_amanda - nagios plugin to checks the last amanda backup status
+
+=head1 SYNOPSIS
+
+check_amanda [B<-C>|B<--configfile> string] [B<-t>|B<--time> string] [B<-b>|B<--binary> string]
+
+check_amanda [B<-h>|B<--help>]
+
+check_amanda [B<-m>|B<--man>]
+
+check_amanda [B<-V>|B<--version>]
+
+=head1 OPTIONS
+
+=over
+
+=item B<-C>|B<--configfile> I<string>
+
+The name of your amanda configuration (default: DailySet1)
+
+=item B<-t>|B<--time> I<string>
+
+The maximum time difference from the current time (default: 1day)
+
+=item B<-b>|B<--binary> I<string>
+
+Path to amstatus binary (default: /usr/sbin/amstatus)
+
+=item B<-h>|B<--help>
+
+Print detailed help screen.
+
+=item B<-m>|B<--man>
+
+Print manual page.
+
+=item B<-V>|B<--version>
+
+Print version information.
+
+=back
+
+=head1 DESCRIPTION
+
+This nagios plugin I<search> on a specified I<url> for a version string and compare the I<search> result with an installed I<package> version.
+
+=head1 VERSION
+
+This man page is current for version 1.2 of B<check_amanda>.
+
+=head1 AUTHOR
+
+Written by Christian Arnold L<arnold@schlittermann.de>
+
+=head1 COPYRIGHT
+
+Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.  This is free software, and you are welcome to
+redistribute it under certain conditions.
+See the GNU General Public Licence for details.
+
+=cut