#!/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'} || '';

# 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 1;
	}
        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 1;
	}
    }

    if ($ARGV[0] eq "config") {
	print "graph_title Oracle library and buffer cache hit ratios from $dbname\n";
	print "graph_args --upper-limit 110 -l 0\n";
	print "graph_vlabel %\n";
	print "graph_category Oracle\n";
	print "graph_info This graph shows the percentage of blocks and libraries read from the cache\n";
	print "read_hitratio.label Cache hitratio\n";
	print "read_hitratio.type GAUGE\n";
	print "read_hitratio.draw LINE2\n";
	print "lib_hitratio.label Library hitratio\n";
	print "lib_hitratio.type GAUGE\n";
	print "lib_hitratio.draw LINE2\n";
	exit 0;
    }
}

#cache hitratio
my $sql_curr = "select (1-(pr.value/(dbg.value+cg.value)))*100 \
		from v\$sysstat pr, v\$sysstat dbg, v\$sysstat cg \
		where pr.name='physical reads' and dbg.name='db block gets' and cg.name='consistent gets'";

my $sth_curr = $dbh->prepare($sql_curr);
$sth_curr->execute();
my ($read_hitratio) = $sth_curr->fetchrow();
print "read_hitratio.value $read_hitratio\n";

#libray hit ratio
$sql_curr = "select sum(lc.pins)/(sum(lc.pins)+sum(lc.reloads))*100 \
		from v\$librarycache lc";

$sth_curr = $dbh->prepare($sql_curr);
$sth_curr->execute();
my ($lib_hitratio) = $sth_curr->fetchrow();
print "lib_hitratio.value $lib_hitratio\n";

