#!/perl/bin/perl
# Program : ProcessStatus
# Version : 0.01
# Date    : 20 April 2017
# Author  : Lace Joy
# The Program will check the running  status  and date of running of a process

use Win32::Service;
use warnings;
my $filename = "Output.txt";
my $file1 = "24HOUR.ini";
my $Event = "Event1";
my $val2 = "";
my $first = "";
my $V_CurrDir;
my %exitval     =   ('OK'=>1,'NOT OK'=>2,'ERROR'=>3);
our($second);
mainpgm();

sub mainpgm
{
    $V_CurrDir = "C:/Users/lawerenceb_admin/Desktop/test";
	$filename = "$V_CurrDir/Output.txt";
	open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
	print $fh  "\n *************Log file  started ******";
	$answer = serviceChk($fh);
	print $fh  "\n Today's Date:  $second";
	print $fh  "\n Exit Code:  $exitval{$answer}";
	print $fh  "\n **************End of Log ************";
	close $fh;
	exit $exitval{$answer};
}

sub serviceChk
{
	#set up a hash of known service states
	my %statcodeHash = (
	 '1' => 'stopped',
     '2' => 'start pending',
     '3' => 'stop pending',
     '4' => 'running',
     '5' => 'continue pending',
     '6' => 'pause pending',
     '7' => 'paused',
	 '8' => 'no Service'
	);
	
  my %serviceHash;
  my $servstatus="";
  
    #my $serv = "aspnet_s"; 
  my $serv = "aspnet_state"; 
  my $fhandle =shift;  
  my %status;
  my $ret = Win32::Service::GetStatus("", $serv, \%status);
    if ($ret)
    {  
		$servstatus = "$statcodeHash{$status{CurrentState}}";
		if ( $servstatus eq "stopped")
		{
			#calling the proces date
			print $fhandle "\n Process :$serv\n";  
			print $fhandle "\n Process status :$servstatus\n";  
			print $fhandle "\n Checking the  process  date";
			$file1 = "$V_CurrDir/24HOUR.ini";
		
			my $Checkdatertn =   checkdateVal($file1);
						
			if ($Checkdatertn) {print $fhandle "\n Previous date"; $answer="NOT OK"} 
			elsif ($Checkdatertn < 0){print $fhandle "\n Tomorrow's date"; $answer="OK"} 
			elsif ($Checkdatertn eq 0){print $fhandle "\n Today's date"; $answer="NOT OK"} 
			else{$answer="ERROR";}
						
		}
		else{ 
			print $fhandle "\n$servstatus\t$serv\n";
			$answer="OK";
			}
    }
    else
    {      
         $servstatus = "$statcodeHash{8}";
		 print $fhandle "\n$servstatus\n";
		 $answer = "ERROR";
	}
	return $answer;
}
 
sub checkdateVal
{
	my $file1 =shift;
	my $answer ="";
	open(my $inputf, '<', $file1) or die "Could not open file '$filename' $!";
	while(my $row = <$inputf>)
	{ 
		 @details1 = split(/\=+/,$row);
	  
		 if ($details1[0] eq $Event)
		 {
		   $val2 = $details1[1];
		   @dateval = split(/\,+/,$val2);
		   $first =substr("$dateval[1]", 4,4).substr("$dateval[1]", 2,2).substr("$dateval[1]", 0,2);
		 }
	}
	close $inputf;	   
		   		  	  
	my ($sec,$min,$hour,$day,$month,$yr19,@rest) =   localtime(time);
	$yr19 =$yr19+1900;
	my $prvMon ="";
	my $prvDay ="";
	
	if ($month < 10){$prvMon ="0";}
	if ($day < 10){$prvDay ="0";}
	
	$second = $yr19.$prvMon.++$month.$prvDay.$day;
	$diffval = datediff_sub($first) - datediff_sub($second);
	return $diffval;
}

sub datediff_sub{
    use integer;
    my ( $y, $m, $d ) = $_[0] =~ /\A([0-9]{4})([0-9]{2})([0-9]{2})\z/
        or return;
    my $adj;
    if ( $m <= 2 ) {
        $y -= ( $adj = ( 14 - $m ) / 12 );
        $m += 12 * $adj;
    }
    elsif ( $m > 14 ) {
        $y += ( $adj = ( $m - 3 ) / 12 );
        $m -= 12 * $adj;
    }
    $d += ( $m * 367 - 1094 ) / 12 + $y % 100 * 1461 / 4 + ( $y / 100 * 36524 + $y / 400 ) - 306;
}
