#!/usr/bin/perl
# 
# diagcollection.pl
# 
# Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      diagcollection.pl - collects and cleans up diagnostic information 
#
#    DESCRIPTION
#      <short description of component this file declares/defines>
#
#    NOTES
#
# Usage:
# diagcollection --collect :: collects diagnostic information
#                --clean   :: cleans up diagnostic information archive
#                --coreanalyze   :: extracts diag info from core files 
#
#    MODIFIED   (MM/DD/YY)
#       papatel  08/28/09 - fixing bug 8811954
#       papatel  04/29/08 - making changes for bug 6899547 wrt crshome
#       jcreight 06/19/07 - XbranchMerge jcreight_bug-6138698 from main
#       jcreight 06/19/07 - review comments + collect patch logs
#       jcreight 06/18/07 - Fix 6138698
#       rajayar  04/02/07 - adr changes
#       rajayar  02/15/07 - make adr changes
#       gdbhat   01/11/07 - added code for AIX usage
#       rajayar  02/24/06 - core collection 
#       rajayar  08/05/05 - filename fix
#       rajayar  06/01/05 - update copyright information
#       rajayar  05/23/05 - change perl location, improve core analyze. 
#       rajayar  04/19/05 - updates to the log location 
#       rajayar  11/22/04 - rajayar_diagcoll_script
#       rajayar  11/09/04 - Creation 

print "Production Copyright 2004, 2007, Oracle.  All rights reserved\n"; 
print "Cluster Ready Services (CRS) diagnostic collection tool\n";


use Sys::Hostname;
use Getopt::Long;
use POSIX;
use File::Glob qw(:globally :case);
use File::Spec::Functions;
use File::Find ();
use DirHandle;
#use Local::Time;

sub help();
sub collect_unix();
sub collect_win32();
sub clean_win32();
sub clean_unix();
sub coreanalyze();
sub collectDiagForAdr();

$HOSTNAME = hostname;

# archive files in chunks to avoid command line length limits
# this is the limit for the command line filename list length
$FLISTLEN = 1024;

if ($HOSTNAME eq "")
{
  print "Set HOSTNAME environment variable and then re-invoke this script.\n";
  exit;
}

# If IP address, do not strip.
if ($HOSTNAME !~ /(\d{1,3}\.){3}\d{1,3}/)
{
  $HOSTNAME =~ s/^([^.]+)\..*/$1/; # strip domain name off of host

  #lowercase the hostname
  $HOSTNAME=lc($HOSTNAME);
}

$PLATFORM = $^O;
$HPUX = "hpux";
$WIN32 = "MSWin32";
$LINUX = "linux";
$AIX = "aix";
$SOLARIS = "solaris";


if ($PLATFORM eq $WIN32)
{
  $ZIP = "zip -q";
  $DEL = "del";
  $COPY = "copy";
  #zips
  $CRSDATA_ZIP = "crsdata.zip";
  $ORADATA_ZIP = "oradata.zip";
  $OCRDATA_ZIP = "ocrdata.zip";
  require Win32::TieRegistry;
  import Win32::TieRegistry (Delimiter => '/');
}
else
{
  our $cwd;
  chop($cwd = `pwd`) || die "Can't find current directory: $!\n";
  if (!(-w $cwd))
  {
     print("Error: current directory $cwd is not writable\n");
     exit;
  }

  if ($PLATFORM eq $HPUX) {
    $GZIP = "/usr/contrib/bin/gzip";
  }
  else {
    $GZIP = "/bin/gzip";
  }

  $COPY = "cp";
  $TAR = "/bin/tar ";
  $CF = "-cf";
  $RF = "-rf";
  $RM = "/bin/rm -rf";
                                                                                
  # TARS
  $CRSDATA_TAR = crsData_.$HOSTNAME.'.'.tar;
  $ORADATA_TAR = oraData_.$HOSTNAME.'.'.tar;
  $OCRDATA_TAR = ocrData_.$HOSTNAME.'.'.tar;
  $COREDATA_TAR = coreData_.$HOSTNAME.'.'.tar;
  $SYSLOG_TAR = osData_.$HOSTNAME.'.'.tar;

  #GZIPS
  $CRSDATA_GZIP = $CRSDATA_TAR.'.'.gz;
  $ORADATA_GZIP = $ORADATA_TAR.'.'.gz;
  $OCRDATA_GZIP = $OCRDATA_TAR.'.'.gz;
  $COREDATA_GZIP = $COREDATA_TAR.'.'.gz;
  $SYSLOG_GZIP = $SYSLOG_TAR.'.'.gz;

  $CRSDATA_PATH = catfile($cwd, $CRSDATA_TAR);
  $SYSLOG_PATH = catfile($cwd, $SYSLOG_TAR);
}

