TOP ▲ itcore TOPTIPSsyslog.php

syslogチェックシェル | itcore 2018年

mkdir -p /root/cron
vi /root/cron/logcheck.sh
#/bin/sh
#----------------
# メールタイトル
#----------------
to='xxx@xxx.jp'
pgm="logcheck"
host=`hostname`
date=`date "+%Y-%m-%d %H:%M:%S"`
subject="[$host-$pgm]$date"
#echo subject=$subject
#----------------
# 検索キーワード
#----------------
keyword="error"
# test
#keyword="CMD"
log=/var/log/syslog
tmp=/root/cron/$pgm.tmp
grep -i "$keyword" $log > $tmp 2>&1
if [ -s $tmp ]; then
  echo "find!"
  cp -ip $log ${log}_`date "+%Y%m%d_%H%M%S"`
  cp /dev/null $log
  echo -e "[$log]keyword=$keyword\n`cat $tmp`" | mail -s "$subject" $to
fi

chmod +x /root/cron/logcheck.sh
crontab -e
2 * * * * /root/cron/logcheck.sh > /root/cron/logcheck.log 2>&1