#!/bin/bash
# log file name
filename="filename"
# path
path="/home/logs/filename"
# 当天日期,年月日
cur_date=`date +"%Y-%m-%d"`
# 告警时间
time=`date +"%Y-%m-%d %H:%M:S"`
# log file
file=${path}"/"${filename}"_"${cur_date}".log"
# offset file
offset='/home/monitor/OffSet.txt'
# 上次最后一行
old_line_num=`cat ${offset}`
# 本次最后一行
new_line_num=`cat $file | wc -l`
# 覆盖offset文件
echo $new_line_num > $offset
error=$(sed -n "$old_line_num,${new_line_num}p" $file | grep -i 'error' )
exception=$(sed -n "$old_line_num,${new_line_num}p" $file | grep -i 'exception')
msg=${error}${exception}
if test -z "${msg}"
then
code=0
msg1="${cur_date}_日志正常"
else
code=1
msg1="${cur_date}_日志异常 ${msg}"
fi
echo "{\"value\" : $code, \"info\" : \"$msg1\"}"
exit 0
日志文件监控告警脚本
最新推荐文章于 2024-04-27 00:26:59 发布