#!/usr/bin/perl

# Variable Declaration

$dtstg		= localtime();
$shhm		= "/usr/bin/sh";
$VRTShm		= "/opt/VRTSvcs/bin";
$HaStat		= "$VRTShm/hastatus";
$opcmsgloc	= "/opt/OV/bin/OpC/opcmsg";
$osstat		= 0;
$ora_stg	= "ora_";
$cr		= "\n";
@ora_proc	= ("ora_pmon","ora_smon","ora_lgwr","ora_dbw","ora_ckpt","ora_reco");
@ora_proc81p	= ("ora_arch","ora_s0","ora_d0");
$lsn_proc	= "tnslsnr";
@ora_serv	= ("Agent","DataGatherer");

$dashstg	= "------------------------------------------------------------------\n";
$orprocstg	= "---------------------*** Oracle Processes ***---------------------\n";
$tnsprocstg	= "---------------*** Listener / tnslsnr Processes ***---------------\n";
$mettststg	= ("$ARGV[0]");
$prdtstg	= "-------------------".$dtstg."-------------------\n";

#------------------------------------------------------------------------
# OS Check

$OSstg		= $^O;chop($OMhm);	# OS variable

if ( $OSstg =~ /[wW]in/ ) {			# Windows
  $OMhm		= '.';
  $Winsqlrunstg	= "Winsqlrun";
  $OSsqlrunstg	= $Winsqlrunstg;
  $opcmsgloc	= "C:\\usr\\OV\\bin\\OpC\\intel\\opcmsg";
  $osstat		= 1;
}
else {						# Unix
  $OMhm		= `/usr/bin/pwd`;chop($OMhm);
  $HPsqlrunstg	= "$shhm $OMhm/HPsqlrun.sh";
  $SUNsqlrunstg	= "$shhm $OMhm/SUNsqlrun.sh";
  @OSstgsplt 	= split (/\s+/, $OSstg);
  $mchnm 		= $OSstgsplt[1];

  if ( -e "$HaStat" ) {				# Cluster Check
    $hastats = `$HaStat -sum|grep ONLINE`;
    @hastatssplt = split (/\s+/, $hastats);
    $mach_online = $hastatssplt[2];
    print "Found Primary Cluster Server: $mach_online\n";
    if ( $mach_online ne $mchnm ) {die "Current Server: $mchnm....Is Not Primary Cluster Server\n"}
  }

  if ( $OSstg =~ /[hH][pP]/ ) {						# HP
    $OSsqlrunstg	= $HPsqlrunstg;
  }
  elsif ( ($OSstg =~ /[sS]un/) || ($OSstg =~ /solaris/) ) {		# Sun
    $OSsqlrunstg	= $SUNsqlrunstg;
  }
}

#------------------------------------------------------------------------
# Environment allow file

$sid_env	= "$OMhm/oraenv.allow";

open(SEFILE, $sid_env) || die "Could not open $sid_env\n$!";
@sidenvstg = <SEFILE>;
close (SEFILE);

for ( $a = 0 ; $a < @sidenvstg ; $a++ ) {
  chop($sidenvstg[$a]);
  ($sidstg[$a],$lsnrstg[$a],$orahm[$a]) = split (/\|/, $sidenvstg[$a] );
}

#ARBORDEV|LISTENER|/oracle/base/product/8.1.5

#------------------------------------------------------------------------
# Metric allow file

$met_env	= "$OMhm/dbmetric.allow";

open(MEFILE, $met_env) || die "Could not open $met_env\n$!";
@metenvstg = <MEFILE>;
close (MEFILE);

for ( $b = 0 ; $b < @metenvstg ; $b++ ) {
  chop($metenvstg[$b]);
  @metricar = split (/\|/, $metenvstg[$b] );
  if ( $metricar[0] eq $mettststg ) {
    @nmetricar = @metricar
  }
}

#O5|ARBORDEV,ARBORTST|OTS,OTF,OLG,OAS,OPS,ORS,OHR
# Arg[0]|SID's|Sql Metric Script to run

@dbstg = split (/\,/, $nmetricar[1] );
@sliststg = split (/\,/, $nmetricar[2] );

#------------------------------------------------------------------------
# Metric Limit allow file

