NetSNMP の traptoemail で Date ヘッダを追加する

NetSNMP の snmptrapd で traptoemail を使うと簡単にトラップをメール送信することが出来ますが、Date ヘッダが付きません。

Date ヘッダが無い場合は MTA が勝手に付けたりするらしいですが、qmail-1.03 は Date ヘッダを付けないため、送信日時(トラップの発生日時)が判らなくて不便でした。


そこで、traptoemail を弄って Date ヘッダを付けるようにしてみました。traptoemail は perl のスクリプトなので、簡単に弄ることが出来ます。

必要な perl モジュールをインストールする

CentOS5 で epel リポジトリからインストールしました。

yum --enablerepo=epel list | grep perl-DateTime
yum --enablerepo=epel install perl-DateTime perl-DateTime-Format-Mail
perl -MDateTime -e ''
perl -MDateTime::Format::Mail -e ''

traptoemail を弄る

CentOS5 なら「/usr/bin/traptoemail」にあるので、次の様に追記します。

# use Getopt::Std; の下辺りに追記
use DateTime;
use DateTime::Format::Mail;

# $message->datasend("Subject: 〜〜"); の下辺りに追記
$date = DateTime::Format::Mail->format_datetime( DateTime->now() );
$message->datasend("Date: $date\n");

diff だと以下の様な感じです。

--- /usr/bin/traptoemail        2012-05-21 17:59:59.000000000 +0900
+++ traptoemail 2012-06-20 18:15:33.000000000 +0900
@@ -11,6 +11,8 @@
 
 use Net::SMTP;
 use Getopt::Std;
+use DateTime;
+use DateTime::Format::Mail;
 
 $opts{'s'} = "localhost";
 $opts{'f'} = 'root@' . `hostname`;
@@ -63,6 +65,10 @@
 $message->datasend("To: " . join(", ",@ARGV) . "\n");
 $message->datasend("From: $opts{f}\n");
 $message->datasend("Subject: trap received from $hostname: $values[1]\n");
+
+$date = DateTime::Format::Mail->format_datetime( DateTime->now() );
+$message->datasend("Date: $date\n");
+
 $message->datasend("\n");
 $message->datasend("Host: $hostname ($ipaddress)\n");
 for($i = 0; $i <= $#oids; $i++) {