LoginSignup
10
10

More than 5 years have passed since last update.

ログを定期監視してメール

Posted at

大量に出力されても、cronで指定したタイミングでしか送信しないので安心。

使い方

crontab
* * * * * /bin/sh /root/watch.sh /var/log/messages 【通知先メールアドレス】>> /tmp/watch.log

スクリプト

watch.sh
#!/bin/sh
TARGET=$1
MAILTO=$2

BASE="$(basename $0)"
TFILE="/tmp/$BASE.tail"
PFILE="/tmp/$BASE.prev"
DFILE="/tmp/$BASE.diff"
LFILE="/tmp/$BASE.log"

tail $TARGET > $TFILE
if (diff $PFILE $TFILE | grep '^>' > $DFILE); then
   SUBJECT="`hostname -s` watch ${TARGET}"
   cat $DFILE | mail -s "$SUBJECT" $MAILTO
else
   date >> $LFILE
fi

mv $TFILE $PFILE

補足

いちいち報告しなくて良い項目は、diff -X で指定する除外ファイルを作ってやると良し

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10