plugins/check_amanda-client
changeset 21 2247be0e2a13
parent 20 92818898bb2c
child 23 6c2728f0c6f7
--- a/plugins/check_amanda-client	Thu Jan 23 23:31:08 2014 +0100
+++ b/plugins/check_amanda-client	Sat Jan 25 23:08:21 2014 +0100
@@ -1,7 +1,7 @@
 #! /usr/bin/perl
 # source: https://ssl.schlittermann.de/hg/ius/nagios/nagios-plugin-amanda-client
 
-use 5.010;
+use 5.014;
 use strict;
 use warnings;
 use Getopt::Long;
@@ -13,8 +13,6 @@
 use Pod::Usage;
 use Const::Fast;
 
-our $VERSION = '0.0.2';
-
 const my $NAME  => 'AMANDA-CLIENT';
 const my $USER  => 'backup';
 const my $CFDIR => '/etc/amanda';
@@ -30,28 +28,30 @@
 
 sub main;
 
-sub ok;
-sub warning;
-sub critical;
-sub unknown;
+sub OK;
+sub WARNING;
+sub CRITICAL;
+sub UNKNOWN;
 sub verbose;
 sub unique { my %h; @h{@_} = (); keys %h }
 
-$SIG{__DIE__} = sub { unknown @_ unless $^S };
+$SIG{__DIE__} = sub { UNKNOWN @_ unless $^S };
 
-exit main @ARGV;
+exit main @ARGV if not caller;
 
 #----
 
 sub main {
-    my @opt_ignore;
+    my @opt_exclude;
+    my @opt_include;
     my $opt_verbose = 0;
 
     GetOptions(
-        'i|ignore=s@' => \@opt_ignore,
-        'h|help'      => sub { pod2usage(-verbose => 1, -exit => 0) },
-        'm|man'       => sub { pod2usage(-verbose => 2, -exit => 0) },
-        'v|verbose'   => \$opt_verbose,
+        'e|x|exclude=s@' => \@opt_exclude,
+        'i|include=s@'   => \@opt_include,
+        'h|help'         => sub { pod2usage(-verbose => 1, -exit => 0) },
+        'm|man'          => sub { pod2usage(-verbose => 2, -exit => 0) },
+        'v|verbose'      => \$opt_verbose,
     ) or pod2usage;
 
     *::verbose = $opt_verbose ? sub { say '# ', @_ } : sub { };
@@ -64,7 +64,7 @@
     # by the backup user/group
     verbose q{checking permissions for `amservice'};
     eval { check_perms find_tool('amservice'), 04750, 'root', $) }
-      or unknown $@;
+      or UNKNOWN $@;
 
     # find the backup sets we know about
     # here we suppose that it's possible to find strings like
@@ -72,13 +72,18 @@
 
     verbose qq{find config names from $CFDIR};
     my @confs = sort +unique eval { config_names $CFDIR }
-      or unknown $@;
+      or UNKNOWN $@;
 
-    eval { amchecks @confs } or critical $@;
+    eval { amchecks @confs } or CRITICAL $@;
 
-    my @dles = eval { compare_lists confs => \@confs, ignore => \@opt_ignore }
-      or critical $@;
-    ok 'config: ' . join(', ', @confs), @dles;
+    my @dles = eval {
+        compare_lists
+          confs   => \@confs,
+          exclude => \@opt_exclude,
+          include => \@opt_include;
+    }
+      or CRITICAL $@;
+    OK 'config: ' . join(', ', @confs), @dles;
 
     # never reached
     return 0;
@@ -154,7 +159,8 @@
     find(
         sub {
             -f and /^amanda-client\.conf$/ or return;
-            open(my $fh, '<', $_) or die "Can't open  $File::Find::name: $!\n";
+            open(my $fh, '<', $_)
+              or die "Can't open  $File::Find::name: $!\n";
             push @configs, map { /^conf\s+"(.+?)"/ ? $1 : () } <$fh>;
         },
         $dir
@@ -212,27 +218,30 @@
 }
 
 sub compare_lists {
-    my %arg    = @_;
-    my @confs  = @{ $arg{confs} } or croak 'missing list of confs';
-    my @ignore = @{ $arg{ignore} };
+    my %arg     = @_;
+    my @confs   = @{ $arg{confs} } or croak 'missing list of confs';
+    my @exclude = @{ $arg{exclude} };
+    my @include = @{ $arg{include} };
 
-    warning
-      "ignored filesystem(s) @$_ does not exist, update the config please!\n"
-      if @ignore and @$_ = grep { not -e } unique map { /^.*?:(.*)/ } @ignore;
+    WARNING
+      "excluded filesystem(s) @$_ does not exist, update the config please!\n"
+      if @exclude
+          and @$_ = grep { not -e } unique map { /^.*?:(.*)/ } @exclude;
 
-    my @candidates = get_devices;
+    my @candidates = (get_devices, @include);
     my %missing;
 
     foreach my $conf (@confs) {
         my @dles = _amlist $conf;
         foreach my $candidate (@candidates) {
+            next if not $candidate =~ m{^/|\Q$conf\E:};
 
             # we're satisfied if either the name of the device is in
             # the disklist, or the device id is found
             $candidate->[0] ~~ [map { $_->[0] } @dles] and next;
             $candidate->[1] ~~ [map { $_->[1] } @dles] and next;
             push @{ $missing{$conf} }, $candidate->[0]
-              if not "$conf:$candidate->[0]" ~~ @ignore;
+              if not "$conf:$candidate->[0]" ~~ @exclude;
         }
     }
     die map { "$_ missing: " . join(', ' => @{ $missing{$_} }) . "\n" }
@@ -242,10 +251,12 @@
     return map { $_->[0] } @candidates;
 }
 
-sub ok { say "$NAME OK\n", join "\n" => @_; exit 0 }
-sub warning  { print "$NAME WARNING\n",  join "\n" => @_; exit 1 }
-sub critical { print "$NAME CRITICAL\n", join "\n" => @_; exit 2 }
-sub unknown  { print "$NAME UNKNOWN\n",  join "\n" => @_; exit 3 }
+sub OK { say "$NAME OK\n", join "\n" => @_; exit 0 }
+sub WARNING  { print "$NAME WARNING\n",  join "\n" => @_; exit 1 }
+sub CRITICAL { print "$NAME CRITICAL\n", join "\n" => @_; exit 2 }
+sub UNKNOWN  { print "$NAME UNKNOWN\n",  join "\n" => @_; exit 3 }
+
+1;
 
 __END__
 
@@ -268,11 +279,23 @@
 
 =over
 
-=item B<-i>|B<--ignore> I<config:filesystem>
+=item B<-x>|B<--exclude> [I<config:>]I<filesystem>
+
+The name of a filesystem to be excluded. 
+No config means all configs.
+
+    check_amanda-client --exclude weekly:/var/spool/squid --exclude /boot
+
 
-The name of a filesystem to be ignored. Example:
+=item B<-i>|B<--include> [I<config>:]I<filesystem>
+
+Which filesystems/directories we additionally want to check.
+No config means all configs.
 
-    check_amanda-client --ignore weekly:/var/spool/squid --ignore daily:/boot
+    check_amanda-client --include weekly:/etc
+
+If the directory is contained already in another directory/filesystem,
+the test is satisfied.
 
 =item B<-h>|B<--help>