Thursday, August 23, 2007

monitor large mailbox users

Monitoring mailbox users can be done in many serveral ways via web interface or via terminal or via bash scripts.

With the usual mbox type emails, incoming new email messages are automatically redirected to each users' own spool file. This spool mail is by default located and stored in /var/spool/mail. From there, the spool file just waits for its owner to pull or pop it out for retrieval via any mail retrieving software agents (MUAs) like Thunderbird, Outlook, Eudora, The Bat and the like.

Linux has been equipped with thousand usable tools that when combined creates another function from these combined set of linux tools. This document entry would cover how to monitor large mailbox (mbox) users using different linux commands and sends out mail notification.

This can be achieved with the following steps.

First, listing the file size of all spool mails listed under /var/spool/mail would be the first step in order to determine your top or large mailbox users. Listing file usage of /var/spool/mail can be done as follows:

# du -h /var/spool/mail/*

From the above, we are appending -h parameter for a more human-readable output form. Here's a sample result from issuing the above command:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 /var/spool/mail/vertita
20K /var/spool/mail/vertito
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Secondly, the next thing to do is to sort these spool size in order. Sorting can be done from highest to lowest or vice versa. This is possible using sort linux command.

Sorting data gives us way to have the top and last list of data. From here, we just need to pipe out the resulting result from disk usage as an input value to sort linux command. Hence, we can now have a numerically sorted list of mailbox users as shown below:

# du -h /var/spool/mail/* | sort -rn

From the above, -r parameter is for reversal sorting. A sample output would be

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
832M /var/spool/mail/vertito1
.
.
0K /var/spool/mail/vertito100
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Finally, all we need is to have fetch only first or top 20 or 10 of them. Top 20 large mailbox users can be done by using the head linux command. Dumping the first two results from du and sort and redirecting it to head linux command would be done like so

# du /var/spool/mail/* | sort -rn | head -10
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
879768 /var/spool/mail/vertito1
879572 /var/spool/mail/vertito2
846540 /var/spool/mail/vertito3
768680 /var/spool/mail/vertito4
695664 /var/spool/mail/vertito5
684264 /var/spool/mail/vertito6
577660 /var/spool/mail/vertito7
553740 /var/spool/mail/vertito8
520856 /var/spool/mail/vertito9
506880 /var/spool/mail/vertito10
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hola amigos! Now, you have the top 10 large mailbox users from your screen.

Now, remember this can be sent as an email on daily basis too by using linux mail command combined with crontab utility.

Linux job scheduling can be found here while sending mail from terminal sample can be found here and here.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy