HEX
Server: Apache/2.4.34 (Red Hat) OpenSSL/1.0.2k-fips
System: Linux WORDPRESS 3.10.0-1160.118.1.el7.x86_64 #1 SMP Thu Apr 4 03:33:23 EDT 2024 x86_64
User: digital (1020)
PHP: 7.2.24
Disabled: NONE
Upload Files
File: //opt/OV/newconfig/instrumentation/PostDeployActions.pl
#!/usr/bin/perl
#
###################################################################################
# @(#)			PostDeployActions.pl	 11.00.000	31AUG2009    =*=
###################################################################################
#
#	The perl script runs after the successful deployment of policy
#  to check the syntax of the parm or alarmdef file
#
# Argument 1: parm/alarmdef
# Argument 2: <Filename>
#
# Returns status of syntax check by opcmsg
#
# OAPCC-PDA-XX - Message ids (Operations Agent Performance Collection Component Post Deployment Actions )
###################################################################################

use strict;
use warnings;
use File::Basename;

#
# Constants
#

my ($checkthis, $filename) = ($ARGV[0], $ARGV[1]);
my $perf_bin_dir  = "/opt/perf/bin/";
my $perf_data_dir = "/var/opt/perf/";
my $ov_bin_dir    = "/opt/OV/bin/";
my $ov_data_dir   = "/var/opt/OV/";
my $error_num = 0;

#
# Global Variables
#

my $policy_dir;
my $output;
my $ret_status;
my $fullfilename;
my @platform;
my $logfile;


#
# Set the bin dir location
# Set the data dir location
#

if ($^O eq "aix") { 
	$perf_bin_dir="/usr/lpp/perf/bin/"; 
	$ov_bin_dir="/usr/lpp/OV/bin/";
} 
elsif ($^O eq "dec_osf") { 
	$perf_bin_dir="/usr/opt/perf/bin/"; 
	$ov_bin_dir="/usr/opt/OV/bin/";
}
# Removing the ov_bin_dir from opcmsg, as it seems that opcmsg is in PATH
# Stale code start
elsif ($^O eq "MSWin32") { 
	#
	# Using opcagt command to determine the PATH
	# as Registry.pm is not packaged in perl by OVO.
	# This is to be modified to read the registry
	# once OVO packages it
	#
	open($output, "opcagt -type -verbose |") || 
	send_opcmsg("DIE","\[OAPCC-PDA-01\] Unable to determine the Install and Data dir of Performance Collection Component. The Post Deployment Policy actions are not performed.");
	while(<$output>) {
		# chop \n character
		chop; 
		my @fields = split(/=/,$_);

		# chop last " character
		chop $fields[1]; 

		# chop the first " character
		$fields[1] =~ s/^.//; 
		if ($fields[0] eq "OVO_INSTALLDIR") {
			$ov_bin_dir = "$fields[1]" . "bin\\";
			$perf_bin_dir = $ov_bin_dir;
		}		
		if ($fields[0] eq "OVO_DATADIR") {
			$ov_data_dir = "$fields[1]";
			$perf_data_dir = $ov_data_dir;
		}		
	}
	close($output);
}
# Stale code End

#
# Check if OVPA is present
#
my $check_ovpa;

if ($^O eq "MSWin32") {
	$check_ovpa = "$perf_bin_dir\\ovpacmd.exe";
} 
else {
	$check_ovpa = "$perf_bin_dir/ovpa";
}

( -e $check_ovpa ) || send_opcmsg("DIE","\[OAPCC-PDA-02\] Performance Collection Component is not installed on the managed node.");

#
# Set the policy dir and spi_alarmdef dir
#
my $tmp_checkthis;

if ( $checkthis eq "parm" ) {
	$tmp_checkthis="Parmfile";

}
elsif ( $checkthis eq "alarmdef" ) {
	$tmp_checkthis="Alarmdef";
}

@platform = split(/\./,basename($filename));

