# $Id: monitor.pl,v 1.8.2.2 2004/01/09 16:41:38 $ #
#ident "@(#)VCS:$Source: /ovsrc/int/CVS/src/nb_ha/vcs/monitor.pl,v $"
# START COPYRIGHT-NOTICE: 1998-2003
# 
# Copyright 1998-2003 VERITAS Software Corporation.
# All rights reserved.
# 
# VERITAS,  the  VERITAS Logo and all other  VERITAS  product names and
# slogans are trademarks  or registered trademarks of  VERITAS Software
# Corporation.  VERITAS and the VERITAS Logo Reg.  U.S. Pat. & Tm. Off.
# Other product names and/or slogans mentioned herein may be trademarks
# or registered trademarks of their respective companies.
# 
# The Licensed Software and Documentation are  deemed to be  commercial
# computer software  and  commercial computer software documentation as
# defined in FAR Sections 12.212 and DFARS Section 227.7202.
# 
# END COPYRIGHT-NOTICE.
#

eval 'exec /usr/openv/netbackup/bin/cluster/vcs/perl -S $0 ${1+"$@"}'
        if 0;

# ARGV variables
my ($ResName, $ServerType, $RobotDaemon, $MonScript, $AgentDebug);

# utility variables
my ($ARCH, $NbuRoot, $SHARED_NBU_SPACE, $PROBE_PROCS, @PROC_ARRAY);
my ($BPPS, $BPPSOPT, $VMPS, $ENVSAVE, $HALOG, $monitorProcs, $downList);
my ($VCS_HOME, $NBUSTATE);

# process variables

($ResName, $ServerType, $RobotDaemon, $MonScript, $AgentDebug) = @ARGV;

# initialize process variables
$VCS_HOME = $ENV{"VCS_HOME"};
$VCS_HOME = "/opt/VRTSvcs" unless (defined ($VCS_HOME));
$BPPS = "/usr/openv/netbackup/bin/bpps";
$VMPS = "/usr/openv/volmgr/bin/vmps";
$BPPSOPT = "-a";

if ( -f "$VCS_HOME/bin/ag_i18n_inc.pl" )
{
	require "$VCS_HOME/bin/ag_i18n_inc.pl";
	VCSAG_SET_ENVS ($ResName, 20001);
}

$ARCH = `uname`;

if ( ! -x $BPPS )
{
	my ($msg);
	$msg = "Unable to locate bpps utility, exiting";
	if ( -f "$VCS_HOME/bin/ag_i18n_inc.pl" )
	{
		VCSAG_LOG_MSG ("E", "$msg", 5);
	}
	exit 99;
}


$NbuRoot = "/usr/openv";

my $currTime = localtime();

open(AGENTLOG, ">> ./VCS_AGENT_DEBUG.log");

sub doCommand
{
	my $doCmd = $_[0];
	chomp($doCmd);
	system("$doCmd\n");
	if ( $? ne 0 )
	{
		print AGENTLOG "$currTime $doCmd Failed.\n";
		close(AGENTLOG);
		exit 99;
	}
}


sub CheckNBProcs
{
	my ($Pid, $PPid, $Rest, $User);

	$ENV{"LANG"}="C";
	open(BPPSOUT, "$VMPS $BPPSOPT|");
	while (<BPPSOUT>)
	{
		chomp;
		next if ( /^---|Process/ );
		if ( /\w/ )
		{
			($User, $Pid, $PPid, $Rest) = split;
			push @Pids, $Pid;
		}
        }
	$ENV{"LANG"} = $ENVSAVE;
	close(BPPSOUT);
	if ( @Pids )
	{
		return (1);
	}
	return (0);
}


if ( ! -f "$NbuRoot/netbackup/bin/cluster/vcs/.NBU_STATE" )
{
	open(STATEFILE, "> ./.NBU_STATE");
	print STATEFILE "STATE=OFFLINE\n";
	close(STATEFILE);
}
else
{
	chomp($NBUSTATE=`grep "^STATE" $NbuRoot/netbackup/bin/cluster/vcs/.NBU_STATE | sed -e s/STATE=//`);
	#$NBUSTATE =~ s/^ *=//;    # Remove = from the start
	$NBUSTATE =~ s/ -*//;    # Remove timestamp
	$NBUSTATE =~ s/^ +//;     # Remove spaces from the start
	$NBUSTATE =~ s/ +$//;     # Remove spaces from the end
}