@scrstg		= ("OTS","ORS","OAS","OHR","OTF","OLG","OPS");
$ora_lim	= "$OMhm/oralimit.allow";

open(OLFILE, $ora_lim) || die "Could not open $ora_lim\n$!";
@oralimstg = <OLFILE>;
close (OLFILE);

for ( $z0 = 0 ; $z0 < @oralimstg ; $z0++ ) {chop($oralimstg[$z0])}

#OTS	=SYSTEM		ONLINE
#ORS	=SYSTEM		ONLINE
#OTF	=SYSTEM		13.6386719
#OHR	=97.73865 
#OPS	=1.40541734
#OLG	=.999995155
#OSR	=100
#OAS	=STOPPED
#OOV	=Oracle8i Enterprise Edition Release 8.1.5.0.0 - Production

#OTS|ONLINE|!=|normal
#ORS|ONLINE|!=|normal
#OAS|STARTED|!=|normal
#OHR|80|<|normal
#OTF|80|<|normal
#OLG|1|>|normal
#OPS|15|>|normal
#OOV|8.1.5|<|normal
#OSR|99|<|normal

#------------------------------------------------------------------------

print $dashstg.$prdtstg.$dashstg;

$li		= 0;

for ( $xyz = 0 ; $xyz < @dbstg ; $xyz++ ) {
  for ( $xx = 0 ; $xx < @sidstg ; $xx++ ) {
    if ( $sidstg[$xx] eq $dbstg[$xyz] ) {$OHM = $orahm[$xx]}
  }
  $inschkval[$xyz] = 0;
  $sqlscrhm1 = $OMhm."/ODS";

  if ($osstat) {$sqlscrhm1 = "ODS"}

  $insrunsh = "$OSsqlrunstg $dbstg[$xyz] $OHM $sqlscrhm1";
  print $dashstg;
  print $insrunsh."\n";
  system($insrunsh);

  $tmpdbstg[$xyz] = &toUpperCase($dbstg[$xyz]);
  $inslimlst = "ODS_".$tmpdbstg[$xyz].".txt";

  open(TMP2FILE, $inslimlst) || die "Could not open $inslimlst\n$!";
  @limtmpar2 = <TMP2FILE>;
  close (TMP2FILE);
  unlink $inslimlst;

  for ( $e1 = 0 ; $e1 < @limtmpar2 ; $e1++ ) {
    chop($limtmpar2[$e1]);
    $limtmpar2[$e1] =~ s/\s+$//;
    if ( $limtmpar2[$e1] eq "" ) {} 
    elsif ( $limtmpar2[$e1] eq "ACTIVE" ) {
      print "Success - Oracle DB Instance: $dbstg[$xyz] IS ACTIVE\n";
      $inschkval[$xyz] = 1;
    }
    elsif ( $limtmpar2[$e1] ne "ACTIVE" ) {
      $alert = "CRITICAL - Oracle DB Instance: $dbstg[$xyz] IS NOT ACTIVE";
      print "\t*****Failure - ITO Trap Being Sent *****\n";
      $sevstg = "critical";
      &opcmsgrun($alert,$dbstg[$xyz],$sevstg);
      $inschkval[$xyz] = 0;
    }
  }
  if ( $inschkval[$xyz] ) { 
    if (!$osstat) { &oraprocchk($dbstg[$xyz]) }
    else { &oraservchk($dbstg[$xyz]) }
  }
  else { print "Database Status is not in Proper State to continue.\n"}
}

