check_ldap_repl.pl
changeset 3 1be7213b89b1
parent 1 36f66a013838
child 4 fae19ea0292f
equal deleted inserted replaced
2:8ec2ac83a94c 3:1be7213b89b1
    41 	DEPENDENT => 4
    41 	DEPENDENT => 4
    42 );
    42 );
    43 
    43 
    44 my $ME      = basename $0;
    44 my $ME      = basename $0;
    45 my $NAME    = "LDAPREPL";
    45 my $NAME    = "LDAPREPL";
    46 my $VERSION = "0.2";
    46 my $VERSION = "0.3";
       
    47 
       
    48 my $master_default = "ldap://ldap-master:389/";
       
    49 my $slave_default = "ldap://ldap-slave:389/";
       
    50 my $cn_default = "replcheck";
    47 
    51 
    48 my %opt = (
    52 my %opt = (
    49 	init	=> 0,
    53 	init	=> 0,
    50 	delete  => 0,
    54 	delete  => 0,
    51 	refresh => 0,
    55 	refresh => 0,
    52 	cn      => "replcheck",
    56 	cn      => $cn_default,
    53 	wait    => 1,
    57 	wait    => 1,
    54 	file    => "/etc/nagios/ius/plugins/config/check_ldap_repl.cfg",
    58 	file    => "/etc/nagios/ius/plugins/config/check_ldap_repl.cfg",
    55 	master	=> "ldap://ldap-master:389/",
    59 	master	=> $master_default,
    56 	slave	=> "ldap://ldap-slave:389/"
    60 	slave	=> $slave_default
    57 );
    61 );
    58 
    62 
    59 MAIN: {
    63 MAIN: {
    60 	Getopt::Long::Configure('bundling');
    64 	Getopt::Long::Configure('bundling');
    61 	GetOptions(
    65 	GetOptions(
    72 		"h|help"       => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
    76 		"h|help"       => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
    73 		"m|man"        => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
    77 		"m|man"        => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
    74 		"V|version"    => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
    78 		"V|version"    => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
    75 	) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
    79 	) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
    76 
    80 
       
    81 	# init or delete the ldap object
    77 	if ($opt{init}) {
    82 	if ($opt{init}) {
    78 		ldap_object("init");
    83 		ldap_object("init");
    79 		print "new object successfully initialized\n";
    84 		print "new object successfully initialized\n";
    80 		exit $ERRORS{OK};
    85 		exit $ERRORS{OK};
    81 	} elsif ($opt{delete}) {
    86 	} elsif ($opt{delete}) {
    82 		ldap_object("delete");
    87 		ldap_object("delete");
    83 		print "object successfully deleted\n";
    88 		print "object successfully deleted\n";
    84 		exit $ERRORS{OK};
    89 		exit $ERRORS{OK};
    85 	}
    90 	}
    86 
    91 
       
    92 	# refresh our ldap object
    87 	ldap_object("refresh") if ($opt{refresh});
    93 	ldap_object("refresh") if ($opt{refresh});
    88 
    94 
    89 	my ($master, $slave, $cn) = undef;
    95 	my ($master, $slave, $cn) = undef;
    90 	my @slaves = ();
    96 	my @slaves = ();
    91 	my %results = ();
    97 	my %results = ();
    92 
    98 
    93 	if (-r $opt{file}) {
    99 	# preparing for the comparison of ldap entries
    94 		(undef, undef, $master, $slave, $cn) = read_config();
   100     if (($opt{master} ne $master_default) || ($opt{slave} ne $slave_default) || ($opt{cn} ne $cn_default)) {
    95 		@slaves = split(/,/, $slave);
   101         $master = $opt{master};
    96 	} else {
   102         @slaves = split(/,/, $opt{slave});
    97 		$master = $opt{master};
   103         $cn = $opt{cn};
    98 		@slaves = split(/,/, $opt{slave});
   104     } elsif (-r $opt{file}) {
    99 		$cn = $opt{cn};
   105         (undef, undef, $master, $slave, $cn) = read_config();
   100 	}
   106         @slaves = split(/,/, $slave);
   101 
   107     } else {
       
   108         $master = $opt{master};
       
   109         @slaves = split(/,/, $opt{slave});
       
   110         $cn = $opt{cn};
       
   111     }
       
   112 
       
   113 	# get the values from the ldap
   102 	$results{$master}{'master'} = get_stamp($master, $cn);
   114 	$results{$master}{'master'} = get_stamp($master, $cn);
   103 	sleep $opt{wait};
   115 	sleep $opt{wait};
   104 	foreach (@slaves) {
   116 	foreach (@slaves) {
   105 		$results{$_}{'slave'} = get_stamp($_, $cn);
   117 		$results{$_}{'slave'} = get_stamp($_, $cn);
   106 	}
   118 	}
   107 
   119 
       
   120 	# compare the time stamps and generate the output
   108 	compare_results(\%results);
   121 	compare_results(\%results);
   109 }
   122 }
   110 
   123 
   111 sub compare_results(%) {
   124 sub compare_results(%) {
   112 	my @output = ();
   125 	my @output = ();
   146 	my ($binddn, $password) = undef;
   159 	my ($binddn, $password) = undef;
   147 
   160 
   148 	my $master = $opt{master};
   161 	my $master = $opt{master};
   149 	my $cn = $opt{cn};
   162 	my $cn = $opt{cn};
   150 
   163 
       
   164 	# ldap object init/delete is only allowed at the prompt
   151 	if ( ($type eq "init") || ($type eq "delete") ) {
   165 	if ( ($type eq "init") || ($type eq "delete") ) {
   152 		$binddn = prompt('BindDN: ');
   166 		$binddn = prompt('BindDN: ');
   153 		$password = prompt('Password: ', -e => '*');
   167 		$password = prompt('Password: ', -e => '*');
   154 		if (-r $opt{file}) {
   168 		if (($opt{master} ne $master_default) || ($opt{cn} ne $cn_default)) {
   155 			(undef, undef, $master, undef, undef) = read_config();
   169 			$master = $opt{master};
       
   170 			$cn = $opt{cn};
       
   171 		} elsif ( -r $opt{file} ) {
       
   172 			(undef, undef, $master, undef, $cn) = read_config();
   156 		}
   173 		}
   157 	} else {
   174 	} else {
   158 		if ($opt{binddn} && $opt{password}) {
   175 		if ($opt{binddn} && $opt{password}) {
   159 			$binddn = $opt{binddn};
   176 			$binddn = $opt{binddn};
   160 			$password = $opt{password};
   177 			$password = $opt{password};
   161 		} elsif (-r $opt{file}) {
       
   162 			($binddn, $password, $master, undef, $cn) = read_config();
       
   163 		} else {
   178 		} else {
   164 			$binddn = prompt('BindDN: ');
   179 			$binddn = prompt('BindDN: ');
   165 			$password = prompt('Password: ', -e => '*');
   180 			$password = prompt('Password: ', -e => '*');
   166 		}
   181 		}
       
   182 
       
   183 		if (($opt{master} ne $master_default) || ($opt{cn} ne $cn_default)) {
       
   184 			$master = $opt{master};
       
   185 			$cn = $opt{cn};
       
   186 		} elsif ( -r $opt{file} ) {
       
   187 			(undef, undef, $master, undef, $cn) = read_config();
       
   188 		}
   167 	}
   189 	}
   168 	
   190 	
   169 	my $ldap = Net::LDAP->new( $master ) or die "$@";
   191 	my $ldap = Net::LDAP->new( $master );
       
   192 
       
   193 	if (!$ldap) {
       
   194 		print "$NAME CRITICAL: [$master] $!\n";
       
   195 		exit $ERRORS{CRITICAL};
       
   196 	}
   170 
   197 
   171 	my $mesg = $ldap->bind("$binddn", password => $password);
   198 	my $mesg = $ldap->bind("$binddn", password => $password);
   172 	if ($mesg->code) {
   199 	if ($mesg->code) {
   173 		$ldap->unbind() if ($ldap);
   200 		$ldap->unbind() if ($ldap);
   174 		print "$NAME CRITICAL: " . $mesg->error . "\n";
   201 		print "$NAME CRITICAL: " . $mesg->error . "\n";
   202 
   229 
   203 	# delete check object
   230 	# delete check object
   204 	$mesg = $ldap->delete("cn=$cn,$context") if ($type eq "delete");
   231 	$mesg = $ldap->delete("cn=$cn,$context") if ($type eq "delete");
   205 
   232 
   206 	if ($mesg->code && ($type eq "delete" || $type eq "init")) {
   233 	if ($mesg->code && ($type eq "delete" || $type eq "init")) {
   207 		print "$NAME CRITICAL: " . $mesg->error . "\n";
   234 		print "$NAME WARNING: [ldapt] " . $mesg->error . "\n";
   208 		exit $ERRORS{CRITICAL};
   235 		exit $ERRORS{WARNING};
   209 	}
   236 	}
   210 
   237 
   211 	# refresh check object
   238 	# refresh check object
   212 	$mesg = $ldap->modify(
   239 	$mesg = $ldap->modify(
   213 		"cn=$cn,$context",
   240 		"cn=$cn,$context",
   220 	return 0;
   247 	return 0;
   221 }
   248 }
   222 
   249 
   223 sub get_stamp($$) {
   250 sub get_stamp($$) {
   224 	my ($server, $cn) = @_;
   251 	my ($server, $cn) = @_;
   225 	my $ldap = Net::LDAP->new( $server ) or die "$@";
   252 
       
   253 	my $ldap = Net::LDAP->new( $server );
       
   254 	if (!$ldap) {
       
   255 		print "$NAME CRITICAL: [$server] $!\n";
       
   256 		exit $ERRORS{CRITICAL};
       
   257 	}
       
   258 
   226 	my $mesg = $ldap->bind();
   259 	my $mesg = $ldap->bind();
   227 
   260 
   228 	if ($mesg->code) {
   261 	if ($mesg->code) {
   229 		$ldap->unbind() if ($ldap);
   262 		$ldap->unbind() if ($ldap);
   230 		print "$NAME CRITICAL: " . $mesg->error . "\n";
   263 		print "$NAME CRITICAL: " . $mesg->error . "\n";
   306 
   339 
   307 =item B<-i>|B<--init>
   340 =item B<-i>|B<--init>
   308 
   341 
   309 Add the check object cn=replcheck,I<namingContext> to the master server if not exists. See also the B<--cn> option.
   342 Add the check object cn=replcheck,I<namingContext> to the master server if not exists. See also the B<--cn> option.
   310 You will ask for an B<binddn> and B<password>, if not given B<--binddn> and B<--password> options.
   343 You will ask for an B<binddn> and B<password>, if not given B<--binddn> and B<--password> options.
   311 Your B<binddn> must have write permission to the ldap master server.
   344 LDAP object initialisation is only allowed at the prompt. Your B<binddn> must have write permission to the ldap master server.
   312 
   345 
   313 =item B<-d>|B<--delete>
   346 =item B<-d>|B<--delete>
   314 
   347 
   315 Delete the check object from the ldap master server if exists. See also the B<--cn> option.
   348 Delete the check object from the ldap master server if exists. See also the B<--cn> option.
   316 You will ask for an B<binddn> and B<password>, if not given B<--binddn> and B<--password> options.
   349 You will ask for an B<binddn> and B<password>, if not given B<--binddn> and B<--password> options.
   317 Your B<binddn> must have write permission to the ldap master server.
   350 LDAP object deletion is only allowed at the prompt. Your B<binddn> must have write permission to the ldap master server.
   318 
   351 
   319 =item B<-r>|B<--refresh>
   352 =item B<-r>|B<--refresh>
   320 
   353 
   321 Refresh the stamp attribute of the check attribute with current unix time.
   354 Refresh the stamp attribute of the check attribute with current unix time.
   322 You will ask for an B<binddn> and B<password>, if not given B<--binddn> and B<--password> options. See also B<--file> option.
   355 You will ask for an B<binddn> and B<password>, if not given B<--binddn> and B<--password> options. See also B<--file> option.
   382 
   415 
   383 This plugin checks if the ldap replication works correctly.
   416 This plugin checks if the ldap replication works correctly.
   384 
   417 
   385 =head1 VERSION
   418 =head1 VERSION
   386 
   419 
   387 This man page is current for version 0.2 of B<check_ldap_repl>.
   420 This man page is current for version 0.3 of B<check_ldap_repl>.
   388 
   421 
   389 =head1 AUTHOR
   422 =head1 AUTHOR
   390 
   423 
   391 Written by Christian Arnold L<arnold@schlittermann.de>
   424 Written by Christian Arnold L<arnold@schlittermann.de>
   392 
   425