quota_usage.pl: update from Jose Nilton <jniltinho@gmail.com> - add print of total domain and total mailbox

git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@547 a1433add-5e2c-0410-b055-b7f2511e0802
postfixadmin-2.3
David Goodwin 16 years ago
parent ef768fe142
commit de76d028a2

@ -1,16 +1,18 @@
#!/usr/bin/perl
# vim:ts=4:sw=4:et
#
# Virtual quota_usage 0.3
# Contributed to Postfixadmin by Jose Nilton <jniltinho@gmail.com>
#
# See also : http://www.russelldare.net/media/perl/dirsizeSource.pdf
# License: GPL v2.
# Usage:
# perl quota_usage.pl --list or
# perl quota_usage.pl --addmysql for add mysql database postfix
# perl quota_usage.pl --list
# perl quota_usage.pl --list --addmysql
# for add mysql database postfix
#
# Requires: perl perl-DBD-mysql perl-DBD (may be named differently depending on your platform).
# Requirements - the following perl modules are required:
# DBD::Pg or DBD::mysql; perl perl-DBD-mysql perl-DBD (may be named differently depending on your platform).
# and the 'du' binary in $ENV{'PATH'} (see below).
#
# You will need to modify the postfix DATABASE to add a quota_usage column.
@ -35,61 +37,43 @@ my $db_user = 'postfix';
my $db_password = '123456';
my $root_path = '/home/vmail';
# Pg or mysql
my $db_type = 'Pg';
my $db_type = 'mysql';
##END EDIT##
(help()) if (!$ARGV[0]);
$ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
my($domain_dir, $full_domain_dir, $user_dir, $usage, $email, $sql, $dbh);
GetOptions ('list' => \&list_quota_usage, 'addmysql' => \&insert_to_db);
my $list = 0;
my $insert_db = 0;
my $total_mailbox = 0;
my $total_domain = 0;
GetOptions ('l|list' => \$list, 'i|addmysql' => \$insert_db, 'help|h|man' => \&help) or (help());
sub list_quota_usage {
opendir(DOMAINDIR, $root_path) or die ("Unable to access directory '$root_path' ($!)");
foreach $domain_dir (sort readdir DOMAINDIR) {
next if $domain_dir =~ /^\./; # skip dotted dirs
$full_domain_dir = "$root_path/$domain_dir"; #print "$full_domain_dir\n";
(list_quota_usage()) if ($list == 1 || $insert_db == 1 );
opendir(USERDIR, $full_domain_dir) or die ("Unable to access directory '$full_domain_dir' ($!)");
foreach $user_dir (sort readdir USERDIR) {
next if $user_dir =~ /^\./; # skip dotted dirs
$email = "$user_dir\@$domain_dir";
my $i = `du -0 --summarize $full_domain_dir/$user_dir`;
($usage) = split(" ", $i);
if ($usage < 100) {
$usage = 0;
} elsif ($usage < 1000) {
$usage = 1;
} else {
$usage = $usage + 500;
$usage = int $usage / 1000;
}
list_out();
}
}
close(DOMAINDIR);
close(USERDIR);
}
sub insert_to_db {
sub list_quota_usage {
opendir(DOMAINDIR, $root_path) or die ("Unable to access directory '$root_path' ($!)");
$dbh = DBI->connect("DBI:$db_type:database=$db_database;host=$db_host", $db_user, $db_password) or die ("cannot connect the database");
execSql("UPDATE mailbox set quota_usage = 0");
if($insert_db == 1){
$dbh = DBI->connect("DBI:$db_type:database=$db_database;host=$db_host", $db_user, $db_password) or die ("cannot connect the database");
execSql("UPDATE mailbox set quota_usage = 0");
}
foreach $domain_dir (sort readdir DOMAINDIR) {
next if $domain_dir =~ /^\./; # skip dotted dirs
$full_domain_dir = "$root_path/$domain_dir"; #print "$full_domain_dir\n";
next if $domain_dir =~ /^\./; # skip dotted dirs
$full_domain_dir = "$root_path/$domain_dir"; #print "$full_domain_dir\n";
$total_domain++;
opendir(USERDIR, $full_domain_dir) or die ("Unable to access directory '$full_domain_dir' ($!)");
foreach $user_dir (sort readdir USERDIR) {
next if $user_dir =~ /^\./; # skip dotted dirs
$email = "$user_dir\@$domain_dir";
$email = "$user_dir\@$domain_dir";
$total_mailbox++;
my $i = `du -0 --summarize $full_domain_dir/$user_dir`;
($usage) = split(" ", $i);
@ -102,39 +86,66 @@ sub insert_to_db {
$usage = $usage + 500;
$usage = int $usage / 1000;
}
if($insert_db == 1){execSql("UPDATE mailbox set quota_usage = $usage, quota_usage_date = CAST(NOW() AS DATE) WHERE username = '$email'");}
print_list() if ($list == 1);
execSql("UPDATE mailbox set quota_usage = $usage, quota_usage_date = CAST(NOW() AS DATE) WHERE username = '$email'");
#list_out(); #Debug
}
}
close(DOMAINDIR);
close(USERDIR);
(print_total()) if ($list == 1);
}
sub execSql {
my $sql = shift;
my $ex;
$ex = $dbh->do($sql) or die ("error when running $sql");
}
sub list_out {
sub print_total{
print "---------------------------------------------------------\n";
print "TOTAL DOMAIN\t\t\t\tTOTAL MAILBOX\n";
print "---------------------------------------------------------\n";
print "$total_domain\t\t\t\t\t\t$total_mailbox\n";
}
sub print_list {
format STDOUT_TOP =
Report of Quota Used
--------------------------
---------------------------------------------------------
EMAIL QUOTA USED
------------------------------------------------------------------
---------------------------------------------------------
.
format =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<
$email, "$usage\bMB"
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<
$email, "$usage MB"
.
write;
}
sub help {
print "$0 [options...]\n";
print "-l|--list List quota used\n";
print "-i|--addmysql For insert quota used in database mysql\n";
}

Loading…
Cancel
Save