

#======================================================
# SCMS-SM VCS agents
# SubscriberManager agent - monitor
# Created: Aug 2004
#======================================================
# Developed to monitor the SM resource
# the scripts runs the following algorithm:
# - checks if the SM process is alive
# - if not restarts the SM and verifies it it up
# - calls the SM monitor operation
# - returns the SM's state and acts accordingly: 
#    - if the state is active returns online
#    - if the state is standby returns offline
#    - if the state is failure and the VCS thinks it should be active returns offline
#    - if the state is failure and the VCS thinks it should be stanby creates monitor timeout (180 sec)
#    - if the state is initialization return 70 (unknown)
#    - otherwise return 50 (unknown)
#======================================================
# Modification list:
# 5/8/2004: created
#
#======================================================
# Messages Used:
# MSGID               Message
#------------------------------------------------------
# 37011: SubscriberManager:% s: monitor: invalid path to the SM '$SmBinPathname'
# 37012: SubscriberManager:% s: monitor: parameters: $SmBinPathname, $State, $DebugLevel
# 37013: SubscriberManager:% s: monitor: SM is in failure state, script is causing timeout of $longTimeout seconds
# 37014: SubscriberManager:% s: monitor: the SM is not up ...
# 37015: SubscriberManager:% s: monitor: the CLU terminated successfully
# 37016: SubscriberManager:% s: monitor: SM State is $smState
# 37017: SubscriberManager:% s: monitor: the pattern was not found: $cluResult
# 37018: SubscriberManager:% s: monitor: monitor script is about to return $retState
# 37019: SubscriberManager:% s: monitor: the CLU failed
# 37020: SubscriberManager:% s: monitor: the CLU failed to run or timed out
#======================================================


eval 'exec $VCS_HOME/bin/perl5 -S $0 ${1+"$@"}'
        if 0;
($ResID, $SmBinPathname, $State, $DebugLevel) = @ARGV;

use SmOperations;
use Args;

# a long timeout in seconds that should cause a timeout of the VCS
$longTimeout = 180;

#
# Set the OS locale
#
$ENV{"LANG"}="C";
$vcs_home =$ENV{"VCS_HOME"} || "/opt/VRTSvcs";
my ( $log_message );

$SmBinPathname = cleanString($SmBinPathname);

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

if (($SmBinPathname eq "") || ( ! -d $SmBinPathname )) {
   VCSAG_LOG_MSG("E","invalid path to the SM '$SmBinPathname'",37011,"$SmBinPathname");
   exit 30;
}

sub convertState 
{
   my $myState =  $State;

   # if not mapped => check the State parameter to check the wanted state
   if (($myState == 0) ||
       ($myState == 1) ||
       ($myState == 4) ||
       ($myState == 5))
   {
      # offline
      return "standby";
   }
   elsif (($myState == 2) ||
         ($myState == 3))
   {
      #online
      return "active";
   }
   else
   {
      return "other";
   }
}

sub convertSmState 
{
   # handle args
   my $args = shift;
   checkArgs($args,{"State" => "REQ",
               "VcsState" => "REQ",
               "DebugLevel" => "REQ"});
   my $argState = $args->{"State"};
   my $argVcsState = $args->{"VcsState"};

   if ($argState eq "Active") 
   {
      # online 
      return 110;
   }
   elsif ($argState eq "Standby") 
   {
      # offline
      return 100;
   }
   elsif ($argState eq "Initialization") 
   {
      # "Unknown 
      # return 70;
      # offline
      return 100;

   }
   elsif ($argState eq "Failure") 
   {
      if ($argVcsState eq "active") 
      {
         # offline - to cause the resource to be faulted and to
         # trigger failover
         return 100;
      }

      # cause a timeout
      VCSAG_LOG_MSG("E","SM is in failure state, script is causing timeout of $longTimeout seconds",37013,"$longTimeout"); 
      sleep $longTimeout;
      return 60;
   }
   else
   {
      # "Unknown 
      return 50;
   }
}


if ($DebugLevel > 0) 
{
   VCSAG_LOG_MSG("I","parameters: $SmBinPathname, $State, $DebugLevel",37012,"$SmBinPathname","$State", "$DebugLevel");
}

# these paraeters define how long and in what rate the SM is polled
$startUpTimeout = 30;
$initialSleep   = 5;
$pollInterval   = 5;

# if the process is running 
if (smProcessExists() == 0) 
{
   %args = ("Timeout" => ($startUpTimeout - $initialSleep),
            "PollInterval" => $pollInterval,
            "InitialSleep" => $initialSleep,
            "SmBinPathname" => $SmBinPathname,
            "DebugLevel" => $DebugLevel,
            "ResID" => $ResID);
   # call the SM to start
   if (smStartAndWait(\%args) == 0)
   {
      # the operation timed-out
      if ($DebugLevel > 0)
      {
         VCSAG_LOG_MSG("C","the SM is not up ...",37014);

      }
      # return an "UNKNOWN" value
      exit 90; 
   }
}

# get the state the VCS thinks the SM is in
$vcsState = convertState();



%args = ("Operation" => "su - pcube -c \"$SmBinPathname/p3cluster --monitor --state=other\"" ,
      "DontReturnResult" => "0",
      "DebugLevel" => $DebugLevel,
         "ResID" => $ResID,
         "maxRetries" => "5");

my $cluResult = runOperation(\%args);

if (defined $cluResult) 
{
   my $success = grep("Command terminated successfully",$cluResult);
   
   if ($success > 0)
   {
      if ($DebugLevel > 0)
      {
         VCSAG_LOG_MSG("I","the CLU terminated successfully",37015);     
      }
      # the CLU terminated successfully
      # look for the sentence: the SM is in XXX state
      @words = split / /, $cluResult;
      $inFound = 0;
      $found = 0;
      foreach $word (@words)
      {
         if ($inFound == 0)
         {
            # look for the word within
            if ($word eq "in") 
            {
               $inFound = 1;
               $found = 1;
            }
         }
         else
         {
            # when 'in' is found the next word is the state !!!
            $smState = $word;
            $inFound = 0;
            if ($DebugLevel > 0)
            {
               VCSAG_LOG_MSG("I","SM State is $smState",37016,"$smState");         
            }
         }
      }
      if ($found == 0)
      {
         # the pattern was not found - warn !!!
         VCSAG_LOG_MSG("C","the pattern was not found: $cluResult",37017,"$cluResult"); 
         exit 40;
      }
      
      %args = ("State" => $smState,
               "VcsState" => $vcsState,
               "DebugLevel" => $DebugLevel);
      my $retState = convertSmState(\%args);
      if ($DebugLevel > 0)
      {
         VCSAG_LOG_MSG("I","monitor script is about to return $retState",37017,"$retState");           
      }
      exit $retState;	
   }
   else
   {
      # CLU failed
      VCSAG_LOG_MSG("C"," the CLU failed",37019);           
      exit 80;
   }
}
else
{
   # offline - to cause the resource to be faulted and to
   # trigger failover
   VCSAG_LOG_MSG("C"," the CLU failed to run or timed out",37020);       
   exit 100;

}