GetOptions( "clean" => \$clean,
            "collect" => \$collect,
            "help" => \$help,
            "crs" =>  \$crs,
            "oh" => \$oh,
            "all" => \$all,
            "core" => \$core,
            "coreanalyze" => \$coreanalyze,
            "adr:s" => \$adr,
            "crshome:s" => \$crshome,
            "afterdate:s" => \$afterdate,
            "aftertime:s" => \$aftertime,
            "beforetime:s" => \$beforetime,
            "level:s" => \$level);

#Set crshome from init.cssd, only if user has not supplied the value from cmdline
if (! $crshome )
{
  if ($PLATFORM eq $HPUX) {
	$ID="/sbin/init.d";
  } elsif ($PLATFORM eq $LINUX) {
	$ID="/etc/init.d";
  } elsif ($PLATFORM eq $SOLARIS) {
	$ID="/etc/init.d";
  } elsif ($PLATFORM eq $AIX) {
	$ID="/etc";
  } elsif ($PLATFORM eq $WIN32) {
        my $swkey=$Registry->{"LMachine/Software/Oracle/"};
        foreach $entry ( $swkey->SubKeyNames ) {
           $crshome = $swkey->{$entry . "/ORA_CRS_HOME"};
	   last if( $crshome );
        }
	if(! $crshome) {
	   print "CRS home not present in the registry\n";
           print "specify -crshome argument\n";
	   exit;
	}
  } else  {
      print "Error: Unknown Operating System for looking up CRS home\n";
      print "specify -crshome argument\n";
      exit;
  }
  if ($PLATFORM ne $WIN32) {
      $crshome = system("$ID/init.cssd home");
  }
}

#Set help to true if none of the args are passed 
if (! $clean && ! $collect && ! $coreanalyze)
{
  help(); 
}

#Collect all by default
if ($collect) 
{
  if (! $crs && !$oh && !$adr) {
    $all=true;
  }

  if ($oh || $all)
  {
    $ORACLE_HOME=$ENV{'ORACLE_HOME'};

    if($ORACLE_HOME eq "")
    {
      print "Set ORACLE_HOME environment variable and then re-invoke this script.\n";
      exit;
    }
  }

  if ($PLATFORM eq $WIN32) {
    collect_win32();
  }
  else {
    collect_unix();
  }
} 

#clean up archives
if ($clean)
{
  if ($PLATFORM eq $WIN32) {
   clean_win32();
  }
  else {
   clean_unix();
  }
}

sub clean_win32() {
  print "Cleaning up zip files \n";
  system "$DEL ${CRSDATA_ZIP} ${ORADATA_ZIP} ${OCRDATA_ZIP} ${HOSTNAME}_OCRDUMP ${HOSTNAME}_OCRCHECK ${HOSTNAME}_OCRBACKUP";
  print "Done";
  exit;
}

sub clean_unix() {
  print "Cleaning up tar and gzip files\n";
  system("${RM}  ${CRSDATA_GZIP} ${ORADATA_GZIP} ${OCRDATA_GZIP} ${COREDATA_GZIP} ${CRSDATA_TAR} ${ORADATA_TAR} ${OCRDATA_TAR} ${COREDATA_TAR}");
  print "Done\n";
  exit;
}