if ( $platform[0] eq "win") {
	$policy_dir          = "$ov_data_dir" . "conf\\Performance_Collection_Component\\$tmp_checkthis" . "_for_Windows\\";
}
elsif ( $platform[0] eq "aix") {
	$policy_dir 	     = "$ov_data_dir" . "conf/Performance_Collection_Component/$tmp_checkthis" . "_for_AIX/";
}
elsif ( $platform[0] eq "linux") {
	$policy_dir 	     = "$ov_data_dir" . "conf/Performance_Collection_Component/$tmp_checkthis" . "_for_Linux/";
}
elsif ( $platform[0] eq "tru64") {
	$policy_dir 	     = "$ov_data_dir" . "conf/Performance_Collection_Component/$tmp_checkthis" . "_for_TRU64/";
}
elsif ( $platform[0] eq "hpux") {
	$policy_dir 	     = "$ov_data_dir" . "conf/Performance_Collection_Component/$tmp_checkthis" . "_for_HP-UX/";
}
elsif ( $platform[0] eq "sunos") {
	$policy_dir 	     = "$ov_data_dir" . "conf/Performance_Collection_Component/$tmp_checkthis" . "_for_SunOS/";
}
elsif ( $platform[0] eq "vmware") {
	$policy_dir 	     = "$ov_data_dir" . "conf/Performance_Collection_Component/$tmp_checkthis" . "_for_VMWare/";
}
else {
	send_opcmsg("DIE","\[OAPCC-PDA-03\] Unable to determine the location of policy directory. The Post Deployment Policy actions are not performed.");
}

#
# Set fullfilename 
#

$fullfilename		= "$policy_dir" . "$filename";

#
# Define the logfile and open it for writing.
# 

open($logfile,">$perf_data_dir" . "policydep_$checkthis.log");

#
# Print out all the variable values
#

print $logfile "===================== Perf Post Deployment Actions Start =================\n";
if ($^O eq "MSWin32") {
 print $logfile "NOTE:     The post deploy actions started at : " . `date /T` . `time /T` . "\n";
}
else {
 print $logfile "NOTE:     The post deploy actions started at : " . `date` . "\n";
}
print $logfile "NOTE:     Performance Component Bin Dir    :$perf_bin_dir\n";
print $logfile "NOTE:     Performance Component Data Dir   :$perf_data_dir\n";
print $logfile "NOTE:     Operations Manager Bin Dir   :$ov_bin_dir\n";
print $logfile "NOTE:     Operations Manager Data Dir  :$ov_data_dir\n";
print $logfile "NOTE:     Configfile Policy Dir        :$policy_dir\n";
print $logfile "NOTE:     Filename Passed              :$fullfilename\n";
print $logfile "NOTE:     What to Check                :$checkthis\n";
print $logfile "NOTE:     Logfile name		       :$perf_data_dir" . "policydep_$checkthis.log\n";

#
# 1. Check if the input file is available.
# 2. If available, run the appropriate 'utility' command based on '$checkthis' variable.
# 3. Parse the utility output to determine on what action to take.
# 4. If the syntax check passed, then check for OSSPI co-existence.
# 5. Restart the services
#

