dmesg convert timestamps to human format

3.2k views Asked by At

I have the following sample of dmesg:

 throttled log output.
57458] bar 3: test 2 on bar 8 is available
[   19.696163] bar 1403: test on bar 1405 is available
[   19.696167] foo: [   19.696168] bar 3: test 5 on bar 1405 is available
[   19.696178] foo: [   19.696179] bar 1403: test 5 on bar 1405 is available
[   20.928730] foo: [   20.928733] bar 1403: test on bar 1408 is available
[   20.928742] foo: [   20.928745] bar 3: test on bar 1408 is available
[   24.878861] foo: [   25.878861] foo: [   25.878863] bar 1403: bar 802 is present

I would like to convert all timestamps in the line to human format ("%d/%m/%Y %H:%M:%S")

Notes: This system does not have dmesg -T nor has perl installed. I would prefer a solution w/ sed or awk, but python is also an option.

I've found a few solutions to this problem, but none quite answers what I need. Nor do I know how to modify it to my needs.

awk -F"]" '{"cat /proc/uptime | cut -d \" \" -f 1" | getline st;a=substr( $1,2, length($1) - 1);print strftime("%d/%m/%Y %H:%M:%S",systime()-st+a)" "$0}'

Or

sed -n 's/\]//;s/\[//;s/\([^.]\)\.\([^ ]*\)\(.*\)/\1\n\3/p' |  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

There's also a python script in here. But outputs some errors which I have zero understanding in.

Thanks!

2

There are 2 answers

0
Wintermute On

This is a bit touch-and-go, but it should at least give you something to work with:

awk '
  {
    # tail will be the part of the line that still requires processing
    tail = $0;                               

    # Read uptime from /proc/uptime and use it to calculate the system
    # start time
    "cat /proc/uptime | cut -d \" \" -f 1" | getline st;
    starttime = systime() - st;

    # while we find matches
    while((start = match(tail, /\[[^[]*\]/)) != 0) {
      # pick the timestamp from the match
      s = substr(tail, start + 1, RLENGTH - 2);

      # shorten the tail accordingly
      tail = substr(tail, start + RLENGTH);

      # format the time to our preference
      t = strftime("%d/%m/%Y %H:%M:%S", starttime + s);

      # substitute it into the original line. [] are replaced with || so
      # the match is not re-replaced in the next iteration.
      sub(/\[[^[]*\]/, "|" t "|", $0);
    }

    # When all matches have been replaced, print the line.
    print $0
  }' foo.txt
1
jsxt On

The following code simulates dmesg -T outcomes. It's inline awk within shell and can be stored as a standalone script or shell function:

awk -v UPTIME="$( cut -d' ' -f1 /proc/uptime )" '
BEGIN {
    STARTTIME = systime() - UPTIME
}
match($0, /^\[[^\[\]]*\]/) {
    s = substr($0, 2, RLENGTH - 2) + STARTTIME;
    s = strftime("%a %b %d %H:%M:%S %Y", s);
    sub(/^\[[^\[\]]*\]/, "[" s "]", $0);
    print
}
'

It doesn't guarantee precision as dmesg -T provides but makes results a bit closer.