On a central Dovecot IMAP server it is rather easy to determine which
users are currently online: a process list with ps ax
displays at least one
imap process for each connected user detailing the username and the IP
address of the client. That was the basis of what I used for monitoring on-
line IMAP connections. With POP3 servers, the situation is quite
different. Even though the process list will show a pop3 process with the
username and the IP address of the client, that process is usually so short-
lived, that it is difficult to use that in determining whether a user is on-
line or not. The mail log files would give an indication, but log file
rotation would have to be accounted for, making the whole thing a bit more
difficult. I’ve created a quick and dirty patch for Dovecot which updates
a file in /var/spool/dovestats
when a client disconnects. The filename is
the username and contains a counter which is incremented at each call and a
single line of statistics (specified by Dovecot’s pop3_logout_format
option). The patch modifies the POP3 code to retrieve the username, create or
open the file, lock it, write the data and close it. Each time a user now
connects to the POP3 service, a file is updated to record that fact. A
comparison of the modification time of the files in the dovestats directory
to the current time (say within the last ten minutes) shows whether the user
is “on-line” or not. In order to visualize that on a large map, I then created
an XML-RPC service with XML-RPC for PHP which returns the currently
“connected” users. Since we allow users to login either with their username or
with their e-mail address, the name of the file in the dovestats directory
(which could be either username or e-mail address) is normalized by searching
the local LDAP directory to retrieve the unique userid for the user
(|(uid=filename)(mail=filename))
. In addition to the username, the service
returns the number of files in the user’s mail spool (allowing us to check
that she doesn’t have the leave mail on server switch enabled in the MUA) as
well as their total size in bytes. A Perl program with the Frontier-RPC
module collects that information and drops it into a database recording the
host name from which the information was gleaned.
host user active nfiles bytes
pop-a john 1 0 0
pop-a sue 0 12 14525
pop-b tom 1 1 9003
The rest is a matter of slapping up a PHP script that reads the values from
the database table and updates a JPEG image to reflect the counters on the
map. The image is then served on the fly to a web browser. The red and green
colors on the map indicate availability of the mail server.