HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Installation/Configuration (http://www.howtoforge.com/forums/forumdisplay.php?f=4)
-   -   Postfix Quota Notification Email Script (http://www.howtoforge.com/forums/showthread.php?t=10883)

asyadiqin 25th February 2007 13:06

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.

falko 26th February 2007 13:31

http://www.howtoforge.com/forums/sho...Y+QUOTA+REPORT

asyadiqin 26th February 2007 17:25

Quote:

Originally Posted by falko

Final post from the forum above .. :D

Quote:

To be honest I don't know what's wrong...
__________________
Falko
-----
Would be simpler if you just wrote that down here .. :D

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.

vigremrajesh 7th July 2010 09:02

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

falko 8th July 2010 13:24

Does your script begin with
Code:

#!/usr/bin/perl -w
?

vigremrajesh 9th July 2010 12:47

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

falko 10th July 2010 11:24

What exactly did you put in your cron job?

vigremrajesh 12th July 2010 05:10

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

falko 12th July 2010 13:30

* */0 * * *
doesn't look ok. Please take a look at http://www.howtoforge.com/a-short-in...n-to-cron-jobs

vigremrajesh 13th July 2010 09:00

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.


All times are GMT +2. The time now is 03:13.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.