

#======================================================
# SCMS-SM VCS agents
# TimesTenRep agent - monitor
# Created: Aug 2004
#======================================================
# Developed to monitor the TimesTen Replication agent resource
# the scripts runs the following algorithm:
# - checks input arguments
# - checks whether there was an attribute change. if so it stops and drops the replication,
#   so it will be re-configured and restarted at the next steps.
# - checks whether the replication process is running,
#   whether the replication is running and whether the 
#   replication state is OK.
# - if there is a recoverable problem the replication is restarted
# - if the subscriber database (local) is in a fatal state the 3007067 error
#   is logged and the user must recover by his self.
# - if the replication is up at the end of the operations above an online state is
#   returned otherwise an offline state is returned.
#======================================================
# Modification list:
# 5/8/2004: created
#
#======================================================
# Messages Used:
# MSGID               Message
#------------------------------------------------------
# 37062: TimesTenRep:%s:%s: invalid path to the TimesTen replication agent '%s'
# 37063: TimesTenRep:%s:%s: 
# 37064: TimesTenRep:%s:%s: 
# 37065: TimesTenRep:%s:%s: parameters: %s, %s,
# 37066: TimesTenRep:%s:monitor: replication agent state is OK
# 37067: TimesTenRep:%s:monitor: replication agent failed: the datastore must be duplicated from redundant machine
#======================================================


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

use TtOperations;
use Args;

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

$TtBinPathname = cleanString($TtBinPathname);

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

if (($TtBinPathname eq "") || ( ! -d $TtBinPathname )) {
   VCSAG_LOG_MSG("E"," invalid path to the TimesTen replication agent '$TtBinPathname'",37062, "$TtBinPathname");
   exit 90;
}

$Threshold = 20;


if ($DebugLevel > 0) 
{
   VCSAG_LOG_MSG("I"," parameters: $TtBinPathname, $DebugLevel",37065,"$TtBinPathname", "$DebugLevel");
}


my $hasAttrChange = isAttributeChanged();
if ($hasAttrChange) 
{
   # attribute change => stop replication so it will be reapplied
   my %args5 = ("EntryType" => "monitor",
               "ResID" => $ResID,
               "TtBinPathname" => $TtBinPathname ,
               "DebugLevel" => $DebugLevel);
   stopReplication(\%args5);

   # drop old replication scheme
   my %args15 = ("EntryType" => "monitor",
      "ResID" => $ResID ,
      "TtBinPathname" => $TtBinPathname,
      "DebugLevel" => $DebugLevel);

   dropReplicationScheme(\%args15);

}

my %args2 = ("EntryType" => "monitor",
               "ResID" => $ResID,
               "TtBinPathname" => $TtBinPathname,
               "DebugLevel" => $DebugLevel,
               "Threshold" => $Threshold);

my $isStateOk = makeSureRepIsUp(\%args2);
if ($isStateOk > 0) 
{
   if ($DebugLevel > 0) 
   {
      VCSAG_LOG_MSG("I"," replication agent state is OK",37066);
   }
   # all if fine
   exit 110;
}
else
{
   # replication agent state is failed !!!!
   VCSAG_LOG_MSG("E"," replication agent failed: the datastore must be duplicated from redundant machine",37067);
   
   # signall off line to make the resource faulted, so the administrator
   # will come and recover the database
   exit 100;
}
