sma でsendmailの統計情報を取得する

■対象
 kfreebsd-squeeze amd64版sendmail
 設定はdebian-lennya amd64版と同じなので割愛。

 「sharutils」でuuencodeを使って添付ファイルとして送る方法も試したが、
 textのままで読みたかったので、以前作った、telnetmail.sh を使用した。

■「netstat」の結果は、kfreebsdの場合、「:」ではなく「.」で区切られる。

$ sudo netstat -a | grep .smtp
tcp4 0 0 localhost.39129 localhost.smtp TIME_WAIT
tcp4 0 0 kfreesqueeze.bsd.smtp kfreesqueeze.bsd.25878 TIME_WAIT
tcp4 0 0 *.smtp *.* LISTEN

■smaを導入

$ apt-cache search ^sma\$
sma - Sendmail log analyser

$ sudo apt-get install sma

■オプションが多いので。filesに関連したところだけ。
 デフォルトの設定を使い(オプション指定なし)、「log files」を指定します。
 HTML形式もありますが、どうしても必要な場合に限った方が良いです。

$ sma -h | grep -i file
usage: sma [OPTIONS] [files...]
-f use configuration from instead of default
-F do not use default configuration file even if it exists
-o write output to 
files log files

■動作するか確認

$ sudo /usr/bin/sma /var/log/mail.log 2> /dev/null | head -3

Sendmail Log Analysis Report
Generated at Mon Oct 10 00:15:18 2011 by SMA, version 1.4

■crontabに設定
 「-l」は表示。「-e」に変更して修正する。
 中身は一応、フルバス指定。
 ※メールを出すので、レポートは追記ではなく上書き。
 ※「 > /dev/null 2>&1」はお好みで。

$ su root -c 'env EDITOR=vim crontab -l' | grep -v ^#
SHELL=/bin/bash
PATH=/sbin:/usr/sbin:/usr/bin:/bin
MAILTO=labunix@kfreesqueeze.bsd.vm
HOME=/root
0 * * * * /usr/bin/sma /var/log/mail.log > /var/log/sma_report.log 2> /var/log/sma_report.err
5 * * * * /bin/sh /home/labunix/sma_report.sh > /dev/null 2>&1

■「sma_report.sh」の中身
 kfreebsdのsendmailの場合、「EHLO」コマンドの前に「sleep 2」する必要があった。
 postfixでのpflogsumm_reportのようなログメッセージがメールで送られる。

$ cat sma_report.sh
#!/bin/sh -x

TDATE=`date '+%Y/%m/%d %H:%M'`
TFQDN=`hostname -f`
TUSER=`whoami`
PORT=25
TPASS=$1
THOST=`hostname -s`
TDATA=`cat /var/log/sma_report.log`

(sleep 2; echo "EHLO ${THOST}";
sleep 1; echo "MAIL FROM: ${TUSER}@${TFQDN}";
sleep 1; echo "RCPT TO: ${TUSER}@${TFQDN}";
sleep 1; echo "DATA"
sleep 1; echo "From: MAIL ${TUSER} <${TUSER}@${TFQDN}>"
echo "Subject: mail from ${TUSER}"
echo "To: MAIL <${TUSER}@${TFQDN}>"
echo "${TDATE}"
echo "${TDATA}"
echo "."
sleep 1; echo "QUIT"
sleep 1; echo; ) | telnet $TFQDN $PORT

■上記のtelnetメールで届くのが、以下のようなレポートです。
 外に出さないネットワークのサーバの仮想マシンのvmnet8内なので。。。
 ドメインは、「example.com」か、「example.jp」の方が良いです。

2011/10/10 00:26

Sendmail Log Analysis Report
Generated at Mon Oct 10 00:00:01 2011 by SMA, version 1.4
-----------------------------------------------------------------------------


Server: kfreesqueeze

General information
-----------------------------------------------------------------------------
First log entry Sun Oct 9 20:12:07 2011
Last log entry Sun Oct 9 23:59:01 2011
Alias table rebuilds 5
Too many hops 0
Mail loops 0
Other SYSERR 62
Ruleset based rejections 12
Sendmail daemon restarts 13


Inbound messages Outbound messages
-----------------------------------------------------------------------------
Total 117 Total 123
Average size (kB) 1.08 Sent 113
Messages/hour 30.94 Deferred 0
Messages/min 0.52 Queued 0
Messages/sec 0.01 Other error 10

Top envelope senders
-----------------------------------------------------------------------------
Nr Sender Msgs MB %
-----------------------------------------------------------------------------
1 root@kfreesqueeze.bsd.vm 76 0.10 64.96
2 root 26 0.01 22.22
3 labunix@kfreesqueeze.bsd.vm 5 0.00 4.27
4 <> 4 0.01 3.42
5 MAILER-DAEMON 3 0.00 2.56
6 labunix 3 0.00 2.56

Top envelope recipients
-----------------------------------------------------------------------------
Nr Recipient Msgs MB %
-----------------------------------------------------------------------------
1 labunix@kfreesqueeze.bsd.vm 100 0.11 81.30
2 labunix 7 0.01 5.69

3 s 5 0.00 4.07
4 root 3 0.00 2.44
5 report 2 0.00 1.63
6 mail 2 0.00 1.63
7 sendmail 2 0.00 1.63
8 new 1 0.00 0.81
9 mail@kfreesqueeze.bsd.vm 1 0.00 0.81

Top relay addresses, sender
-----------------------------------------------------------------------------
Nr Relay Msgs MB %
-----------------------------------------------------------------------------
1 localhost 113 0.13 96.58
2 kfreesqueeze.bsd.vm 4 0.00 3.42

Top relay addresses, recipient
-----------------------------------------------------------------------------
Nr Relay Msgs MB %
-----------------------------------------------------------------------------
1 [127.0.0.1] 65 0.05 52.85
2 localhost 58 0.07 47.15


Inbound messages per day Outbound messages per day
-----------------------------------------------------------------------------
Day Total Average Day Total Average

-----------------------------------------------------------------------------
Sunday 117 117.00 Sunday 123 123.00


Inbound messages per hour Outbound messages per hour
-----------------------------------------------------------------------------
Hour Total Average Hour Total Average
-----------------------------------------------------------------------------
21-22 3 3.00 21-22 4 4.00
22-23 1 1.00 22-23 1 1.00
23-00 113 113.00 23-00 118 118.00


-----------------------------------------------------------------------------
Copyright (c) 2000 - 2003 Jarkko Turkulainen. All rights reserved.

■おまけ

☆以下の2箇所を様々な宛て先に重ねると1通のメールが何通分にもなります。

 echo "RCPT TO: ${TUSER}@${TFQDN}";
echo "To: MAIL <${TUSER}@${TFQDN}>"

☆「.forward」を使って一覧に転送する方法もあります。
 自身のアドレスは、ピンポン防止の為、バックスラッシュでエスケープします。
「Fw:」は付きませんがToとも違うので、メールクライアントや状況に応じて使い分けて下さい。

$ cat .forward
\labunix
root

☆ユーザの追加方法
 ご存知だとは思いますが。。。

$ echo "user" >> ~/.forward
$ echo "user@domain" >> ~/.forward

それではまた。