#!/usr/bin/perl -w use strict; ## Scott Wiersdorf ## Created: Tue May 30 09:27:01 MDT 2006 ## $Id: qmon,v 1.2 2006/05/30 16:25:37 scott Exp $ ## quota monitor our $VERSION = '$Revision: 1.2 $'; $VERSION =~ s/.*?([\d\.]+).*/$1/; use Getopt::Long; my %opt = ( threshold => 95, ); unless( GetOptions(\%opt, 'help|h', 'version|v', 'threshold|t=s', 'exclude|e=s', \@{$opt{exclude}}, ) ) { usage(); } usage() if $opt{help}; die "This is qmon, version $VERSION\n" if $opt{version}; my %exclude = map { $_ => 1 } @{$opt{exclude}}; die "Must be run as root.\n" if $>; ## open pipe to repquota open RQ, "-|", "repquota", "-u", "/" or do { die "Could not open repquota: $!\n" }; while( ) { chomp; my($user, $used, $hard) = (split(' '))[0,2,4]; next if $exclude{$user}; next unless $hard; next unless $hard =~ /^\d+$/; my $pct = sprintf("%.2f", ($used/$hard) * 100); next if $pct < $opt{threshold}; printf( "%14s %8d / %-8d (%s%%)\n", $user, $used, $hard, $pct ); } close RQ; exit; sub usage { die <<_USAGE_; usage: qmon [--threshold=xx[.yy]] [--exclude=user1 [--exclude=user2]] [-t xx[.yy]] [-e user1 [-e user2]] 'qmon' reports quota usage for each user when their percentage used reaches a specified threshold (default = 95). Users with no quotas are skipped. Users specified with the '--exclude' flag are also skipped. If no users are over quota, no report will print, making this suitable for cron jobs. Options: --help show this help --threshold=xx[.yy] set the percentage threshold for quota display -t xx[.yy] abbreviation for 'threshold' --exclude=user exclude 'user' from the output, even if they're over the threshold limit. May be specified multiple times to exclude multiple users -e user abbreviation for 'exclude' Example usage: qmon -t 90 -e bob -e tom -e meg This sets the threshold to 90, and excludes bob, tom, and meg from the final output, even if these are over the threshold. _USAGE_ } =head1 NAME qmon - report excessive user quotas =head1 SYNOPSIS qmon --threshold=97 --exclude=piggy =head1 DESCRIPTION B prints users who have reached a percentage threshold (default = 95%) of their quota. =head1 OPTIONS =over 4 =item B Show a brief usage message. =item B Changes the default threshold of 95% to a percentage you specify. You may optionally specify hundredths of a percent. qmon --threshold=75.93 qmon -t 75.93 =item B Excludes specified users from the report, even if they are over the quota threshold. May be specified multiple times for multiple users. qmon --exclude=bob --exclude=bill qmon -e bob -e bill =back =head1 INSTALLATION Copy to F or F (or anywhere in your path), and set as executable: chmod 0555 /usr/local/sbin/qmon B must be run as root, since only root can read the quota files. =head1 EXAMPLES =over 4 =item * Change the threshold to 70%: qmon -t 70 =item * Exclude two users from the output: qmon -e bob -e mary =item * Both: qmon -t 70 -e bob -e mary =back =head2 Crontab examples Because B shows no output when no users are over the quota threshold, B is suitable for putting in your F file: 0 5 * * * root /usr/local/sbin/qmon -t 98 =head1 AUTHOR Scott Wiersdorf, Escott@perlcode.orgE =head1 COPYRIGHT Copyright (c) 2006 Scott Wiersdorf. All rights reserved. =head1 LICENSE B is released under the the same terms as Perl itself. =head1 SEE ALSO L, L =cut