#!/usr/bin/perl -w

use strict;
use DBI;
use Getopt::Long;
use Sys::Hostname;

my %opts;
usage() if (! GetOptions(\%opts, "help|?", "asm|a=s@", "sid|s=s@", "user|u=s@", "pass|p=s@"));
usage() if (defined($opts{'help'}));

my @asm = @{$opts{'asm'}} if (defined($opts{'asm'}));
#print "asm = @asm\n";
my @usr = @{$opts{'user'}} if (defined($opts{'user'}));
#print "usr = @usr\n";
my @pwd = @{$opts{'pass'}} if (defined($opts{'pass'}));
#print "pwd = @pwd\n";
my @sid = @{$opts{'sid'}} if (defined($opts{'sid'}));
#print "sid = @sid\n";

$0 = 'test_monitor.apl';

my ($user, $pass, $sid) = (@usr, @pwd, @sid);

my $string = 'dbi:Oracle:' . $sid;

# Connect to the database
my $dbh = DBI->connect($string,
			$user,$pass) || die "$DBI::errstr\n";

my ($asm_size, $asm_free, $asm_type);

eval {
	my $proc = $dbh->prepare(q{
		begin
			dp_monitor.query_tablespace(
				asm_name => :asm_name,
				asm_type => :asm_type,
				asm_size => :asm_size,
				asm_free => :asm_free
			);
		end;
	});

	$proc->bind_param(":asm_name", @asm);
	$proc->bind_param_inout(":asm_type", \$asm_type, 100);
	$proc->bind_param_inout(":asm_size", \$asm_size, 100);
	$proc->bind_param_inout(":asm_free", \$asm_free, 100);
	$proc->execute;
};

if ( $@ ) {
	warn "Execution of stored procedure failed: $DBI::errstr\n";
}

print "asm_name = @asm, asm_type = $asm_type, asm_size = $asm_size, asm_free = $asm_free, pct_used = ";

my $asm_used = $asm_size - $asm_free;
printf "%3.2f%\n", $asm_used / $asm_size * 100.00;

$dbh->disconnect;

sub usage {
  print STDERR <<EUSAGE;

    Defaults: 
              --ts 'tablespace' 

EUSAGE
  exit(1);
}

#sleep(1);
exit
