Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: disk space trending into table....

Re: disk space trending into table....

From: Mladen Gogala <gogala_at_sbcglobal.net>
Date: Sun, 23 Jul 2006 05:12:12 GMT
Message-Id: <pan.2006.07.23.05.12.11.793082@sbcglobal.net>


On Sat, 22 Jul 2006 21:58:03 +0000, Mladen Gogala wrote:

> I would probably need between
> one and two hours to recreate it, but I am lazy on the weekends.

Hmmm, I was intrigued enough to re-create the script. Here it is:

The underlying database table:

CREATE TABLE DFREE

(	RECORDED DATE NOT NULL ENABLE, 
	HOSTNAME VARCHAR2(64) NOT NULL ENABLE, 
	FILESYSTEM VARCHAR2(64) NOT NULL ENABLE, 
	USED NUMBER(24,0), 
	FREE NUMBER(24,0), 
	TOT NUMBER(24,0), 
        CONSTRAINT FREE_SPACE_PK PRIMARY KEY (FILESYSTEM, HOSTNAME,
                                              RECORDED)
                   USING INDEX TABLESPACE INDX  ENABLE
) TABLESPACE USERS ; An example of a configuration file, named dfree.conf by default:

# This is a template dfree configuration file.
# Dfree is a utility that puts the output of "df" into an
# Oracle database.
# Database parameters defined like this:
# Database: username password TNS

Database: scott tiger local
Filesystems:

 /tmp
 /usr
 /misc
 /mp3

The script:
#!/usr/bin/perl -w
use strict;
use DBI;
use Filesys::DiskFree;
use Getopt::Long;

my $file = "dfree.conf";
my ( $tns, $usr, $passw );
my $host = ( split( '\.', `hostname` ) )[0]; my ( $dbh, $fs_flag );
my @fs;
my $handle = new Filesys::DiskFree;
my $INS =

    qq(insert into DFREE(RECORDED,HOSTNAME,FILESYSTEM,USED,FREE,TOT)

       values(SYSDATE,:HOST,:FS,:FREE,:USED,:TOT));

# Parse command line options

my $stat = GetOptions( "f|file=s" => \$file,

                       "h|help|?" => \&usage );

if ( !$stat ) { usage(); }

# Parse the configuration files. Skip empty lines and the lines
# beginning with '#'.

open( CONF, "<", $file ) or die "Cannot open $file for reading$!\n"; while (<CONF>) {

    chomp;
    next if (/^(\s*#|\s*$)/);
    if (/^\s*database:\s*(\w+)\s+(\w+)\s+(\w+)/i) {

        ( $usr, $passw, $tns ) = ( $1, $2, $3 );
        $dbh = db_connect( $usr, $passw, $tns );
    }
    if (/^\s*filesystems:/i) {
        $fs_flag = 1;
        next;

    }
    if ($fs_flag) {
        s/^\s*//;
        s/\s*$//;
        push @fs, $_;

    }
}
close(CONF);

die "Database not configured in the configuration file.\n"

    if ( !defined($dbh) );
die "No filesystems defined.\n" if ( scalar(@fs) == 0 );

# Get free space info for each filesystem and insert into the database.
# The list of file system was, at this point, read from the configuration
# file and resides in the "fs" array.

my $sth = $dbh->prepare($INS);
$sth->bind_param_inout( ":HOST", \$host, 64 ); foreach my $flsys (@fs) {

    $handle->df($flsys);
    my $free  = $handle->avail($flsys);
    my $total = $handle->total($flsys);
    my $used  = $handle->used($flsys);
    $sth->bind_param( ":FS",   $flsys );
    $sth->bind_param( ":FREE", $free );
    $sth->bind_param( ":USED", $used );

    $sth->bind_param( ":TOT", $total );     $sth->execute();
}
$dbh->commit();

END {
    if ( defined($dbh) ) { $dbh->disconnect(); } }

sub usage {

    use File::Basename;
    my $nm = basename($0);
    print qq($nm -> Store disk free space into the database

      USAGE:$nm -f <configuration file> [ -h ]
      Options: -f specifies the location of the configuration file.
                  The default is "dfree.conf". Lines beginning with '#'
                  are comments.
               -h This screen.

);

    exit(0);
}

sub db_connect {

    my ( $username, $passwd, $db ) = ( @_, $ENV{"TWO_TASK"} );     my $dbh = DBI->connect( "dbi:Oracle:$db", $username, $passwd )

        || die( $DBI::errstr . "\n" );

    $dbh->{AutoCommit}    = 0;
    $dbh->{RaiseError}    = 1;
    $dbh->{ora_check_sql} = 0;
    $dbh->{RowCacheSize}  = 16;

    return ($dbh);
}

In order for the script to work, you need DBI, DBD::Oracle, Filesys::DiskFree and Getopt::Long modules which are usually not included in default distribution. File::Basename module is included in all Perl distributions that I've ever seen.

The script, provided you name it "dfree" as I did, will then be called like this: dfree -f /usr/local/etc/dfree.conf. The output sizes are in bytes. Last but not least, this script is a public domain. You may use it as you feel fit.

-- 
http://www.mgogala.com
Received on Sun Jul 23 2006 - 00:12:12 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US