#!/usr/local/bin/perl -w use strict; # # Mark Sharkey - PrecisionPros.com # Creation date: January 30, 2006 # # Revision History # # Februray 2, 2006 - added 'threshold' percent option so user can define # threshold percent to be something other than 80. # # February 4, 2006 - added 'user' and 'quota' option. By specifying a # group of users and their max quota, you can monitor the server to see # if the group of users disk usage added together exceeds their overall quota. # use Quota; use Getopt::Long; my %opt = (); my $percent = (); my $threshold_percent = (); my $amount_to_format = (); my $user_sum = 0; my @accts = (); my @users = (); my $user = (); my $all_users = (); unless( GetOptions(\%opt, 'all|a', ## show all users 'help|h', ## show help 'over|o', ## show only users over quota (this is the default) 'threshold|t=s', ## set percent threshold (default is 80) 'user|u=s', ## show only these user(s) 'quota|q=s', ## group --users together and use this value as the max allowed ) ) { usage(); } usage() if $opt{'help'}; if ($opt{'threshold'}){ $threshold_percent=$opt{'threshold'}; }else{ $threshold_percent=80; #this is the default value } ## define sorts my $sort = ( sub { $a->[0] cmp $b->[0] } ); ## do sortuser ## get info from passwd file if ($opt{'user'}) { $opt{'user'} =~ s/ //g; #remove spaces from user list @users = split(/\,/, $opt{'user'}); } setpwent(); while( my @rec = getpwent ) { next unless $rec[2] >= 1000; # get rid of the system users if ($opt{'user'}) { # only look at these users foreach $user (@users) { if (!( $user eq $rec[0])) { next; }else{ push @accts, [ @rec[0,2,6,7] ]; next; } } }else { push @accts, [ @rec[0,2,6,7] ]; } } endpwent(); ## sort and report data my ( $qs, $qc ) = (0, 0); for my $acct ( sort { &$sort } @accts ) { next if $< && $acct->[1] != $<; my( $bc, $bs ) = (Quota::query(Quota::getqcarg('/home'), $acct->[1]))[0,1]; $bc = ( defined $bc ? $bc : 0 ); $bs = ( defined $bs ? $bs : 0 ); $qc += $bc; $qs += $bs; if (${bs} == 0) { $percent = "NO QUOTA"; }else{ $percent = &format_the_amount((${bc}/${bs})*100) . "%"; } if ( $opt{'quota'} ) { # can only be used with corresponding -u parameter if (! ($opt{'user'}) ) { print "--quota parameter requires corresponing --user parameter"; exit; }else{ $all_users = $all_users . "$acct->[0]=${bc} "; $user_sum = $user_sum + ${bc}; } } if( $opt{'all'} ) { print <<_USER_; Username: $acct->[0] Long Name: $acct->[2] Directory: $acct->[3] Disk Usage: ${bc}k Disk Quota: ${bs}k $percent _USER_ }else{ if (${bs} == 0) {next;} if ( (${bc}/${bs}) > ($threshold_percent/100) ) { print <<_USER_; Username: $acct->[0] Long Name: $acct->[2] Directory: $acct->[3] Disk Usage: ${bc}k Disk Quota: ${bs}k $percent _USER_ } } } if ( $opt{'quota'} ) { #print "( $user_sum > (($opt{'quota'}*1000)/($threshold_percent/100)) )"; if ( $user_sum > (($opt{'quota'}*1000)/($threshold_percent*100)) ) { my $new_quota = $opt{'quota'} * 1000; $percent = &format_the_amount(($user_sum/$new_quota)*100) . "%"; print <<_USER_; Grouping these users: $all_users Disk Usage: $user_sum k Disk Quota: $new_quota k $percent _USER_ } } exit; sub usage { die <<_USAGE_; usage: checkquota [options] options: -a show all users -h show help -o show only users over quota (this is the default) -t change threshold percentage (default is 80) -u='user1,user2,user3..userN' show only these user(s) -q=N group -u users together and use this value as the max MB allowed You may also use the following "long-style" options to achieve the same effects as above: --all show all users --help show help --over show only users over quota (this is the default) --threshold change the threshold percentage (default is 80) --user='user1,user2,user3..userN' ## show only these user(s) --quota=N group --user together and use this value as the max MB allowed _USAGE_ } sub format_the_amount { my ($amount_to_format) = @_; my $formatted_amount; $formatted_amount = sprintf ("%#8.2f", $amount_to_format); return $formatted_amount; }