Linux dmesg

引言

一直用 dmesg,但在服务器上查看时,时间戳不易读。
所以查下用什么方法让其变成 human readable.

dmesg 简介


dmesg - print or control the kernel ring buffer
dmesg is used to examine or control the kernel ring buffer.

The default action is to read all messages from kernel ring buffer.

终端输入 dmesg,可以看到每行最开始显示的是一个中括号,里面的数字为 timestamp,时间戳, 该时间指示的系统从开机到现在的运行时间,单位为秒。

dmesg 时间戳转换

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

这个参数在较老版本的 util-linux 中并没有,还要注意这个时间可能不准确。

2. 使用命令或其他工具转换

使用 bash 命令转换:

date -d "1970-01-01 UTC `echo "$(date +%s)-$(cat /proc/uptime|cut -f 1 -d' ')+[dmesg显示的时间]"|bc ` seconds"

当然还有其他工具,比如这个上 Python 写的pydmesg

3. 配置 rsyslog,将 kernel 日志输出到文件

修改/etc/[r]syslog.conf,添加如下配置:


kern.*                                                  /var/log/kern.log

后续可以在/var/log/kern.log中查看 kernel 日志信息。

Ref

Linux dmesg 输出没有时间列
Linux 时间戳转换/dmesg 时间转换 关于 dmesg 的 timestamp
Linux 系统中‘dmesg’命令处理故障和收集系统信息的 7 种用法
dmesg 以 human readable 的方式显示时间
CentOS 上配置 rsyslog 客户端用以远程记录日志