if ( ! -f "$NbuRoot/netbackup/bin/cluster/vcs/VCS_NBU_RSP" )
{
	print AGENTLOG "$currTime MONITOR - VCS_NBU_RSP file NOT found.\n";
	close(AGENTLOG);
	exit 99;
}


#Get the names of the procs to probe from the VCS_NBU_RSP file created by cluster_config
chomp($PROBE_PROCS=`grep "^PROBE_PROCS" $NbuRoot/netbackup/bin/cluster/vcs/VCS_NBU_RSP | sed -e s/PROBE_PROCS=//`);

$monitorProcs = 0;
@PROC_ARRAY = split(' ',$PROBE_PROCS);
$sizeProcArr = @PROC_ARRAY;
foreach $proc ( @PROC_ARRAY )
{
	system("$BPPS $BPPSOPT | grep ${proc} > /dev/null 2>&1");
	if ( $? ne 0 )
	{
		push @downList, $proc;
		$monitorProcs++;
	}
}


if ( "$monitorProcs" != 0 )
{
	if ("$monitorProcs" == "$sizeProcArr" )
	{ ### every monitored process is down
		$rc = 0;
		$rc = CheckNBProcs;
		if ( $rc )
		{ ### but some non-monitored processes are STILL running
			if ( "$NBUSTATE" ne "ONLINE" )
			{ ### we are going offline and some processes are still running...do not return 100 yet
				print AGENTLOG "$currTime MONITOR - some MM processes are running.\n";
				close(AGENTLOG);
				exit 101;
			}
			exit 100;
		}
		#open(STATEFILE, "> ./.NBU_STATE");
		#print STATEFILE "STATE=OFFLINE";
		#close(STATEFILE);
		exit 100;       ### looks like everything is down
	}

	if ( "$NBUSTATE" ne "ONLINE" )
	{ ### we are going offline and some processes are still running...do not return 100 yet
		exit 101;
	}

	### we are still online...
	print AGENTLOG "$currTime MONITOR - some \(@downList\) processes are NOT running.\n";
	close(AGENTLOG);
	exit 100;       ### some monitored processes are down
}


chomp($SHARED_NBU_SPACE=`grep "^SHARED_DISK" $NbuRoot/netbackup/bin/cluster/vcs/VCS_NBU_RSP | sed -e s/SHARED_DISK=//`);

if ( ("$SHARED_NBU_SPACE" ne "$NbuRoot") && ("$SHARED_NBU_SPACE" ne "$rootLink") )
{
    ### Check for links to shared disk...
    chomp($SH_LINKS=`grep "^LINK" $NbuRoot/netbackup/bin/cluster/vcs/VCS_NBU_RSP | sed -e s/LINK//`);
    @SHLINK_ARRAY = split('\n',$SH_LINKS);
    $sizeLink = @SHLINK_ARRAY;

    if ( "$sizeLink" == 0 )
    {
	print AGENTLOG "$currTime MONITOR - NO LINKS found in VCS_NBU_RSP file.\n";
	close(AGENTLOG);
	exit 99;
    }

    foreach $shlink ( @SHLINK_ARRAY )
    {
	chomp($shlink);
	$shlink =~ s/^ *=//;    # Remove = from the start
	$shlink =~ s/^ +//;     # Remove spaces from the start
	$shlink =~ s/ +$//;     # Remove spaces from the end
	if ( ! -l "$NbuRoot/$shlink" )
	{ # Link does not exist on local disk
		print AGENTLOG "$currTime MONITOR - link $NbuRoot/$shlink NOT found.\n";
		close(AGENTLOG);
		exit 99;
	}
    }
}


if ( ($MonScript ne "") && (-x $MonScript) )
{
	my $rc = 100;

	# return code must be in the range of 100-110
	# see agent developers guide for additional
	# information

	$rc = system("$MonScript");
	exit $rc/256;
}

close(AGENTLOG);

exit 110;

