Kernel

OpenWRT 上的人類可讀 dmesg 時間戳

  • September 18, 2016

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

我認為您正在尋找的內容-T如以下文件所述man dmesg

-T, –ctime

列印人類可讀的時間戳。時間戳可能不准確!

用於日誌的時間源在系統 SUSPEND/RESUME 後不會更新。

因此,例如:

[  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

引用自:https://unix.stackexchange.com/questions/111145