一个 perl 处理文本的问题

2014-04-22 19:15:20 +08:00
 terry
要处理一个 XenServer dom0 崩溃的问题,感觉是硬件问题(应该是处理器)。需要查 MCE 错误代码,但问题是 XenServer dom0 (CentOS 5.x) kern.log 的格式是非标准的,用 mcelog 程序没法直接读,需要处理一下,查到一个 perl 1-liner 但本人对 perl 一无所知,调试了一下无果,错误看不懂。

cat kern.log | perl -ne '/CPU(\d+), BANK(\d+), addr (\S+), state (\S+)/; print "CPU $1 BANK $2 TSC 00000000000000\nMISC 0000000000000000 ADDR $3 \nSTATUS $4\n" > mce_fmtd.log'
Can't take log of 0 at -e line 1, <> line 1.

请问一下,有没有办法用 bash 脚本,利用 awk, sed, GNU 命令行工具处理得到同样的结果呢?

kern.log 的格式如下

Apr 16 16:42:10 server109 kernel: MCE_DOM0_LOG: enter dom0 mce vIRQ handler
Apr 16 16:42:10 server109 kernel: MCE_DOM0_LOG: No more urgent data
Apr 16 16:42:10 server109 kernel: [CPU8, BANK0, addr db484ffc0, state cc0000c00001009f]
Apr 16 16:42:10 server109 kernel: MCE_DOM0_LOG: No more nonurgent data
Apr 16 16:42:10 server109 kernel: MCE_DOM0_LOG: enter dom0 mce vIRQ handler
Apr 16 16:42:10 server109 kernel: MCE_DOM0_LOG: No more urgent data
Apr 16 16:42:10 server109 kernel: [CPU8, BANK0, addr db484ffc0, state cc0005000001009f]
Apr 16 16:42:10 server109 kernel: MCE_DOM0_LOG: No more nonurgent data
Apr 16 16:42:26 server109 kernel: MCE_DOM0_LOG: enter dom0 mce vIRQ handler
Apr 16 16:42:26 server109 kernel: MCE_DOM0_LOG: No more urgent data
Apr 16 16:42:26 server109 kernel: [CPU8, BANK0, addr db484fcc0, state cc0004c00001009f]
Apr 16 16:42:26 server109 kernel: MCE_DOM0_LOG: No more nonurgent data
Apr 16 16:42:57 server109 kernel: MCE_DOM0_LOG: enter dom0 mce vIRQ handler
Apr 16 16:42:57 server109 kernel: MCE_DOM0_LOG: No more urgent data
Apr 16 16:42:57 server109 kernel: [CPU8, BANK0, addr db484ef40, state cc0002800001009f]
Apr 16 16:42:57 server109 kernel: MCE_DOM0_LOG: No more nonurgent data
3841 次点击
所在节点    程序员
7 条回复
11138
2014-04-22 19:45:31 +08:00
cat kern.log | perl -ne '/CPU(\d+), BANK(\d+), addr (\S+), state (\S+)/; print "CPU $1 BANK $2 TSC 00000000000000\nMISC 0000000000000000 ADDR $3 \nSTATUS $4\n"' > mce_fmtd.log
terry
2014-04-22 19:51:20 +08:00
其实我只要知道

cat kern.log | perl -ne '/CPU(\d+), BANK(\d+), addr (\S+), state (\S+)/; print "CPU $1 BANK $2 TSC 00000000000000\nMISC 0000000000000000 ADDR $3 \nSTATUS $4\n"

是要把下面的文本弄成什么样的目标格式就行了

CPU8, BANK0, addr db484ffc0, state cc0000c00001009f

转成

CPU 8, BANK 0, TSC 00000000000000
MISC 0000000000000000 ADDR $3
STATUS $4

这样么?

谢谢先
terry
2014-04-22 19:54:55 +08:00
@11138 谢了,仔细看后就发现问题了。

下班前果然没心思看,竟然没发现单引号把重定向符号给包起来了...
terry
2014-04-22 20:04:11 +08:00
给感兴趣的人看一下结果

decode 之后

sudo mcelog --cpu=westmere --ascii --no-dmi --ignorenodev --file mce_fmtd.log > mce_decoded.log

CPU 8 BANK 0 TSC 00000000000000
MISC 0000000000000000 ADDR db484ef40
Hardware event. This is not a software error.
CPU 8 BANK 0
MISC 0 ADDR db484ef40
MCG status:
MCi status:
Error overflow
Corrected error
MCi_MISC register valid
MCi_ADDR register valid
MCA: MEMORY CONTROLLER RD_CHANNELunspecified_ERR
Transaction: Memory read error
Memory read ECC error
Memory corrected error count (CORE_ERR_CNT): 10
Memory transaction Tracker ID (RTId): 0
Memory DIMM ID of error: 0
Memory channel ID of error: 0
Memory ECC syndrome: 0
STATUS cc0002800001009f MCGSTATUS 0
STATUS cc0002800001009f]
CPU 8 BANK 0 TSC 00000000000000
MISC 0000000000000000 ADDR db484ef40
Hardware event. This is not a software error.

知道的一看就明白是什么问题了...
ngn999
2014-04-22 21:04:55 +08:00
cat kern.log | perl -ne 'print "CPU $1 BANK $2 TSC 00000000000000\nMISC 0000000000000000 ADDR $3 \nSTATUS $4\n" if /CPU(\d+), BANK(\d+), addr (\S+), state (\S+)/' > mce_fmtd.log
ngn999
2014-04-22 21:05:39 +08:00
perl在命令行下当 sed + awk用
leoYu
2014-04-23 14:05:34 +08:00
用sed也可以解决的,lz可以邮件我

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/109574

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX