Category:
Author/Contact Info Val Polyakov vpolyakov@katrillion.com
Description: This is a script that you can check disk usage on remote servers with. Very easy to configure, and you can put it in...*drom roll* cron! and it'll email you daily (or whatever) with disk usage reports :)
#!/usr/bin/perl

############################################################
#
# Config Section


# The @server_list array is just that - duh.
# The "Local" value is a keyword and refers to the local machine.

@server_list = ("Local", "host1.server.com", "host2.server.com.com", "
+host3.server.com", "host4.server.com");

# This is the warning level (in percent). When a filesystem passes
# this percentage in used space, a warning report is generated

$warning_threshold = 80;

############################################################

if ($ARGV[0] eq "-?" || $ARGV[0] eq "--help")
{
        print "Usage: diskfree [-w] [-? | --help]\n";
        print "\t-w\t\t Display warnings only\n";
        print "\t-? | --help\t Usage screen\n";
        exit 1;
}

$warnings = "";
$detail_report = "";

foreach $server (@server_list)
{
        $detail_report = $detail_report . "$server Disk Usage\n";
        $detail_report = $detail_report . "---------------------------
+---------------\n";
        $detail_report = $detail_report . "Use% |  Mount  | Size | Ava
+il\n";
        $detail_report = $detail_report . "---------------------------
+---------------\n";

        if ($server eq "Local") {
                @diskusage = `df -hP`;
        } else {
                @diskusage = `ssh "$server" "df -hP"`;
        }

        for ($i = 1; $i <= $#diskusage; $i++)
        {
                @usage_detail = split (/\s+/,$diskusage[$i]);

                $usage_detail[4] =~ s/%//g;

                if ($usage_detail[4] > $warning_threshold)
                {
                        $warnings = $warnings . "*WARNING* $server: $u
+sage_detail[5] ($usage_detail[0]) is at $usage_detail[4]%\n";
                        $detail_report = $detail_report . "$usage_deta
+il[4]% \t $usage_detail[5] \t $usage_detail[1] \t $usage_detail[3]" .
+ "\n";
                } else {
                        $detail_report = $detail_report . "$usage_deta
+il[4]% \t $usage_detail[5] \t $usage_detail[1] \t $usage_detail[3]" .
+ "\n";
                }
        }

        $detail_report = $detail_report . "---------------------------
+---------------\n\n";

}

if ($ARGV[0] eq "-w")
{
        if ($warnings eq "")
        {
                print "No warnings.\n";
        } else {
                print "$warnings\n";
        }
} else {
        print "$warnings\n";
        print "$detail_report\n";
}
Replies are listed 'Best First'.
Re: diskfree
by rob_au (Abbot) on Aug 23, 2002 at 23:12 UTC
    You may be interested to have a look at the Filesys::Df module and the discussions that followed on this module and comparative merits of using it over parsing df output here and here.