Postfix Quota Notification Email Script

Discussion in 'Installation/Configuration' started by asyadiqin, Feb 25, 2007.

  1. asyadiqin

    asyadiqin New Member

    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

    I am getting the emails sent to my email account. Content of the email are as follows :-

    As you can see, no mailbox details was included in the email.

    Anyone can help me with this. Thanks.
     
  2. falko

    falko Super Moderator ISPConfig Developer

  3. asyadiqin

    asyadiqin New Member

    Final post from the forum above .. :D

    Would be simpler if you just wrote that down here .. :D

    Anyway, I sorted this out.

    Look for these

    Change it to

    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.
     
  4. vigremrajesh

    vigremrajesh New Member

    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 = '[email protected]' 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
     
  5. falko

    falko Super Moderator ISPConfig Developer

    Does your script begin with
    Code:
    #!/usr/bin/perl -w
    ?
     
  6. vigremrajesh

    vigremrajesh New Member

    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
     
  7. falko

    falko Super Moderator ISPConfig Developer

    What exactly did you put in your cron job?
     
  8. vigremrajesh

    vigremrajesh New Member

    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
     
  9. falko

    falko Super Moderator ISPConfig Developer

  10. vigremrajesh

    vigremrajesh New Member

    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.
     
  11. falko

    falko Super Moderator ISPConfig Developer

    Did you modify the script as shown in the tutorial?
     
  12. vigremrajesh

    vigremrajesh New Member

    mail quota alert postfix

    No I didn't modify the script that is given..


    The Highlighted alone I changed as I followed thread. other than that I didn't change anything.

    #!/usr/bin/perl -w
    2
    3 use strict;
    4
    5 my $POSTFIX_CF = "/etc/postfix/main.cf";
    6 my $MAILPROG = "/usr/sbin/sendmail -t";
    7 my $WARNPERCENT = 1;
    8 my @POSTMASTERS = '[email protected]';
    9 my $CONAME = 'PMG Helpdesk';
    10 my $COADDR = '[email protected]';
    11 my $SUADDR = '[email protected]';
    12 my $MAIL_REPORT = 1;
    13 my $MAIL_WARNING = 1;
    14
    15 #get virtual mailbox base from postfix config
    16 open(PCF, "< $POSTFIX_CF") or die $!;
    17 my $mboxBase;
    18
    19 while (<PCF>) {
    20 next unless /virtual_mailbox_base\s*=\s*(.*)\s*/;
    21 $mboxBase = $1;
    22 }
    23 close(PCF);
    24
    25 #assume one level of subdirectories for domain names
    26 my @domains;
    27 opendir(DIR, $mboxBase) or die $!;
    28 while (defined(my $name = readdir(DIR))) {
    29 next if $name =~ /^\.\.?$/; #skip '.' and '..'
    30 next unless (-d "$mboxBase/$name");
    31 push(@domains, $name);
    32 }
    33 closedir(DIR);
    34
    35 #iterate through domains for username/maildirsize files
    36 my @users;
    37 chdir($mboxBase);
    38 foreach my $domain (@domains) {
    39 opendir(DIR, $domain) or die $!;
    40 while (defined(my $name = readdir(DIR))) {
    41 next if $name =~ /^\.\.?$/; #skip '.' and '..'
    42 next unless (-d "$domain/$name");
    43
    44 push(@users, {"$name\@$domain" => "$mboxBase/$domain/$name"});
    45
    46
    47 # Added to show if the path to the mailboxes are correct.
    48 # At this point, its showing all the correct path to each mailbox
    49 printf($mboxBase."/".$domain."/".$name."\n");
    50
    51
    52 }
    53 }
    54 closedir(DIR);
    55
    56 #get user quotas and percent used
    57 my (%lusers, $report);
    58 foreach my $href (@users) {
    59 foreach my $user (keys %$href) {
    60 my $quotafile = "$href->{$user}/maildirsize";
    61 next unless (-f $quotafile);
    62 open(QF, "< $quotafile") or die $!;
    63 my ($firstln, $quota, $used);
    64 while (<QF>) {
    65 my $line = $_;
    66 if (! $firstln) {
    67 $firstln = 1;
    68 die "Error: corrupt quotafile $quotafile"
    69 unless ($line =~ /^(\d+)S/);
    70 $quota = $1;
    71 last if (! $quota);
    72 next;
    73 }
    74 die "Error: corrupt quotafile $quotafile"
    75 unless ($line =~ /\s*(-?\d+)/);
    76 $used += $1;
    77 }
    78 close(QF);
    79 next if (! $used);
    80 my $percent = int($used / $quota * 100);
    81 # $lusers{$user} = $percent unless not $percent;
    82 $lusers{$user} = $percent;
    83 }
    84 }
    85
    86 #send a report to the postmasters
    87 if ($MAIL_REPORT) {
    88 open(MAIL, "| $MAILPROG");
    89 select(MAIL);
    90 map {print "To: $_\n"} @POSTMASTERS;
    91 print "From: $COADDR\n";
    92 print "Subject: Daily Quota Report.\n";
    93 print "DAILY QUOTA REPORT:\n\n";
    94 print "----------------------------------------------\n";
    95 print "| % USAGE | ACCOUNT NAME |\n";
    96 print "----------------------------------------------\n";
    97 foreach my $luser ( sort { $lusers{$b} <=> $lusers{$a} } keys %lusers ) {
    98 print ("| %3d | %32s |\n", $lusers{$luser}, $luser);
    99 print "---------------------------------------------\n";
    100 }
    101 print "\n--\n";
    102 print "$CONAME\n";
    103 close(MAIL);
    104 }
    105
    106 #email a warning to people over quota
    107 if ($MAIL_WARNING) {
    108 foreach my $luser (keys (%lusers)) {
    109 next unless $lusers{$luser} >= $WARNPERCENT; # skip those under quota
    110 open(MAIL, "| $MAILPROG");
    111 select(MAIL);
    112 print "To: $luser\n";
    113 map {print "BCC: $_\n"} @POSTMASTERS;
    114 print "From: $SUADDR\n";
    115 print "Subject: WARNING: Your mailbox is $lusers{$luser} full.\n";
    116 print "Reply-to: $SUADDR\n";
    117 print "Your mailbox: $luser is $lusers{$luser} full.\n\n";
    118 print "Once your e-mail box has exceeded your monthly storage quota\n";
    119 print "your monthly billing will be automatically adjusted.\n";
    120 print "Please consider deleting e-mail and emptying your trash folder to clear some space.\n\n";
    121 print "Contact <$SUADDR> for further assistance.\n\n";
    122 print "Thank You.\n\n";
    123 print "--\n";
    124 print "$CONAME\n";
    125 close(MAIL);
    126 }
    127 }
     
  13. falko

    falko Super Moderator ISPConfig Developer

    Which tutorial (URL) did you use?
     
  14. vigremrajesh

    vigremrajesh New Member

    mail quota alert postfix

    I don't know what you are asking? But I didn't refer any tutorial for the above script. I have just copied the script from the same thread and try to execute. So when I got the error, I again while going through the same thread I noticed there is small change in that script & I did that....even though I got the output as I mentioned earlier.........


    thanks & regards
    vigrem rajesh
     
  15. falko

    falko Super Moderator ISPConfig Developer

    So you didn't use any of the "Virtual Users And Domains With Postfix, Courier And MySQL" tutorials here on HowtoForge?
     

Share This Page