bash计时器的实现案例:检查并记录网络稳定性的脚本

简介:

bash的帮助中提到SECONDS系统变量:

SECONDS Each  time this parameter is referenced, the number of seconds since shell invocation is returned.  If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned.  If SECONDS is unset, it loses its special properties, even if it is subsequently reset.


我们在程序执行期间,检查这个变量,即可实现记时的功能。


本例是用来每隔一个固定时间,ping一个IP,出现故障时记录下来的脚本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash 
#set -o errexit
 
export  SCRIPT=$0
SID=$PPID
usage(){
     echo  -e  "Usage:"
     echo  -e  "\tbash $0 [OPTs]"
     echo  -e  "\t-W|--timeout\t\t<M>"
     echo  -e  "\t-I|--interval\t\t<N>"
     echo  -e  "\t-i|--ip\t\t\t<A.B.C.D>"
     echo  -e  "\t-L|--level\t\t<info|error>"
     echo  -e  "\t-l|--log\t\t</tmp/${SCRIPT}-${SID}.log>"
     echo  -e  "\t-h\t\t\tPrint this info"
     echo  -e  "Example:"
     echo  -e  "\tbash $0 -W 1 -I 3 -i 127.0.0.1 -L error -log /tmp/${SCRIPT}-${SID}.log"
     echo  -e  "\tbash $0"
     exit  255
}
 
[ $ # -eq 4 ] && usage
# 默认参数
LOGFILE= "/tmp/${SCRIPT}-${SID}.log"
touch  $LOGFILE
TIMEOUT=1
INTERVAL=10
LEVEL=info
IP=127.0.0.1
MyIP=$(ip addr sh eth0 | awk  '/^[\ ]*inet /{split($2,IP,"/");printf IP[1]}' )
# 参数获取
TEMP=$(getopt -o W:I:i:L:l:h --long timeout:,interval:,ip:,level:,log:,help --  "$@" )
[ $? != 0 ] && usage
eval  set  --  "$TEMP" 
 
 
# 参数处理
while  true do
     case  "$1"  in
         -W|--timeout) TIMEOUT=$2;  shift  2;;
         -I|--interval) INTERVAL=$2;  shift  2;;
         -i|--ip) IP=$2;  shift  2;;
         -L|--level) LEVEL=$2;  shift  2;;
         -l|--log) LOGFILE=$2;  shift  2;;
         -h|--help) usage;  shift ;;
         --)  shift break  ;;
     esac
done
 
 
"$IP"  ""  ] && usage
adjust(){
     return  $((SECONDS%INTERVAL))
}
 
pinger(){
     ping  $1 -W $TIMEOUT -c 1 |  tail  -1 | awk  -F/  '{print $(NF-1)}'  2> /dev/null  
}  
 
logger(){
     echo  -e  "$(date +%F"  "%T)\t" "$@"   
}
 
trapper(){
     trap  "logger \"EXIT($SID)\t$MyIP -> $IP\" >> $LOGFILE && exit 0"   1 2 9 15 
}
 
trapper
logger  "START($SID)\t$MyIP -> $IP"  >> $LOGFILE 
while  true
do
     if  adjust;  then
         unset 
         t=$(pinger $IP)
         if  "$t"  ""  ];  then
             logger  "ERROR($SID)\t$MyIP -> $IP ${TIMEOUT}(S)"  >> $LOGFILE &
         else
             "$LEVEL"  "erorr"  ] || logger  "INFO($SID)\t$MyIP -> $IP ${t}(ms)"  >> $LOGFILE &
         fi
         sleep  1
     fi
     sleep  0.1
done

执行

bash pinger.sh -W 1 -I 2 -i a.b.c &

执行结果如下:

# tail -f -n 10 /tmp/bash-26729.log 

2015-04-28 17:24:50 START(26729)192.168.202.2 -> a.b.c

2015-04-28 17:24:50 INFO(26729)192.168.202.2 -> a.b.c 0.630(ms)

2015-04-28 17:24:52 INFO(26729)192.168.202.2 -> a.b.c 0.575(ms)

2015-04-28 17:24:54 INFO(26729)192.168.202.2 -> a.b.c 0.875(ms)






     本文转自xiaoyuwang 51CTO博客,原文链接:http://blog.51cto.com/wangxiaoyu/1639861,如需转载请自行联系原作者

相关文章
|
5月前
|
网络协议 Python
154 python网络编程 - TCP案例(模拟QQ聊天)
154 python网络编程 - TCP案例(模拟QQ聊天)
38 0
|
5月前
|
网络协议 Python
149 python网络编程 - TFTP下载案例
149 python网络编程 - TFTP下载案例
27 0
|
5月前
|
Python
147 python网络编程 - UDP案例
147 python网络编程 - UDP案例
28 0
|
5天前
|
缓存 安全 网络协议
代理ip会不会影响网络速度和稳定性
代理ip会不会影响网络速度和稳定性
|
7天前
|
数据建模
R语言网络分析友谊悖论案例
R语言网络分析友谊悖论案例
R语言网络分析友谊悖论案例
|
1月前
|
存储 网络协议 Go
Golang网络聊天室案例
Golang网络聊天室案例
31 2
Golang网络聊天室案例
|
1月前
|
运维 监控 网络虚拟化
|
1月前
|
机器学习/深度学习 Python
神经网络回归案例(python
神经网络回归案例(python
25 0
|
2月前
|
Shell
|
2月前
|
网络协议 应用服务中间件 数据安全/隐私保护
网络协议的初步认识-以详细案例分析
网络协议的初步认识-以详细案例分析
18 0

热门文章

最新文章