Set Up Postfix Mail Queue Monitoring: Ubuntu VPS Alert System

By Raman Kumar

Share:

Updated on May 26, 2026

Set Up Postfix Mail Queue Monitoring: Ubuntu VPS Alert System

Understanding Postfix Mail Queue Monitoring

Your email server's queue is where messages wait for delivery. When the queue grows too large or messages sit too long, it signals delivery problems that can hurt your sender reputation. Setting up proper monitoring helps you catch issues before they affect your customers' email flow.

Postfix maintains several queues: incoming, active, deferred, and hold. Each serves a different purpose in the mail processing pipeline. The deferred queue is particularly important to monitor since it contains messages that failed initial delivery attempts.

At Hostperl VPS hosting, we see many customers struggle with email delivery issues that could have been prevented with proper queue monitoring. This tutorial shows you how to build an automated system that alerts you before problems escalate.

Prerequisites and Initial Setup

You need a working Postfix installation on Ubuntu 22.04 or newer. Your system should have basic monitoring tools and email capabilities configured. We'll use standard Unix utilities and shell scripting rather than complex monitoring frameworks.

First, verify your Postfix installation and check the current queue status:

sudo systemctl status postfix
sudo postqueue -p
mailq

Create a dedicated user for monitoring activities. This follows security best practices by limiting privileges:

sudo adduser --system --group mailmon
sudo mkdir /var/lib/mailmon
sudo chown mailmon:mailmon /var/lib/mailmon

Creating Queue Monitoring Scripts

Build a comprehensive monitoring script that checks queue size, message age, and delivery status. This script will form the foundation of your alerting system.

Create the main monitoring script at `/usr/local/bin/check-mail-queue.sh`:

#!/bin/bash

# Mail queue monitoring script
DATE=$(date "+%Y-%m-%d %H:%M:%S")
QUEUE_SIZE=$(postqueue -p | tail -n 1 | awk '{print $5}')
DEFERRED_COUNT=$(postqueue -p | grep -c "^[A-F0-9]*[*!]")
OLD_MESSAGES=$(postqueue -p | awk '/^[A-F0-9]/ {if ($3 > 3600) count++} END {print count+0}')

# Set thresholds
MAX_QUEUE_SIZE=100
MAX_DEFERRED=50
MAX_OLD_MESSAGES=10

# Log current status
echo "[$DATE] Queue: $QUEUE_SIZE, Deferred: $DEFERRED_COUNT, Old: $OLD_MESSAGES" >> /var/lib/mailmon/queue.log

# Check thresholds and alert
if [ "$QUEUE_SIZE" -gt "$MAX_QUEUE_SIZE" ] || [ "$DEFERRED_COUNT" -gt "$MAX_DEFERRED" ] || [ "$OLD_MESSAGES" -gt "$MAX_OLD_MESSAGES" ]; then
    echo "ALERT: Mail queue issues detected" | mail -s "Mail Queue Alert - $(hostname)" admin@yourdomain.com
fi

Make the script executable and set proper ownership:

sudo chmod +x /usr/local/bin/check-mail-queue.sh
sudo chown mailmon:mailmon /usr/local/bin/check-mail-queue.sh

Advanced Queue Analysis and Reporting

Create a detailed analysis script that provides deeper insights into queue contents and delivery patterns. This helps identify specific domains or recipients causing problems.

Build `/usr/local/bin/analyze-queue.sh` for comprehensive queue analysis:

#!/bin/bash

# Detailed queue analysis
echo "=== Mail Queue Analysis $(date) ==="
echo

# Overall queue status
echo "Queue Summary:"
postqueue -p | tail -n 1
echo

# Top problematic domains
echo "Top Deferred Domains:"
postqueue -p | awk '/MAILER-DAEMON/ {print $7}' | sort | uniq -c | sort -rn | head -10
echo

# Messages by age
echo "Messages by Age (hours):"
postqueue -p | awk '/^[A-F0-9]/ {age=int($3/3600); ages[age]++} END {for (a in ages) print a"h: "ages[a]}' | sort -n
echo

