|
要求不高,定期ping网络远端,如果不通就记入日志,日志要求:1、格式:起始故障时间 终止故障时间 对端IP
2、如果相临两次ping都失败,则相应日志条目合并。
请诸位兄台指点。
自己曾试写,无奈是新手,问题多多,现将拙作贴出,并问:如何合并日志?
let timeslice=60 ##the time between two check routine
LOG_FILE=/var/log/wan.log
chkwan()
{
i=11
while [ $i -lt 121 ]
do
hping2 -S 1.1.$i.1 -c 1 &>/dev/null
if [ "$?" -ne 0 ]
then
let timestamp=$(date +%s)
timestamp_easy_to_read=$(date)
let timestamp_last=$(tac $LOG_FILE|awk '/1.1.'$i'.1/ && NR == 1{print $3}')
timestamp_easy_to_read_last=$(tac $LOG_FILE|awk '/10.16.'$i'.5/ && NR == 1{print $4" "$5" "$6" "$7" "$8" "$9}')
if [ -z timestamp_last ]
then
echo "$timestamp' --> '$timestamp $timestamp_easy_to_read 'to ' $timestamp_easy_to_read 'site 1.1.'$i'.1 can'\''t be reached'" >>$LOG_FILE
break
fi
let time_interval=$timestamp-$timestamp_last
echo time_interval=$time_interval
if (( $time_interval < $timeslice*2 ))
then
tac $LOG_FILE |awk '/1.1.'$i'.1/ && NR ==1 ##此处如何用awk将日志中的第二个时间戳换为新戳??
tac $LOG_FILE |awk '/1.1.'$i'.1/ && NR ==1 ##此处如何用awk 或sed将日志中第四个时间戳换为新戳??
else
echo $timestamp' --> '$timestamp $timestamp_easy_to_read 'to ' $timestamp_easy_to_read 'site 1.1.'$i'.1 can'\''t be reached' >>$LOG_FILE
break
fi
fi
let "i+=10"
done
}
## start of the main function ##
while true
do
chkwan
sleep $timeslice
done |
|