dark

dmesg with human readable timestamps

When you are not using Debian unstable or some kind of Linux distro that hasn’t got the latest version of the dmesg command, you will not be able to execute ‘dmesg -T‘ to see human readable timestamp in the output.

A small Perl script does the trick:

#!/usr/bin/perl -wn
use strict;

foreach my $line (<>) {
    my ($uptime) = do { local @ARGV='/proc/uptime';<>}; ($uptime) = ($uptime =~ /^(\d+)\./);
    $line=~/^\[\s*(\d+)\.\d+\](.+)/; 
    printf "[%s]%s\n", scalar localtime(time - $uptime + $1), $2;
}

The script itself is actually pretty straightforward: it reads in output from STDIN and calculates the timestamp based on:
– the first part of the line (which are the seconds since boot)
– reads /proc/uptime (which contains seconds past since boot)
– takes the current time in seconds (the time() subroutine)

The output produced looks like:

$ dmesg | perl dmesg.pl | head
[Thu Sep 12 08:15:59 2013] Initializing cgroup subsys cpu
[Thu Sep 12 08:15:59 2013] Initializing cgroup subsys cpuacct
[Thu Sep 12 08:15:59 2013] Linux version 3.10-2-amd64 (debian-kernel@lists.debian.org) (gcc version 4.7.3 (Debian 4.7.3-6) ) #1 SMP Debian 3.10.7-1 (2013-08-17)
[Thu Sep 12 08:15:59 2013] Command line: BOOT_IMAGE=/vmlinuz-3.10-2-amd64 root=/dev/mapper/gams-root ro quiet
[Thu Sep 12 08:15:59 2013] e820: BIOS-provided physical RAM map:
[Thu Sep 12 08:15:59 2013] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[Thu Sep 12 08:15:59 2013] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
[Thu Sep 12 08:15:59 2013] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
[Thu Sep 12 08:15:59 2013] BIOS-e820: [mem 0x0000000000100000-0x00000000dffeffff] usable
[Thu Sep 12 08:15:59 2013] BIOS-e820: [mem 0x00000000dfff0000-0x00000000dfffffff] ACPI data

Update 12.09.2013: I’ve updated the script

9 comments
  1. Good idea!
    Also in bash:
    $ base=$(cat /proc/uptime | cut -d ‘.’ -f1); seconds=`date +%s`; dmesg | sed ‘s/^\[\(.*\)\..*\]\(.*\)$/\1\n\2/’ | while read first; do read second; first=`date +”%d/%m/%Y %H:%M:%S” –date=”@$(($seconds – $base + $first))”`; echo “[$first] $second”; done

  2. Hi

    I tried this script on Centos 5.8 and it didn’t worked. It gave error as below with all dmesg line.

    Use of uninitialized value in printf at ./dmesg.pl line 7, line 11623.

    Here is the dmesg script codes if I am missing somthing here.

    #!/usr/bin/perl -wn
    use strict;

    foreach my $line () {
    my ($uptime) = (do { local @ARGV=’/proc/uptime’;} =~ /^(\d+)\./);
    $line=~/^\[\s*(\d+)\.\d+\](.+)/;
    printf “[%s]%s\n”, scalar localtime(time – $uptime + $1), $2;
    }

    1. Hi Mike,

      I’ve updated the code snippit, could you test it again? On my Debian it actually works like a charm

  3. Hi Johnny,

    Thank You for your reply. Well I am still getting error as below:

    Use of uninitialized value in printf at ./dmesg.pl line 7, line 1.

  4. Hi Johnny,

    Thank You for your reply. Well I am still getting error as below:


    Use of uninitialized value in printf at ./dm-tes.pl line 7, line 1.

  5. Or just:

    $ dmesg -T|head
    [Sun Jun 8 10:13:52 2014] Initializing cgroup subsys cpuset
    [Sun Jun 8 10:13:52 2014] Initializing cgroup subsys cpu
    [Sun Jun 8 10:13:52 2014] Linux version 3.2.0-4-amd64 (debian-kernel@lists.debian.org) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Debian 3.2.57-3+deb7u2
    [Sun Jun 8 10:13:52 2014] Command line: BOOT_IMAGE=/boot/vmlinuz-3.2.0-4-amd64 root=UUID=2c60bbd2-7b5d-4f30-a4f0-dcea4410b5f7 ro quiet
    [Sun Jun 8 10:13:52 2014] BIOS-provided physical RAM map:
    [Sun Jun 8 10:13:52 2014] BIOS-e820: 0000000000000000 – 000000000009dc00 (usable)
    [Sun Jun 8 10:13:52 2014] BIOS-e820: 000000000009f800 – 00000000000a0000 (reserved)
    [Sun Jun 8 10:13:52 2014] BIOS-e820: 00000000000f0000 – 0000000000100000 (reserved)
    [Sun Jun 8 10:13:52 2014] BIOS-e820: 0000000000100000 – 00000000bafc0000 (usable)
    [Sun Jun 8 10:13:52 2014] BIOS-e820: 00000000bafc0000 – 00000000bafc3000 (ACPI NVS)

    1. Yes … but when I wrote this blog post (and code snippit), the option wasn’t available 😉 At least not in the version of Debian Squeeze

Leave a Reply to Johnny Morano Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Previous Post

Finder and Samba share support

Next Post

Watch a directory for uploaded files

Related Posts