
25th February 2007, 13:06
|
|
Senior Member
|
|
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Postfix Quota Notification Email Script
Hi,
I've followed the guide for "Virtual Users And Domains With Postfix, Courier And MySQL".
In one of the page, there was something about setting up a notification guide using a file called quota_notify, which runs in crontab. This is the content
Quote:
#!/usr/bin/perl -w
use strict;
my $POSTFIX_CF = "/etc/postfix/main.cf";
my $MAILPROG = "/usr/sbin/sendmail -t";
my $WARNPERCENT = 80;
my @POSTMASTERS = ('postmaster@mydomain.com');
my $CONAME = 'Company-A Postmaster';
my $COADDR = 'postmaster@mydomain.com';
my $SUADDR = 'postmaster@mydomain.com';
my $MAIL_REPORT = 1;
my $MAIL_WARNING = 1;
#get virtual mailbox base from postfix config
open(PCF, "< $POSTFIX_CF") or die $!;
my $mboxBase;
while (<PCF>) {
next unless /virtual_mailbox_base\s*=\s*(.*)\s*/;
$mboxBase = $1;
}
close(PCF);
#assume one level of subdirectories for domain names
my @domains;
opendir(DIR, $mboxBase) or die $!;
while (defined(my $name = readdir(DIR))) {
next if $name =~ /^\.\.?$/; #skip '.' and '..'
next unless (-d "$mboxBase/$name");
push(@domains, $name);
}
closedir(DIR);
#iterate through domains for username/maildirsize files
my @users;
chdir($mboxBase);
foreach my $domain (@domains) {
opendir(DIR, $domain) or die $!;
while (defined(my $name = readdir(DIR))) {
next if $name =~ /^\.\.?$/; #skip '.' and '..'
next unless (-d "$domain/$name");
push(@users, {"$name\@$domain" => "$mboxBase/$domain/$name"});
# Added to show if the path to the mailboxes are correct.
# At this point, its showing all the correct path to each mailbox
printf($mboxBase."/".$domain."/".$name."\n");
}
}
closedir(DIR);
#get user quotas and percent used
my (%lusers, $report);
foreach my $href (@users) {
foreach my $user (keys %$href) {
my $quotafile = "$href->{$user}/maildirsize";
next unless (-f $quotafile);
open(QF, "< $quotafile") or die $!;
my ($firstln, $quota, $used);
while (<QF>) {
my $line = $_;
if (! $firstln) {
$firstln = 1;
die "Error: corrupt quotafile $quotafile"
unless ($line =~ /^(\d+)S/);
$quota = $1;
last if (! $quota);
next;
}
die "Error: corrupt quotafile $quotafile"
unless ($line =~ /\s*(-?\d+)/);
$used += $1;
}
close(QF);
next if (! $used);
my $percent = int($used / $quota * 100);
$lusers{$user} = $percent unless not $percent;
}
}
#send a report to the postmasters
if ($MAIL_REPORT) {
open(MAIL, "| $MAILPROG");
select(MAIL);
map {print "To: $_\n"} @POSTMASTERS;
print "From: $COADDR\n";
print "Subject: Daily Quota Report.\n";
print "DAILY QUOTA REPORT:\n\n";
print "----------------------------------------------\n";
print "| % USAGE | ACCOUNT NAME |\n";
print "----------------------------------------------\n";
foreach my $luser ( sort { $lusers{$b} <=> $lusers{$a} } keys %lusers ) {
printf("| %3d | %32s |\n", $lusers{$luser}, $luser);
print "---------------------------------------------\n";
}
print "\n--\n";
print "$CONAME\n";
close(MAIL);
}
#email a warning to people over quota
if ($MAIL_WARNING) {
foreach my $luser (keys (%lusers)) {
next unless $lusers{$luser} >= $WARNPERCENT; # skip those under quota
open(MAIL, "| $MAILPROG");
select(MAIL);
print "To: $luser\n";
map {print "BCC: $_\n"} @POSTMASTERS;
print "From: $SUADDR\n";
print "Subject: WARNING: Your mailbox is $lusers{$luser} full.\n";
print "Reply-to: $SUADDR\n";
print "Your mailbox: $luser is $lusers{$luser} full.\n\n";
print "Once your e-mail box has exceeded your monthly storage quota\n";
print "your monthly billing will be automatically adjusted.\n";
print "Please consider deleting e-mail and emptying your trash folder to clear some space.\n\n";
print "Contact <$SUADDR> for further assistance.\n\n";
print "Thank You.\n\n";
print "--\n";
print "$CONAME\n";
close(MAIL);
}
}
|
I am getting the emails sent to my email account. Content of the email are as follows :-
Quote:
DAILY QUOTA REPORT:
----------------------------------------------
| % USAGE | ACCOUNT NAME |
----------------------------------------------
--
Company-A Postmaster
|
As you can see, no mailbox details was included in the email.
Anyone can help me with this. Thanks.
|