#collect os system logs
#linux system logs are /var/log/messages*
#solaris system logs are /var/adm/messages*
#hpux system logs are /var/adm/syslog/*syslog.log
#aix system logs are /var/adm/ras/*log*
#  System log locations should be defined as absolute paths, but
#   will be converted to relative paths for archiving
sub collectsyslog() {
  my $system_logs = "";
  if ($PLATFORM eq $LINUX)
  {
    $system_logs = "/var/log/messages*";
  }
  elsif ($PLATFORM eq $SOLARIS)
  {
    $system_logs = "/var/adm/messages*";
  }
  elsif ($PLATFORM eq $HPUX)
  {
    $system_logs = "/var/adm/syslog/*syslog.log";
  }
  elsif ($PLATFORM eq $AIX)
  {
    $system_logs = "/var/adm/ras/*log*";
  }
  else
  {
    print "No support for OS log collection on this platform: $PLATFORM\n";
    return;
  }
  print "Collecting OS logs\n";
  # change to / to remove leading slashes
  chdir("/") || die "unable to change directory to /: $!\n";

  # remove leading slashes to convert absolute pathnames to relative
  #  paths to avoid warning messages from TAR.
  grep(s#^/##, $system_logs);

  system("${TAR} ${afterdatevar} ${CF} ${SYSLOG_PATH} $system_logs");

  # change back to initial current directory
  chdir($cwd) || die "Unable to change directory back to $cwd: $!\n";

  system("${GZIP} ${SYSLOG_TAR}");
}


sub collect_win32() {
  #Collecting log files
  if ($crs || $all)
  {
     if ($crshome)
     {
     print "The following CRS diagnostic archives will be created in the local directory.\n";
     print "${CRSDATA_ZIP} -> logs and traces from CRS home \n";
     print "${OCRDATA_ZIP} -> ocrdump, ocrcheck etc \n";
     print "Collecting crs data\n";

     system("${ZIP} ${CRSDATA_ZIP} ${crshome}/log/${HOSTNAME}/alert${HOSTNAME}.log ${crshome}/log/${HOSTNAME}/cssd/* ${crshome}/log/${HOSTNAME}/crsd/* ${crshome}/log/${HOSTNAME}/evmd/* ${crshome}/log/${HOSTNAME}/client/* ${crshome}/log/${HOSTNAME}/racg/*;");

     print "Collecting OCR data \n";
     system("${crshome}/bin/ocrdump ${HOSTNAME}_OCRDUMP");
     system("${crshome}/bin/ocrcheck >  ${HOSTNAME}_OCRCHECK");
     system("${crshome}/bin/ocrconfig -showbackup >  ${HOSTNAME}_OCRBACKUP");
     system("${ZIP} ${OCRDATA_ZIP}  ${HOSTNAME}_OCRDUMP ${HOSTNAME}_OCRCHECK ${HOSTNAME}_OCRBACKUP;");
     }
     else {
      print "Mandatory argument 'crshome' is missing. \n";
      exit;
     }
  }
  if ($oh || $all)
  {
    print "The following Oracle Home diagnostic archives will be created in the local directory.\n";
    print "${ORADATA_ZIP} -> logs and traces from Oracle Home\n";
    print "Collecting oracle home data\n";
    system("${ZIP} ${ORADATA_ZIP} ${ORACLE_HOME}/log/${HOSTNAME}/racg ${ORACLE_HOME}/log/${HOSTNAME}/client; }");
  }

  if ($adr)
  {
    collectDiagForAdr();
  }

  print "Done \n";

  exit;
 }

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

# match non-core files for find command
sub noncore {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);

    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
    -f $_ &&
    ! /^core.*\z/s &&
    push @filelist, $File::Find::name;
}

# match all files for find command
sub allfiles {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);

    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
    -f $_ &&
    push @filelist, $File::Find::name;
}