print $logfile "\nNOTE:     Entering function MAIN.\n";
( -e $fullfilename ) || send_opcmsg ("DIE","\[OAPCC-PDA-04\] The input file $fullfilename is not available. The Post Deployment Policy actions are not performed.");
{
	if ($checkthis eq "parm") {
		if ($^O eq "MSWin32") {
			print $logfile "NOTE:     Running \"$perf_bin_dir\\utility\" -xp $fullfilename 2>&1 \n";
			open($output, "\"$perf_bin_dir\\utility\" -xp $fullfilename 2>&1 |");
		}
		else {
			print $logfile "NOTE:     Running $perf_bin_dir/utility -xp $fullfilename 2>&1 \n";
			open($output, "$perf_bin_dir/utility -xp $fullfilename 2>&1 |");
		}
	}
	elsif ($checkthis eq "alarmdef") {
		if ($^O eq "MSWin32") {
			print $logfile "NOTE:     Running \"$perf_bin_dir\\utility\" -xc $fullfilename 2>&1 \n";
			open($output, "\"$perf_bin_dir\\utility\" -xc $fullfilename 2>&1 |");
		}
		else {
			spi_coexistence();
			print $logfile "NOTE:     Running $perf_bin_dir/utility -xc $fullfilename \n";
			open($output, "$perf_bin_dir/utility -xc $fullfilename 2>&1 |");
		} 
	}
	$ret_status = parse_output();
	if ($ret_status == 0) {
		perform_copy();
		perform_restart();
	}
	else {
		send_opcmsg("ERROR","\[OAPCC-PDA-05\] Syntax check of $checkthis failed. Please run the 'View Status of Post Deploy Actions' tool for further information."); 
	}
	close($output);
} # else of [ -e $fullfilename) ]

if ( $error_num == 0 ) {
	send_opcmsg("NOTE","\[OAPCC-PDA-06\] The $checkthis configuration file has been successfully deployed and all the necessary services are restarted."); 
}

print $logfile "NOTE:     Leaving function MAIN.\n";
if ($^O eq "MSWin32") {
  print $logfile "NOTE:     The post deploy actions finished at : " . `date /T` .  `time /T` . "\n";
}
else {
  print $logfile "NOTE:     The post deploy actions finished at : " . `date` . "\n";
}
print $logfile "===================== Perf Post Deployment Actions End =================\n";
close($logfile);
exit 0;

#
# Sub routine to parse the output of utility command.
# 

sub parse_output {
	my $line;

	print $logfile "NOTE:     Entering function parse_output.\n";

	while($line = <$output>) {
		print $logfile "NOTE:     Processing $line";
		if ( ($checkthis eq "parm") && ($line =~ /^Parm File:/) ) {
			#
			# Check for either:
			# Parm File: defaults will be used.  OR
			# Parm File: ...... had 0 warnings.
			#
			if ( ($line =~ /Parm File: defaults will be used./) ) {
				return 1;
			}
			else {
				my @fields = split(/had /,$line);
				my @warn = split(/ /,$fields[1]);
				if ($warn[0] != 0) {
					print $logfile "NOTE:     Leaving function parse_output. Status Failure.\n";
					return $warn[0];
				}
			}
		}
		if ( ($checkthis eq "alarmdef") && ($line =~ /^Program ended due to error/) ) {
			print $logfile "NOTE:     Leaving function parse_output. Status Failure.\n";
			return 1;
		}
	}
	print $logfile "NOTE:     Leaving function parse_output. Status Success.\n";
	return 0;
}

#
# To take care of OSSPI co-existence. 
# 
# If "alarmdef_integration" file found
#	Copy the file, and restart alarm
# else 
#    if "/var/opt/perf/alarmdef has "include osspi_alarmdef" then
#		Create "alarmdef_integration" file 
#		with "include osspi_alarmdef" content,
#		Copy the file, and restart alarm.
# 	else
#		Create empty "alarmdef_integration",
#		Copy the file, and restart alarm.
#	endif
# endif
#
# "alarmdef_integration" is in - $perf_data_dir
# Copy and Restart will be done by the caller of this function.
#

