#!/usr/bin/perl -w
# Plugin for monitor oracle database reads.
#
# Licenced under GPL v2.
#
# Usage:
#
#       Symlink into /etc/munin/plugins/ and add the monitored
#       database to the filename. e.g.:
#
#       ln -s /usr/share/munin/plugins/oracle__database__hitratio \
#         /etc/munin/plugins/oracle_<databasename>_database__hitratio
#       This should, however, be given through autoconf and suggest.
#
#       If required, give username, password and/or Oracle server
#       host through environment variables.
#
#
# Parameters:
#
#       config   (required)
#
# Config variables:
#
#       dbhost     - Which database server to use. Defaults to
#                    'localhost'.
#       dbname     - Which database to use. Defaults to orl1
#       dbuser     - A Oracle user account with read permission to
#                    the given database. Defaults to
#                    'oracle'. Anyway, Munin must be told which user
#                    this plugin should be run as.
#       dbpass     - The corresponding password, if
#                    applicable. Default to undef.
#
# Magic markers
#%# family=auto
#%# capabilities=autoconf

use strict;
use DBI;

my $dbhost = $ENV{'dbhost'} || '127.0.0.1';
my $dbname = $ENV{'dbname'} || 'orl1';
my $dbuser = $ENV{'dbuser'} || 'oracle';
my $dbport = $ENV{'dbport'} || '1521';
my $dbpass = $ENV{'dbpass'} || '';
my $warning = $ENV{'warning'} || '90';
my $critical = $ENV{'critical'} || '96';

# Check for DBD::Oracle
if (! eval "require DBD::Oracle;") {
     exit 1;
}

my $dsn = "DBI:Oracle:dbname=$dbname;host=$dbhost;port=$dbport;sid=$dbname";
#print "$dsn\n";
my $dbh = DBI->connect ($dsn, $dbuser,
			$dbpass,
			{RaiseError =>1}) || die "";



if (exists $ARGV[0]) {
    if ($ARGV[0] eq 'autoconf') {
	# Check for DBD::Oracle
	if (! eval "require DBD::Oracle;") {
	     print "no (DBD::Oracle not found)";
	     exit 0;
	}
        if ($dbh) {
            print "yes\n";
            exit 0;
        } else {
            print "no Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr;
            exit 0;
	}
    }

    if ($ARGV[0] eq "config") {
	print "graph_title Oracle tablespace usage (in %) from $dbname\n";
	print "graph_args --upper-limit 100 -l 0\n";
	print "graph_vlabel %\n";
	print "graph_category db\n";
	print "graph_info This graph shows the tablespace usage (in %)\n";
        print "graph_scale no\n";
        print "warning $warning\n";
        print "critical $critical\n";
	my $sql_curr = "SELECT unique tablespace_name from dba_data_files";
	my $sth_curr = $dbh->prepare($sql_curr);
	$sth_curr->execute();
        while ( my ($datname) = $sth_curr->fetchrow_array ) {
                print "$datname.label $datname\n";
                print "$datname.info $datname tablespace\n";
                print "$datname.type GAUGE\n";
	        print "$datname.draw LINE2\n";
	}
	exit 0;
    }
}

my $sql_curr = "select a.tablespace_name, b.free,a.total,trunc(b.free/a.total * 1000) / 10 prc \
		from ( \
		select tablespace_name,sum(bytes) total \
		from dba_data_files group by tablespace_name) A, \
		( select tablespace_name,sum(bytes) free \
		from dba_free_space group by tablespace_name) B \
		where a.tablespace_name=b.tablespace_name";
my $sth_curr = $dbh->prepare($sql_curr);
$sth_curr->execute();
while ( my ($datname,$free,$total,$prc) = $sth_curr->fetchrow_array ) {
	my $in_use = 100 - ($free/$total)*100;
	print "$datname.value $in_use\n";
}
