LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1066|回复: 1

如何编写如下要求的网络监控小脚本?

[复制链接]
发表于 2004-9-10 16:39:00 | 显示全部楼层 |阅读模式
要求不高,定期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
 楼主| 发表于 2004-9-14 16:03:21 | 显示全部楼层

又改写了一个,请大家指正。

#!/bin/bash
let timeslice=600 ##the time between two check routine
LOG_FILE=/var/log/wan.log
initarray()
{
iplists="11 21 31 41 51 63 80 81 91 102 111"
for i in `echo $iplists`
do
link_status[$i]=1
done
}
chkwan()
{
iplists="11 21 31 41 51 61 71 81 91 101 111"
for i in `echo $iplists`
    do
           ping 10.16.$i.254 -c 1 &>/dev/null
        link_status_new=$?
    if [ $link_status_new -ne ${link_status[$i]} ]
    then
        let link_status[$i]=$link_status_new
        if [ ${link_status[$i]} -eq 1 ]
        then
                echo $(date +%s)' ---> ' $(date)' net 1.1.'$i'.0 can'\''t be reached now!' >>$LOG_FILE
        else
                echo $(date +%s)' ---> ' $(date)' net 1.1.'$i'.0 can be reached now!' >>$LOG_FILE
        fi
    fi
done
    }
## start of the main function  ##
initarray
while true
do
  chkwan
  sleep $timeslice
done
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表