2008-10-22 79 views
9

什么是监控Linux服务器上Postfix安装的好命令?它应该是一个可以在SSH会话中从命令提示符运行的命令,并返回有关后缀进程是否处于活动状态以及相关邮件队列大小的信息。如何监控Postfix MTA状态

编辑

这可能似乎不是切合主题的堆栈溢出(用于服务器故障?也许更好),但它确实涉及到软件的开发。当时,我正在创建一个使用多个MTA进行邮件传递的复杂邮件发件人应用程序。如果一台服务器上的邮件队列变得太大,那么它应该更喜欢其他不太忙的MTA。因此,控制应用程序需要一种方法来维护与每个MTA的SSH连接并定期监视其状态。

如果监控需求仅仅是为了管理目的,有很多GUI工具可以更轻松地完成。

回答

17

您可以使用附带的后缀命令行程序:

  • MAILQ:显示哪些队列有邮件被 '处理'
  • postqueue -p:是什么MAILQ实际引用的
  • postqueue - F:刷新所有排队的邮件,立即设法得到它交付
  • 的ps aux:在服务器上运行的过程显示,认准的事,如:
    • smtpd的
    • proxymap
    • 琐碎的重写
    • QMGR
    • 皮卡
    • showq

你使用ps命令找到什么将取决于你如何改变设置的东西。

5

按优先顺序排列:

最好的办法是通过邮件服务器发送心跳消息和监测在抵达目的地。

使用mailq和qshape(附带最新的postfix分发版)来监视队列。

您可以检查smtpd的是倾听和使用netcat的返回横幅(选项netcat的由OS有所不同;这些是用于Linux):

 
nc -w 1 localhost 25 </dev/null 

下面将报告的进程数为每个后缀守护,由主人分组(多个主人,如果你有多个postfix实例)。

ps -e -o pid,ppid,fname | perl -lane ' 
    if ($F[1] != 1) { 
     ++$c{$F[1]}->{$F[2]}; 
    } elsif ($F[2] eq "master") { 
     push(@masters, $F[0]); 
    } 
    END { 
     foreach $master (@masters) { 
      print "=== master: $master ==="; 
      foreach $agent (keys %{$c{$master}}) { 
       printf "\t%-5d %s\n", $c{$master}->{$agent}, $agent; 
      } 
     } 
    } 
' 
5

除了上面Bryan Rehbein的回答,这里是我用来监视postfix队列长度的脚本。它所做的只是在队列长度超过X个消息时发送电子邮件警报。该警报是使用msmtp(一个微型命令行smtp客户端)发送的,因此它绕过了本地postfix安装(如果队列正在增长,您不能依靠它来及时获取警报......)

#!/bin/bash 
# 
# Postfix queue length monitoring script (requires msmtp) 
# 
# This script checks the active, incoming, deferred and maildrop postfix queue directories. 
# 
# If the number of messages in any of these directories is more than $MAX_QUEUE_LENGTH, 
# the script will generate an alert email and send it using msmtp. We use msmtp so that 
# we can bypass the local postfix installation (since if the queues are getting big, 
# the alert email may not be sent in time to catch the problem). 
# 

######################################################### 
# SET SCRIPT VARS 
######################################################### 

# Path to msmtp binary (e.g. /usr/bin/msmtp on Debian systems) 
MSMTP=/usr/bin/msmtp 

# Remote mail host (this is the mail server msmtp will use to send the alert. It should NOT be the local postfix installation) 
MAILHOST=backup.mailserver.com 

# Remote mail port 
MAILPORT=25 

# Mail protocol 
MAILPROTO=smtp 

# Fully qualified domain name of local postfix installation 
DOMAIN=primary.mailserver.com 

# From address 
[email protected] 

# Recipient (this address should not route to the local postfix installation, for obvious reasons) 
MAILTO="[email protected]" 

# Email subject 
MAILSUBJECT="Postfix queue length alert for ${DOMAIN}" 

# MSMTP log file 
LOGFILE=/var/log/msmtp.log 

# Root of the postfix queue dirs (e.g. /var/spool/postfix on Debian systems). Note: no trailing slash. 
QUEUEDIR_ROOT="/var/spool/postfix" 

# Max queue length (if there are more messages in a queue than this number, we will send an alert) 
MAX_QUEUE_LENGTH=10 


######################################################### 
# SCRIPT LOGIC STARTS HERE 
######################################################### 

# Check msmtp binary exists 
if [ ! -f ${MSMTP} ] 
then 
     echo "Cannot find ${MSMTP}. Exiting." 
     exit 1 
fi 

# Get the number of messages sitting in each postfix queue directory 
Q_ACTIVE=$(find ${QUEUEDIR_ROOT}/active -type f | wc -l) 
Q_INCOMING=$(find ${QUEUEDIR_ROOT}/incoming -type f | wc -l) 
Q_DEFERRED=$(find ${QUEUEDIR_ROOT}/deferred -type f | wc -l) 
Q_MAILDROP=$(find ${QUEUEDIR_ROOT}/maildrop -type f | wc -l) 

# If any of these queues contain more than $MAX_QUEUE_LENGTH issue an alert 
if [ ${Q_ACTIVE} -gt ${MAX_QUEUE_LENGTH} -o ${Q_INCOMING} -gt ${MAX_QUEUE_LENGTH} -o ${Q_DEFERRED} -gt ${MAX_QUEUE_LENGTH} -o ${Q_MAILDROP} -gt ${MAX_QUEUE_LENGTH} ]; then 

    (
     echo "From: ${MAILFROM} " 
     echo "To: ${MAILTO} " 
     echo "Mime-Version: 1.0" 
     echo 'Content-Type: text/plain; charset="iso-8859-1"' 
     echo "Subject: ${MAILSUBJECT}" 
     echo "" 
     echo "One or more of the postfix queues on ${DOMAIN} has grown beyond ${MAX_QUEUE_LENGTH} messages in length." 
    ) | ${MSMTP} --host=${MAILHOST} --port=${MAILPORT} --protocol=${MAILPROTO} --domain=${DOMAIN} --auth=off --tls=off --from=${MAILFROM} --logfile=${LOGFILE} --syslog=off --read-recipients 

    exit 2 

fi 

exit 0 
+0

另外值得一提的是,msmtp支持TLS/SSL并指定用户名/密码。因此,例如,如果您愿意,可以稍微调整脚本以通过gmail帐户发送警报电子邮件。 – corford 2012-05-27 01:19:38