sub spi_coexistence {
	my $alarmdef_integration_file = "$perf_data_dir" . "alarmdef_integration";
	my $alarmdef_file = "$perf_data_dir" . "alarmdef";
	my $tmp_file;
	my $integ_file;
	my $line;

	print $logfile "NOTE:     Entering function spi_coexistence.\n";

	if ( -e $alarmdef_integration_file ) {
		print $logfile "NOTE:     Found $alarmdef_integration_file.\n";
		print $logfile "NOTE:     Leaving function spi_coexistence.\n";
		return;
	}
	else {
		print $logfile "NOTE:     Not Found $alarmdef_integration_file.\n";
		print $logfile "NOTE:     Parsing $alarmdef_file.\n";
		open($tmp_file, "$alarmdef_file");
		while($line = <$tmp_file>) {
			if ($line =~ /^include \"\/\/var\/opt\/OV\/conf\/osspi\/osspi_alarmdef\"/) {
				print $logfile "NOTE:     Found OSSPI alarmdef entry in alarmdef file.\n";
				print $logfile "NOTE:     Creating predefined $alarmdef_integration_file.\n";
				open($integ_file, ">$alarmdef_integration_file");
				print $integ_file "# edited by OSSPI --- BEGIN\n";
				print $integ_file "\n";
				print $integ_file "include \"//var/opt/OV/conf/osspi/osspi_alarmdef\"\n";
				print $integ_file "\n";
				print $integ_file "# edited by OSSPI --- END\n";
				print $logfile "NOTE:     Leaving function spi_coexistence.\n";
				close($integ_file);
				close($tmp_file);
				return;
			}
		}
		close($tmp_file);
		print $logfile "NOTE:     Not Found OSSPI alarmdef entry in alarmdef file.\n";
		print $logfile "NOTE:     Creating empty $alarmdef_integration_file.\n";
		open($integ_file, ">$alarmdef_integration_file");
		close($integ_file);		
	}
	print $logfile "NOTE:     Leaving function spi_coexistence.\n";
}

# 
# Perform copy of alarmdef/parm file to data dir
#

sub perform_copy {
	use File::Copy;
	my $dst_file;

	print $logfile "NOTE:     Entering function perform_copy.\n";
	if ($^O eq "MSWin32" ) {
		$dst_file = "$perf_data_dir" . "$checkthis" . "\.mwc";
	}
	else {
		$dst_file = "$perf_data_dir" . "$checkthis";
	}

	print $logfile "NOTE:     Copying $fullfilename to $dst_file.\n";
	copy("$fullfilename", "$dst_file");

	print $logfile "NOTE:     Leaving function perform_copy.\n";
}

#
# To start the OVPA Services.
# On error, send opcmsg but do not quit.
#

sub perform_restart {
	
	print $logfile "NOTE:     Entering function perform_restart.\n";

	if ($^O eq "MSWin32") {
		if ($checkthis eq "parm") {
			print $logfile "NOTE:     Running \"$perf_bin_dir\\ovpacmd\" refresh col\n";
			system("\"$perf_bin_dir\\ovpacmd\" refresh col");
			print $logfile "\n";
		}
		if ($checkthis eq "alarmdef") {
			print $logfile "NOTE:     Running \"$perf_bin_dir\\ovpacmd refresh alarm\n";
			system("\"$perf_bin_dir\\ovpacmd\" refresh alarm");
			print $logfile "\n";
		}
		if ($? != 0) {
			print $logfile "ERROR:     Failed to start the services. Sening opcmsg.\n";
			send_opcmsg("ERROR","\[OAPCC-PDA-07\] The configuration files are successfully copied to Performance Component Data dir. The services could not be restarted. ");		
		}
		else {
			print $logfile "NOTE:     Successfully started the services.\n";
		}
	}
	else {
		if ($checkthis eq "parm") {
			if ( $^O eq "linux" ) {
				print $logfile "NOTE:     Running $perf_bin_dir/ovpa restart scope > /dev/null 2>&1 \n";
				system("$perf_bin_dir/ovpa restart scope > /dev/null 2>&1");
			}
			else {
				print $logfile "NOTE:     Running $perf_bin_dir/mwa restart scope > /dev/null 2>&1 \n";
				system("$perf_bin_dir/mwa restart scope > /dev/null 2>&1");
			}
		}
		if ($checkthis eq "alarmdef") {
			if ( $^O eq "linux" ) {
				print $logfile "NOTE:     Running $perf_bin_dir/ovpa restart alarm > /dev/null 2>&1 \n";
				system("$perf_bin_dir/ovpa restart alarm > /dev/null 2>&1");
			}
			else {
				print $logfile "NOTE:     Running $perf_bin_dir/mwa restart alarm > /dev/null 2>&1 \n";
				system("$perf_bin_dir/mwa restart alarm > /dev/null 2>&1");
			}
		}
		if ($? != 0) {
			print $logfile "ERROR:     Failed to start the services. Sening opcmsg.\n";
			send_opcmsg("ERROR","\[OAPCC-PDA-08\] The configuration files are successfully copied to Performance Component Data dir. The services could not be restarted. ");		
		}
		else {
			print $logfile "NOTE:     Successfully started the services.\n";
		}
	}
	print $logfile "NOTE:     Leaving function perform_restart.\n";
}

