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

因为一个程序定期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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值