check_ldap_repl.pl
changeset 13 ce6d09692989
parent 12 7202e55a0713
child 14 bd26976ed704
equal deleted inserted replaced
12:7202e55a0713 13:ce6d09692989
    33 use File::stat;
    33 use File::stat;
    34 use Pod::Usage;
    34 use Pod::Usage;
    35 use if $ENV{DEBUG} => "Smart::Comments";
    35 use if $ENV{DEBUG} => "Smart::Comments";
    36 
    36 
    37 my %ERRORS = (
    37 my %ERRORS = (
    38 	OK        => 0,
    38     OK        => 0,
    39 	WARNING   => 1,
    39     WARNING   => 1,
    40 	CRITICAL  => 2,
    40     CRITICAL  => 2,
    41 	UNKNOWN   => 3,
    41     UNKNOWN   => 3,
    42 	DEPENDENT => 4
    42     DEPENDENT => 4
    43 );
    43 );
    44 
    44 
    45 my $ME      = basename $0;
    45 my $ME      = basename $0;
    46 my $NAME    = "LDAPREPL";
    46 my $NAME    = "LDAPREPL";
    47 my $VERSION = "0.3.3";
    47 my $VERSION = "0.3.3";
    64 $SIG{__DIE__} = sub { print STDERR "$NAME UNKNOWN: ", @_; exit $ERRORS{UNKNOWN}; };
    64 $SIG{__DIE__} = sub { print STDERR "$NAME UNKNOWN: ", @_; exit $ERRORS{UNKNOWN}; };
    65 
    65 
    66 sub stamp {
    66 sub stamp {
    67     my ($u, $dn, $attr) = @_;
    67     my ($u, $dn, $attr) = @_;
    68 
    68 
    69     my $l = ref $u eq 'Net::LDAP' ? $u : Net::LDAP->new($u, onerror => 'die') or die "$@";
    69     my $l = ref $u eq 'Net::LDAP' ? $u : Net::LDAP->new($u, onerror => 'die')
       
    70       or die "$@";
    70     my $r = $l->search(base => $dn, scope => 'base', filter => '(objectClass=*)');
    71     my $r = $l->search(base => $dn, scope => 'base', filter => '(objectClass=*)');
    71     die "unexpected result count: ", $r->count unless $r->count == 1;
    72     die "unexpected result count: ", $r->count unless $r->count == 1;
    72     my @v = $r->entry(0)->get_value($attr);
    73     my @v = $r->entry(0)->get_value($attr);
    73     die "unexpected value count [@v]" unless @v == 1;
    74     die "unexpected value count [@v]" unless @v == 1;
    74     return $v[0];
    75     return $v[0];
    75 
    76 
    76 }
    77 }
    77 
    78 
    78 sub version {
    79 sub version {
    79 	my ( $progname, $version ) = @_;
    80     my ($progname, $version) = @_;
    80 	
    81 
    81 	print <<_VERSION;
    82     print <<_VERSION;
    82 $progname version $version
    83 $progname version $version
    83 Copyright (C) 2012 by Christian Arnold and Schlittermann internet & unix support.
    84 Copyright (C) 2012 by Christian Arnold and Schlittermann internet & unix support.
    84 Copyright (C) 2016 by Matthias Förste and Schlittermann internet & unix support.
    85 Copyright (C) 2016 by Matthias Förste and Schlittermann internet & unix support.
    85 
    86 
    86 $ME comes with ABSOLUTELY NO WARRANTY. This is free software,
    87 $ME comes with ABSOLUTELY NO WARRANTY. This is free software,
    89 _VERSION
    90 _VERSION
    90 }
    91 }
    91 
    92 
    92 MAIN: {
    93 MAIN: {
    93 
    94 
    94     my $c = AppConfig->new( { CASE => 1 },
    95     my $c = AppConfig->new(
    95         map { $_, { ref $defaults->{$_} eq 'CODE' ? 'ACTION' : 'DEFAULT' => $defaults->{$_} } } keys %{$defaults}
    96         { CASE => 1 },
       
    97         map {
       
    98             $_,
       
    99               { ref $defaults->{$_} eq 'CODE'
       
   100                 ? 'ACTION'
       
   101                 : 'DEFAULT' => $defaults->{$_} }
       
   102         } keys %{$defaults}
    96     ) or die "Can't initialize";
   103     ) or die "Can't initialize";
    97 
   104 
    98     my $cf = $c->get('config');
   105     my $cf = $c->get('config');
       
   106 
    99     # ignore default configuration file if it does not exist
   107     # ignore default configuration file if it does not exist
   100     $c->file($cf) if -e $cf;
   108     $c->file($cf) if -e $cf;
   101 
   109 
   102     # read configuration file if passed on command line
   110     # read configuration file if passed on command line
   103     $c->getopt(qw(no_ignore_case));
   111     $c->getopt(qw(no_ignore_case));
   104     $c->file($cf) if $cf ne ($cf = $c->get('config'));
   112     $c->file($cf) if $cf ne ($cf = $c->get('config'));
       
   113 
   105     # make sure that command line options override any config file options
   114     # make sure that command line options override any config file options
   106     $c->getopt;
   115     $c->getopt;
   107 
   116 
   108     my %o = $c->varlist('.');
   117     my %o = $c->varlist('.');
   109     my $t = time();
   118     my $t = time();
   110 
   119 
   111     my $p = Net::LDAP->new($o{provider}, onerror => 'die' ) or die $@;
   120     my $p = Net::LDAP->new($o{provider}, onerror => 'die') or die $@;
   112     $p->bind($o{binddn}, password => $o{password});
   121     $p->bind($o{binddn}, password => $o{password});
   113     $p->modify($o{dn}, replace => { $o{attribute} => $t });
   122     $p->modify($o{dn}, replace => { $o{attribute} => $t });
   114 
   123 
   115     my $tp = stamp($p, $o{dn}, $o{attribute});
   124     my $tp = stamp($p, $o{dn}, $o{attribute});
   116     die "Provider update failed for unknown reason\n" unless $tp == $t;
   125     die "Provider update failed for unknown reason\n" unless $tp == $t;
   117     sleep $o{wait};
   126     sleep $o{wait};
   118     for (@{$o{consumer}}) { critical "'$_' out of sync\n" unless $tp == stamp($_, $o{dn}, $o{attribute}); }
   127     for (@{ $o{consumer} }) {
       
   128         critical "'$_' out of sync\n"
       
   129           unless $tp == stamp($_, $o{dn}, $o{attribute});
       
   130     }
   119 
   131 
   120     print "$NAME OK: servers are in sync\n";
   132     print "$NAME OK: servers are in sync\n";
   121     exit $ERRORS{OK};
   133     exit $ERRORS{OK};
   122 
   134 
   123 }
   135 }