From 02eb2401ecba6dd0328bdf3ece915127c2f93c69 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Wed, 28 Aug 2013 13:44:07 +0200 Subject: [PATCH] add updatequota.pl --- updatequota.pl | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 updatequota.pl diff --git a/updatequota.pl b/updatequota.pl new file mode 100644 index 0000000..8e5e20c --- /dev/null +++ b/updatequota.pl @@ -0,0 +1,68 @@ +#!/usr/local/bin/perl -w + +use strict; +use Net::LDAP; +use Sys::Hostname; +use Quota; + +my $server = 'ldap://ldap.domain.tld'; +my $base = 'dc=domain,dc=tld'; +my $dn = 'cn=quota,ou=DSA,dc=domain,dc=tld'; +my $pass = 'secret'; + +my $ldap = Net::LDAP->new($server) or die "Couldn't connect to $server: $!"; + +$ldap->start_tls( + verify => 'require', + cafile => '/etc/pki/tls/certs/ca-bundle.crt'); + +$ldap->bind( + dn => $dn, + password => $pass); + +my $res = $ldap->search( + base => "ou=People,$base", + filter => '(objectClass=systemQuotas)', + attrs => ['uid', 'quota']); + +$res->code && die "Error while looking for quota entries: " . $res->error; + +foreach my $entry ($res->entries){ + my $user = $entry->get_value('uid'); + #print "Checking quota for user $user\n"; + foreach my $quota ($entry->get_value('quota')){ + unless ($quota =~ m/^(\/.*):(\d+):(\d+):(\d+):(\d+):(\w+)$/){ + print "$quota doesn't look like a valid quota entry\n"; + next; + } + my ($dir,$blksoft,$blkhard,$inodesoft,$inodehard,$fileserver) = ($1, $2, $3, $4, $5, $6); + my $hostname = hostname; + unless ($fileserver eq $hostname or $hostname =~ /^$fileserver\..*/){ + print "$quota doesn't match $hostname, skiping\n"; + next; + } + unless (-d $dir){ + print "$dir doesn't exists, skiping this rule\n"; + } + my $uid = getpwnam($user); + my $dev = Quota::getqcarg($dir); + Quota::sync($dev); + if ($!){ + print "Quota are not enabled on $dev, skiping this rule (error is " . Quota::strerr() . "\n"; + next; + } + my ($curblk,$curblksoft,$curblkhard,undef,$curinode,$curinodesoft,$curinodehard,undef) = + Quota::query($dev, $uid); + #print "User $user is using $curblk out of its $blkhard allowed\n"; + if ($curblksoft ne $blksoft || + $curblkhard ne $blkhard || + $curinodesoft ne $inodesoft || + $curinodehard ne $inodehard){ + print "Quota for user $user on $dir needs to be updated\n"; + Quota::setqlim($dev,$uid,$blksoft,$blkhard,$inodesoft,$inodehard); + } + } +} + +$ldap->unbind; +