0

需要帮助查找“WAM1”和“WAM2”最近连续启动之间的时间差(仅秒)/var/log/messages

WAM1 是在推出的2013-07-09T02:20:10.735639Z2013-07-09T02:20:12.635639Z所以现在 WAM2 的最新版本2013-07-09T02:20:12.635639Z如此不同(15.84675 - 12.635639)= 3.211111,结果以秒为单位

2013-07-09T02:20:10.735639Z user.info WbAppMg WAM1
2013-07-09T02:20:11.817372Z user.info sam SAM 
2013-07-09T02:20:12.635639Z user.info WbAppMg WAM1
2013-07-09T02:20:13.817372Z user.info sam SAM  
2013-07-09T02:20:14.818442Z user.info sam SAM  
2013-07-09T02:20:15.846751Z user.info WbAppMg WAM2
2013-07-09T02:20:16.846636Z user.info sam SAM 
2013-07-09T02:20:16.848751Z user.info WbAppMg WAM2 
2013-07-09T02:20:17.851727Z user.info sam SAM
2013-07-09T02:20:18.935639Z user.info WbAppMg WAM1
2013-07-09T02:20:19.535639Z user.info WbAppMg WAM1
2013-07-09T02:20:20.817372Z user.info sam SAM  
2013-07-09T02:20:21.818442Z user.info sam SAM  
2013-07-09T02:20:22.746751Z user.info WbAppMg WAM2
2013-07-09T02:20:23.846636Z user.info sam SAM  
2013-07-09T02:20:24.851727Z user.info sam SAM

下面的程序对上述日志消息给出了错误的结果

$ awk '
/WAM1/{ split($1,start,/[:Z]/); wam1[++i]=start[3] }
/WAM2/{ split($1,stop,/[:Z]/); wam2[++j]=stop[3] }
END   { len=i>j?i:j; for(;x<len;) 
print "Delta "++x" = "wam2[x]-wam1[x] }' /var/log/messages
4

1 回答 1

1

试试下面的代码:

awk '/WAM1/{ split($1,start,/[:Z]/); wam1=start[3] }
/WAM2/{ split($1,stop,/[:Z]/); wam2=stop[3]; print "Delta "++x" = "wam2-wam1}' 1

此代码产生:

Delta 1 = 3.21111
Delta 2 = 4.21311
Delta 3 = 3.21111

另一个代码:

awk '/WAM1/{ split($1,start,/[:Z]/); wam1=start[3]; i=1; }
/WAM2/{ split($1,stop,/[:Z]/); wam2=stop[3]; if (i==1) print "Delta "++x" = "wam2-wam1; i=0;}' 1

产量:

Delta 1 = 3.21111
Delta 2 = 3.21111
于 2013-07-13T06:05:24.563 回答