sub oraprocchk {
  my($dbstgtst)=@_;
  $orapsfile	= "$OMhm/tmp_ps_ora.txt";
  $ft = $ft2 = $tst = $tst1 = $tst2 = $tst3 = 0;
  $psstg1 = "ps -ef | grep ora_smon | grep ? | grep $dbstgtst >";
  system($psstg1.$orapsfile);

  open(TPS2FILE, $orapsfile) || die "Could not open $orapsfile\n$!";
  @tstprocstg = <TPS2FILE>;
  close (TPS2FILE);
  unlink $orapsfile;

  for ( $tst1 = 0 ; $tst1 < @tstprocstg ; $tst1++ ) {
    chop($tstprocstg[$tst1]);
    @ntstprocstg = split (/\s+/, $tstprocstg[$tst1]);
    for ( $tst = 0 ; $tst < 12 ; $tst++ ) {
      if (($ntstprocstg[$tst] =~ /ora_/)&&($ntstprocstg[1] =~ /oracle/)) {$ft=$tst}
    }
  }

  if (!$ft) {$ft=9}
  $psstg = "ps -ef | grep oracle | grep ora_ | grep ? | grep $dbstgtst | awk '{print \$$ft}'>";
  system($psstg.$orapsfile);

  open(TPSFILE, $orapsfile) || die "Could not open $orapsfile\n$!";
  @procstg = <TPSFILE>;
  close (TPSFILE);
  unlink $orapsfile;

  #----------------------------------------------------------------------

  $oratnsfile	= "$OMhm/tmp_tns_ora.txt";

  $tnsstg1 = "ps -ef | grep tnslsnr | grep ? >";
  system($tnsstg1.$oratnsfile);

  open(TNS2FILE, $oratnsfile) || die "Could not open $oratnsfile\n$!";
  @tlsnr_stg = <TNS2FILE>;
  close (TNS2FILE);
  unlink $oratnsfile;

  for ( $tst3 = 0 ; $tst3 < @tlsnr_stg ; $tst3++ ) {
    chop($tlsnr_stg[$tst3]);
    @ntlsnr_stg = split (/\s+/, $tlsnr_stg[$tst3]);
    for ( $tst2 = 0 ; $tst2 < 12 ; $tst2++ ) {
      if (($ntlsnr_stg[$tst2] =~ /tnslsnr/)&&($ntlsnr_stg[1] =~ /oracle/)) {$ft2=($tst2+1)}
    }
  }

  if (!$ft2) {$ft2=10};
  $tnsstg = "ps -ef | grep oracle | grep tnslsnr | grep ? | awk '{print \$$ft2}'>";
  system($tnsstg.$oratnsfile);

  open(TNSFILE, $oratnsfile) || die "Could not open $oratnsfile\n$!";;
  @lsnr_stg = <TNSFILE>;
  close (TNSFILE);
  unlink $oratnsfile;

  #----------------------------------------------------------------------

  for ( $x3 = 0 ; $x3 < @procstg ; $x3++ ) { chop($procstg[$x3]) }
  for ( $y3 = 0 ; $y3 < @lsnr_stg ; $y3++ ) { chop($lsnr_stg[$y3]) }

  for ( $j = 0 ; $j < @ora_proc ; $j++ ) {
    $fndflag = 0;
    for ( $i = 0 ; $i < @procstg ; $i++ ) {
      if ( ( $procstg[$i] =~ /$ora_proc[$j]/ ) && ( $procstg[$i] =~ /$dbstgtst/ ) ) {
        print "Suiccess - Found Process: $ora_proc[$j] For Oracle DB: $dbstgtst in $procstg[$i]".$cr;
        $fndflag = 1;
      }
    }
    if ($fndflag) { print "\t*****Found Match - No Trap Sent*****\n" }
    elsif ( $procstg[$i] =~ /$ora_stg/ ) { print "Not Oracle Process: $procstg[$i]\n" }
    else {
      $alert = "CRITICAL - Oracle Process $ora_proc[$j] For DB $dbstgtst NOT Running";
      print "\t*****Failure - ITO Trap Being Sent *****\n";
      $sevstg = "critical";
      &opcmsgrun($alert,$dbstgtst,$sevstg);
    }
  }

  print $dashstg.$tnsprocstg.$dashstg;
  for ( $q = 0 ; $q < @lsnrstg ; $q++ ) {
    $fndflag2 = 0;
    if ( $lsnrstg[$q] =~ /none/ ) {
      print "No Oracle tnslsnr-Listener assigned for : $dbstg[$q]\n";
      $fndflag2 = 1;
    }
    for ( $s = 0 ; $s < @lsnr_stg ; $s++ ) {
      if ( ( $lsnr_stg[$s] =~ /$lsnrstg[$q]/ ) && (!$fndflag2) ) {
        print "Found $lsn_proc Process: $lsnrstg[$q] in $lsnr_stg[$s] for DB: $dbstgtst".$cr;
        $fndflag2 = 1;
      }
    }
    if ($fndflag2) { print "\t*****Found Success - No Trap Sent*****\n" }
    else {
      $alert = "CRITICAL - Oracle $lsn_proc Process $lsnrstg[$q] in $lsnr_stg[$s] for DB: $dbstgtst NOT Running";
      print "\t*****Failure - ITO Trap Being Sent *****\n";
      $sevstg = "critical";
      &opcmsgrun($alert,$lsn_proc,$sevstg);
    }
  }

  for ( $j = 0 ; $j < @sliststg ; $j++ ) {
    if ($fndflag && $fndflag2) {
      &sidhmsub($dbstgtst,$sliststg[$j])
    }
    else {
      print "Database Services are not in Proper State to continue.\n";
      print "Environment (.allow) files entries are case sensitive.\n";
    }
  }
}

sub oraservchk {
  my($dbstgtst)=@_;
  $oraservfile	= "tmp_serv_ora.txt";
  $ft = $ft2 = $tst = $tst1 = $tst2 = $tst3 = 0;
  $servstg1 = "net start >";
  system($servstg1.$oraservfile);

  open(TSERV2FILE, $oraservfile) || die "Could not open $oraservfile\n$!";
  @servstg = <TSERV2FILE>;
  close (TDSERV2FILE);
  unlink $oraservfile;

  for ( $x3 = 0 ; $x3 < @servstg ; $x3++ ) {chop($servstg[$x3])}

  for ( $j = 0 ; $j < @ora_serv ; $j++ ) {
    $fndflag = 0;
    for ( $i = 0 ; $i < @servstg ; $i++ ) {
      if ( ( $servstg[$i] =~ /$ora_serv[$j]/ ) && ( $servstg[$i] =~ /Oracle/ ) ) {
        print "Success - Found Service: $ora_serv[$j] for Oracle DB: $dbstgtst in $servstg[$i]".$cr;
        $fndflag = 1;
      }
    }
    if ($fndflag) { print "\t*****Found Match - No Trap Sent*****\n" }
    else {
      $alert = "ERROR - Oracle Service $ora_serv[$j] For DB $dbstgtst NOT Running";
      print "\t*****Failure - ITO Trap Being Sent *****\n";
      $sevstg = "minor";
      &opcmsgrun($alert,$dbstgtst,$sevstg);
    }
  }

  $osfndflag = 0;
  for ( $i2 = 0 ; $i2 < @servstg ; $i2++ ) {
    if ( ( $servstg[$i2] =~ /OracleService/ ) && ( $servstg[$i2] =~ /$dbstgtst/ ) ) {
      print "Success - Found Service: OracleService for DB: $dbstgtst\n";
      $osfndflag = 1;
    }
  }

  if ($osfndflag) { print "\t*****Found Match - No Trap Sent*****\n" }
  else {
    $alert = "CRITICAL - Oracle Service OracleService For DB $dbstgtst NOT Running";
    print "\t*****Failure - ITO Trap Being Sent *****\n";
    $sevstg = "critical";
    &opcmsgrun($alert,$dbstgtst,$sevstg);
  }

  print $dashstg.$tnsprocstg.$dashstg;
  for ( $q = 0 ; $q < @lsnrstg ; $q++ ) {
    $fndflag2 = 0;
    for ( $s = 0 ; $s < @servstg ; $s++ ) {
      if ( ( $servstg[$s] =~ /$lsnrstg[$q]/ ) && ( $servstg[$s] =~ /Oracle/ ) ) {
        print "Success - Found Listener Service: $lsnrstg[$q] for DB: $dbstgtst".$cr;
        $fndflag2 = 1;
      }
    }
    if ($fndflag2) { print "\t*****Found Success - No Trap Sent*****\n" }
    else {
      $alert = "CRITICAL - Oracle Listener Service: $lsnrstg[$q] for DB: $dbstgtst NOT Running";
      print "\t*****Failure - ITO Trap Being Sent *****\n";
      $sevstg = "critical";
      &opcmsgrun($alert,Listener,$sevstg);
    }
  }

  for ( $j = 0 ; $j < @sliststg ; $j++ ) {
    if ($osfndflag && $fndflag2) {
      &sidhmsub($dbstgtst,$sliststg[$j])
    }
    else {
      print "Database Services are not in Proper State to continue.\n";
      print "Environment (.allow) files entries are case sensitive.\n";
    }
  }
}

