# $Header: has/crfutl/ologdbg.pl.sbs /st_has_12.2.0.1.0/1 2016/11/11 17:53:17 shullur Exp $
#
# ologdbg.pl.sbs
# 
# Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
#
#    NAME
#      ologdbg.pl.sbs - This is the wrapper script for running loggerd in
#                       debug mode.
#
#    DESCRIPTION
#      This provides ability to run loggerd in debug mode and mine 
#      historical data from a fully backed up offline BDB database.
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#    MODIFIED   (MM/DD/YY)
#    shullur     11/08/16 - Backport shullur_bug-24686678 from main
#    shullur     11/03/16 - For fixing bug 24686678
#    mprasant    05/26/10 - spawn osysmond in debug mode
#    sujkumar    01/31/10 - CRF_HOME as ORACLE_HOME
#    sujkumar    01/06/10 - Remove LIBPATH
#    mprasant    09/12/09 - fix ologgerd debug mode breakage(due to dependency
#                           on osysmond)
#    sujkumar    01/05/09 - set correct PATH
#    sukumar     11/05/08 - Add check for stack up.
#    sukumar     11/05/08 - Creation.
#
use English;
use strict;
use Sys::Hostname;

my $FSSEP = "";
my $OSNAME = $^O;
my $CRF_HOME = "";
my $CRF_EXEC_PATH = "";
my $localhost  = hostname();
my $LIB_PATH;

if ($OSNAME =~ "Win")
{
  $FSSEP = "\\";
}
else
{
  $FSSEP = "/";
}
$CRF_HOME = "%CRFHOME%";

if ($CRF_HOME eq "")
{
  print "\n";
  print "Invalid CRFHOME. Did you miss the crfinst.pl -finish under 'root'?";
  print "\n";
  exit 1;
}

if ($OSNAME =~ "Win")
{
  $ENV{'PATH'}="$CRF_HOME"."$FSSEP"."bin;$ENV{'PATH'}";
}
else
{
  $ENV{'LD_LIBRARY_PATH'}="$CRF_HOME/lib:$ENV{'LD_LIBRARY_PATH'}";
}
$ENV{'CRF_HOME'}=$CRF_HOME;

my $crfhome="$CRF_HOME"."$FSSEP"."crf";

# strip out the domain name because that's what we used while creating pidfile.
$localhost =~ s/\..*//;

# let's see if the stack is up
my $runpth="$crfhome"."$FSSEP"."admin"."$FSSEP"."run";
my @clnfiles = ("$runpth"."$FSSEP"."crfmond"."$FSSEP"."s${localhost}.pid",
                "$runpth"."$FSSEP"."crflogd"."$FSSEP"."l${localhost}.pid",
                "$runpth"."$FSSEP"."crfcheck"."$FSSEP"."c${localhost}.pid",
                "$runpth"."$FSSEP"."crfproxy"."$FSSEP"."p${localhost}.pid");
my $found = 0;
my $pidf;
foreach $pidf (@clnfiles)
{
  open(PID_FILE, $pidf);
  while (<PID_FILE>)
  {
    if (kill(0, $_))
    {
      $found = 1;
    }
  }
  close(PID_FILE);
}

if ($found == 1)
{
  print("\nThe OS Tool stack is running on this node.\n");
  print("\nThe OS Tool stack needs to be down on this node for ");
  print("the debug mode \nloggerd to work. When trying to run ");
  print("loggerd in debug mode against an \nexisting BDB database, ");
  print("it is required that the stack be down.\nOnce the stack is ");
  print("down, please rerun the command.\n");
  print "\nBoth oclumon and crfgui must provide -m <nodename> and will work\n";
  print "in historical mode only i.e. '-d' must be passed to crfgui and\n";
  print "date range must be specified with oclumon using '-s' and '-e' or\n";
  print "using -last option.\n";
  print "\nUsage: ologdbg -d <bdb_path>\n";
  exit 3;
}
$ENV{'CRF_HOME'}=$CRF_HOME;
my $count = scalar(@ARGV);
if ($count != 2)
{
  print "\nThis is debug mode loggerd. It is used to run loggerd in a\n";
  print "special mode where an existing BDB database is specified on\n";
  print "the command line and the daemon starts to serve oclumon and\n";
  print "crfgui clients against that database. This can be used to analyze\n";
  print "a complete backup of OS Tool BDB offline on a separate machine.\n";
  print "It is useful to for scenarios where BDB expiration may be an issue\n";
  print "and the full BDB backup is made available offline for analysis.\n";
  print "\nThe OS Tool stack must not be running on this node.\n";
  print "\nBoth oclumon and crfgui must provide -m <nodename> and will work\n";
  print "in historical mode only i.e. '-d' must be passed to crfgui and\n";
  print "date range must be specified with oclumon using '-s' and '-e' or\n";
  print "using -last option.\n";
  print "\nUsage: ologdbg -d <bdb_path>\n";
  exit 1;
}
my $bdbp = @ARGV[1];
if (! -f "$bdbp"."$FSSEP"."crfclust.bdb")
{
  print "\nInvalid BDB path. BDB files must exist in path.\n";
  exit 2;
}

# set ORA_CRF_DEBUG to true.
$ENV{'ORA_CRF_DEBUG'}='1';
system ("runcrf");
system ("$CRF_HOME/bin/ologgerd -D @ARGV &");
exit 0;