#
# Send opcmsg to server
#
# Argument 1
# DIE - exit after sending the message
# NOTE - Do not exit
# WARNING - Do not exit
# ERROR - Do not exit
#
# Argument 2
# Message to be sent
#

sub send_opcmsg {
	
	use Sys::Hostname;

	# Constants
	my $application = "OA Performance Collection Component";
	my $object = "PostDeployActions";
	my $msg_grp = "Performance";
	my $hostname = hostname();
	
	# Variables	
	my $severity;
	my @run_command;
	my $runcommand;
	print { $logfile ? $logfile : \*STDOUT } "NOTE:     Entering function send_opcmsg.\n";

	if ($_[0] eq "DIE") {
		$severity = "critical";
		$error_num++;
	}
	elsif ($_[0] eq "ERROR") {
		$severity = "major";
		$error_num++;
	}
	elsif ($_[0] eq "WARNING") {
		$severity = "warning";
	}
	elsif ($_[0] eq "NOTE") {
		$severity = "normal";
	}
	else {
		$severity = "normal";
	}
	
	print { $logfile ? $logfile : \*STDOUT } "NOTE:     Sending opcmsg to management server.\n";
	if ( $^O eq "MSWin32" ) {
		my $run_command;
		$run_command = " \"opcmsg\" severity=\"$severity\" application=\"$application\" object=\"$object\" msg_text=\"$_[1]\" msg_grp=\"$msg_grp\" node=\"$hostname\" ";
		print { $logfile ? $logfile : \*STDOUT } "NOTE:     Running $run_command\n";
		print { $logfile ? $logfile : \*STDOUT } "NOTE:     Message sent is - $_[1]\n";
		system($run_command);
	}
	elsif ($^O eq "solaris") {
		$runcommand = " \"$ov_bin_dir/opcmsg\" severity=\"$severity\" application=\"$application\" object=\"$object\" msg_text=\"$_[1]\" msg_grp=\"$msg_grp\" node=\"$hostname\" ";
		print { $logfile ? $logfile : \*STDOUT } "NOTE:     Running $runcommand\n";
		print { $logfile ? $logfile : \*STDOUT } "NOTE:     Message sent is - $_[1]\n";
		system($runcommand);
	}
	else {
		my @run_command;
		@run_command = ("$ov_bin_dir/opcmsg", "severity=$severity", "application=$application", "object=$object", "msg_text=$_[1]", "msg_grp=$msg_grp", "node=$hostname");
		print { $logfile ? $logfile : \*STDOUT } "NOTE:     Running @run_command\n";
		print { $logfile ? $logfile : \*STDOUT } "NOTE:     Message sent is - $_[1]\n";
		system(@run_command);
	}

	if ($_[0] eq "DIE") {
		print { $logfile ? $logfile : \*STDOUT } "NOTE:     The Post Deploy Actions encountered a CRITICAL error, quitting.\n";
		print { $logfile ? $logfile : \*STDOUT } "NOTE:     Leaving function send_opcmsg.\n";
		print { $logfile ? $logfile : \*STDOUT } "===================== Perf Post Deployment Actions End =================\n";
		if ( $logfile ) {
			close($logfile);
		}
		exit 1;
	}
	print { $logfile ? $logfile : \*STDOUT } "NOTE:     Leaving function send_opcmsg.\n";
}