2008-10-22 92 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