I want to see the last login of all email accounts, sorted by oldest first


Enter Your Query:
Use '%' for wildcards and quotes for "exact phrases"


Top Level » Email » Dovecot

I want to see the last login of all email accounts, sorted by oldest firstLast Modified: Mar 30, 2016, 8:52 pm
If you want to find out who isn't logging in, and see the account who have not been used in a long time, you can create a script:

/root/last_login.sh

and add the code

#!/bin/sh
DAUSER=*
DOMAIN=*
EMLUSER=*

LOGIN_CACHE=/tmp/last_login.cache
echo -n '' > $LOGIN_CACHE
chmod 600 $LOGIN_CACHE

for c in `ls /home/$DAUSER/imap/$DOMAIN/$EMLUSER/Maildir/dovecot.index.log`; do
{
       LL=`stat $c | grep 'Change: ' | cut -d' ' -f2,3`
       U=`echo $c | cut -d/ -f3`
       D=`echo $c | cut -d/ -f5`
       E=`echo $c | cut -d/ -f6`
       echo "$LL=user=$U&domain=$D&email=$E" >> $LOGIN_CACHE
};
done;

cat $LOGIN_CACHE | sort -n
rm -f $LOGIN_CACHE

exit 0;

then chmod the script to 700, and run it:

cd /root
chmod 755 last_login.sh
./last_login.sh

which will output all email accounts on the system, sorted by oldest login first.

If you only want the oldest 10 accounts, for example use "head":

./last_login.sh | head -n 10


For each line, the date will be on the far left, followed by the = character, followed by a url list, eg:

2014-10-07 01:31:08.684887527=user=fred&domain=fred.com&email=sales


The last login is determined only by checking the last change time of the dovecot.index.log file using "stat".
Note that an idle imap account won't keep updating this file, so it only refers to the actual time something was added to the log, like the original login or alteration of the account (I'm not 100% sure what it logs, so that's just a rough guess).
Point being, it's often several hours older than a potentially active but idle imap connection.
So only use it to track down accounts that are day weeks or months old.
Don't use it to determine a login to the hour or second.

© 2018 JBMC Software, Suite 173  3-11 Bellerose Drive, St Albert, AB  T8N 1P7  Canada.  Mon-Fri 9AM-5PM MST