How-to: Count POP3/IMAP bandwidth

smtalk

Administrator
Staff member
Joined
Aug 22, 2006
Messages
10,628
Location
LT, EU
Use the following script to see POP3/IMAP usage in bytes (it reads /var/log/maillog file and outputs the bandwidth usage for every email user (period of time counted depends on logrotate)):
Code:
#!/bin/sh
for u in `ls /usr/local/directadmin/data/users`; do
{
  echo "$u";
  echo -n "POP3: "
  grep "($u)" /var/log/maillog | awk '/pop3/ && /retr/ && /Disconnected/ {print $11}' | cut -d/ -f2 | cut -d, -f1 | awk '{sum+=$1} END {printf("%0.0f\n", sum)}'
  echo -n "IMAP: "
  grep "($u)" /var/log/maillog | awk '/imap/ && /bytes/ && /Disconnected/ {print $10}' | cut -d/ -f2 | cut -d, -f1 | awk '{sum+=$1} END {printf("%0.0f\n", sum)}'
   for d in `cat /usr/local/directadmin/data/users/$u/domains.list`; do
   {
     for eu in `cat /etc/virtual/$d/passwd | cut -d: -f1`; do
     {
       echo "$eu@$d";
       echo -n "POP3: "
       grep "($eu@$d)" /var/log/maillog | awk '/pop3/ && /retr/ && /Disconnected/ {print $11}' | cut -d/ -f2 | cut -d, -f1 | awk '{sum+=$1} END {printf("%0.0f\n", sum)}'
       echo -n "IMAP: "
       grep "($eu@$d)" /var/log/maillog | awk '/imap/ && /bytes/ && /Disconnected/ {print $10}' | cut -d/ -f2 | cut -d, -f1 | awk '{sum+=$1} END {printf("%0.0f\n", sum)}'
      }
      done;
   }
   done;
}
done;

exit 0;
 
Last edited:
Just out of curiosity, why would someone want this ability? I would think that POP3 and IMAP counts are already included in the bandwidth usage within DA. If that is not the case, how would I go about using your script and inserting these numbers into the bytes count for each master user?
 
Hi Martinas,

A question about your script - does it also count the imap/pop traffic for the main account? It seems to ignore those in the test that I have run, only showing [email protected] users.

Does that mean that the main account imap is already counted by DA? It is not quite clear in the overview provided by DA (the link you added).

Finally, is there a logical method to merge the bandwidth results from your script with the DA tally? (basically my question is whether it has been done, not really asking you to spend time teaching me how the DA tally is done, I should delve into that myself).

Bye,

Harro
 
Please try a newer version of the script. It should count users now (and both RETR and TOP bandwidth for POP3 (as DirectAdmin's version of dovecot.conf doesn't have %i/%o set for POP3 logs yet)). It shouldn't be hard to send these results to tally, but I haven't tried that yet.
 
Hi again,

Thanks for your reply - when you say "try a newer version of the script", do you mean that there is a newer one available, or that I should make a newer version? Or do you mean that DA has updated its own script, so that now all bandwidth is included?

Kind regards,

Harro
 
I tried this script, really sounds interesting.

Yet it shows all accounts with usage 0, making me think there is something different in my system.

What should I double check to make this script work?

Thanks!

Jose
 
If nobody uses IMAP, then those figures should all be zero. If everything else is also zero, then maybe the maillog has just been rotated (i.e. the log is still empty?). Or, maybe you have set the maillog to be in a different location or file, so that you are not parsing the correct file?

Those would be my first thoughts, hope they give you a clue!
 
Hi, I checked the script and I get lost in all the awk etc. Yet I do see that /var/log/maillog has entries and yet the script outputs zero for all accounts.

I'm pasting some of the Disconnected lines to show my problem.

Code:
2010-10-11T00:04:00.964924-05:00 gruta dovecot[2839]: POP3([email protected]): Disconnected: Logged out top=0/0, retr=0/0, del=0/0, size=0
2010-10-11T00:04:01.008800-05:00 gruta dovecot[2839]: POP3([email protected]): Disconnected: Logged out top=0/0, retr=0/0, del=0/1, size=30242
2010-10-11T00:04:01.070764-05:00 gruta dovecot[2839]: POP3([email protected]): Disconnected: Logged out top=0/0, retr=0/0, del=0/3, size=115679

Another consern I see is that some of my users use Blackberries and it uses IMAP with IDLE to allow push (hence it's a "permament" connection) and there is never (in theory) a Disconnection. Will this script also count that?

Thanks for your help!

Jose
 
Actually the script seems to doesnt work for me.

Im usign CentOS


This example is a grep of the result:

Code:
MYEMAIL@MYDOMAIN
POP3: 0
IMAP: 0

Is needed any specific tool or have any suggestion?

Thanks
 
Back
Top