对 Centos7 重要系统文件定时校验 md5 码,并发信告警

写在前面:

对于生产环境系统来说,系统文件被动了手脚之后,排查起来很花精力,我们今天来看其中的一个小方法:对系统重要目录文件,定期计算校验 MD5 码,初步了解文件变化情况,并可把校验结果以 E-mail 的方式,提交给指定邮箱!

对 Centos7 重要系统文件定时校验 md5 码,并发信告警

一,导出重要文件列表:

  • # ls /etc/{passwd,shadow,group} > important.file
  • # find /usr/sbin /usr/bin -perm /6000 >>important.file

二,计算初始 MD5值:

  • # vim md5.sh
#!/bin/bash
for filename in $(cat important.file)
do
md5sum $filename >> finger1.file
done
  • # sh md5.sh
  • # 输出 finger1.file
  • # chattr +i finger1.file
  • finger1.file 和 finger_new.file 进行比较,决定是否发送邮件!
  • # vim md5.checkfile.sh
[ -f finger_new.file ] && rm finger_new.file
for filename in $(cat important.file)
do
md5sum $filename >> finger_new.file
done

testing=$(diff finger1.file finger_new.file)

if [ "$testing" != "" ] ; then
diff finger1.file finger_new.file | mail -s 'finger trouble.' [email protected]
fi

if [ "$testing" = "" ] ; then
diff finger1.file finger_new.file | mail -s 'Well done! -ALL are OK!' [email protected]
fi

四,运行测试,邮件到达:

  • # sh ./md5.checkfile.sh
  • Null message body; hope that's ok
对 Centos7 重要系统文件定时校验 md5 码,并发信告警

对 Centos7 重要系统文件定时校验 md5 码,并发信告警

五,设置定时执行,例如N小时执行一次!

  • # vim /etc/crontab
  • 0 */6 * * * cd /; sh md5.checkfile.sh


分享到:


相關文章: