#!/proj/ahd01/CAisd/ActivePerl-5.6.0.618/bin/perl
#-------------------------------------------------------------------------------------------
# Script Name:	clientlogoff.pl  		 
# Script Version:  1.0
# Date: 03/12/2002
# Author:  Rebecca Jeyapaulan
# Original Script Owner: Ben Dakos
# Department:  AHD Support
# Description:
#
#	This script zaps any vbop processes that have been on the system for
#	longer than a defined time.  The cutoff time is set in the CUTOFFTIME constant.
#
#	The file vbopzap_cmd_resp.txt is used as a temporary store of the slstat query.
#   (ie. the list of bop_cmd processes found on the system)
#
#
#   The log files for this script are kept in the directory $NX_ROOT/site/mods/scripts/log
#   File: vbopzap-yyyy-mm-dd.log
#
# Revision History:
#
#--------------------------------------------------------------------------------------------

##############################################################################################################

use FileHandle;
use Time::Local;

#############################################
# 	Define the constant values            
#   CUTOFFTIME - set for 10 pm
#############################################

use constant CUTOFFTIME => 22;

#############################################
#   Define the initial variables
#############################################

$LogOpen = "no";

#############################################
#   Get start date/time and write to log
#############################################

($Sec, $Min, $Hr, $Day, $MonthIdx, $Yr, $WeekDayIdx) = localtime(time);

$Yr += 1900;
$Month = $MonthIdx + 1;

# Create Save Date using Day Month Year format

$SaveDate = ("$Day-$MonthList[$MonthIdx]-$Yr");

#############################################
#  Main body of the program
#############################################

# Create DateTime using eg. hh:mm:ss Sun-1-Jan-2002

$DateTime = sprintf("%02d:%02d:%02d ${WeekDayList[$WeekDayIdx]}-$Day-${MonthList[$MonthIdx]}-$Yr", $Hr, $Min, $Sec);

# testing ---> print "$DateTime\n";

write_log("--- Starting the vbop slzap process...\n"); 
print("--- Starting the vbop slzap process...\n");

#############################################
# Commence slzap process
#############################################

write_log("Searching for vbop processes using the slstat command...\n");
print("Searching for vbop processes using the slstat command...\n");

# Run slstat command and write to command response file

$vbop_slstat_cmd_resp1 = `. /home/paradigm/.profile >> /home/paradigm/log/STDOUT-$TestDay-$TestMonth-$TestYear.txt 2>>/home/paradigm/log/STDERR-$TestDay-$TestMonth-$TestYear.txt; /proj/ahd01/CAisd/bin/slstat|grep vbop >/proj/ahd01/CAisd/site/mods/scripts/vbopzap_cmd_resp.txt`;

#############################################
# Check for null slstat response (ie. check if response file is empty)
# Exit program if no vbop processes found
#############################################

unless (open (VBOP_SLSTAT_CMD_RESP, '/proj/ahd01/CAisd/site/mods/scripts/vbopzap_cmd_resp.txt'))
{
	write_log("Error opening slstat command response file for read\n");
	print("Error opening slstat command response file for read\n");
	exit (1);
}

if (-z VBOP_SLSTAT_CMD_RESP)
{
	write_log("No vbop processes were found.\n--- Zap process completed ---\n");
	print "No vbop processes were found.\n--- Zap process completed ---\n";
	exit;
}

#############################################
# Write slstat responses to array variable and remove any trailing return characters
#############################################

read (VBOP_SLSTAT_CMD_RESP, $contents, 2000);
print "The file contains the following :\n $contents \n";

@vbop_cmd_array1 = split("\n", $contents);

chomp(@vbop_cmd_array1);

# Determine the number of elements in the array 

$vbop_number1 = @vbop_cmd_array1;

#############################################
# Write to log the vbop_cmd process(es) that have been found
#############################################

print "$vbop_number1 vbop process(es) found:\n";
write_log("$vbop_number1 vbop process(es) found:\n");

for ($index1=0; $index1 < $vbop_number1; $index1++)
{
	write_log("found...@vbop_cmd_array1[$index1]\n");
	print "found...@vbop_cmd_array1[$index1]\n";	
}

#############################################
# Strip parts of each array instance into further arrays
#############################################

%hash = (Jan, 1, Feb, 2, Mar, 3, Apr, 4, May, 5, Jun, 6, Jul, 7, Aug, 8, Sep, 9, Oct, 10, Nov, 11, Dec, 12);

