#!/usr/bin/perl # vim: set ci et ts=4 sw=4: # reportusage.pl: a script to report storage usage of a specific domain or # all domain(s), can send user a email about it. # # Author: He zhiqiang # Last Update: Tue Nov 20 2007 20:19:00 # Version: 0.3 use vars qw($DIR $base); BEGIN { my $path = $0; if ($path =~ s/tools\/[0-9a-zA-Z\-\_]+\.pl$//) { if ($path !~ /^\//) { $DIR = "./$path"; } else { $DIR = $path; } } else { $DIR = '../'; } unshift @INC, $DIR .'libs'; unshift @INC, $DIR .'../extmail/libs'; select((select(STDOUT), $| = 1)[0]); }; use strict; use CmdTools; use POSIX qw(strftime); use Ext::Utils qw(human_size); die "Warning: you need to install extmail at the same top direcotry\n". " in order to call extmail modules.\n\n". "Usage: $0 [domain|-all] mailbase [recipient]\n" unless $#ARGV == 2; $base = $ARGV[1]; my $recip = $ARGV[2]; my $domain = "$ARGV[0]"; my $SENDMAIL = '/usr/sbin/sendmail -t -oi'; my $ctx = CmdTools->new( config => $DIR .'/webman.cf', directory => $DIR ); my $mgr = $ctx->ctx; # backend object my $now = strftime("%Y-%m-%d %H:%M:%S", localtime); $ctx->_lock; open (CMD, "|$SENDMAIL") or die "Sendmail fail: $!\n"; print CMD "Content-type: text/html; charset=UTF-8\n"; print CMD "To: $recip\n"; print CMD "Subject: [$now] Disk usage report for $domain domain\n"; print CMD "\n"; # terminator; print CMD "\n"; print CMD "\n"; if ($domain eq '-all') { # check all my $all = $mgr->get_domains_list || []; for my $dm (@$all) { report_domain($dm->{domain}); } } else { report_domain($domain); } print CMD "
\n"; close CMD; $ctx->_unlock; exit 0; sub report_domain { my $domain = shift; my $ul = $mgr->get_users_list($domain); return unless scalar @$ul; for my $u (@$ul) { my $user = $u->{mail}; my $quota = $u->{quota}; my $netdiskquota = $u->{netdiskquota}; my $maildir = $u->{maildir}; require Ext::Storage::Maildir; Ext::Storage::Maildir->import(qw(get_quota get_curquota)); $Ext::Storage::Maildir::CFG{path} = "$base/$maildir"; $ENV{QUOTA} = $quota || '1024000S'; my ($qst, $ref); eval { $qst = get_quota(); $ref = get_curquota(); }; if ($@) { print CMD " $user QUOTA[0/0] USAGE[0/0] (0%) ERROR: $@ \n"; } else { my $unlimit = 0; my $qsize = $qst->{size} || 0; my $qcount = $qst->{count} || 0; my $csize = $ref->{size} || 0; my $ccount = $ref->{count} || 0; $unlimit = 1 if (!$qsize && !$qcount); my $quota_pc = ($qsize ? sprintf("%.2f",($csize/$qsize)) : 0)*100; $qsize = human_size($qsize); $csize = human_size($csize); my $class = ($quota_pc >= 85? 'blue' : 'normal'); print CMD " $user QUOTA[$qsize/$qcount] USAGE[$csize/$ccount] ($quota_pc%)   \n"; } } } 1;