dmesg
的时间戳,并发现很难将其转换为 Java 日期或自定义日期格式。示例的 dmesg 日志条目:
[14614.647880] airo(eth1): link lost (missed beacons)
如何将
14614.647880
转换为标准日期?-T, --ctime
Print human readable timestamps. The timestamp could be inaccurate!
The time source used for the logs is not updated after system SUSPEND/RESUME.
在dr的帮助下,我编写了一个解决方法,可以将转换放入您的.bashrc中。如果您没有时间戳或已经正确的时间戳,则它不会影响任何内容。
dmesg_with_human_timestamps () {
$(type -P dmesg) "$@" | perl -w -e 'use strict;
my ($uptime) = do { local @ARGV="/proc/uptime";<>}; ($uptime) = ($uptime =~ /^(\d+)\./);
foreach my $line (<>) {
printf( ($line=~/^\[\s*(\d+)\.\d+\](.+)/) ? ( "[%s]%s\n", scalar localtime(time - $uptime + $1), $2 ) : $line )
}'
}
alias dmesg=dmesg_with_human_timestamps
另外,有关dmesg时间戳转换逻辑以及在没有时间戳的情况下如何启用时间戳的好文章: https://supportcenter.checkpoint.com/supportcenter/portal?eventSubmit_doGoviewsolutiondetails=&solutionid=sk92677
local dmesg_bin = $(type -a dmesg | awk 'END {print $ NF}')
- Brian Onn对于没有"dmesg -T"命令的系统,例如RHEL/CentOS 6,我喜欢之前由lucas-cimon提供的"dmesg_with_human_timestamps"函数。但是它在一些具有长时间运行的服务器上存在问题。原来,dmesg中的内核时间戳是根据各个CPU保持的正常运行时间值计算出来的。随着时间的推移,它会与实时时钟不同步。因此,对于最新的dmesg条目,最准确的转换将基于CPU时钟而不是/proc/uptime。例如,在这里的某个特定的CentOS 6.6服务器上:
# grep "\.clock" /proc/sched_debug | head -1
.clock : 32103895072.444568
# uptime
15:54:05 up 371 days, 19:09, 4 users, load average: 3.41, 3.62, 3.57
# cat /proc/uptime
32123362.57 638648955.00
考虑到CPU正常运行时间以毫秒为单位,这里有一个近5个半小时的偏差。因此,我重新编写了脚本并将其转换为本地bash:
dmesg_with_human_timestamps () {
FORMAT="%a %b %d %H:%M:%S %Y"
now=$(date +%s)
cputime_line=$(grep -m1 "\.clock" /proc/sched_debug)
if [[ $cputime_line =~ [^0-9]*([0-9]*).* ]]; then
cputime=$((BASH_REMATCH[1] / 1000))
fi
dmesg | while IFS= read -r line; do
if [[ $line =~ ^\[\ *([0-9]+)\.[0-9]+\]\ (.*) ]]; then
stamp=$((now-cputime+BASH_REMATCH[1]))
echo "[$(date +"${FORMAT}" --date=@${stamp})] ${BASH_REMATCH[2]}"
else
echo "$line"
fi
done
}
alias dmesgt=dmesg_with_human_timestamps
所以KevZero要求一个更简洁的解决方案,因此我想出了以下方法:
sed -r 's#^\[([0-9]+\.[0-9]+)\](.*)#echo -n "[";echo -n $(date --date="@$(echo "$(grep btime /proc/stat|cut -d " " -f 2)+\1" | bc)" +"%c");echo -n "]";echo -n "\2"#e'
这是一个例子:
$ dmesg|tail | sed -r 's#^\[([0-9]+\.[0-9]+)\](.*)#echo -n "[";echo -n $(date --date="@$(echo "$(grep btime /proc/stat|cut -d " " -f 2)+\1" | bc)" +"%c");echo -n "]";echo -n "\2"#e'
[2015-12-09T04:29:20 COT] cfg80211: (57240000 KHz - 63720000 KHz @ 2160000 KHz), (N/A, 0 mBm), (N/A)
[2015-12-09T04:29:23 COT] wlp3s0: authenticate with dc:9f:db:92:d3:07
[2015-12-09T04:29:23 COT] wlp3s0: send auth to dc:9f:db:92:d3:07 (try 1/3)
[2015-12-09T04:29:23 COT] wlp3s0: authenticated
[2015-12-09T04:29:23 COT] wlp3s0: associate with dc:9f:db:92:d3:07 (try 1/3)
[2015-12-09T04:29:23 COT] wlp3s0: RX AssocResp from dc:9f:db:92:d3:07 (capab=0x431 status=0 aid=6)
[2015-12-09T04:29:23 COT] wlp3s0: associated
[2015-12-09T04:29:56 COT] thinkpad_acpi: EC reports that Thermal Table has changed
[2015-12-09T04:29:59 COT] i915 0000:00:02.0: BAR 6: [??? 0x00000000 flags 0x2] has bogus alignment
[2015-12-09T05:00:52 COT] thinkpad_acpi: EC reports that Thermal Table has changed
如果你希望它表现得更好一些,那就将进程中的时间戳放入一个变量中 :)
dmesg -T
可能显示错误的时间,与 date
命令输出不同。
解决方法是使用带有 -k, --dmesg 的 journalctl 命令。由于它更短,我选择使用 -k:
journalctl -k
它将只显示内核消息和正确时间。
要仅显示匹配短语的内核行:
journalctl -kg phrase
-T
选项,你可以使用busybox
版本。下面的代码还可以解决其他一些问题:
[0.0000]
格式前面是看起来错位的颜色信息,前缀像<6>
。#!/bin/sh
# Translate dmesg timestamps to human readable format
# uptime in seconds
uptime=$(cut -d " " -f 1 /proc/uptime)
# remove fraction
uptime=$(echo $uptime | cut -d "." -f1)
# run only if timestamps are enabled
if [ "Y" = "$(cat /sys/module/printk/parameters/time)" ]; then
dmesg | sed "s/[^\[]*\[/\[/" | sed "s/^\[[ ]*\?\([0-9.]*\)\] \(.*\)/\\1 \\2/" | while read timestamp message; do
timestamp=$(echo $timestamp | cut -d "." -f1)
ts1=$(( $(busybox date +%s) - $uptime + $timestamp ))
ts2=$(busybox date -d "@${ts1}")
printf "[%s] %s\n" "$ts2" "$message"
done
else
echo "Timestamps are disabled (/sys/module/printk/parameters/time)"
fi
dmesg -T
即可。您需要在/proc/stat中引用“btime”,这是系统最新启动时的Unix纪元时间。然后,您可以基于系统启动时间并添加在dmesg中给出的经过的秒数来计算每个事件的时间戳。
以下命令可以给您更好的输出
dmesg | awk -F ] '{"cat /proc/uptime | cut -d \" \" -f 1" | getline st;a=substr( $1,2, length($1) - 1);print strftime("%F %H:%M:%S %Z",systime()-st+a)" -> "$0}'
rpm -qf /bin/dmesg => util-linux-2.13-0.56.0.2.el5
。 - michaelutil-linux 2.20
中。根据发行说明:ftp://ftp.kernel.org/pub/linux/utils/util-linux/v2.20/v2.20-ReleaseNotes - ks1322