#!/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-154041_0100_Filesystem_checker_mini_fscheck_for_OpenWrt/ # mail configuration SERVER=mailserver.yourdomain SENDER=root@yourdevice.yourdomain RECIPIENT=you@yourdomain # general paths DATABASE=/tmp/fscheckdb DATABASEMD5=${DATABASE}md5 # temporary files for this run TMPDATABASE=${DATABASE}_`hexdump -n4 -e\"%x\" /dev/urandom` TMPDATABASEMD5=${DATABASEMD5}_`hexdump -n4 -e\"%x\" /dev/urandom` TMPMESSAGE=/tmp/fscheckmessage_`hexdump -n4 -e\"%x\" /dev/urandom` # the find command that makes it all happen find / -type f \ ! -path /proc/\* \ ! -path /sys/\* \ ! -path /tmp/\* \ -exec ls -ldei {} \; >$TMPDATABASE find / -type f \ ! -path /proc/\* \ ! -path /sys/\* \ ! -path /tmp/\* \ -exec md5sum {} \; >$TMPDATABASEMD5 # check against prevous if it exists touch $TMPMESSAGE chmod og-rwx $TMPMESSAGE echo "From: $SENDER" >$TMPMESSAGE echo "To: $RECIPIENT" >>$TMPMESSAGE echo "Subject: fscheck Report from OpenWrt Device" >>$TMPMESSAGE echo "Date: `date -R`" >>$TMPMESSAGE echo >>$TMPMESSAGE if [ -f $DATABASE ]; then echo "Filesystem changed generated by $0:" >>$TMPMESSAGE diff -U 0 $DATABASE $TMPDATABASE >>$TMPMESSAGE diff0=$? echo >>$TMPMESSAGE diff -U 0 $DATABASEMD5 $TMPDATABASEMD5 >>$TMPMESSAGE diff1=$? if [ $diff0 -ne 0 -o $diff1 -ne 0 ]; then # changes - send message mini_sendmail -s${SERVER} -f${SENDER} ${RECIPIENT} <$TMPMESSAGE # put new database in place mv $TMPDATABASE $DATABASE mv $TMPDATABASEMD5 $DATABASEMD5 fi # clean up temporary files if [ -f $TMPDATABASE ]; then rm $TMPDATABASE; fi if [ -f $TMPDATABASEMD5 ]; then rm $TMPDATABASEMD5; fi else echo "First run - generating databases" >>$TMPMESSAGE echo >>$TMPMESSAGE mini_sendmail -s${SERVER} -f${SENDER} ${RECIPIENT} <$TMPMESSAGE # first run - put the database in place mv $TMPDATABASE $DATABASE mv $TMPDATABASEMD5 $DATABASEMD5 fi # remove the temporary message rm $TMPMESSAGE