How to Count Messages in Postfix Mail Queue Using Command Line Tools


4 views

The mailq command in Postfix shows all queued messages with detailed information. However, when managing email servers, we often need just the message count for monitoring or scripting purposes.

The simplest way to count messages in Postfix's queue is:

mailq | grep -c '^[0-9A-F]'

This pipeline works because:

  • mailq outputs each message with a queue ID starting with hex characters
  • grep -c counts lines matching the pattern
  • The pattern '^[0-9A-F]' matches queue IDs (alphanumeric at start of line)

For different use cases, consider these variations:

1. Count Active Queue Only

postqueue -p | grep -c '^[0-9A-F]'

2. Count Deferred Queue

find /var/spool/postfix/deferred -type f | wc -l

3. Using postsuper

postsuper -c | awk '/messages in queue/ {print $1}'

Here's a bash script example for monitoring:

#!/bin/bash
QUEUE_COUNT=$(mailq | grep -c '^[0-9A-F]')
if [ $QUEUE_COUNT -gt 100 ]; then
    echo "Warning: High queue count ($QUEUE_COUNT)" | mail -s "Postfix Alert" admin@example.com
fi

For very large queues, the find method on the deferred directory is more efficient than parsing mailq output. The mailq command locks the queue while running, which can impact performance on busy servers.

For comprehensive monitoring, you might want to break down counts by queue type:

#!/bin/bash
ACTIVE=$(find /var/spool/postfix/active -type f | wc -l)
DEFERRED=$(find /var/spool/postfix/deferred -type f | wc -l)
HOLD=$(find /var/spool/postfix/hold -type f | wc -l)
echo "Active: $ACTIVE, Deferred: $DEFERRED, Hold: $HOLD"

When managing a Postfix mail server, administrators often need to check the number of messages currently queued for delivery. The standard mailq command displays the entire queue, which can be overwhelming when you only need the total count.

Postfix provides a more efficient way to get just the message count:

postqueue -p | grep -c '^[0-9A-F]'

This command:

  • Uses postqueue -p (equivalent to mailq)
  • Pipes the output to grep
  • Counts lines starting with message IDs (hexadecimal characters)

For more detailed queue analysis, you can use qshape:

qshape deferred | wc -l

Note that qshape might need to be installed separately on some systems.

For regular monitoring, you might want to create a script:

#!/bin/bash
COUNT=$(postqueue -p | grep -c '^[0-9A-F]')
echo "Current mail queue: $COUNT messages"

Save this as mailq_count.sh and make it executable with chmod +x mailq_count.sh.

To distinguish between active and deferred messages:

postqueue -p | awk '/^[0-9A-F]/ {if($3 == "active") a++; else d++} END {print "Active:",a,"Deferred:",d}'

For large queues, these methods can be resource-intensive. In such cases, consider:

  • Running counts during off-peak hours
  • Caching results for monitoring systems
  • Using Postfix's built-in logging for message counts

Here's how to output the count in a format suitable for monitoring tools like Nagios:

#!/bin/bash
WARNING=100
CRITICAL=500

COUNT=$(postqueue -p | grep -c '^[0-9A-F]')

if [ $COUNT -ge $CRITICAL ]; then
  echo "CRITICAL: $COUNT messages in queue"
  exit 2
elif [ $COUNT -ge $WARNING ]; then
  echo "WARNING: $COUNT messages in queue"
  exit 1
else
  echo "OK: $COUNT messages in queue"
  exit 0
fi