package Monitor::oracle;
######################################################################
# Copyright (c) 2000 Trivadis AG, Schweiz
# Copyright (c) 2000 Philip Markwalder  Philip.Markwalder@trivadis.com
# Desc: Big Sister Modul to check oracle databases
#######################################################################

require 5.001;
require Exporter;

use strict;
use vars qw(@ISA @EXPORT $VERSION);
use FileHandle;
use POSIX;
use DBI;
use DBI::DBD;   # simple test to make sure it's okay
use DBD::Oracle;

@ISA = qw(Exporter Monitor::Monitor);


sub new
{
    ####################
    # Var definitions
    ####################
    my ($this, $timeout) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};
    ######################


    bless($self, $class);
    $timeout=15 unless( $timeout );

    $self->{"htable"} = [];
    $self->{"timeout"} = $timeout;

    return($self);
}


sub add_check {
    ####################
    # Var definitions
    ####################  
    my ($server,$db_sid,$db_name,$item);
    my( $self, $alias, $ORACLE_CONNECT, $ORACLE_BASE,$ORACLE_HOME,$ORACLE_NLS_LANG,$ORACLE_NLS33,$TNS_ADMIN,$ORA_USER,$ORA_PASS) = @_;
    my( $htable ) = $self->{"htable"};

    #######################
    ($server,$db_sid,$db_name)=split(/:/,$ORACLE_CONNECT);
    $item = $alias . ".$db_name";
    ($item = $alias.".oracle") unless( $item );
    push( @$htable, join ("\x00" , $item,$ORACLE_CONNECT, $ORACLE_BASE,$ORACLE_HOME,$ORACLE_NLS_LANG,$ORACLE_NLS33,$TNS_ADMIN,$ORA_USER,$ORA_PASS ) );
}



sub check {
    ####################
    # Var definitions
    ####################  
    my( $self ) = @_;
    my $htable = $self->{"htable"};
    my ( $error_check );
    my ( $dbh,$sth,@row,$db_target,$server,$db_sid,$db_name,$line,@time_arr,$date );
    my ( $sql_statment,$rc,$fetch_array );    
    my( $item, $ORACLE_CONNECT, $ORACLE_BASE,$ORACLE_HOME,$ORACLE_NLS_LANG,$ORACLE_NLS33,$TNS_ADMIN,$ORA_USER,$ORA_PASS, $i );
    ########################

    @time_arr = localtime;
    $date=POSIX::strftime("%c",localtime);
		
	for( $i=0 ; $i<=$#$htable; $i++ )
	{
		($item,$ORACLE_CONNECT, $ORACLE_BASE,$ORACLE_HOME,$ORACLE_NLS_LANG,$ORACLE_NLS33,$TNS_ADMIN,$ORA_USER,$ORA_PASS) = split("\x00",$$htable[$i]);
		$ENV{ORACLE_HOME}=$ORACLE_HOME;
		$ENV{ORACLE_BASE}=$ORACLE_BASE;
		$ENV{TNS_ADMIN}=$TNS_ADMIN;
		print "ORACLE_MODULE:\n$item:\n$ORACLE_CONNECT\n$ORACLE_BASE\n$ORACLE_HOME\n$ORACLE_NLS_LANG\n$ORACLE_NLS33\n$ORA_USER\n$ORA_PASS\n" if ( $BigSister::common::dl>1 ) ;



		($server,$db_sid,$db_name)=split(/:/,$ORACLE_CONNECT);
	        $ENV{ORACLE_SID}=$db_sid;  
		my $DBI_data_source = "dbi:Oracle:$db_sid";
  		if( !( $dbh = DBI->connect($DBI_data_source,$ORA_USER,$ORA_PASS) ) )
  		{
			my $status = 0;
			$error_check=$DBI::errstr;
    			$line="status $server.$db_name red $date Cannot connect database $db_sid \n$error_check\n";
    			# restricted mode check bei connect usern.
    			if( index(uc($error_check),"RESTRICTED") >= 0)
    			{
      				$line="status $server.$db_name yellow $date Database $db_sid up in RESTRICTED mode\n";
				$status = 1;
    			}
			$self->report( $item, $status , $line  );
			print $item . " $line \n" if  ( $BigSister::common::dl>1 ) 
  		}
  		else
  		{
			$sql_statment='select LOGINS from check_db_inst';	
			if ( !( $sth = $dbh->prepare("$sql_statment") ) ) 
    			{
      				$line="status $server.$db_name red $date $db_sid Cannot prepare statement\n$DBI::errstr\n";
				$self->report( $item, 0 , $line  );
				print $item . " $line \n" if  ( $BigSister::common::dl>1 ) 
    			}
    			else
    			{
      				$rc=$sth->execute;
      				if( $rc )
      				{
        				$fetch_array = $sth->fetchall_arrayref;
        				print $fetch_array->[0][0]."\n";
        				if ($fetch_array->[0][0] eq "ALLOWED")
        				{
		          			$line="status $server.$db_name green $date Database $db_sid available\n";
						$self->report( $item, 3 , $line  );
						print $item . " $line \n" if  ( $BigSister::common::dl>1 ) 
        				}
        				else
        				{
          					if ($fetch_array->[0][0] eq "RESTRICTED")
          					{
            						$line="status $server.$db_name yellow $date Database $db_sid up with RESTRICTED mode\n";
							$self->report( $item, 1 , $line  );
							print $item . " $line \n" if  ( $BigSister::common::dl>1 ) 
          					}
        				}
      				}
      				else
      				{
        				$line="status $server.$db_name red $date $db_sid Cannot prozess statement\n$DBI::errstr\n";
					$self->report( $item, 2 , $line  );
					print $item . " $line \n" if  ( $BigSister::common::dl>1 ) 
      				}
      				$sth->finish;
    			}
    			$dbh->disconnect;
  		}                        

		
	};
}




1;
