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 = 'pmghelpdesk@premediaglobal.com';
9 my $CONAME = 'PMG Helpdesk';
10 my $COADDR = 'pmghelpdesk@premediaglobal.com';
11 my $SUADDR = 'pmghelpdesk@premediaglobal.com';
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 }
|
Recent comments
1 day 3 hours ago
1 day 12 hours ago
1 day 15 hours ago
1 day 16 hours ago
1 day 17 hours ago
1 day 19 hours ago
1 day 21 hours ago
1 day 22 hours ago
2 days 14 hours ago
2 days 15 hours ago