December 06, 2003

Ensim: /var/spool/mqueue remedy.

In my 1 year experience with Ensim what I found most disturbing is when the sendmail daemon starts fighting for its life trying to keep up sending thousands of email with huge attachment to some users whose email domain cannot be resolved or user cannot be found or because users' quota exceeded.

Somtimes, there are people sending applications sized at 20/30 MB to other people through email, and unfortunately as the users quota is already full, those emails start getting queued in the /var/spool/mqueue directory and gets owned by the 'root' user by default resulting the a waste of harddisk space.

I had one case when I found a staggering 77GB of queued mails in the /var/spool/mqueue directory of a site. I cannot resit showing the finding here:

[root@ensim1 virtual]# find . -name mqueue -exec du -hsc {} \;
.
..
4.0k ./site10/fst/var/spool/mqueue
4.0k total
77G ./site11/fst/var/spool/mqueue
77G total
64k ./site12/fst/var/spool/mqueue
64k total
..
.
^C

[root@ensim1 virtual]# cd site11/fst/var/spool/mqueue
[root@ensim1 mqueue]# ls -lah
.
.
-rw------- 1 root mail 38MB Sep 11 14:19 dfh8B6Jpa13658
-rw------- 1 root root 38MB Sep 11 14:49 dfh8B6nsJ21407
-rw------- 1 root mail 38MB Sep 11 14:57 dfh8B6vFi23054
-rw------- 1 root root 38MB Sep 11 15:51 dfh8B7pAN03118
^C

[root@ensim1 mqueue]# ls -lah |grep 38MB |wc -l
2074
[root@ensim1 mqueue]#

As you can see from above, someone tried sending 2074 emails each 38MB of size. Phewwwwww... What a waste of bandwidth, money and energy.

I took a deeper look into the mail and found that the user sent the same mail to all the users of that particular domain and when he found that the mail did not go, he tried again and then tried again and then tried again... Neither the owner of the domain nor the sending user had any idea that their site is out of quota and all the mails are being stored in the system as 'root', so the system's disk space keep filling up. Its fortunate that there was enough space in the hard drive to sustain this activity.

Now, the question is why in the world the system keep storing the emails while the site's quota was full? Because, Ensim is designed not to bind the incoming email space with the site's quota. I find this as a misconception. You cant call this a bug, its definitely a misconception for virtual domain hosting.

What is the solution to this? I asked myself this question. I threw some queries to the Ensim forum and found the correct way to fix and cure it.

Here is what needs to be done to fix this problem:

Step 1:


[root@ensim1 root]# cd /home/virtual
[root@ensim1 virtual]# for i in `sitelookup -a domain`;do du -hsc $i/var/spool/mqueue;done;

This command will help you find the current culprit sites that are holding up the space. Then you can just delete away the queues. But, becarefull about deleting legitimate queues. You may want to delete only those which are huge in size.

Step 2:


Save the following code in a file called /bin/fixmqueue.sh:
#!/bin/sh
#
# Author: Ziaur Rhaman
# Copyleft 2003-2004.
# Description: This script will setgid the /var/spool/mqueue directory of a domain to make the queues owned
# by their siteadmin. It takes a series fo steps to achive this, including regenerating the sendmail.cf file
# for the domain.
#
DOMAIN=$1

export WP_USER=`/usr/local/bin/sitelookup -d $DOMAIN wp_user`

export WP_USER_UID=`/usr/local/bin/sitelookup -d $DOMAIN wp_user |/usr/bin/xargs id -u`

echo "[x] Owning /home/virtual/$WP_USER/var/spool/mqueue/..."

chown $WP_USER:$WP_USER /home/virtual/$WP_USER/var/spool/mqueue/

sleep 1
echo "[x] Changing mode to 777 on /home/virtual/$WP_USER/var/spool/mqueue/..."

chmod 777 /home/virtual/$WP_USER/var/spool/mqueue/

sleep 1
echo "[x] Setting setgid on /home/virtual/$WP_USER/var/spool/mqueue/..."

chmod g+s /home/virtual/$WP_USER/var/spool/mqueue/

sleep 1
echo "[x] Owning /home/virtual/$WP_USER/etc/smrsh..."

chown -R $WP_USER:$WP_USER /home/virtual/$WP_USER/etc/smrsh

sleep 1
echo "[x] Owning /home/virtual/$WP_USER/etc/alaiases*..."
chown -R $WP_USER:$WP_USER /home/virtual/$WP_USER/etc/aliases
chown -R root:$WP_USER /home/virtual/$WP_USER/etc/aliases.db

sleep 1
echo "[x] Making backup of /home/virtual/$WP_USER/etc/sendmail.cf..."

/bin/cp /home/virtual/$WP_USER/etc/sendmail.cf /home/virtual/$WP_USER/etc/sendmail.cf.zorig