#collect diag 
sub collect_unix() {
  
   if ($afterdate)
   {
     $afterdatevar="--after-date \"${afterdate}\"";
   }
   else
   {
     $afterdatevar = "";
   }
 
  #Collecting log files 
  if ($crs || $all)
  {
     my @log_list;
     my $patchlogs;

     if ($crshome)
     {
       print "The following CRS diagnostic archives will be created in the local directory.\n";
       print "${CRSDATA_GZIP} -> logs,traces and cores from CRS home. Note: core files will be packaged only with the -core option. \n";
       print "${OCRDATA_GZIP} -> ocrdump, ocrcheck etc \n";
       print "${COREDATA_GZIP} -> contents of CRS core files in text format\n\n";

       print "Collecting crs data\n";

       # any patch logs under <crs home>/install/*.log?
       opendir(LOG, catfile(${crshome}, "install"));
       @log_list = sort(grep(/.log$/, readdir(LOG)));
       closedir(LOG);
       if (scalar(@log_list) >= 1) 
       {
         $patchlogs="${crshome}/install/\*.log";
       }
       # make install log file names relative to CRS home
       grep($_ = catfile("install", $_), @log_list);

       # change to CRS home to shorten archive path names for tar
       chdir($crshome) || die "unable to change directory to ${crshome}: $!\n";

       our @filelist;

       @filelist = @log_list;
    
       if ($core)
       {
         # Traverse desired directories
         File::Find::find({wanted => \&allfiles}, "log/${HOSTNAME}");
         File::Find::find({wanted => \&allfiles}, "cfgtoollogs");
       }
       else
       {
         # Traverse desired directories
         File::Find::find({wanted => \&noncore}, "log/${HOSTNAME}");
         File::Find::find({wanted => \&noncore}, "cfgtoollogs");
       }

       $first = 1;

       while ($#filelist > 0) {
           @cmdlist = ();
           $cmdpos = 0;         # file # in cmdlist
           $cmdlistlen = 0;

           # put $FLISTLEN chars of filenames at a time into cmdlist
           while ($cmdlistlen < $FLISTLEN) {
              last if(!defined($filelist[0])) ;
              $cmdlist[$cmdpos++] = $filelist[0];
              shift(@filelist);
              $cmdlistlen = length(join(' ', @cmdlist));
           }

           if ($first == 1)
           {
                @cmd = (${TAR}, ${afterdatevar}, ${CF}, ${CRSDATA_PATH}, @cmdlist);
                $first = 0;
           }
           else
           {
                @cmd = (${TAR}, ${afterdatevar}, ${RF}, ${CRSDATA_PATH}, @cmdlist );
           }
           system("@cmd");
       }
       # archive remaining files
       if ($#filelist > 0) {
         @cmd = (${TAR}, ${afterdatevar}, ${RF}, ${CRSDATA_PATH}, @filelist );
         system("@cmd");
       }

       system("${GZIP} ${CRSDATA_PATH}");

       # change back to initial current directory
       chdir($cwd) || die "Unable to change directory back to $cwd: $!\n";

       print "Collecting OCR data \n";
       system("${crshome}/bin/ocrdump ${HOSTNAME}_OCRDUMP");
       system("${crshome}/bin/ocrcheck >  ${HOSTNAME}_OCRCHECK");
       system("${crshome}/bin/ocrconfig -showbackup >  ${HOSTNAME}_OCRBACKUP");
       system("${TAR}  ${afterdatevar} ${CF} ${OCRDATA_TAR}  ${HOSTNAME}_OCRDUMP ${HOSTNAME}_OCRCHECK ${HOSTNAME}_OCRBACKUP; ${GZIP} ${OCRDATA_TAR}");

       print "Collecting information from core files\n";
       coreanalyze();
       system("${RM}  ${HOSTNAME}_OCRDUMP ${HOSTNAME}_OCRCHECK ${HOSTNAME}_OCRBACKUP");
      }
      else {
        print "Mandatory argument 'crshome' is missing. \n";
        exit;
     }
  }

  if ($oh || $all)
  {
    print "The following Oracle Home diagnostic archives will be created in the local directory.\n";
    print "${ORADATA_GZIP} -> logs, traces and cores from Oracle Home\n";
    print "Collecting oracle home data\n";
    system("${TAR}  ${afterdatevar} ${CF} ${ORADATA_TAR} ${ORACLE_HOME}/log/${HOSTNAME}/racg ${ORACLE_HOME}/log/${HOSTNAME}/client ; ${GZIP} ${ORADATA_TAR}");
  }

   if ($adr)
  {
    collectDiagForAdr();
  }
  collectsyslog();
  exit;
}

#analyze corefiles in CRS Home
if ($coreanalyze) { 
  if ($PLATFORM eq $WIN32)
  {
   help();
   exit;
  }

  if ($crshome)
  {
   coreanalyze();
  }
  else 
  {
   print "Mandatory argument 'crshome' is missing.\n";
   exit;
  }

}