sub sidhmsub {
  my($dbstgfstg,$scrrunstg)=@_;

  for ( $zz = 0 ; $zz < @sidstg ; $zz++ ) {
    if ( $sidstg[$zz] eq $dbstgfstg ) {$OHM = $orahm[$zz]}
  }

  $sqlscrhm = $OMhm."/".$scrrunstg;
  $orarunsh = "$OSsqlrunstg $dbstgfstg $OHM $sqlscrhm";
  print $dashstg;
  print $orarunsh."\n";
  system($orarunsh);

  $dbstgfstg2 = &toUpperCase($dbstgfstg);
  $limfllst = $scrrunstg."_".$dbstgfstg2.".txt";

  for ( $c2 = 0 ; $c2 < @oralimstg ; $c2++ ) {
    ($scrlststg[$c2],$lmtstg[$c2],$cmpstg[$c2],$limsev[$c2]) = split (/\|/, $oralimstg[$c2] );
    if ( $scrlststg[$c2] eq $scrrunstg ) {
      &flchk($limfllst,$scrlststg[$c2],$lmtstg[$c2],$cmpstg[$c2],$limsev[$c2],$dbstgfstg);
    }
  }
}

# ERROR - Oracle  <metric_name> (<metric_value>) below threshold (<threshold_value>), Db instance: <instance>, tablespace: <tablespace>
# ERROR - Oracle tablespace freespace percentage (10.45%) below threshold (20%), Db instance: DEVJETT, tablespace: tableX

