use DBI;

$ENV{ORACLE_HOME} = '/opt/oracle/product/8.1.7';
my $dbuser = 'db_username';
my $dbpass = 'db_password';
my @db = qw( instance1 );

foreach my $db (@db) {
	my $result = check_db($db);
	if($result) {
		foreach (@$result) {
			if ( @$_[0] =~ /^\bOPEN\b$/ ) {
				debug($db,'OK');
			} else {
				debug($db,'DOWN');
				logit("ORACLE $db down, monitor should be true");
				exit 0;
			}
		}
	} else {
		logit("ORACLE $db down, but should be true");
		exit 0;
	}
}

sub check_db {
	my ($db) = shift;
	my ($dbh) = DBI->connect("dbi:Oracle:$db", $dbuser, $dbpass, {
			RaiseError => 0,
			AutoCommit => 1,
			PrintError => 0 });

	unless ( $dbh ) {
		warn "Could not connect to $db: $DBI::errstr\n";
		return 0;
	}

	my $sth = $dbh->prepare("select status from v\$instance");
	$sth->execute;
	my $row = $sth->fetchall_arrayref;
	$sth->finish;
	$dbh->disconnect;
	return $row;
}
