最近公司疑难杂症特别多,日志一打看完又一打,令我最心痛的是dmesg查看。
直接命令行你看到的是
[944273.286389] [31933] 1005 31933 13189 144 29 0 0 sftp-server
[944273.286390] [31982] 0 31982 37064 332 73 0 0 sshd
[944273.286392] [31984] 1005 31984 37413 622 73 0 0 sshd
[944273.286393] [31985] 1005 31985 13189 144 29 0 0 sftp-server
[944273.286395] [32005] 0 32005 37064 332 73 0 0 sshd
[944273.286396] [32007] 1005 32007 37064 335 72 0 0 sshd
[944273.286397] [32008] 1005 32008 28845 96 15 0 0 bash
[944273.286399] [32035] 1005 32035 26984 26 10 0 0 tail
[944273.286400] [32081] 0 32081 37064 331 74 0 0 sshd
[944273.286401] [32083] 1005 32083 37064 334 72 0 0 sshd
[944273.286402] [32084] 1005 32084 28845 98 15 0 0 bash
[944273.286404] [32110] 1005 32110 26984 27 11 0 0 tail
[944273.286405] [32116] 0 32116 37064 333 74 0 0 sshd
[944273.286406] [32118] 1005 32118 37413 685 73 0 0 sshd
[944273.286407] [32119] 1005 32119 13189 160 29 0 0 sftp-server
[944273.286409] Out of memory: Kill process 31583 (java) score 185 or sacrifice child
[944273.286447] Killed process 31583 (java) total-vm:3646416kB, anon-rss:719452kB, file-rss:0kB, shmem-rss:0kB
这样的时间是对查看不太友好,所以需要转化为直观的时间,dmesg的原始时间戳,是系统的产生mesg的系统uptime时间,故需要获取系统的启动时间
下面这个脚本可以将如此的字符串转发能够输入友好的时间方便我们查看日志
#!/bin/bash
uptime_ts=`cat /proc/uptime | awk'{ print $1}'`
#echo $uptime_ts
dmesg | awk -v uptime_ts=$uptime_ts 'BEGIN {
now_ts = systime();
start_ts = now_ts - uptime_ts;
#print "system start time seconds:", start_ts;
#print "system start time:", strftime("[%Y/%m/%d %H:%M:%S]", start_ts);
}
{
printstrftime("[%Y/%m/%d %H:%M:%S]", start_ts + substr($1, 2, length($1) - 2)), $0
}'