sub coreanalyze()
{
  @dir_array = ("crsd", "evmd", "cssd") ;
  @prod_array = ("crsd", "evmd", "ocssd") ;
                                                                                                                                                                       
  $ending_value = scalar(@prod_array) ;
                                                                                                                                                                       
  # @Array returns a scalar in this context
  for($counter=0 ; $counter < $ending_value ; $counter++)
  {
    $dirtoget=$crshome."/log/".$HOSTNAME."/".$dir_array[$counter];
    opendir(IMD, $dirtoget) || die("Cannot open directory");
    @thefiles= readdir(IMD);
    closedir(IMD);
                                                                                                                                                                       
   foreach $f (@thefiles)
   {
    unless ( ($f eq ".") || ($f eq "..") )
    {
      if ( $f =~ /core/)
      {
        $coreexists=true;
        $corefile = $dirtoget."/".$f;
        $execname = $crshome."/"."bin/".$prod_array[$counter]."."."bin";
        $outfile = $f.".".$prod_array[$counter]."."."txt";

        
        $info_cmd="gdb_info_cmd";
        $RMF="/bin/rm -f";

        if (-e "/usr/bin/gdb")
        {
          # Get the backtrace of all threads
          open OUTFILE, ">> $info_cmd";
          print OUTFILE "set pagination off\n";
          #print OUTFILE "set logging on\n";
          #print OUTFILE "set logging overwrite on\n";
          print OUTFILE "where\n";
          print OUTFILE "display\n";
          print OUTFILE "thread apply all bt\n";
          print OUTFILE "quit\n";


          $DEBUGGER="/usr/bin/gdb"; 
          $DEBUGCMD="${DEBUGGER} --nx --bat --comm=${info_cmd} --e=${execname} --core=${corefile} > ${outfile}";
        }

        elsif (-e "/opt/SUNWspro/bin/dbx")
        {
          open OUTFILE, ">> $info_cmd";
          print OUTFILE "where\n";
          print OUTFILE "enable all\n";
          print OUTFILE "corefile\n";
          print OUTFILE "thread info\n";
          print OUTFILE "dump\n";
          print OUTFILE "list\n";
          print OUTFILE "quit\n";
 
          $DEBUGGER="/opt/SUNWspro/bin/dbx";
          $DEBUGCMD="${DEBUGGER} --r --c ${info_cmd} ${execname} ${corefile} > ${outfile}";
        }
 
        elsif (-e "/bin/dbx")
        {
          open OUTFILE, ">> $info_cmd";
          print OUTFILE "where\n";
          print OUTFILE "enable all\n";
          print OUTFILE "corefile\n";
          print OUTFILE "thread info\n";
          print OUTFILE "dump\n";
          print OUTFILE "list\n";
          print OUTFILE "quit\n";
 
          $DEBUGGER="/bin/dbx";
          $DEBUGCMD="${DEBUGGER} -c ${info_cmd} ${execname} ${corefile} > ${outfile} 2>&1";
        }

        else
        {
          print "Debugger could not be located on this system\n";
          return;
        }
     
        system("${DEBUGCMD}");
        system("${RMF} ${info_cmd}");
        system("${RMF} core");

       }
      }
    }
  }

  if ($coreexists)
  {
    system("${TAR} ${CF} ${COREDATA_TAR} core*.txt; ${GZIP} ${COREDATA_TAR}");
    system("${RMF} core*.txt");
  }

  else 
  {
    print "No corefiles found \n";
  }
}

if ($help)
{
 help();
}

# This collects information as required by ADR.
# If aftertime or before time are passed, this collects files
# after matching the clsd timestamp in the logs.
# 2 latest files from CRSD logs, 2 from CSSD, 1 from the rest and 
# the alert log and copies them to the destination directory
sub collectDiagForAdr() {

    if (! $crshome)
    {
     print "Mandatory argument 'crshome' is missing. \n";
     exit;
    }

    
    # copy alert file
    my $alertfile = "${crshome}/log/${HOSTNAME}/alert${HOSTNAME}.log";
    system("${COPY} $alertfile ${adr}");

    if ($beforetime ne "" || $aftertime ne "")
    {

      # match files in the follwoing directories
      my @logDirs = ("cssd", "evmd", "crsd", "racg", "client");

      $clsdbeforetime= convertTime($beforetime);
      $clsdaftertime= convertTime($aftertime);

      foreach $dir (@logDirs)
      {
       my @files = getFilesInDir("${crshome}/log/${HOSTNAME}/$dir");

       # control loop with the LOOP label
       LOOP: foreach $infile (@files)
       { 
        if (-f  "${crshome}/log/${HOSTNAME}/$dir/$infile")
        {
          open (INF, "${crshome}/log/${HOSTNAME}/$dir/$infile") or
            next;
        }

        while (<INF>)  # While still input lines in the file...
        { 

            #build pattern based on clsd timestamp (yyyy-mm-dd hh:min:sec.msec)
            $pattern = '(\d{4})-(\d{2})-(\d{2}).(\d{2}):(\d{2}):(\d{2}).(\d{3})';


            if ($_ =~  /$pattern/ ) 
            {
              my $year  = $1;
              my $month = $2;
              my $day   = $3;
              my $hour  = $4;
              my $min   = $5;
              my $sec   = $6;

              my $time = "$year$month$day$hour";

              #if time is greater than aftertime and less than before time, copy the file
              if (($time ge $clsdaftertime) && ($time le $clsdbeforetime))
              {
                print "Copying  ${crshome}/log/${HOSTNAME}/${dir}/${infile} to ${adr} \n";
                system("${COPY} ${crshome}/log/${HOSTNAME}/${dir}/${infile} ${adr}");             
                next  LOOP;  
              }
            }
         }
         close (INF);
      }  
     }
    }
    else
    {
      # copy 2 latest files from CRSD  
      my @files = getFilesInDir("${crshome}/log/${HOSTNAME}/crsd");

      foreach $file (@files)
      { 
       if ($file =~ m/log/ || $file =~ m/l01/)
       {
        print "Copying  ${crshome}/log/${HOSTNAME}/crsd/${file} to ${adr} \n";
        system("${COPY} ${crshome}/log/${HOSTNAME}/crsd/${file} ${adr}");
       }
      }
    

      # copy 2 latest files from CSSD : the .log file and .l01 file 
      my @files = getFilesInDir("${crshome}/log/${HOSTNAME}/cssd");

      foreach $file (@files)
      { 
       if ($file =~ m/log/ || $file =~ m/l01/)
       {
        print "Copying  ${crshome}/log/${HOSTNAME}/cssd/${file} to ${adr} \n";
        system("${COPY} ${crshome}/log/${HOSTNAME}/cssd/${file} ${adr}");
       }
      }

      # copy the latest file from EVMD 
      my @files = getFilesInDir("${crshome}/log/${HOSTNAME}/evmd");

      foreach $file (@files)
      { 
       if ($file =~ m/log/ ) 
       {
         print "Copying  ${crshome}/log/${HOSTNAME}/evmd/${file} to ${adr} \n";
         system("${COPY} ${crshome}/log/${HOSTNAME}/evmd/${file} ${adr}");
       }
      }

      # copy the latest file from client  
      my @files = getFilesInDir("${crshome}/log/${HOSTNAME}/client");

      foreach $file (@files)
      { 
       if ($file =~ m/log/ ) 
       {
         print "Copying  ${crshome}/log/${HOSTNAME}/client/${file} to ${adr} \n";
         system("${COPY} ${crshome}/log/${HOSTNAME}/client/${file} ${adr}");
       }
      }
    }

    print "Done. \n";
    exit;
}

