#!/bin/sh # Copyright (C) 2011 Glen Pitt-Pladdy # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # # See: http://www.pitt-pladdy.com/blog/_20110501-112534_0100_Log_checker_mini_logcheck_for_OpenWrt/ # mail configuration SERVER=mail.yourdomain SENDER=root@yourrouter.yourdomain RECIPIENT=you@yourdomain # general paths LOG=/var/log/messages LOGPOS=$LOG.pos RULES=/etc/logcheck.ignore.d # temporary files TMPTAIL=/tmp/tmplog_`hexdump -n 8 -e \"%x\" /dev/urandom` TMPRULES=/tmp/tmprules_`hexdump -n 8 -e \"%x\" /dev/urandom` TMPMESSAGE=/tmp/tmpmessage_`hexdump -n 8 -e \"%x\" /dev/urandom` # read in existing position if available position=1 inode=`ls -i $LOG | cut -d' ' -f1` if [ -f $LOGPOS ]; then position=`cat $LOGPOS | cut -d' ' -f1` inode=`cat $LOGPOS | cut -d' ' -f2` fi echo $position $inode # get the unread part of the file touch $TMPTAIL chmod og-rwx $TMPTAIL if [ -f $LOG.0 -a `ls -i $LOG | cut -d' ' -f1` -ne $inode ]; then # looks like logs got rotated (inode changed) TMPOLD=/tmp/tmplog_`hexdump -n 8 -e \"%x\" /dev/urandom` touch $TMPOLD chmod og-rwx $TMPOLD echo "------- log rotation : old log" >$TMPOLD # re-start with old file tail -n +$position $LOG.0 >>$TMPOLD # then do the old file position=0 tail -n +$position $LOG >$TMPTAIL newposition=$(($position+`wc -l <$TMPTAIL`)) # put them together echo "------- log rotation : new log" >>$TMPOLD cat $TMPTAIL >>$TMPOLD mv $TMPOLD $TMPTAIL else tail -n +$position $LOG >$TMPTAIL newposition=$(($position+`wc -l <$TMPTAIL`)) fi # get the rules together touch $TMPRULES chmod og-rwx $TMPRULES grep -hv -e ^# $RULES/* -e ^$ >$TMPRULES # generate message touch $TMPMESSAGE chmod og-rwx $TMPMESSAGE echo "From: $SENDER" >$TMPMESSAGE echo "To: $RECIPIENT" >>$TMPMESSAGE echo "Subject: Logcheck Report from OpenWrt Device" >>$TMPMESSAGE echo "Date: `date -R`" >>$TMPMESSAGE echo >>$TMPMESSAGE echo "Logs from $LOG filtered by $RULES/*:" >>$TMPMESSAGE echo >>$TMPMESSAGE linesbefore=`wc -l <$TMPMESSAGE` if [ `wc -l <$TMPRULES` -gt 0 ]; then # grep stalls with empty rules file grep -vEf $TMPRULES $TMPTAIL >>$TMPMESSAGE else # no rules so take everything cat $TMPTAIL >>$TMPMESSAGE fi newlines=$((`wc -l <$TMPMESSAGE`-$linesbefore)) # did we find something if [ $newlines -gt 0 ]; then echo $? # send the message mini_sendmail -s${SERVER} -f${SENDER} ${RECIPIENT} <$TMPMESSAGE fi # tidy up rm $TMPTAIL rm $TMPRULES rm $TMPMESSAGE # write the new position and inode echo $newposition `ls -i $LOG | cut -d' ' -f1` >$LOGPOS