Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

pqset

by neilwatson (Priest)
on Sep 30, 2003 at 15:14 UTC ( [id://295309]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info Neil Watson
Description: pqset enables you to check and set user disk quotas on a linux system.

03/10/02 updated code with cleaner option/switches.

#!/usr/bin/perl

# try this for the report
# repquota /home|grep +

use strict;
use warnings;
use Getopt::Std;
use Mail::Sender;
use Sys::Hostname;
use Quota;

my ($to, $x, $sender, @names, $size, $uid, $name, $passwd);
my ($error, $user, $limit, $dev,$bc,$bs,$bh,$bt, $ic,$is,$ih,$it);
my $gid = -1;
my $members = "none";
my $smtp = "mail";
my $hostname = hostname;
my $support = "vgsupport\@foodomain.com";
my $part = "/home"; #partition to check quotas
my %opt;


# the : following means that switch takes and arguement.
getopts("u:l:seqc", \%opt);

# print <<"*END*";
# u = $opt{u}
# l = $opt{l}
# s = $opt{s}
# e = $opt{e}
# q = $opt{q}
# c = $opt{c}
# *END*

# target user 
if (defined $opt{u}){
    $user = $opt{u};
    # get uid 
    $uid = getpwnam $user;

    }else{
    $error = "You must specify a user";
    usage();
}

getquota();

# clear quota
if (defined $opt{c}){
    setquota();
    exit;
}

# size limit in kilobytes
if (defined $opt{l}){
    $limit = $opt{l};
}else{
    $error = "You must set a size limit";
    usage();
}

# check limit and take report
if ($size > $limit) {

    # if match then print report to stdout
    if (defined $opt{s}){
        report();

    # or email owner a warning
    }elsif (defined $opt{e}){
        $to = $user;
        email();
    }else{
        $error = "You must specify a report type";
        usage();
    }

    # set quota ?
    if (defined $opt{q}){
        setquota();
    }
}

# check usage
sub usage {
    print "Error $error\n";
    print "Usage: pqset -u <user> ";
    print "-l <quota limit in kilobytes> ";
    print "-s report to stdout ";
    print "-e send email to user ";
    print "-q set quota ";
    print "-c clear quota\n\n";
    exit;
}

# write report to stdout
sub report{
    print <<"*END*";
    Current disk usage is $size but limit is only $limit
*END*
}

# send email warning
sub email{
    $sender = new Mail::Sender;

    $sender->Open({
        smtp => $smtp,
        to => $to,
        from => "nobody\@foodomain.com",
        encoding => "quoted-printable",
        subject => "Computer operations: Your disk usage on $hostname"
+,
        }) or die "Sender error: $sender, $Mail::Sender::Error!\n";

        $sender->SendEnc(<<"*END*");
Your disk usage on $hostname has exceeded the allowed limit.
Current size (kilobytes): $size
Allowable limite (kilobytes): $limit

Please reduce your usage to below the allowed limit. Your write privil
+eges on this server have been revoked.

If you have any questions or concerns please email $support.  Do NOT r
+eply to this email.

Thank you.
*END*
    $sender->Close or die "Sender error: $sender, $Mail::Sender::Error
+!\n";
}

# set disk quota for offenders
sub setquota{

    # definitions
    # $bc = block count
    # $ic = inode count
    # $bs = soft block limit
    # $is = soft inode limit
    # $bh = hard block limit
    # $ih = hard inode limit
    # $bt = block grace time
    # $it = inode grace time

    if (defined $opt{c}){

        # clear quota
        $limit = 0;
        Quota::setqlim($dev, $uid, $bs,$limit, $is,$ih); # || die Quot
+a::strerr,"can't set quota $!\n";

        print "Quota cleared for $user\n";
        print "see quota $user\n";

    }else{

        # set quota to hard limit
        Quota::setqlim($dev, $uid, $bs,$limit, $is,$ih); # || die Quot
+a::strerr,"can't set quota $!\n";

        print "Quota set to $limit kilobytes for $user\n";
        print "see quota $user\n";
    }

}

# check current disk quotas for user
sub getquota{

    # not that the die seems to die even on success
    # thus I have commented them out, for now

    # get device
    $dev = Quota::getqcarg($part); # || die Quota::strerr,"can't get d
+ev $!\n";
    #$dev = Quota::getdev($part) || die Quota::strerr,"can't get dev $
+!\n";

    # get current quota
    #print "dev = $dev\n";
    #print "uid = $uid\n";

    ($bc,$bs,$bh,$bt, $ic,$is,$ih,$it) = Quota::query($dev, $uid); # |
+| die Quota::strerr,"can't read quotas $!\n";
    # definitions
    # $bc = block count
    # $ic = inode count
    # $bs = soft block limit
    # $is = soft inode limit
    # $bh = hard block limit
    # $ih = hard inode limit
    # $bt = block grace time
    # $it = inode grace time

    $size = $bc;
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://295309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 07:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found