#! /usr/bin/perl -w
use strict;
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_w $opt_c $opt_d $opt_H $opt_sr $opt_cr $opt_srp $opt_crp $opt_SD $opt_CD $opt_SP $opt_CP $opt_t $PROGNAME);
use lib "nagios/plugins/" ;
use utils qw(%ERRORS &print_revision &support &usage);

$PROGNAME = "check_ldap_repl.pl";
my $state = "UNKNOWN";
my $insync = "/var/Sun/mps/shared/bin/insync";
sub print_help ();
sub print_usage ();

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';
$ENV{'LD_LIBRARY_PATH'}='/var/Sun/mps/lib';
my ($opt_V,$opt_h,$opt_w,$opt_c,$opt_H,$opt_S,$opt_p,$opt_P,$opt_d,$opt_D,$opt_x,$opt_X);
Getopt::Long::Configure('bundling');
GetOptions
    ("V"   => \$opt_V, "version"    => \$opt_V,
     "h"   => \$opt_h, "help"       => \$opt_h,
     "w=s" => \$opt_w, "warn=s"  => \$opt_w,  # 30
     "c=s" => \$opt_c, "critical=s" => \$opt_c, # 40
     "H=s" => \$opt_H, "hostname=s" => \$opt_H,
     "S=s" => \$opt_S, "supplier=s" => \$opt_S,
     "p=s" => \$opt_p, "support=s" => \$opt_p,
     "P=s" => \$opt_P, "conport=s" => \$opt_P,
     "d=s" => \$opt_d, "supbinddn=s" => \$opt_d,
     "D=s" => \$opt_D, "conbinddn=s" => \$opt_D,
     "x=s" => \$opt_x, "supbindpass=s" => \$opt_x,
     "X=s" => \$opt_X, "conbindpass=s" => \$opt_X);

if ($opt_V) {
    print_revision($PROGNAME,'$Revision: 0.5 $');
    exit $ERRORS{$state};
}

if ($opt_h) {print_help(); exit $ERRORS{$state};}

($opt_H) || usage("Host name/address not specified\n");
($opt_S) || usage("Supplier Host name/address not specified\n");
($opt_x) || usage("Supplier password not specified\n");
($opt_X) || usage("Consumer password not specified\n");
($opt_w) || usage("Warning threshold not specified\n");
($opt_c) || usage("Critical threshold not specified\n");

($opt_p) || ($opt_p = "389") ;
($opt_P) || ($opt_P = "389") ;
($opt_d) || ($opt_d = "Directory Manager") ;
($opt_D) || ($opt_D = "Directory Manager") ;

#################################################################################################################
# Command													#
#insync -s "cn=directory manager:password@216.54.168.55:389" -c "cn=directory manager:password@10.25.1.212:389"	#
#################################################################################################################
my $cmd = $insync." -s \"cn=".$opt_d.":".$opt_x."\@".$opt_S.":".$opt_p."\" -c \"cn=".$opt_D.":".$opt_X."\@".$opt_H.":".$opt_P."\"";
my $sig = `$cmd` || exit $ERRORS{$state};

#################################################################
# Out put							#
#ReplicaDn Consumer          Supplier          Delay		#
#o=na      10.25.1.212:389   216.54.168.56:389 0		#
#################################################################
my ($line1,$line2) = split (/\n/,$sig);
chomp ($line2);
my @values = split(' ',$line2);
my $delay = $values[3];
my $perfdata="";
for (my $i=0; $i<=$#values; $i++)
{
	if ( $i > 3 )
	{
		chomp ($perfdata);
		$perfdata = join (' ', $perfdata,$values[$i]);
	}
}

if ( $delay < 0 ) { # If Delay is -ve then replication is not initiallized
    $state = "CRITICAL";
}

elsif ( $delay >= $opt_c ) {  # CRIT if result is higher then crit
    $state = "CRITICAL";
}
    
elsif ( $delay >= $opt_w ) {  # WARN if result is higher then warn
    $state = "WARNING";
}

elsif ( $delay < $opt_w ) {  # OK if lower then warn
    $state = "OK";
}

else {
    $state = "UNKNOWN";  # if all else fails, toss UNKNOWN
}

print "Delay : $delay : $state | $perfdata\n" ;

# print correct message

#print "Entropy Available". ": " . $result . ": " . $state . "\n";
#print "Delay". ": " . $state . ": " . $result . "\n";

exit $ERRORS{$state};


sub print_usage () {
    print "Usage: $PROGNAME -H <consumer/host> -S <supplier host> -p <supplier port> -P <consumer port> -d <supplier bind DN> -D <consumer bind Dn> -x <supplier password> -X <consumer password> -w <warn> -c <crit>\n";
}

sub print_help () {
    print_revision($PROGNAME,'$Revision: 0.5 $');
    print "Copyright (c) 2008 Pravin Rane

Licence : GPL - http://www.fsf.org/licenses/gpl.txt

This plugin checks the replication status between given consumer and supplier

";
    print_usage();
    print "
-H, --hostname=HOST
   Name or IP address of host to check
-w, --warn=WARN
   Replication delay in seconds at which a WARNING status will result
-c, --critical=CRIT
   Replication delay in seconds at which a CRITICAL status will result
-S --supplier=SUPPLIER HOST
  Name or IP address of supplier to check
-p --support=SUPLIER PORT
   Supplier port number (default 389)
-P --conport=CONSUMER PORT
   Consumer port number (default 389)
-d --supbinddn=BIND DN
   Supplier bind DN (default \"Directory Manager\")
-D --conbinddn=BIND DN
   Consumer bind DN (default \"Directory Manager\")
-x --supbindpass=SUPPLIER BIND PASSWORD
   Supplier bind password
-X --conbindpass=CONSUMER BIND PASSWORD
   Consumer bind password
";
    support();
}