##
# convertTime 
# getlocal time if arg passed in empty or construct time.
#
sub convertTime   {

  my $timetoconvert = shift;

  # if time is not passed, get the local time 
  if ($timetoconvert eq "")
  {
    ($sec,$min,$hour,$day,$month,$year,$wday,$yday,$isdst) = localtime();

    $year += 1900;
    return "$year$month$day$hour";   
  }
  else 
  {
    $year  = substr $timetoconvert,0, 4;
    $month = substr $timetoconvert,4, 2;
    $day   = substr $timetoconvert,6, 2;
    $hour  = substr $timetoconvert,8, 2;
  }

  return "$year$month$day$hour";

}

sub getFilesInDir {

  my $dir = shift;
  my $dh = DirHandle->new($dir); #die "can't opendir $dir: $!";

  # sort based on access time and return a reversed list. The latest first.
  # dont list cors files and default files
  # Use this if you want the latest file to be listed 
  # my @files = #reverse map {$_ => (stat($_))[9]}
  my @files = 
           grep {  !/^\./   } 
           grep {  !/^core/   } 
           grep {  !/^*OUT/   } 
           $dh->read();
  return @files; 
}

sub help() {
 print "diagcollection\n";
 print "    --collect  \n";    
 print "             [--crs] For collecting crs diag information \n";
 print "             [--oh]  For collecting oracle home diag information \n";
 print "             [--adr] For collecting diag information for ADR\n";
 print "             [--all] Default.For collecting all diag information. \n";
 print "             [--core] UNIX only. Package core files with CRS data \n";
 print "             [--afterdate] UNIX only. Collects archives from the specified date. Specify in mm/dd/yyyy format\n";
 print "             [--aftertime] Supported with -adr option. Collects archives after the specified time. Specify in YYYYMMDDHHMISS24 format\n";
 print "             [--beforetime] Supported with -adr option. Collects archives before the specified date. Specify in YYYYMMDDHHMISS24 format\n";
 print "             [--crshome] Argument that specifies the CRS Home location \n";
 print "             NOTE: \n";
 print "             1. You can also do the following \n";
 print "                ./diagcollection.pl --collect --crs --oh \n";
 print "             2. ORACLE_HOME env variable needs to be set. \n";
 print " \n";

 print "     --clean        cleans up the diagnosability\n";
 print "                    information gathered by this script\n";

 print " \n";
 print "     --coreanalyze  UNIX only. Extracts information from core files\n";
 print "                    and stores it in a text file\n";
}