for ($index1=0; $index1 < $vbop_number1; $index1++)
{
	write_log("$vbop_cmd_array1[$index1]\n");

	@str_array = split(" ",$vbop_cmd_array1[$index1]);

	chomp(@str_array);

	$mnth1 = $str_array[4];
	print "Month: $mnth1\n";

	$day1 = $str_array[5];
	print "Day: $day1\n";

	$time1 = $str_array[6];
	print "Time: $time1\n";

	$mnth2 = $hash{$mnth1};
	print "Month after hash: $mnth2\n";

	$time2 = substr($time1, 0, 2);
	print "Time after sub string: $time2\n";

	if ($Day == 1)
	{
		$Day = $day1 + 1;
	}

	if ($day1 < $Day)
	{
		if ($time2 < CUTOFFTIME)
		{
			# Extract pattern from slstat response

			@pattern1[$index1] = $str_array[0];

			write_log("pattern to be zapped is : $str_array[0] \n");
			print "pattern to be zapped is : $str_array[0] \n";

			$zap_output = `. /home/paradigm/.profile >> /home/paradigm/log/STDOUT-$TestDay-$TestMonth-$TestYear.txt 2>>/home/paradigm/log/STDERR-$TestDay-$TestMonth-$TestYear.txt; /proj/ahd01/CAisd/bin/slzap $str_array[0]`;
			$vbop_slstat_cmd_resp3 = `. /home/paradigm/.profile >> /home/paradigm/log/STDOUT-$TestDay-$TestMonth-$TestYear.txt 2>>/home/paradigm/log/STDERR-$TestDay-$TestMonth-$TestYear.txt; /proj/ahd01/CAisd/bin/slstat|grep vbop`;
			
			# check if the zap was successful
			if ($vbop_slstat_cmd_resp3 =~ /$str_array[0]/)
			{
				write_log("Unable to zap $pattern1[$index1]\n");
			}
			else
			{
				write_log("$pattern1[$index1] zapped successfully\n");
			}
		}
		else
		{
			# Extract pattern from slstat response

			@pattern1[$index1] = $str_array[0];

			write_log("Client : $str_array[0] can not be zapped as the logon time is after 10 PM \n");
			print "Client : $str_array[0] can not be zapped as the logon time is after 10 PM \n";			
		}
	}
	else
	{
		# Extract pattern from slstat response
		@pattern1[$index1] = $str_array[0];
		write_log("Client : $str_array[0] can not be zapped as the client was logged on today \n");
		print "Client : $str_array[0] can not be zapped as the client was logged on today \n";			
	}
}

close VBOP_SLSTAT_CMD_RESP;

write_log("--- vbop zap process completed ---\n");
print "--- vbop zap process completed ---\n";

exit;

#--------------------------------------------
# Subroutine: write_log
# Input: One argument passed to this subroutine by the caller.  
#        This is the message that needs to be written to the log file.   
# Output: 
#
#--------------------------------------------

sub write_log
{

	local($LogMsgs) = @_;

	($Sec, $Min, $Hr, $Day, $MonthIdx, $Yr, $WeekDayIdx) = localtime(time);
	
	$Yr += 1900;
	
	$Month = $MonthIdx + 1;

	# Create Current Date Using Day Month Year
	$CurrentDate = ("$Day-$MonthList[$MonthIdx]-$Yr");

	if ($CurrentDate ne $SaveDate)
 	{
		close LOGFILE;
		$LogOpen = "no";

		print "$LogOpen\n";
	}

	if ($LogOpen ne "yes")
	{
		# Create Log File Name Using Year Month Day
		$LogFileName = sprintf("/proj/ahd01/CAisd/site/mods/scripts/log/vbopzap-%04d-%02d-%02d.log", $Yr, $Month, $Day);

		

		unless (open (LOGFILE, ">>$LogFileName"))
		{
		print ("Error opening Log file - $!\n");
			exit (1);
		}

		autoflush LOGFILE 1;

		$LogOpen = "yes";

		# Create Save Date Using Day Month Year
		$SaveDate = ("$Day-$MonthList[$MonthIdx]-$Yr");
	}

	$LogBuffer = sprintf("%04d-%02d-%02d %02d:%02d:%02d $LogMsgs", $Yr, $Month, $Day, $Hr, $Min, $Sec);

	print (LOGFILE $LogBuffer);

	return(1);
}