OpenWRT上可读的dmesg时间戳


21

dmesg的输出显示自系统启动以来的秒数+毫秒数。

[   10.470000] ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver
[   14.610000] device eth0 entered promiscuous mode
[   18.750000] cfg80211: Calling CRDA for country: DE
[   18.750000] cfg80211: Regulatory domain changed to country: DE

问:如何将秒+毫秒设置为可读格式?

我的dmesg:

root@OpenWrt:/tmp# dmesg -h
dmesg: invalid option -- h
BusyBox v1.19.4 (2013-03-14 11:28:31 UTC) multi-call binary.

Usage: dmesg [-c] [-n LEVEL] [-s SIZE]

Print or control the kernel ring buffer

    -c      Clear ring buffer after printing
    -n LEVEL    Set console logging level
    -s SIZE     Buffer size

因为没有太多可用空间,所以无法安装util-Linux:

root@OpenWrt:~# df -h
Filesystem                Size      Used Available Use% Mounted on
rootfs                    1.1M    956.0K    132.0K  88% /
/dev/root                 2.0M      2.0M         0 100% /rom
tmpfs                    14.3M    688.0K     13.6M   5% /tmp
tmpfs                   512.0K         0    512.0K   0% /dev
/dev/mtdblock3            1.1M    956.0K    132.0K  88% /overlay
overlayfs:/overlay        1.1M    956.0K    132.0K  88% /

root@OpenWrt:/tmp# which awk  perl sed bash sh shell tcsh
/usr/bin/awk
/bin/sed
/bin/sh


root@OpenWrt:~# date -h
date: invalid option -- h
BusyBox v1.19.4 (2013-03-14 11:28:31 UTC) multi-call binary.

Usage: date [OPTIONS] [+FMT] [TIME]

Display time (using +FMT), or set time

    [-s,--set] TIME Set time to TIME
    -u,--utc    Work in UTC (don't convert to local time)
    -R,--rfc-2822   Output RFC-2822 compliant date string
    -I[SPEC]    Output ISO-8601 compliant date string
            SPEC='date' (default) for date only,
            'hours', 'minutes', or 'seconds' for date and
            time to the indicated precision
    -r,--reference FILE Display last modification time of FILE
    -d,--date TIME  Display TIME, not 'now'
    -D FMT      Use FMT for -d TIME conversion
    -k      Set Kernel timezone from localtime and exit

您所说的“可读”格式是什么?
UVV

恐怕那时您可能不走运。如果您的系统将内核输出记录到某种日志中(例如,/var/log/syslog在Debian系统上,请检查该日志;它可能包含相同的信息,但带有可读的时间戳。)
Martin von Wittich 2014年

1
“可读”为人类可读的日期时间戳,就像您为“ -T” arg所解释的那样。

1
嗯,这将非常复杂,因为您似乎无法使用日期处理功能访问任何内容。您的date命令不支持该-d标志,对吗?我猜也没有python吗?这awk是哪个实现?它是GNU awk至少?
terdon

1
太好了,如果您有date -d,我更新的答案应该可以了。
terdon

Answers:


29

我认为您要查找的内容-Tman dmesg

-T,--ctime

打印人类可读的时间戳。时间戳可能不正确!

挂起/恢复系统后,用于日志的时间源不会更新。

因此,例如:

[  518.511925] usb 2-1.1: new low-speed USB device number 7 using ehci-pci
[  518.615735] usb 2-1.1: New USB device found, idVendor=1c4f, idProduct=0002
[  518.615742] usb 2-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[  518.615747] usb 2-1.1: Product: USB Keykoard

成为:

[Mon Jan 27 16:22:42 2014] hid-generic 0003:1C4F:0002.0007: input,hidraw0: USB HID v1.10 Keyboard [USB USB Keykoard] on usb-0000:00:1d.0-1.1/input0
[Mon Jan 27 16:22:42 2014] input: USB USB Keykoard as /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1/2-1.1:1.1/input/input24
[Mon Jan 27 16:22:42 2014] hid-generic 0003:1C4F:0002.0008: input,hidraw1: USB HID v1.10 Device [USB USB Keykoard] on usb-0000:00:1d.0-1.1/input1

我在这里找到了一个很酷的把戏。在sed表达式中使用有错误的,因为不止一个时,有它会失败]dmesg行。我已经对其进行了修改,以处理在自己的dmesg输出中发现的所有情况。因此,这应该在您的date行为符合预期的情况下起作用:

base=$(cut -d '.' -f1 /proc/uptime); 
seconds=$(date +%s); 
dmesg | sed 's/\]//;s/\[//;s/\([^.]\)\.\([^ ]*\)\(.*\)/\1\n\3/' | 
while read first; do 
  read second; 
  first=`date +"%d/%m/%Y %H:%M:%S" --date="@$(($seconds - $base + $first))"`;
  printf "[%s] %s\n" "$first" "$second"; 
done 

输出如下:

[27/01/2014 16:14:45] usb 2-1.1: new low-speed USB device number 7 using ehci-pci
[27/01/2014 16:14:45] usb 2-1.1: New USB device found, idVendor=1c4f, idProduct=0002
[27/01/2014 16:14:45] usb 2-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[27/01/2014 16:14:45] usb 2-1.1: Product: USB Keykoard

我们也可以添加,-T标志仅支持util-linux-ng-2.20.x或更高版本,因此它将支持Ubuntu 12.04或更高版本,而不支持CentOS / RHEL 6.3或更低版本
Rahul Patil 2014年

3
我已经使用dmesg了多年,现在才了解到这个标志。为什么没人告诉我呢?:D
Martin von Wittich 2014年

1
@MartinvonWittich在这里还是一样,我今天才第一次读手册页:)
terdon

抱歉,我没有事先说过我使用openwrt。

仅作记录:dmesg -T答案中的脚本也同样会在休眠时显示错误的时间。
Hi-

4

您的版本dmesg显然不是的完整版本,util-linux而是由提供busybox

busybox提供了许多实用程序的基础知识,但并未提供所有实用的功能。

如果要使用-Tterdon所建议的标志(正确),则需要使用由terdon dmesg提供的二进制文件util-linux

me@server:/tmp$ busybox sh
BusyBox v1.21.1 (Debian 1:1.21.0-1) built-in shell (ash)
Enter 'help' for a list of built-in commands.

/tmp $ dmesg -T
dmesg: invalid option -- 'T'
BusyBox v1.21.1 (Debian 1:1.21.0-1) multi-call binary.

Usage: dmesg [-c] [-n LEVEL] [-s SIZE]

Print or control the kernel ring buffer

    -c      Clear ring buffer after printing
    -n LEVEL    Set console logging level
    -s SIZE     Buffer size

/tmp $ /bin/dmesg -T | tail -5
[Mon Jän 27 13:37:24 2014] hid-generic 0003:046D:C03E.0006: input,hidraw2: USB HID v1.10 Mouse [Logitech USB-PS/2 Optical Mouse] on usb-0000:00:1d.0-1.8/input0
[Mon Jän 27 15:59:32 2014] NVRM: API mismatch: the client has the version 304.117, but
[Mon Jän 27 15:59:32 2014] NVRM: this kernel module has the version 304.116.  Please
[Mon Jän 27 15:59:32 2014] NVRM: make sure that this kernel module and all NVIDIA driver
[Mon Jän 27 15:59:32 2014] NVRM: components have the same version.
/tmp $
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.