2010-01-29 83 views
0

的区别我有一个文本文件,并在最后2行看起来是这样......计算从平面文件

Uptime: 822832 Threads: 32 Questions: 13591705 Slow queries: 722 Opens: 81551 Flush tables: 59 Open tables: 64 Queries per second avg: 16.518 
Uptime: 822893 Threads: 31 Questions: 13592768 Slow queries: 732 Opens: 81551 Flush tables: 59 Open tables: 64 Queries per second avg: 16.618 

如何找到每个参数的两个值之间的差异? 预期的输出是:

61 -1 1063 10 0 0 0 0.1 

换句话说,我会想从早期的正常运行时间扣除当前运行时间值。 找到线程和问题等的区别。

本练习的目的是观察此文件并在差异过高时提醒用户。对于例如如果慢查询超过500或“问题”参数过低(< 100)

(这是MySQL的状态,但有没有关系,所以MySQL的标签不适用)

回答

0

这是一种方法。 tail用于获取最后2行,如果你有一个大文件,在效率方面尤其有用。

tail -2 file | awk ' 
{ 
gsub(/[a-zA-Z: ]+/," ") 
m=split($0,a," ") 
if (f) { 
    for (i=1;i<=m;i++){ 
     print -(b[i]-a[i]) 
    } 
    # to check for Questions, slow queries etc 
    if ( -(b[3]-a[3]) < 100){ 
     print "Questions parameter too low" 
    }else if (-(b[4]-a[4]) > 500){ 
     print "Slow queries more than 500" 
    }else if (a[1] - b[1] < 0){ 
     print "mysql ...... " 
    } 
    exit 
} 
for(i=1;i<=m;i++){ b[i]=a[i] ;f=1 } 
} ' 

输出

$ ./shell.sh 
61 
-1 
1063 
10 
0 
0 
0 
0.1 
+0

我会想增加一个,如果/其他。如果正常运行时间小于先前的时间,则显示消息“mysql service restarted”。我不能在if子句中使用“curl”。 – shantanuo 2010-01-29 11:00:25

+0

如果正常运行时间小于前一次,则该值将小于0.请参阅更新后的代码。 (注意:下次试试吧)。如有疑问,请阅读awk文档。 – ghostdog74 2010-01-29 11:25:01

+0

我设法使用Ramashalanka建议的方法获取值。谢谢大家。 – shantanuo 2010-01-29 12:21:51

0

GAWK:

BEGIN { 
    arr[1] = "0" 
} 
length(arr) > 1 { 
    print $2-arr[1], $4-arr[2], $6-arr[3], $9-arr[4], $11-arr[5], $14-arr[6], $17-arr[7], $22-arr[8] 
} 
{ 
    arr[1] = $2 
    arr[2] = $4 
    arr[3] = $6 
    arr[4] = $9 
    arr[5] = $11 
    arr[6] = $14 
    arr[7] = $17 
    arr[8] = $22 
} 
1

刚上ghostdog74的(原来的)答案略有变化:

tail -2 file | awk ' { 
    gsub(/[a-zA-Z: ]+/," ") 
    m=split($0,a," "); 
    for (i=1;i<=m;i++) 
    if (NR==1) b[i]=a[i]; else print a[i] - b[i] 
} '