您的位置:首页 > 运维架构 > Shell

shell 判断文件更新是否超过阀值的报警小程序

2014-01-22 15:22 330 查看
因为一个程序定期crontab运行来更新文件,有一天这个程序未运行从而对应的文件未更新,发现时已晚了一小时,

所以针对这个情况简单的写了下对应的shell监控小程序;无非就是读文件修改时间然后和当前时间比较,当超过阀值threshhold时进行短信或邮件报警

时间差distance上可以用t2-t1保证结果大于0,这里用了一个自定义abs函数得到distance结果然后判断是否触发报警

内容如下示

cat monitor_fileupdate.sh

#!/bin/sh

_mobile="18210586395|18210588888"

_msg=file_unupdated

_threshhold=86400

_filename=$1

_ftime=$(stat ${_filename} | awk '{print $2" "substr($3,1,8)}' | tail -2 | head -1)

_today=$(date +"%F %H:%I:%S")

_t1=$(date +%s -d"${_ftime}")

_t2=$(date +%s -d"${_today}")

_distance=$((_t1 - _t2))

abs () { echo ${1#-}; }

_distance=$(abs ${_distance})

if [ ${_distance} -gt ${_threshhold} ]

then

        curl "http://alarms.ops.qihoo.net:8360/intfs/sms_intf?mobiles="${_mobile}"&msg="${_msg}

        #or send mail

fi

echo $_ftime

echo $_today

echo $_t1

echo $_t2

echo $_distance
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息