sleep 1
echo "[x] Making backup of /home/virtual/$WP_USER/etc/mail/sendmail.mc..."

/bin/cp /home/virtual/$WP_USER/etc/mail/sendmail.mc /home/virtual/$WP_USER/etc/mail/sendmail.mc.zorig

sleep 1
echo "[x] Changing DefaultUser for sendmail to $WP_USER..."

/usr/bin/perl -i -p -e 's/mail:mail/$ENV{WP_USER_UID}:$ENV{WP_USER_UID}/;' /home/virtual/$WP_USER/etc/mail/sendmail.mc

sleep 1
echo "[x] Changing directory to /home/virtual/$WP_USER/etc/mail/..."

cd /home/virtual/$WP_USER/etc/mail/

sleep 1
echo "[x] Generating /home/virtual/$WP_USER/etc/sendmail.cf..."

/usr/bin/m4 sendmail.mc > /home/virtual/$WP_USER/etc/sendmail.cf

unset WP_USER

unset WP_USER_UID

Now, issue the following commands:

[root@ensim1 root]# cd /home/virtual
[root@ensim1 virtual]# for i in `sitelookup -a domain`;do if [ -s /home/virtual/$i/etc/mail/local-host-names ];then /bin/fixmqueue.sh $i;fi;done;


This command will convert all the /var/spool/mqueue directories of all the sites currently hosted for the system to save the queue files owned as the siteadmin user of the corresponding sites. This will prevent the system from saving the unnecessary queues as 'root' and wasting space.

The idea is to setgid the /var/spool/mqueue directory of the virtual site. By doing so, files saved in the mqueue directory will automatically be owned by the admin of the site thus restricting the mail queue space to the site's quota.

In order to save the queues as the siteadmin of the sites, the sendmail.cf of the site needs to be modified. By default ensim runs the sendmail for any or all sites as the user 'mail'. So, when it saves the queues it saves them as root:mail. To alter this to siteadmin:siteadmin, the "O DefaultUser=mail:mail" setting in sendmail.cf needs to be changed to "O DefaultUser=siteadmin:siteadmin". This is exactly what the above script does. The script above actually creates a backup of the sendmail.cf before it can make the changes. I used Perl to modify the sendmail.mc because I personally find it more convenient than 'sed' to modify files on the fly.

Step 3:


Now, save the following lines in the /etc/appliance/customization/virtDomain.sh file:

#!/bin/sh # # Author: Ziaur Rhaman # Copyleft 2003-2004. # DOMAIN=$1

export WP_USER=`/usr/local/bin/sitelookup -d $DOMAIN wp_user`

export WP_USER_UID=`/usr/local/bin/sitelookup -d $DOMAIN wp_user |/usr/bin/xargs id -u`

chown $WP_USER:$WP_USER /home/virtual/$WP_USER/var/spool/mqueue/

chmod 777 /home/virtual/$WP_USER/var/spool/mqueue/

chmod g+s /home/virtual/$WP_USER/var/spool/mqueue/

chown -R $WP_USER:$WP_USER /home/virtual/$WP_USER/etc/smrsh

chown -R $WP_USER:$WP_USER /home/virtual/$WP_USER/etc/aliases*

/bin/cp /home/virtual/$WP_USER/etc/sendmail.cf /home/virtual/$WP_USER/etc/sendmail.cf.zorig

/bin/cp /home/virtual/$WP_USER/etc/mail/sendmail.mc /home/virtual/$WP_USER/etc/mail/sendmail.mc.zorig

/usr/bin/perl -i -p -e 's/mail:mail/$ENV{WP_USER_UID}:$ENV{WP_USER_UID}/;' /home/virtual/$WP_USER/etc/mail/sendmail.mc

cd /home/virtual/$WP_USER/etc/mail/

/usr/bin/m4 sendmail.mc > /home/virtual/$WP_USER/etc/sendmail.cf

unset WP_USER

unset WP_USER_UID


then, make the file executable.

This script will automatically setgid the /var/spool/mqueue directory of any newly created site in both Ensim Basic and Pro (3.1.x and 3.5.x).

It can also be downloaded from: http://zort.org/codes/bash/ensim/virtDomain.sh.var-spool-mqueue


DISCLAIMER:

The information contained in this document is tested by the author and believed to be accurate. However, no responsibility is assumed by the author (Ziaur Rahman <zia at zort dot org>) for its use, or for any damage caused by any information provided here. Hence, please use this document or part of its contents at your own risk. A considerable amount of attention and testing, before applying any of the methods stated here, are recommended. Copying or modifying of any information in this document can be done at will, provided minimum credits to author is stated in the modified or copied document.

Posted by zeeky at December 6, 2003 04:16 PM
Comments
Post a comment









Remember personal info?