# Bounce analysis
echo "Recent Bounce Reasons:"
tail -n 50 /var/log/mail.log | grep "bounced" | awk '{print $9" "$10" "$11}' | sort | uniq -c | sort -rn

Setting Up Log Rotation

Configure log rotation to prevent monitoring logs from consuming too much disk space. Create `/etc/logrotate.d/mailmon`:

/var/lib/mailmon/*.log {
    weekly
    rotate 4
    compress
    delaycompress
    missingok
    notifempty
    create 644 mailmon mailmon
}

Implementing Alert Mechanisms

Design a flexible alerting system that can notify you through multiple channels. Start with email alerts and expand to include system notifications or external services.

Create an enhanced alert script at `/usr/local/bin/mail-queue-alert.sh`:

#!/bin/bash

# Enhanced alerting system
CONFIG_FILE="/etc/mailmon/alerts.conf"
ALERT_STATE_FILE="/var/lib/mailmon/alert_state"

# Source configuration
if [ -f "$CONFIG_FILE" ]; then
    source "$CONFIG_FILE"
else
    # Default values
    ALERT_EMAIL="admin@yourdomain.com"
    ESCALATION_EMAIL="ops@yourdomain.com"
    ALERT_COOLDOWN=1800  # 30 minutes
fi

# Check if we're in cooldown period
if [ -f "$ALERT_STATE_FILE" ]; then
    LAST_ALERT=$(cat "$ALERT_STATE_FILE")
    CURRENT_TIME=$(date +%s)
    TIME_DIFF=$((CURRENT_TIME - LAST_ALERT))
    
    if [ $TIME_DIFF -lt $ALERT_COOLDOWN ]; then
        exit 0
    fi
fi

# Send alert and update state
send_alert() {
    local severity=$1
    local message=$2
    
    echo "$message" | mail -s "[$severity] Mail Queue Alert - $(hostname)" "$ALERT_EMAIL"
    
    # Escalate critical alerts
    if [ "$severity" = "CRITICAL" ]; then
        echo "$message" | mail -s "[CRITICAL ESCALATION] Mail Queue - $(hostname)" "$ESCALATION_EMAIL"
    fi
    
    # Update alert state
    date +%s > "$ALERT_STATE_FILE"
}

Your monitoring setup should integrate with existing infrastructure monitoring tools if available. Many hosting providers use tools like Nagios or Zabbix for comprehensive server monitoring.

Configuring Automated Scheduling

Set up cron jobs to run your monitoring scripts at appropriate intervals. Different checks require different frequencies based on your email volume and service requirements.

Add these entries to the mailmon user's crontab:

sudo -u mailmon crontab -e

Insert the following cron schedule:

# Check queue every 5 minutes
*/5 * * * * /usr/local/bin/check-mail-queue.sh

# Detailed analysis every hour
0 * * * * /usr/local/bin/analyze-queue.sh > /var/lib/mailmon/hourly-report.log

# Daily summary at 8 AM
0 8 * * * /usr/local/bin/daily-queue-summary.sh

Creating Daily Summary Reports

Build a daily summary script that provides comprehensive queue health metrics. This helps track trends and identify gradual degradation:

#!/bin/bash

# Daily queue summary
DATE=$(date "+%Y-%m-%d")
REPORT_FILE="/var/lib/mailmon/daily-$DATE.log"

echo "=== Daily Mail Queue Summary for $DATE ===" > "$REPORT_FILE"
echo >> "$REPORT_FILE"

# Calculate daily statistics
grep "$DATE" /var/lib/mailmon/queue.log | awk '{
    split($4, queue, ","); 
    split($6, deferred, ","); 
    total_queue += queue[1];
    total_deferred += deferred[1];
    count++;
} END {
    print "Average queue size: " int(total_queue/count);
    print "Average deferred: " int(total_deferred/count);
    print "Total checks: " count;
}' >> "$REPORT_FILE"

As covered in our complete Postfix mailq guide, understanding queue mechanics is essential for effective monitoring.

Integration with System Monitoring

Connect your mail queue monitoring with broader system health checks. This provides context when diagnosing email delivery issues and helps identify resource constraints affecting mail service.

