

#======================================================
# SCMS-SM VCS agents
# OnOnlyProcess agent - monitor
# Created: Aug 2004
#======================================================
# Developed to monitor the TimesTen Daemon
# based on the Process agent
#======================================================
# Modification list:
# 5/8/2004: created
#
#======================================================
# Messages Used:
# MSGID               Message
#------------------------------------------------------
# 37051: OnOnlyProcess:%s:monitor: invalid file in path-name '%s'
# 37052: OnOnlyProcess:%s:monitor: invalid online command '%s'
# 37053: OnOnlyProcess:%s:monitor: process was not found, restarting it
# 37056: OnOnlyProcess:%s:monitor: '%s' process suspended killing it
#======================================================

eval 'exec $VCS_HOME/bin/perl5 -S $0 ${1+"$@"}'
	if 0;
#
# This is the process on only monitor function
#
use ProcessOperations;

$ResName=$ARGV[0];
shift;
$OnlineCmd=$ARGV[0];
shift;
$Pathname=$ARGV[0];
shift;
$Arguments=join ' ', @ARGV; 
#
# Set the OS locale
#
$ENV{"LANG"}="C";
$vcs_home =$ENV{"VCS_HOME"} || "/opt/VRTSvcs";
my ( $log_message );

use ag_i18n_inc;
VCSAG_SET_ENVS ("OnOnlyProcess", "monitor",$category_id);

$Pathname = cleanString($Pathname);

if (($Pathname eq "") || ( ! -x $Pathname )) {
   VCSAG_LOG_MSG("E"," invalid file in path-name '$Pathname'",37051,"$Pathname");
   exit 90;
}

$OnlineCmd = cleanString($OnlineCmd);

if ($OnlineCmd eq "") {
   VCSAG_LOG_MSG("E"," invalid online command '$OnlineCmd'",37052,"$OnlineCmd");
   exit 80;
}

$Arguments= cleanString($Arguments);

#
# Returns the PID of a given $Pathname $Argument-list pair
# IF THE PROCESS IS SUSPENDED KILL IT !!!!
#
sub getproc {
   #
   # Loop through the proc table and verify there is command + arg matches
   # 	The arg list is ac concatenated list of user args truncated
   #	to 80 characters. The 80 character limit is in ps(1M)).
   #
   open (PIDS, "$vcs_home/bin/OnOnlyProcess/ps.sh -ea -o pid,s,args 2> /dev/null |");
   local $ArgString;
   local @pidlist;
   while (<PIDS>) 
   {
      chomp;
      ($pid, $state, @psargs) = split;
      $arglist = join(' ', @psargs);
      if ($arglist =~ $Pathname && $pid ne $$) 
      {
         $ArgLength = length($arglist);
         ($pathname, @args) = split(/\s/,$arglist);
         if ($Pathname =~ $pathname) 
         {
            $arglist = join(' ', @args);
            
            if ( $ArgLength >= 79 ) 
            {
               $ArgString = substr($Arguments, 0, length($arglist));
            } 
            else 
            {
               $ArgString = $Arguments;
            }
            if ($ArgString eq $arglist) 
            {
               my $suspendedState = "T";
               if ($state eq $suspendedState) 
               {
				  VCSAG_LOG_MSG("C"," '$Pathname' process suspended killing it",37056,"$Pathname");
				  
                  system("kill -9 $pid"); 
               }
               else
               {
                  push(@pidlist, $pid);
               }               
            }
         }
      }
   }
   return (@pidlist);
}
# Here we verify we found the process.
# if it was found return ONLINE
# otherwise make the resource online and return OFFLINE
@pids = getproc ();
if (scalar(@pids)) 
{
   exit 110;
} 
else 
{   
   VCSAG_LOG_MSG("E","process was not found, restarting it",37053);
    
   system("$vcs_home/bin/OnOnlyProcess/online $ResName \"$OnlineCmd\" $Pathname $Arguments");
   
   exit 100;
}
