linux dmesg带时间戳,logging - 将dmesg时间戳转换为自定义日期格式

logging - 将dmesg时间戳转换为自定义日期格式

我试图理解dmesg时间戳,并发现很难将其转换为更改为Java日期/自定义日期格式。

任何帮助深表感谢。

示例dmesg日志:

[14614.647880] airo(eth1): link lost (missed beacons)

谢谢!

8个解决方案

147 votes

了解dmesg时间戳非常简单:它是内核启动后的秒数。 因此,有了启动时间(-T),您可以将秒数加起来并以您喜欢的任何格式显示它们。

或者更好的是,您可以使用-T选项并解析人类可读的格式。

从手册页:

-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.

xealits answered 2019-08-19T04:02:42Z

30 votes

在博士回答的帮助下,我写了一个解决方法,将转换放入.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时间戳转换逻辑&amp; 如何在没有时间戳时启用时间戳:[https://supportcenter.checkpoint.com/supportcenter/portal?eventSubmit_doGoviewsolutiondetails=&solutionid=sk92677]

Lucas Cimon answered 2019-08-19T04:03:24Z

13 votes

对于没有&#34; dmesg -T&#34; 比如RHEL / CentOS 6,我喜欢&#34; dmesg_with_human_timestamps&#34; 由卢卡斯 - 西蒙提供的功能。 虽然我们的一些盒子有很长的正常运行时间,但它有点麻烦。 事实证明,dmesg中的内核时间戳是从各个CPU保持的正常运行时间值得出的。 随着时间的推移,这与实时时钟不同步。 因此,最近dmesg条目的最准确转换将基于CPU时钟而不是/ proc /正常运行时间。 例如,在这里的特定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

Allen Belletti answered 2019-08-19T04:04:00Z

10 votes

所以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

如果您希望它执行得更好,请将proc的时间戳放入变量:)

runejuhl answered 2019-08-19T04:04:36Z

4 votes

在最新版本的dmesg中,您只需致电dmesg -T。

Steffen Heil answered 2019-08-19T04:05:01Z

3 votes

你需要参考&#34; btime&#34; 在/ proc / stat中,这是系统最新启动时的Unix纪元时间。 然后,您可以基于该系统启动时间,然后添加dmesg中给出的经过的秒数来计算每个事件的时间戳。

imcom answered 2019-08-19T04:05:26Z

3 votes

对于较旧的Linux发行版,另一种选择是使用包装脚本,例如 在Perl或Python中。

请在此处查看解

[http://linuxaria.com/article/how-to-make-dmesg-timestamp-human-readable?lang=en][http://jmorano.moretrix.com/2012/03/dmesg-human-readable-timestamps/]

Aliaksei Ramanau answered 2019-08-19T04:06:06Z

2 votes

如果您没有[0.0000]的2487220287322997576选项(例如Andoid),则可以使用<6>版本。 以下解决了其他一些问题:

[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

但请注意,此实现非常慢。

Anne van Rossum answered 2019-08-19T04:07:00Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值