26th February 2007, 13:31
|
|
Super Moderator
|
|
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,665
Thanks: 1,896
Thanked 2,593 Times in 2,444 Posts
|
|
|

26th February 2007, 17:25
|
|
Senior Member
|
|
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Quote:
|
Originally Posted by falko
|
Final post from the forum above ..
Quote:
To be honest I don't know what's wrong...
__________________
Falko
-----
|
Would be simpler if you just wrote that down here ..
Anyway, I sorted this out.
Look for these
Quote:
.......................
close(QF);
next if (! $used);
my $percent = int($used / $quota * 100);
$lusers{$user} = $percent unless not $percent;
}
}
#send a report to the postmasters
.......................
|
Change it to
Quote:
.......................
close(QF);
next if (! $used);
my $percent = int($used / $quota * 100);
# $lusers{$user} = $percent unless not $percent;
$lusers{$user} = $percent;
}
}
#send a report to the postmasters
.......................
|
Now, I am getting the daily email report with all the informations of the email accounts quota.
Hope that will be of some help to anyone else.
PS : Maybe the author of the guide can update and put a note in that guide.
Thanks again.
|

7th July 2010, 09:02
|
|
Junior Member
|
|
Join Date: Jul 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
mail quota alert postfix
Hi when I try to put this script in crontab and then exceute cron tab it says an error in the first 4 lines that
use strict; use command not found
my $POSTFIX_CF = "/etc/postfix/main.cf" my command not found
my $MAILPROG = "/usr/sbin/sendmail -t" my command not found
my $WARNPERCENT = 80 my command not found
my @POSTMASTERS = 'quota@premediaglobal.com' my command not found
can anybody help me out of this. so that I can put this on my crontab for monitoring the quota alert...
thanks and regards
vigrem rajesh. M
|

8th July 2010, 13:24
|
|
Super Moderator
|
|
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,665
Thanks: 1,896
Thanked 2,593 Times in 2,444 Posts
|
|
Does your script begin with
?
|

9th July 2010, 12:47
|
|
Junior Member
|
|
Join Date: Jul 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
postfix quota notification email script
Yes my script starts as u mentioned..
when I try to execute the script from shell simply like
#./mail_quota_alert.pl
it execute normally and no error it shows mails are triggered.
but when I try to put the same script in cron job it shows the above following error.
regards
vigrem
|

10th July 2010, 11:24
|
|
Super Moderator
|
|
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,665
Thanks: 1,896
Thanked 2,593 Times in 2,444 Posts
|
|
What exactly did you put in your cron job?
|

12th July 2010, 05:10
|
|
Junior Member
|
|
Join Date: Jul 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
postfix quota notification email
this is the entry I put in cron tab
* */0 * * * /bin/sh /root/mail_quota_alert.pl
this is the entry I made...I made it make a mail trigger every day midnight..
check this and let me know..if am wrong..
thanks & regards
vigrem rajesh
|

12th July 2010, 13:30
|
|
Super Moderator
|
|
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,665
Thanks: 1,896
Thanked 2,593 Times in 2,444 Posts
|
|
|

13th July 2010, 09:00
|
|
Junior Member
|
|
Join Date: Jul 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
mail quota alert postfix
As you guided I have changed the cron tab..But when mail is triggered there only the layout is shown, no information regarding quota of the user is displayed.
just refer below how the mail is received.
DAILY QUOTA REPORT:
----------------------------------------------
| % USAGE | ACCOUNT NAME |
----------------------------------------------
I want to check the script while the output is given..no username or used quota percentage is displayed.
thanks and regards
vigrem rajesh.
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 23:38.
|
Recent comments
21 hours 14 min ago
1 day 2 hours ago
1 day 3 hours ago
1 day 4 hours ago
1 day 6 hours ago
1 day 10 hours ago
1 day 11 hours ago
1 day 13 hours ago
2 days 2 hours ago
2 days 4 hours ago