Linux Centos with dmesg timestamp

Solution 1:

dmesg reads the Kernel log ring buffer. It doesn't do timestamps. What you should do is configure syslog to grab the kernel logs from that buffer and send them to a file (if it isn't already set to do so). Note, default CentOS 5.x syslog config sends kernel logs to /var/log/messages, as I recall.

If you'd like to send all kernel (dmesg) logs to /var/log/kern.log, using the default syslog daemon, you'd add a line like the following to /etc/syslog.conf

kern.*                         /var/log/kern.log

Solution 2:

There is solution "Enabling Timestamps for dmesg/Kernel Ring Buffer"

You could add:

printk.time=1

to kernel cmdline.

As for me, I have added to rc.local on all machines with puppet. It's easier for me) :

if test -f /sys/module/printk/parameters/time; then
   echo 1 > /sys/module/printk/parameters/time
fi

Solution 3:

I've written this simple script. Yes, it's slow. If you want something faster you either actually write a script on perl, python or something else. I'm sure this simple script can give you the hang of how it can be calculated.

Please note I ignored the seconds fraction registered in each line (after the . in the timestamp).

#!/bin/bash
localtime() {
 perl -e "print(localtime($1).\"\n\");";
}

upnow="$(cut -f1 -d"." /proc/uptime)"
upmmt="$(( $(date +%s) - ${upnow} ))"

dmesg | while read line; do
 timestamp="$(echo "${line}" | sed "s/^\[ *\([0-9]\+\).*/\1/g")"
 timestamp=$(( ${timestamp} + ${upmmt} ))
 echo "${line}" | sed "s/^[^]]\+]\(.*\)/$(localtime "${timestamp}") -\1/g"
done

I hope it helps. :)