#!/usr/bin/perl # # $Id$ # # diskspace.pl # TiChou # # Require iputils. # my $version = "1.0"; my $cmdname = (split('/', $0))[-1]; my $rrdlib = "/var/lib/rrd"; my $df = "/bin/df"; use Getopt::Long; use RRDs; sub usage($) { my $usage = "Usage: $cmdname [options] device[,device...] Options: -f, --file=FILE[,FILE...] specify RRD files (default $rrdlib/disk_DEVICE.rrd) -h, --help display this help and exit -v, --version display version Report bugs to .\n"; if (shift) { print STDOUT $usage; exit 0; } else { print STDERR $usage; exit 1; } } sub version { print "$cmdname $version\n"; exit 0; } my @rrdfiles = (); Getopt::Long::Configure ("bundling"); GetOptions("f|file=s@" => \@rrdfiles, "h|help" => \&usage, "v|version" => \&version) or &usage(0); my @devices = @ARGV or &usage(0); @rrdfiles = split(/,/,join(',',@rrdfiles)); @devices = split(/,/,join(',',@devices)); open (DF, "$df -k|") or die "$0: unable to run $df: $!\n"; while () { /^(\S+)\s+(\d+)\s+\d+\s+(\d+)\s+\d+%\s+\S+$/ && (@$1 = ($2,$3)); }; close (DF); for (my $i = 0; $i < @devices; $i++) { my $device = $devices[$i]; my $file = $rrdfiles[$i] ? $rrdfiles[$i] : "$rrdlib/disk_".(split('/', $device))[-1].".rrd"; if (not -f $file) { RRDs::create $file, qw(DS:size:GAUGE:600:0:U DS:available:GAUGE:600:0:U RRA:AVERAGE:0.5:1:288 RRA:AVERAGE:0.5:6:336 RRA:AVERAGE:0.5:24:360 RRA:AVERAGE:0.5:288:365 RRA:MIN:0.5:1:288 RRA:MIN:0.5:6:336 RRA:MIN:0.5:24:360 RRA:MIN:0.5:288:365 RRA:MAX:0.5:1:288 RRA:MAX:0.5:6:336 RRA:MAX:0.5:24:360 RRA:MAX:0.5:288:365); my $error = RRDs::error; die "$0: $error\n" if $error; } RRDs::update $file, "N:".(@$device ? join(':',@$device) : "U:U"); my $error = RRDs::error; die "$0: $error\n" if $error; }