Marcas de tiempo de dmesg legibles por humanos en OpenWRT


21

La salida de dmesg muestra el número de segundos + milisegundos desde el inicio del sistema.

[   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

P: ¿Cómo poner los segundos + milisegundos en un formato legible?

Mi 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

Para instalar util-Linux no será posible, porque no hay mucho espacio disponible:

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

¿A qué se refiere como formato "legible"?
— UVV

Me temo que probablemente no tengas suerte, entonces. Si su sistema registra la salida del kernel en algún tipo de registro (por ejemplo, /var/log/syslogen los sistemas Debian, verifique ese registro; puede contener la misma información pero con marcas de tiempo legibles.
— Martin von Wittich

1
'legible' como sello de fecha y hora legible para humanos, como usted explicó para el argumento '-T'.

1
Hmm, esto será muy complejo ya que no parece tener acceso a nada con capacidades de manipulación de fechas. Su datecomando no es compatible con la -dbandera, ¿verdad? ¿Y tampoco python, supongo? ¿Qué awkimplementación es esta? ¿Es GNU awkal menos?
— terdon

1
Genial, si es así date -d, mi respuesta actualizada debería funcionar.
— terdon

Respuestas:


29

Creo que lo que estás buscando está -Tdocumentado en man dmesg:

-T, --ctime

Imprima marcas de tiempo legibles por humanos. ¡La marca de tiempo podría ser inexacta!

La fuente de tiempo utilizada para los registros no se actualiza después del sistema SUSPENDER / REANUDAR.

Así por ejemplo:

[  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

Se convierte en:

[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

Encontré un truco genial aquí . La sedexpresión utilizada allí era incorrecta ya que fallaría cuando hubiera más de uno ]en la dmesglínea. Lo modifiqué para que funcione con todos los casos que encontré en mi propia dmesgsalida. Entonces, esto debería funcionar asumiendo que se datecomporta como se esperaba:

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 

La salida se ve así:

[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

también podemos agregar, las -Tbanderas solo admiten util-linux-ng-2.20.xy superiores, por lo que admitirá Ubuntu 12.04 y superior y no en CentOS / RHEL 6.3 y versiones inferiores
— Rahul Patil

3
Lo he estado usando dmesgdurante años y solo aprendí sobre esta bandera ahora. ¿Por qué nadie me dijo eso? : D
— Martin von Wittich

1
@MartinvonWittich mismo aquí, acabo de leer la página del manual por primera vez hoy :)
— terdon

lo siento, no dije esto por adelantado que uso openwrt.

Solo para el registro: al igual que lo dmesg -Thace, el script en la respuesta también muestra un tiempo incorrecto al hibernar.
— Hola Ángel, el

4

su versión de dmesgobviamente no es la completa, util-linuxsino que es proporcionada por busybox.

busyboxproporciona los conceptos básicos de una multitud de utilidades, pero no proporciona todas sus características ingeniosas.

si desea usar la -Tbandera como (correctamente) sugerida por terdon, deberá usar el dmesgbinario proporcionado porutil-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 $
Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.