扫描定义的数组目录的所有文件的 MD5 值,然后在用下面个脚本对比
[root@localhost scripts]# cat md5sum_check.sh
#!/bin/sh
path=(
/deploy/factory/tomcat/webapps
/deploy/passport/tomcat/webapps
/deploy/uniring/tomcat/webapps
/deploy/oms/tomcat/webapps
/deploy/rad/tomcat/webapps
/deploy/dubbo/tomcat/webapps
/deploy/nginx
)
[ -d /md5 ] || mkdir /md5 -p
md5_log=/md5/md5_$(date +%F).log
num_log=/md5/num_$(date +%F).log
LANG=en
for n in ${path[*]}
do
if [ ! -z $n ]; then
find ${path[*]} -type f |xargs md5sum > $md5_log
find ${path[*]} -type f > $num_log
else
exit 1
fi
done
下面这个脚本放到定时任务每天晚上检测如果有异常就邮件报警
[root@localhost scripts]# cat md5_detection.sh
#!/bin/sh
path=(
/deploy/factory/tomcat/webapps
/deploy/passport/tomcat/webapps
/deploy/uniring/tomcat/webapps
/deploy/oms/tomcat/webapps
/deploy/rad/tomcat/webapps
/deploy/dubbo/tomcat/webapps
/deploy/nginx
)
[ -d /md5 ] || mkdir -p /md5
md5_log=/md5/md5_$(date +%F).log
num_log=/md5/num_$(date +%F).log
num=$(cat $num_log |wc -l )
LANG=EN
while true
do
log =/md5/check.log
[ ! -f $log ] && touch $log
md5_count=$(md5sum -c $md5_log 2>/dev/null |grep FAILED|wc -l )
md5_FAILED=$(md5sum -c $md5_log 2>/dev/null |grep FAILED)
echo $md5_FAILED >> $log
for n in ${path[*]}
do
if [ ! -z $n ]; then
num_count=$(find ${path[*]} -type f |wc -l )
find ${path[*]} -type f >/md5/new.log
fi
done
if [ $md5_count -ne 0 ] || [ $num_count -ne $num ]; then
#if [ $md5_count -ne 0 ];then
diff $num_log /md5/new.log >> $log
mail -s "web site is misrepresented in $(date +%F\ %T)" QQ 邮箱 157@qq.com < $log
exit 0
else
exit 3
fi
done