File: /usr/sbin/quotastats
#!/usr/bin/perl
#syntax: quotastats [-h] [-b] [<username>]
#BEGIN { @INC = ('/usr/iports/lib/perl5/site_perl/mach/5.20','/usr/iports/lib/perl5/site_perl','/usr/iports/lib/perl5/5.20/mach','/usr/iports/lib/perl5/5.20','/usr/iports/lib/perl5/site_perl/5.20','/usr/iports/lib/perl5/site_perl/5.20/mach');}
use POSIX qw(locale_h); # Imports setlocale() and the LC_ constants.
use locale;
setlocale(LC_NUMERIC, "de_DE.UTF-8");
use Getopt::Std;
%opts=();
getopts("hb", \%opts);
my $human = $opts{h};
my $bytes = $opts{b};
my $username = "@ARGV";
my $xen = (`/sbin/mount` =~ /zfs/);
my $hostname = `/bin/hostname`;
my $uidmin = 1000;
my $uidmax = 60000;
#print "human: $human; bytes: $bytes; username: $username\n";
if($xen) {
# get all user
my %users;
open my $fh, "/etc/passwd" or die "/etc/passwd: $!";
my @if = sort grep /^[^#]/, <$fh>;
close $fh;
#pgsql:*:1006:1000:pgsql:/var/db/pgsql:/bin/sh
foreach my $pline (@if) {
chomp($pline);
my ($name, $star, $puid, $foobar) = split(/:/,$pline);
if ($puid >= $uidmin && $puid <= $uidmax) {
$users{$puid}{'name'} = $name;
}
}
# get all quota
my @quotas;
# only use human output in our full output
@quotas = `/sbin/zfs userspace -nHp vsd/$hostname`;
my ($curbs, $hardbs, $softbs);
foreach my $quotaline (@quotas) {
chomp($quotaline);
#$uid, $quota
my ($posix, $dummy, $luid, $curb, $hardb) = split(/\s+/,$quotaline);
if ($luid >= $uidmin && $luid <= $uidmax) {
$curbs += $curb; $hardbs += $hardb; $softbs += $hardb;
$users{$luid}{'quota'} = $hardb;
$users{$luid}{'used'} = $curb;
}
}
$hardbs = '0' unless $hardbs;
$softbs = '0' unless $hardbs;
$curbs = '0' unless $curbs;
# default single user info
if ($username && !$bytes) {
#Disk quota for user mysql (uid 1003)
# Limit : 5242880K
# Current use : 180220K
for $vmuser ( keys %users ) {
if ($users{$vmuser}{'name'} eq $username) {
print "Disk quota for user $username (uid $vmuser)\n";
printf " Limit : %iK\n", $users{$vmuser}{'quota'}/1024;
printf " Current use : %iK\n", $users{$vmuser}{'used'}/1024;
}
}
} elsif ($username && $bytes) {
#234235236370
for $vmuser ( keys %users ) {
# give out nothing if we missed to set a quota... vvvv
if (($users{$vmuser}{'name'} eq $username) && ($users{$vmuser}{'quota'} > 0)) {
printf "%i", $users{$vmuser}{'quota'} - $users{$vmuser}{'used'};
}
}
} else {
# We like to see the full show...
# only use human output in our full output if wanted
# overwrite the single values from above
if ($human) {
@quotas = `/sbin/zfs userspace -nH vsd/$hostname`;
foreach my $quotaline (@quotas) {
chomp($quotaline);
#$uid, $quota
my ($posix, $dummy, $luid, $curb, $hardb) = split(/\s+/,$quotaline);
if ($luid >= $uidmin && $luid <= $uidmax) {
$users{$luid}{'quota'} = $hardb;
$users{$luid}{'used'} = $curb;
}
}
}
print "=================+========+=============+============\n";
print "User name | Uid | Limit | Current use\n";
print "=================+========+=============+============\n";
# virtmail| 1008| 104857600K| 32K
for $vmuser ( sort keys %users ) {
# do NOT show for users not existing in /etc/passwd:
next unless $users{$vmuser}{'name'};
printf "% 17s|", $users{$vmuser}{'name'};
printf "% 8i|", $vmuser;
printf "% 12iK|", $users{$vmuser}{'quota'}/1024 unless $human; #K
printf "% 11iK\n", $users{$vmuser}{'used'}/1024 unless $human; #K
printf "% 13s|", $users{$vmuser}{'quota'} if $human; #K
printf "% 12s\n", $users{$vmuser}{'used'} if $human; #K
}
my $masterquota = `/bin/cat /etc/vsd/quota`;
chomp ($masterquota); # is already M
print "=================+========+=============+============\n";
if ($human) {
# save the freesize in K
$freespace = $masterquota*1024 - $curbs/1024;
# find best size to show
$hardbs /= 1024; #now KB
$hunit = "K";
if (length(int($hardbs)) > 3) {
# more than 1000KB - go next unit
$hardbs /= 1024; #now MB
$hunit = "M";
if (length(int($hardbs)) > 3) {
# more than 1000MB - go next unit
$hardbs /= 1024; #now GB
$hunit = "G";
if (length(int($hardbs)) > 3) {
# more than 1000GB - go next unit
$hardbs /= 1024; #now TB
$hunit = "T";
}
}
}
$hardbs = sprintf("%.2f",$hardbs);
$hardbs =~ s/\,00//;
$hardbs =~ s/0$//;
# find best size to show
$curbs /= 1024; #now KB
$cunit = "K";
if (length(int($curbs)) > 3) {
# more than 1000KB - go next unit
$curbs /= 1024; #now MB
$cunit = "M";
if (length(int($curbs)) > 3) {
# more than 1000MB - go next unit
$curbs /= 1024; #now GB
$cunit = "G";
if (length(int($curbs)) > 3) {
# more than 1000GB - go next unit
$curbs /= 1024; #now TB
$cunit = "T";
}
}
}
$curbs = sprintf("%.2f",$curbs);
$curbs =~ s/\,00//;
$curbs =~ s/0$//;
# find best size to show
$masterquota /= 1024; #now GB
$munit = "G";
if (length(int($masterquota)) > 3) {
# more than 1000GB - go next unit
$masterquota /= 1024; #now TB
$munit = "T";
}
$masterquota = sprintf("%.2f",$masterquota);
$masterquota =~ s/\,00//;
$masterquota =~ s/0$//;
# find best size to show
#already KB
$funit = "K";
if (length(int($freespace)) > 3) {
# more than 1000KB - go next unit
$freespace /= 1024; #now MB
$funit = "M";
if (length(int($freespace)) > 3) {
# more than 1000MB - go next unit
$freespace /= 1024; #now GB
$funit = "G";
if (length(int($freespace)) > 3) {
# more than 1000GB - go next unit
$freespace /= 1024; #now TB
$funit = "T";
}
}
}
$freespace = sprintf("%.2f",$freespace);
$freespace =~ s/\,00//;
$freespace =~ s/0$//;
printf "Allocated/used | |% 12s%s|% 11s%s\n",$hardbs, $hunit, $curbs, $cunit;
printf "Max/available | |% 12s%s|% 11s%s\n",$masterquota, $munit, $freespace, $funit;
} else {
printf "Allocated/used | |% 12iK|% 11iK\n",$hardbs/1024, $curbs/1024;
printf "Max/available | |% 12iK|% 11iK\n",$masterquota*1024, $masterquota*1024 - $curbs/1024;
}
print "=================+========+=============+============\n";
}
} else {
my $options = '';
$options .= '-h' if $human;
$options .= '-b' if $bytes;
chomp ($username);
$username =~ s/^\s+//g;
$username =~ s/\s+$//g;
if ($username && $options) {
system("/usr/sbin/quotastats.ufs",$options, $username);
} elsif ($options) {
system("/usr/sbin/quotastats.ufs",$options);
} elsif ($username) {
system("/usr/sbin/quotastats.ufs",$username);
} else {
system("/usr/sbin/quotastats.ufs");
}
}