File: //opt/perf/bin/selfmon_configure.pl
#!/opt/OV/nonOV/perl/a/bin/perl -w
# Script to Configure SelfMon
use Getopt::Long;
use POSIX qw(strftime);
#use Win32::Registry;
my $server = undef;
my $interval= undef;
my $help=0;
my $OS = $^O;
my $OV_INST_DIR="";
my $OV_DATA_DIR="";
my $OV_PERFINST_DIR="";
my $OV_HPCS_DATA_DIR="";
my $OV_HPCS_DIR="";
my $OV_BIN_PATH="";
my $CFG_SET_CMD="";
my $CFG_GET_CMD="";
my $archDir="";
my $osDir="";
my $process_stat="";
my $node_state="";
my $agent_over="";
my $system_txt="";
my $hpcstrace_log="";
my $hbp_server="";
my $agent_update="";
my $collection_mode="";
my $regBBC="";
my $enable_selfmon = undef;
my $avoid_hpcsrun = undef;
my $disable_selfmon= undef;
my $HPCS_START_CMD="";
my $HPCS_STOP_CMD="";
my $CMD_OP="";
my $del_file="";
my $sucess=True;
$CFG_SET_CMD = "ovconfchg";
$CFG_GET_CMD = "ovconfget";
our $HKEY_LOCAL_MACHINE;
if ($^O eq "aix") {
$osDir="AIX";
$OV_PERFINST_DIR ="/usr/lpp/perf/bin/";
$OV_HPCS_DIR= "/usr/lpp/OV/hpcs/";
$OV_HPCS_DATA_DIR= "/var/opt/OV/hpcs/";
$OV_INST_DIR= "/usr/lpp/OV/";
$OV_DATA_DIR= "/var/opt/OV/";
$OV_BIN_PATH = "/usr/lpp/OV/bin/";
}
elsif ($^O =~ /MSWin/) {
$osDir="WIN";
require Win32::Registry;
import Win32::Registry;
my ($rkey);
my ($var);
my ($data);
my ($type);
my ($p) = 'SOFTWARE\Hewlett-Packard\HP OpenView';
$HKEY_LOCAL_MACHINE->Open($p, $rkey);
$rkey->QueryValueEx("InstallDir", $type, $var);
$rkey->QueryValueEx("DataDir", $type, $data);
$OV_INST_DIR = $var;
$OV_DATA_DIR = $data;
my $OSbit = `set`;
if ( $OSbit =~ m/Files\(x86\)/i )
{
$OV_PERFINST_DIR ="\"".$OV_INST_DIR."bin/"."\"";
$OV_HPCS_DIR= $OV_INST_DIR."hpcs/";
$OV_HPCS_DATA_DIR= $OV_DATA_DIR."hpcs/";
$OV_BIN_PATH = "\"".$OV_INST_DIR."bin/Win64/"."\"";
}
else
{
$OV_PERFINST_DIR ="\"".$OV_INST_DIR."bin/"."\"";
$OV_HPCS_DIR= $OV_INST_DIR."hpcs/";
$OV_HPCS_DATA_DIR= $OV_DATA_DIR."hpcs/";
$OV_BIN_PATH = "\"".$OV_INST_DIR."bin/"."\"";
}
}
else{
$osDir="LIN";
$OV_PERFINST_DIR ="/opt/perf/bin/";
$OV_HPCS_DIR= "/opt/OV/hpcs/";
$OV_HPCS_DATA_DIR= "/var/opt/OV/hpcs/";
$OV_INST_DIR= "/opt/OV/";
$OV_DATA_DIR= "/var/opt/OV/";
$OV_BIN_PATH = "/opt/OV/bin/";
}
sub SetTimeDate(){
my $datetime = strftime "%m/%d/%y %H:%M:%S", localtime;
my $datetimestring = "[".$datetime."]";
return $datetimestring;
}
sub SettingPath(){
$CMD_OP=" >selfmon_op 2>&1";
if ( $osDir eq "WIN" ) {
$HPCS_START_CMD=$OV_BIN_PATH."ovc -start hpsensor >NUL 2>&1";
$HPCS_STOP_CMD=$OV_BIN_PATH."ovc -stop hpsensor >NUL 2>&1";
$del_file="del selfmon_op";
}else{
$HPCS_START_CMD=$OV_BIN_PATH."ovc -start hpsensor >/dev/null 2>&1";
$HPCS_STOP_CMD=$OV_BIN_PATH."ovc -stop hpsensor >/dev/null 2>&1";
$del_file="rm -rf selfmon_op";
}
}
sub print_usage(){
print "Usage: selfmon_configure [OPTION [<type>]]\n";
print " [-enable [-server <servername> -interval <interval>]]\n";
print " [-disable]\n";
print "Options:\n";
print " [-h|-help] => Print help\n";
print " [-e|-enable] => Enables AgentHealth feature on the node.(By default its enabled)\n";
print " [-s|-server] <server name> => Server Name where the AgentHealth Dashboard is installed\n";
print " [-i|-interval] <seconds> => Configured AgentHealth Interval to analyze(default:is 300 seconds)\n";
print " [-d|-disable] => Disables the AgentHealth feature on the node\n";
exit 1;
}
sub setXplSettings{
# my $avoid_hpcs_run = $_[0];
print SetTimeDate()." [selfmon_configure] Info: Enabling AgentHealth Feature\n";
$SET_SELFMON = "-ns agent.health -set OPC_SELFMON_ENABLE true";
$SelfMon = `$OV_BIN_PATH$CFG_SET_CMD $SET_SELFMON`;
print SetTimeDate()." [selfmon_configure] Info: Setting OPC_SELFMON_ENABLE = true\n";
if( defined $interval ){
$SET_INTERVAL = "-ns agent.health -set OPC_SELFMON_INTERVAL ".$interval;
my $SelfMonInterval = `$OV_BIN_PATH$CFG_SET_CMD $SET_INTERVAL`;
print SetTimeDate()." [selfmon_configure] Info: Setting OPC_SELFMON_INTERVAL to ".$interval."\n";
}
if( defined $server ){
$set_hbp=undef;
$SET_HBPVAL ="agent.health OPC_SELFMON_HBP";
$set_hbp =`$OV_BIN_PATH$CFG_GET_CMD $SET_HBPVAL`;
chomp($set_hbp);
my $check_hbp = uc($set_hbp);
chomp($server);
my $BBC_PING="bbcutil -ping ";
my $found =0;
$substring="eServiceOK";
my $serverconfig=`$OV_BIN_PATH$BBC_PING $server$CMD_OP`;
open my $fh, '<', "selfmon_op";
while (<$fh>) {
$found=1 if /\Q$substring/;
}
if ($found == 1) {
if(defined $set_hbp && $check_hbp eq FALSE){
$SET_HBP = "-ns agent.health -set OPC_SELFMON_HBP true";
my $set_hbp_false = `$OV_BIN_PATH$CFG_SET_CMD $SET_HBP`;
print SetTimeDate()." [selfmon_configure] Info: Changing the OPC_SELFMON_HBP from false to true\n";
}
$SET_NONDEFAULT_SERVER = "-ns agent.health -set OPC_SELFMON_SERVER ".$server;
my $setnondefaultserver = `$OV_BIN_PATH$CFG_SET_CMD $SET_NONDEFAULT_SERVER`;
print SetTimeDate()." [selfmon_configure] Info: Setting OPC_SELFMON_SERVER to ".$server."\n";
}
else{
$get_existing_server_val=undef;
$get_existing_server ="agent.health OPC_SELFMON_SERVER";
$get_existing_server_val =`$OV_BIN_PATH$CFG_GET_CMD $get_existing_server`;
chomp($get_existing_server_val);
if(defined $get_existing_server_val && $get_existing_server_val eq $server){
$SET_NONDEFAULT_SERVER = "-ns agent.health -clear OPC_SELFMON_SERVER ";
my $setnondefaultserver = `$OV_BIN_PATH$CFG_SET_CMD $SET_NONDEFAULT_SERVER`;
}
print SetTimeDate()." [selfmon_configure] Error: Not able to set OPC_SELFMON_SERVER to ".$server." as it is not reachable\n";
$sucess=False;
}
close $fh;
`$del_file`;
}
}
sub UnSetXplSettings{
#my $avoid_hpcs_run = $_[0];
print SetTimeDate()." [selfmon_configure] Info: Disabling AgentHealth Feature\n";
$SET_SELFMON = "-ns agent.health -set OPC_SELFMON_ENABLE false";
$SelfMon = `$OV_BIN_PATH$CFG_SET_CMD $SET_SELFMON`;
print SetTimeDate()." [selfmon_configure] Info: Setting OPC_SELFMON_ENABLE = false\n";
$SET_SELFMON_HBP = "-ns agent.health -set OPC_SELFMON_HBP false";
$SelfMon = `$OV_BIN_PATH$CFG_SET_CMD $SET_SELFMON_HBP`;
print SetTimeDate()." [selfmon_configure] Info: Setting OPC_SELFMON_HBP = false\n";
}
sub CreateJsonFiles{
if ( $osDir eq "WIN" ) {
my $data_dir = $OV_DATA_DIR;
my $hpcs_dir =$OV_HPCS_DIR;
$data_dir =~ s|/|\\|g;
$hpcs_dir =~ s|/|\\|g;
$process_stat="ProcessLevelStatus=".$data_dir."\\log\\ProcessLevelStatus.json\n";
$node_state="NodeStateInfo=".$data_dir."\\log\\NodeStateInfo.json\n";
$agent_over="AgentOverviewStatus=".$data_dir."\\log\\AgentOverview.json\n";
$system_txt="AgentLog=".$data_dir."\\log\\System.txt\n";
$hpcstrace_log="HpcsLog=".$data_dir."\\hpcs\\hpcstrace.log\n";
}else{
$process_stat="ProcessLevelStatus=".$OV_DATA_DIR."/log/ProcessLevelStatus.json\n";
$node_state="NodeStateInfo=".$OV_DATA_DIR."/log/NodeStateInfo.json\n";
$agent_over="AgentOverviewStatus=".$OV_DATA_DIR."/log/AgentOverview.json\n";
$system_txt="AgentLog=".$OV_DATA_DIR."/log/System.txt\n";
$hpcstrace_log="HpcsLog=".$OV_DATA_DIR."/hpcs/hpcstrace.log\n";
}
$regBBC ="regBBC=true\n";
}
sub setHPCSConfigurations{
my $avoid_hpcs_run = $_[0];
print SetTimeDate()." [selfmon_configure] Info: Setting hpsensor Configuration\n";
CreateJsonFiles();
if($avoid_hpcs_run == 1){
system($HPCS_STOP_CMD);
}
my @hpcs_new;
my @hpcs_final;
my $hpcs_log=0;
my $hpcs_mode=0;
my $hpcs_registry=0;
my $hpcs_runtime=0;
my $hpcs_hbp=0;
my $hpcs_event=0;
my $filename = $OV_HPCS_DATA_DIR.'hpcs.conf';
if(!open(FILE,$filename)){
print SetTimeDate()." [selfmon_configure] Error: Could not open the file ".$filename."\n";
$sucess=False;
return;
}
my @hpcs_lines = <FILE>;
close FILE;
foreach my $line (@hpcs_lines){
push(@hpcs_new,$line);
if( $line =~m/\[hpcs.log\]/){
$hpcs_log=1;
}
if( $line =~m/\[hpcs.runtime\]/){
$hpcs_runtime=1;
}
if( $line =~m/\[hpcs.event\]/){
$hpcs_event=1;
}
if ($line eq $process_stat){
$process_stat ="";
}
if($line eq $node_state){
$node_state ="";
}
if($line eq $agent_over){
$agent_over ="";
}
if ($line eq $system_txt){
$system_txt ="";
}
if ($line eq $hpcstrace_log){
$hpcstrace_log ="";
}
if ($line eq $regBBC){
$regBBC ="";
}
}
if($hpcs_log == 0){
$hpcs_log=1;
push(@hpcs_new,"[hpcs.log]\n");
push(@hpcs_new,$process_stat);
push(@hpcs_new,$node_state);
push(@hpcs_new,$agent_over);
$process_stat="";
$node_state="";
$agent_over="";
}
if($hpcs_event == 0){
$hpcs_event =1;
push(@hpcs_new,"[hpcs.event]\n");
push(@hpcs_new,$system_txt);
push(@hpcs_new,$hpcstrace_log);
$system_txt ="";
$hpcstrace_log ="";
}
if($hpcs_runtime == 0){
$hpcs_runtime =1;
push(@hpcs_new,"[hpcs.runtime]\n");
push(@hpcs_new,$regBBC);
$regBBC="";
}
foreach my $line (@hpcs_new){
push(@hpcs_final,$line);
if( ($line =~m/\[hpcs.log\]/) && ($hpcs_log == 1)){
if ($process_stat ne ""){
push(@hpcs_final,$process_stat);
}
if($node_state ne ""){
push(@hpcs_final,$node_state);
}
if($agent_over ne ""){
push(@hpcs_final,$agent_over);
}
}
if(( $line =~m/\[hpcs.event\]/) && ($hpcs_event == 1)){
if($system_txt ne ""){
push(@hpcs_final,$system_txt);
}
if($hpcstrace_log ne ""){
push(@hpcs_final,$hpcstrace_log);
}
}
if(( $line =~m/\[hpcs.runtime\]/) && ($hpcs_runtime == 1)){
if($regBBC ne ""){
push(@hpcs_final,$regBBC);
}
}
}
if(!open(FILE,">$filename")){
print SetTimeDate()." [selfmon_configure] Error: Could not open the file ".$filename."\n";
$sucess=False;
return;
}
print FILE @hpcs_final;
close FILE;
print SetTimeDate()." [selfmon_configure] Info: Updated the hpcs.conf file with the AgentHealth configurations\n";
if($avoid_hpcs_run == 1){
system($HPCS_START_CMD);
}
}
#####MAIN###
GetOptions("help" => \&print_usage,
"enable" => \$enable_selfmon,
"avoidhpcsrun" => \$avoid_hpcsrun,
"disable" => \$disable_selfmon,
"server=s" => \$server,
"interval=i" => \$interval) or die print_usage();
SettingPath();
if((defined $enable_selfmon || defined $server || defined $interval ) && defined $disable_selfmon) {
print_usage();
}elsif(defined $enable_selfmon) {
if(defined $avoid_hpcsrun){
setXplSettings();
setHPCSConfigurations(0);
}else{
setXplSettings();
setHPCSConfigurations(1);
}
}elsif(defined $disable_selfmon) {
UnSetXplSettings();
}
elsif(!defined $enable_selfmon && (defined $server || defined $interval )){
print SetTimeDate()." [selfmon_configure] Info: Please use the -enable option along with this option\n";
$sucess =False;
print_usage();
}
else{
$sucess =False;
print_usage();
}
if($sucess eq True){
$status_code = 0;
print SetTimeDate()." [selfmon_configure] Info: Updated the AgentHealth Configurations successfully\n";
}
else{
$status_code = 1;
print SetTimeDate()." [selfmon_configure] Error: Not updated the AgentHealth Configurations successfully\n";
}
exit($status_code);