Zimbra Top Sender Report: 4 Easy Commands, Avoid Mistakes
To list the top sender in Zimbra you count the from=<...> field in the mail log with a short shell pipeline. The pipeline from the original article runs fine, but when it was tested on a sample log it listed a sender who never delivered a single message in second place. This rewrite explains each stage, shows that test, and gives three variants that answer different questions: who sent the most messages, who logged in and sent the most, and what to do with rotated logs.
How this was tested: there is no Zimbra server in the author's lab, so the test used a 35-line sample log in Postfix's log format (the format Zimbra's MTA writes to /var/log/zimbra.log), built by a small script with known counts: alice sent 5 messages, bob 3, carol 1 message to 3 recipients, an outside host was rejected 4 times, two bounces had an empty sender, and root sent 2. Because the counts are known, you can see exactly what each command counts. Real Zimbra logs have many more line types, so check the results on your own server before you act on them.
Contents
What the top sender pipeline does
The command from the original post is one line. Run it on the Zimbra MTA server; the original post connected with SSH first (ssh user@your-server):
cat /var/log/zimbra.log | awk -F 'from=<' '{print $2}' | awk -F'>' '{print $1}' | sed '/^$/d' | sort | uniq -c | sort -nk1 -r | sed -n '1,6p'
Each stage has one job. The first awk splits every line on the text from=< and prints what follows it. The second awk cuts that text at the first >, which leaves only the address. sed '/^$/d' deletes the empty results (lines that had no from=<, or bounce messages with an empty sender). sort puts equal addresses next to each other, uniq -c counts them (see the uniq manual), sort -nk1 -r orders by that count from high to low, and sed -n '1,6p' prints the first six lines. Change the numbers to get a longer list.
What the top sender result really counts
Here is the same top sender pipeline on the sample log. The expected order by delivered messages would be alice (5), bob (3), then carol and root with 1 each:
5 alice@example.com
4 spam@bad.example
3 bob@example.com
1 root@mail.example.com
1 root
1 carol@example.com
The old command puts spam@bad.example in second place with 4, although that address never got a message accepted: those are four rejected delivery attempts. The line NOQUEUE: reject: RCPT from ... from=<spam@bad.example> to=... also contains the text from=<, so every refusal is counted as if the sender had sent mail. The output also contains root twice in different forms, because in this sample the pickup line logs root and the queue manager line logs root@mail.example.com. So the plain command measures "log lines that mention a sender", not "messages sent". Is that useless? No: a high count for an unknown address is also a useful hint of an attack, but you should know which one you are looking at.
Count messages for the top sender: use only the queue manager lines
Postfix normally writes one qmgr line per accepted message, with the sender and the number of recipients (nrcpt). Matching only those lines counts messages (in the sample), ignores rejected attempts, and skips the empty bounce sender:
$ grep -o "postfix/qmgr.*from=<[^>]*>" zimbra.log | sed "s/.*from=<//; s/>$//" | grep -v "^$" | sort | uniq -c | sort -rn | head -6
5 alice@example.com
3 bob@example.com
1 root@mail.example.com
1 carol@example.com
Now the rejected spammer is gone and the top sender order is correct. Note that carol's message to three recipients counts once. If you want to rank by the number of recipients instead, take nrcpt= from the same line and add it up per sender, because one mail to a large list is much more load than one mail to a single person.
Find the top sender among logged-in users
When the goal is to catch a compromised account, count the login name that Postfix records for authenticated submissions. The field is sasl_username=, and it only exists for users who logged in, so outside senders and bounces cannot appear:
$ grep -o "sasl_username=[^ ]*" zimbra.log | cut -d= -f2 | sort | uniq -c | sort -rn | head -6
5 alice@example.com
3 bob@example.com
1 carol@example.com
An account that suddenly becomes the top sender with thousands of messages is a strong sign of stolen credentials. Combine this list with the rules in custom SpamAssassin rules for Zimbra and the sender checks described in Zimbra error 450 4.7.1 to limit the damage.
Include rotated logs in the top sender count
The file zimbra.log only holds the current period; older lines are usually in rotated files, often compressed. In the lab the active log and one gzip copy of it were counted together with zgrep, which reads plain and compressed files alike, and the totals simply doubled:
$ ls zimbra.log*
zimbra.log
zimbra.log-20261031.gz
$ zgrep -h -o "sasl_username=[^ ]*" zimbra.log zimbra.log-*.gz | cut -d= -f2 | sort | uniq -c | sort -rn | head -3
10 alice@example.com
6 bob@example.com
2 carol@example.com
The -h option hides the file names so that the same address is added across files. The names of rotated files differ between systems, so list the folder first and adjust the pattern.
Common mistakes with a top sender report
- Treating a top sender count as "messages sent" when the pipeline also counts rejected attempts.
- Counting only today's file and missing the burst from yesterday.
- Reading a high count for a mailing-list address or a monitoring account as abuse; check what the account is for first.
- Running
caton a very large log and waiting for it. Pass the file name togrepdirectly instead, as the examples above do; it is faster and shorter.
FAQ
Can I list more than six top sender addresses? Yes. Change sed -n '1,6p' or head -6 to the number you need.
Can I limit the report to one day? Yes. The log lines start with the date, so add grep "^Nov 1 " at the start of the pipeline (note the two spaces before a single-digit day in this log format).
Does it work on the mailbox server? The original post ran it on the MTA server, where the Postfix lines are written; on a mailbox-only server the lines will not be there.
Thanks for sharing. this is help me
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete