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
所以针对这个情况简单的写了下对应的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
相关文章推荐
- shell中判断文件大小是否超过指定大小
- Shell脚本执行hive语句 | hive以日期建立分区表 | linux schedule程序 | sed替换文件字符串 | shell判断hdfs文件目录是否存在
- shell怎么判断两个文件内容是否相同
- shell或bash中的 判断文件是否存在的脚本语法
- shell判断文件,目录是否存在或者具有权限
- shell判断文件是否存在
- 批处理bat判断一个文件在最近5分钟内是否被更新过的代码
- shell bash判断文件或文件夹是否存在
- shell判断hdfs文件目录是否存在
- 判断文件是否存在的shell脚本代码
- Shell之判断程序是否运行-yellowcong
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- ios判断程序文件夹下是否存在指定文件
- 使用shell判断路径或文件是否存在
- shell判断文件是否为空的代码
- shell判断文件,目录是否存在或者具有权限
- shell bash判断文件或文件夹是否存在
- 判断文件是否存在的shell脚本代码
- linux下c调用shell命令,以及判断文件是否存在