#!/usr/bin/perl
#
use strict;
use Time::localtime;
$ENV{'PATH'} = '/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/oracle/bin';
$ENV('LANG') = 'en_US.UTF-8';
$ENV('USER') = 'oracle';
#
# Input Variables
#
if ( $#ARGV < 1 ) { die "Usage: sync_check.pl \ \"; };
my $lag = $ARGV[0];
my $log_dir = $ARGV[1];
$log_dir =~ s/\/$//; # Get rid of trailing '/' if it's there
#
# Environment variables
#
my $orahome = $ENV{"ORACLE_HOME"}; # Gets ORACLE_HOME
my $hostname = `hostname`; # Runs the OS hostname command.
#
# Open today's log file.
#
my $log_date = `date +%m%d%y`; # system date in mmddyy format
my $log = "$log_dir/sync_check.$log_date"; # in prod use "/usr/logs/sync_check.$log_date";
open (LOG,">>$log") || die "Can't open $log on $hostname\n";
#
# General use variables.
#
my $subject = "AD -> OID sync on $hostname";
my $success = 0;
my $failure = 1;
my $timenow = `date +%H:%M:%S`;
chomp $timenow;
my $mail_text = "WARNING: ";
#
# Write a startup message to the log
#
print LOG "***********************************************************\n";
print LOG $timenow . " - Starting checks of " . $subject;
#
# Look for the odisrv process. Send mail and quit if it's down.
#
my $odiproc = `ps -ef|grep odisrv|wc -l`;
if ( $odiproc == 0 )
{ $mail_text = $mail_text . " odisrv process NOT running.";
print LOG "$timenow - $mail_text\n";
system("echo $mail_text | mail -s \"$subject\" bierman\@firsttrust.com" );
exit $success;
};
#
# Get the time from the logs. It's in yyyymmddhh24miss format.
#
my $odi_logs = $orahome . "/ldap/odi/log";
my $last_user_sync = `grep orclodipLastSuccessfulExecutionTime: $odi_logs/ActiveChgImpUsers.trc | tail -1 `;
chomp $last_user_sync;
$last_user_sync = substr($last_user_sync,37,100);
print LOG "$timenow - Last user sync at $last_user_sync\n";
#
my $last_group_sync = `grep orclodipLastSuccessfulExecutionTime: $odi_logs/ActiveChgImpGroups.trc | tail -1 `;
chomp $last_group_sync;
$last_group_sync = substr($last_group_sync,37,100);
print LOG "$timenow - Last group sync at $last_group_sync\n";
#
# Get the date/time $lag minutes ago yyyymmddhh24miss format.
# time() gets time in seconds since some start point.
# localtime() breaks the date in seconds into year, month, day, ...
#
my $sec;
my $min;
my $hour;
my $mday;
my $mon;
my $year;
my $wday;
my $yday;
my $isdst;

( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
localtime( time() - $lag * 60 );
$year += 1900; # localtime returns number of years since 1900
$mon += 1; # localtime months are Jan = 0 ... Dec = 11
#
# Format date string and lpad with 0 if needed
#
my $start_ymd = sprintf( "%4d%02d%02d%02d%02d%02d", $year, $mon, $mday, $hour, $min, $sec );
#
# Compare the log date to the start date.
# We have problems if it's more than $lag minutes ago
# Write an entry to the log and send email
#
my $send_mail = 0;
if ( $last_user_sync < $start_ymd )
{ $mail_text = $mail_text . "Last user sync more than $lag minutes ago at $last_user_sync. ";
$send_mail = 1;
}
#
if ( $last_group_sync < $start_ymd )
{ $mail_text = $mail_text . "Last group sync more than $lag minutes ago at $last_group_sync. ";
$send_mail = 1;
}
#
if ( $send_mail )
{
print LOG "$timenow - $mail_text\n";
# system("echo $mail_text | mail -s \"$subject\" bierman\@firsttrust.com" );
}
close (LOG);
exit $success;