PDA

View Full Version : System command problem


neo_philiac
06-13-2008, 04:02 PM
I am trying to do a simple script for disk usage but I cant get the nfs mounts. Here is the code i found. The problem if that this parses every line but if you do df the big mount paths show up in the second line. How do I fix this problem?



Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p1 99188468 5442116 88626440 6% /
/dev/cciss/c0d0p3 736300856 2363176 695932308 1% /home
tmpfs 1031284 0 1031284 0% /dev/shm
xx.xx.xx.xx:/usr/users
309811104 251185312 55527680 82% /usr/users
/dev/sr0 3510680 3510680 0 100% /media
/dev/sr0 3510680 3510680 0 100% /dvd
xx.xx.xx.xx:/disk/disk1
480719104 202752 456097280 1% /data/disk1
xx.xx.xx.xx:/disk/disk2
480719104 324577792 131722240 72% /data/disk2
xx.xx.xx.xx:/media



#!/usr/bin/perl
use strict;
sub makeeasy{
my $size = shift;
$size *= 1024;
if($size > 1024){if($size > 1048576){if($size > 1073741824){$size = (int(($size / 1073741824) * 100) / 100) . ' GB';}else{$size = (int(($size / 1048576) * 100) / 100) . ' MB';}}else{$size = (int(($size / 1024) * 100) / 100) . ' KB';}}else{$size = $size . ' B';}
return $size;
}
my (%used,%available);
my $dir = "/home";
my $df = `df`;
my $otherused = 0;
my $otheravailable = 0;
my($actualpath,$totalspace,$usedspace,$availablespace,$usedpercent,$mountedon,$line);
while($df =~ m/\ \ /){$df =~ s/\ \ / /g;}
foreach $line (split("\\n",$df)){
($actualpath,$totalspace,$usedspace,$availablespace,$usedpercent,$mountedon) = split(' ',$line);
if($mountedon =~ m/^\//){
$used{$mountedon} = $usedspace;
$available{$mountedon} = $availablespace;
}
}
foreach $mountedon (keys(%used)){
print "Used space on $mountedon\: " . makeeasy($used{$mountedon}) ."\n";
print "Available space on $mountedon\: " . makeeasy($available{$mountedon}) ."\n";
}


Thanks

neo_philiac
06-13-2008, 05:31 PM
Never Ming - got it !

Its df -P

FishMonger
06-13-2008, 05:47 PM
I haven't tested this with nfs mounts, but let's try a more sensible script.

#!/usr/bin/perl

use strict;
use warnings;
use Filesys::Df;

# there is a better way to code this line, but I kept is simple for now
my $df = df('/home');

if(defined $df) {
print "Total 1k blocks: $df->{blocks}\n";
print "Total 1k blocks free: $df->{bfree}\n";
print "Total 1k blocks avail to me: $df->{bavail}\n";
print "Total 1k blocks used: $df->{used}\n";
print "Percent full: $df->{per}\n";

if(exists($df->{files})) {
print "Total inodes: $df->{files}\n";
print "Total inodes free: $df->{ffree}\n";
print "Inode percent full: $df->{fper}\n";
}
}


Filesys::Df
http://search.cpan.org/~iguthrie/Filesys-Df-0.92/Df.pm

FishMonger
06-13-2008, 06:36 PM
After taking a second look at it, this is probably what you're wanting.

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

my %mounts;

open my $df, 'df -Ph |' or die $!;
while(<$df>) {
next if $. == 1;
chomp;
my ($fs,$size,$used,$avail,$perc,$mnt) = split /\s+/;
$mounts{$fs} = {
size => $size,
used => $used,
available => $avail,
percent => $perc,
mount_point => $mnt
};
}
# I'm just dumping the data, but you'd want to use a for or foreach loop
print Dumper \%mounts;