Default dmesg timestamp format:
[ 680.870878] r8169 0000:02:00.0: em1: link up
[ 680.870889] IPv6: ADDRCONF (NETDEV_CHANGE): em1: link becomes ready
[ 695.002667] tun: Universal TUN/TAP device driver, 1.6
On some systems dmesg has option -T
for example fedora 17:
[Вт. дек. 11 11:52:14 2012] r8169 0000:02:00.0: em1: link up
[Вт. дек. 11 11:52:14 2012] IPv6: ADDRCONF (NETDEV_CHANGE): em1: link becomes ready
[Вт. дек. 11 11:52:29 2012] tun: Universal TUN/TAP device driver, 1.6
for other situations there are scripts on perl and python:
solutions on the perl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #!/usr/bin/perl use strict; use warnings; my @dmesg_new = (); my $dmesg = "/bin/dmesg"; my @dmesg_old = `$dmesg`; my $now = time(); my $uptime = `cat /proc/uptime | cut -d"." -f1`; my $t_now = $now - $uptime; sub format_time { my @time = localtime $_[0]; $time[4]+=1; # Adjust Month $time[5]+=1900; # Adjust Year return sprintf '%4i-%02i-%02i %02i:%02i:%02i', @time[reverse 0..5]; } foreach my $line ( @dmesg_old ) { chomp( $line ); if( $line =~ m/\[\s*(\d+)\.(\d+)\](.*)/i ) { # now - uptime + sekunden my $t_time = format_time( $t_now + $1 ); push( @dmesg_new , "[$t_time] $3" ); } } print join( "\n", @dmesg_new ); print "\n"; |
solutions on the python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #!/usr/bin/env python # coding=utf8 # Copyright (C) 2010 Saúl ibarra Corretgé <saghul@gmail.com> # """ pydmesg: dmesg with human-readable timestamps """ from __future__ import with_statement import re import subprocess import sys from datetime import datetime, timedelta _datetime_format = "%Y-%m-%d %H:%M:%S" _dmesg_line_regex = re.compile("^\[(?P<time>\d+\.\d+)\](?P<line>.*)$") def exec_process(cmdline, silent, input=None, **kwargs): """Execute a subprocess and returns the returncode, stdout buffer and stderr buffer. Optionally prints stdout and stderr while running.""" try: sub = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs) stdout, stderr = sub.communicate(input=input) returncode = sub.returncode if not silent: sys.stdout.write(stdout) sys.stderr.write(stderr) except OSError,e: if e.errno == 2: raise RuntimeError('"%s" is not present on this system' % cmdline[0]) else: raise if returncode != 0: raise RuntimeError('Got return value %d while executing "%s", stderr output was:\n%s' % (returncode, " ".join(cmdline), stderr.rstrip("\n"))) return stdout def human_dmesg(): now = datetime.now() uptime_diff = None try: with open('/proc/uptime') as f: uptime_diff = f.read().strip().split()[0] except IndexError: return else: try: uptime = now - timedelta(seconds=int(uptime_diff.split('.')[0]), microseconds=int(uptime_diff.split('.')[1])) except IndexError: return dmesg_data = exec_process(['dmesg'], True) for line in dmesg_data.split('\n'): if not line: continue match = _dmesg_line_regex.match(line) if match: try: seconds = int(match.groupdict().get('time', '').split('.')[0]) nanoseconds = int(match.groupdict().get('time', '').split('.')[1]) microseconds = int(round(nanoseconds * 0.001)) line = match.groupdict().get('line', '') t = uptime + timedelta(seconds=seconds, microseconds=microseconds) except IndexError: pass else: print "[%s]%s" % (t.strftime(_datetime_format), line) if __name__ == '__main__': human_dmesg() |