summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-01-22 11:04:05 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-01-22 11:04:05 +0000
commitde91a963fc7210e8bcc6e0856f2e1a0c92bab61d (patch)
tree88c7e4c6af8ba9a122d9e7ef35307dface815d4a /contrib
parentb07a598332b5d5ef4a479b5faeea44a2a4e3c88b (diff)
print dmesg messages with human readable timestamps
useful to check when segfaults etc happened
Diffstat (limited to 'contrib')
-rwxr-xr-xcontrib/pydmesg75
1 files changed, 75 insertions, 0 deletions
diff --git a/contrib/pydmesg b/contrib/pydmesg
new file mode 100755
index 000000000..d60e08fe3
--- /dev/null
+++ b/contrib/pydmesg
@@ -0,0 +1,75 @@
+#!/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()