sub flchk {
  my($limfllstin,$scrlststgin,$lmtstgin,$cmpstgin,$sevstg2,$dbstgfstgin)=@_;
  my(@tmp,$tmp,$namestg,$statusstg,@limtmpar);

  print $dashstg;
  $#limtmpar = -1;
  $#tmp = -1;

  open(TMPFILE, $limfllstin) || die "Could not open $limfllstin\n$!";
  @limtmpar = <TMPFILE>;
  close (TMPFILE);
  unlink $limfllstin;

  for ( $e = 0 ; $e < @limtmpar ; $e++ ) {
    chop($limtmpar[$e]);
    if ( $limtmpar[$e] eq "" ) {}
    elsif (($scrlststgin eq "OTS") || ($scrlststgin eq "ORS")) {
      if ($scrlststgin eq "OTS") {$sqlchk="TableSpace"} else {$sqlchk="Segment"}
      $limtmpar[$e] =~ s/\s+$//;
      @tmp = split(/\s+/, $limtmpar[$e]);
      if ( $cmpstgin eq "!=" ) {
        if ( $tmp[1] ne $lmtstgin ) {
          $alert = "ERROR - Oracle DB Instance: $dbstgfstgin With $sqlchk: $tmp[0] IS NOT $lmtstgin";
          print "\t*****Failure - ITO Trap Being Sent *****\n";
          &opcmsgrun($alert,$dbstgfstgin,$sevstg2);
        }
        else { print "Success - Oracle DB Instance: $dbstgfstgin With $sqlchk: $tmp[0] IS $lmtstgin\n" }
      }
    }
    elsif ($scrlststgin eq "OTF") {
      $limtmpar[$e] =~ s/\s+$//;
      @tmp = split(/\s+/, $limtmpar[$e]);
      if ( $tmp[1] < $lmtstgin ) {
        $alert = "ERROR - Oracle tablespace freespace percentage ($tmp[1]%) below threshold ($lmtstgin%), Db instance: $dbstgfstgin, tablespace: $tmp[0]";
        print "\t*****Failure - ITO Trap Being Sent *****\n";
        &opcmsgrun($alert,$dbstgfstgin,$sevstg2);
      }
      else { print "Success - Oracle tablespace freespace percentage ($tmp[1]%) above threshold ($lmtstgin%), Db instance: $dbstgfstgin, tablespace: $tmp[0]\n" }
    }
    elsif (($scrlststgin eq "OHR") || ($scrlststgin eq "OPS") || ($scrlststgin eq "OLG") || ($scrlststgin eq "OSR")) {
      if ($scrlststgin eq "OLG") {$sqlchk="Latches"}
      elsif ($scrlststgin eq "OPS") {$sqlchk="Shared Pool"}
      elsif ($scrlststgin eq "OSR") {$sqlchk="Memory Sort Rate"}
      else {$sqlchk="Hit Ratio"}
      $limtmpar[$e] =~ s/\s+$//;
      $tmp = $limtmpar[$e];
      if ( $cmpstgin eq ">" ) {
        if ( $tmp > $lmtstgin ) {
          $alert = "ERROR - Oracle Metric $sqlchk: percentage ($tmp%) above threshold ($lmtstgin%), Db instance: $dbstgfstgin";
          print "\t*****Failure - ITO Trap Being Sent *****\n";
          &opcmsgrun($alert,$dbstgfstgin,$sevstg2);
        }
        else { print "Success - Oracle Metric $sqlchk: percentage ($tmp%) below threshold ($lmtstgin%), Db instance: $dbstgfstgin\n"; }
      }
      elsif ( $cmpstgin eq "<" ) {
        if ( $tmp < $lmtstgin ) {
          $alert = "ERROR - Oracle Metric $sqlchk: percentage ($tmp%) below threshold ($lmtstgin%), Db instance: $dbstgfstgin";
          print "\t*****Failure - ITO Trap Being Sent *****\n";
          &opcmsgrun($alert,$dbstgfstgin,$sevstg2);
        }
        else { print "Success - Oracle Metric $sqlchk: percentage ($tmp%) above threshold ($lmtstgin%), Db instance: $dbstgfstgin\n" }
      }
    }
    elsif ($scrlststgin eq "OAS") {
      $limtmpar[$e] =~ s/\s+$//;
      $tmp = $limtmpar[$e];
      if ( $cmpstgin eq "!=" ) {
        if ( $tmp ne $lmtstgin ) {
          $alert = "ERROR - Oracle DB Instance: $dbstgfstgin Where Archiver IS NOT $lmtstgin";
          print "\t*****Failure - ITO Trap Being Sent *****\n";
          &opcmsgrun($alert,$dbstgfstgin,$sevstg2);
        }
        else { print "Success - Oracle DB Instance: $dbstgfstgin Where Archiver IS $lmtstgin\n" }
      }
    }
    elsif ($scrlststgin eq "OOV") {
      $limtmpar[$e] =~ /\s+$/;
      @tmp = split(/\s+/, $limtmpar[$e]);
      for ( $t2 = 0 ; $t2 < @tmp ; $t2++ ) {
        if ( $tmp[$t2] =~ /\./ ) { print "Oracle version: $tmp[$t2]\n" }
      }
    }
    else {
      print "Unknown Input Format...$scrlststgin:...\n\t...$limtmpar[$e]\n";
    }
  }
}

#OTS	=SYSTEM		ONLINE
#ORS	=SYSTEM		ONLINE
#OTF	=SYSTEM		13.6386719
#OHR	=97.73865 
#OPS	=1.40541734
#OLG	=.999995155
#OSR	=100
#OAS	=STOPPED
#OOV	=Oracle8i Enterprise Edition Release 8.1.5.0.0 - Production

#OTS|ONLINE|!=|normal
#ORS|ONLINE|!=|normal
#OAS|STARTED|!=|normal
#OHR|80|<|normal
#OTF|80|<|normal
#OLG|1|>|normal
#OPS|15|>|normal
#OOV|8.1.5|<|normal
#OSR|99|<|normal

sub opcmsgrun {
  my($alert,$objstg,$sevstgin) = @_;
  $syscall1 = "$opcmsgloc application=DBView1.4 object=$objstg msg_grp=Database ";
  $syscall2 = "msg_text=\"$alert\" severity=$sevstgin";
  $syscall = $syscall1.$syscall2;
  print $syscall1."\n".$syscall2."\n";
  if ( $ARGV[1] ne "demo" ) {system($syscall)}
}

sub toLowerCase {
  my($string) = @_;
  $string =~ tr/A-Z/a-z/;
  $string;
}

sub toUpperCase {
  my($string) = @_;
  $string =~ tr/a-z/A-Z/;
  $string;
}

$tdshstg	= "-------------------";
$dtstg		= localtime();
print $dashstg.$tdshstg.$dtstg.$tdshstg."\n".$dashstg;
