Friday, May 10, 2013

Postfix limit outgoing mail

You have some postfix based relays in the corporation environment, with thousands of very important email messages. System works properly One day developer wants to test a script or program for sending outbound mail. He asks you about SMTP connection credentials and warn you that there is a chance of mistake, so his software may send thousands messages instead of few.
Anvil will help us to prevent the system from overloading
Note: This features are available in Postfix 2.2 and later.
Open postfix config file in your favourite text editor


#vim /etc/postfix/main.cf
And add next rows
smtpd_client_event_limit_exceptions = #Clients that are excluded from connection count (default: $mynetworks)
anvil_rate_time_unit = 60s #The time unit over which client connection rates and other rates are calculated. (default: 60s)
anvil_status_update_time = 120s #How frequently the server logs peak usage information. (default: 600s)
smtpd_client_message_rate_limit=500 #The
 maximal number of message delivery requests that any client is allowed 
to make to this service per time unit.  (default: 0) To disable this 
feature, specify a limit of 0.
This configuration means that client may send 500 messages per minute, message 501 will be rejected.
Make this changing on your testing environment and check it with next script
#vim bomber.sh

and Insert

#!/bin/bash
i=0
while [ $i != 2500 ]; do
echo test | /bin/mail -s "test message$i" "user@test.com"
i=$(( $i + 1 ))
done;

2500 under while -  means that script will try to send 2500 messages.
Replace "user@test.com" with real mailbox
Now make the script executable 
#chmod 700 bomber.sh
#./bomber.sh 
#tailf /var/log/maillog 
And we are ready.

No comments:

Post a Comment