Create a system integration script that correlates mail queue status with server resources:

#!/bin/bash

# System correlation monitoring
check_system_impact() {
    local queue_size=$1
    
    # Check disk space
    DISK_USAGE=$(df /var/spool/postfix | awk 'NR==2 {print $5}' | sed 's/%//')
    
    # Check memory usage
    MEM_USAGE=$(free | awk 'NR==2{printf "%.0f", $3*100/$2}')
    
    # Check load average
    LOAD_AVG=$(uptime | awk '{print $10}' | sed 's/,//')
    
    if [ "$DISK_USAGE" -gt 90 ] || [ "$MEM_USAGE" -gt 80 ]; then
        echo "System resource constraints may be affecting mail queue"
        echo "Disk: ${DISK_USAGE}%, Memory: ${MEM_USAGE}%, Load: $LOAD_AVG"
    fi
}

This integration approach helps differentiate between mail server configuration issues and broader system problems. For comprehensive email hosting guidance, see our email server performance optimization guide.

Troubleshooting Common Queue Issues

Build automated remediation capabilities into your monitoring system. Many common queue problems can be resolved automatically without administrator intervention.

Create a queue cleanup script that handles routine maintenance:

#!/bin/bash

# Automated queue cleanup
cleanup_queue() {
    # Flush deferred queue for retry
    postqueue -f
    
    # Remove messages older than 7 days
    postqueue -p | awk '/^[A-F0-9]/ {if ($3 > 604800) print $1}' | postsuper -d -
    
    # Clean up corrupted messages
    postsuper -s
    
    echo "Queue cleanup completed at $(date)" >> /var/lib/mailmon/cleanup.log
}

Your remediation strategy should include escalation procedures for issues that can't be resolved automatically. Document these procedures clearly for operations staff.

Performance Optimization and Tuning

Monitor queue performance metrics to identify opportunities for optimization. Track delivery rates, queue processing times, and resource utilization patterns.

Implement performance tracking in your monitoring scripts:

#!/bin/bash

# Performance metrics collection
collect_performance_metrics() {
    local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
    
    # Delivery rate calculation
    DELIVERED_LAST_HOUR=$(grep "$(date -d '1 hour ago' '+%b %d %H')" /var/log/mail.log | grep -c "status=sent")
    
    # Queue processing time
    AVG_QUEUE_TIME=$(postqueue -p | awk '/^[A-F0-9]/ {total+=$3; count++} END {if(count>0) print int(total/count); else print 0}')
    
    # Log metrics
    echo "$timestamp,delivered_rate:$DELIVERED_LAST_HOUR,avg_queue_time:$AVG_QUEUE_TIME" >> /var/lib/mailmon/performance.csv
}

Regular performance analysis helps you understand normal operating patterns and detect anomalies that might indicate configuration problems or increased load.

Reliable email delivery requires robust infrastructure and proactive monitoring. Hostperl VPS hosting provides the performance and reliability your mail servers need, with New Zealand-based support to help you maintain optimal email operations. Our team assists with mail server setup, monitoring implementation, and performance optimization.

Frequently Asked Questions

How often should I check my Postfix mail queue?

Check every 5 minutes for basic queue size monitoring, hourly for detailed analysis, and daily for trend reporting. High-volume mail servers may need more frequent monitoring.

What queue size indicates a problem?

It depends on your normal mail volume, but generally more than 100 messages in the queue for extended periods suggests delivery issues. Set thresholds based on your typical patterns.

How do I handle persistent deferred messages?

Investigate the bounce reasons first. Common causes include DNS issues, recipient server problems, or reputation issues. Use postqueue -f to retry delivery after addressing the underlying cause.

Can monitoring impact mail server performance?

Minimal impact if done correctly. Avoid running queue checks more than every minute, and use efficient commands. The mailq command is less resource-intensive than postqueue -p for basic checks.

What logs should I monitor alongside the queue?

Monitor /var/log/mail.log for delivery details, /var/log/maillog for system messages, and consider enabling Postfix verbose logging for troubleshooting specific issues.