【问题标题】:Log analysis script in shellshell中的日志分析脚本
【发布时间】:2013-05-14 23:35:06
【问题描述】:

我是脚本新手,需要您的帮助。我有一个日志文件,我已经清理干净了。看起来像这样(时间、持续时间(以毫秒为单位)、动作):

2012-04-28 00:00:00;277.406; 
2012-04-28 00:00:00;299.680; 
2012-04-28 00:00:00;282.338; 
2012-02-28 00:00:00;272.241;

我需要制作一个使用持续时间数据并计算动作的脚本。

【问题讨论】:

标签: shell


【解决方案1】:

首先 - 您需要更轻松地解析不同的字段。一种简单的方法是使用

将分号更改为空格
tr ";" " " <logfile|awkscript

其次,您需要创建一个低值和高值表。我正在使用一个关联数组,其索引是列的名称。我在 BEGIN 部​​分执行此操作。

当一个值在低值和高值之间时,您需要进行计数。我在中间部分执行此操作。

在 END 部分,我打印出值。我使用 2 个类似的 printf 格式字符串来确保标题和值排列整齐:

#!/usr/bin/awk -f
BEGIN {
    low["<1ms"]=0;high["<1ms"]=1
    low["1-10ms"]=1;high["1-10ms"]=10
    low["10-100ms"]=10;high["10-100ms"]=100
    low["100-500ms"]=100;high["100-500ms"]=500
    low[">500ms"]=500;high[">500ms"]=1000000000
}
{
# Middle section - for each line
duration=$3

for (i in high) {
    if ((duration > low[i]) && (duration <= high[i]) ) {
#       printf("duration: %d, low: %s,high: %s\n", duration, low[i], high[i]);
        total+=duration # total duration
        bin[i]++ # store a count into different bins
        count++ # total number of measurements
    }
}
}
END {
average=total/count
FMT="%-10s %10s %10s %10s %10s %10s\n"
NFMT="%-10.3f %10s %10s %10s %10s %10s\n"
printf(FMT,"AVG", "<1ms", "1-10ms", "10-100ms", "100-500ms", "500+ms")
printf(NFMT,average, bin["<1ms"], bin["1-10ms"], bin["10-100ms"], bin["100-500m\
s"], bin["500+ms"])

}

当我用你的数据运行它时,我得到

AVG              <1ms     1-10ms   10-100ms  100-500ms     500+ms

282.916                                              4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 2012-09-09
    • 2014-12-22
    • 1970-01-01
    • 2018-01-20
    • 2020-10-02
    • 2019-09-23
    相关资源
    最近更新 更多