該腳本的應用場景是數據分析,一般作線上日志分析的人員需要觀察線上數據的變化,如:
1.訪問數據庫的響應大於1000毫秒的sql
2.apache cookielog響應大於多少毫秒的數據
數據格式:
[html]
- 2010-12-14 00:01:26,427 FATAL FUNCTION_TIME - wmmad.alloffer.get 15511ms
- 2010-12-14 00:01:33,164 FATAL FUNCTION_TIME - wmmad.alloffer.get 14213ms
- 2010-12-14 00:02:31,021 FATAL FUNCTION_TIME - wmmad.alloffer.get 14126ms
- 2010-12-14 00:05:08,160 FATAL FUNCTION_TIME - wmmad.alloffer.get 15295ms
- 2010-12-14 00:24:00,372 ERROR FUNCTION_TIME - wmmad.offer.repost 406ms
腳本:
[plain]
- #/bin/bash
-
- # author: madding.lip
- # date 2010.12.14
- # 統計超n毫秒的數據量
-
- ERROR_USAGE=1
-
- if [ $# != 2 ]; then
- echo "Usage: $0 file times";
- exit $ERROR_USAGE
- fi
-
- myfile=$1
- mytime=$2
-
-
- all=0
- count=0;
-
- data=`cat $myfile | awk '{print $7}' | sed 's/ms//g'`;
-
- for i in $data ;
- do
- all=$(( $all + 1 ));
- if test $(( $mytime < $i )) -eq 1 ;then
- # echo $i; sleep 1;
- count=$(( $count + 1 ));
- fi
- done;
-
- echo "response time over ${mytime}ms: "$count" times"
- echo "all count: "$all" times"
具體根據數據格式作調整即可。