aboutsummaryrefslogtreecommitdiff
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
parentb07a598332b5d5ef4a479b5faeea44a2a4e3c88b (diff)
downloadgnunet-de91a963fc7210e8bcc6e0856f2e1a0c92bab61d.tar.gz
gnunet-de91a963fc7210e8bcc6e0856f2e1a0c92bab61d.zip
print dmesg messages with human readable timestamps
useful to check when segfaults etc happened
-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 @@
1#!/usr/bin/env python
2# coding=utf8
3
4# Copyright (C) 2010 Saúl ibarra Corretgé <saghul@gmail.com>
5#
6
7"""
8pydmesg: dmesg with human-readable timestamps
9"""
10
11from __future__ import with_statement
12
13import re
14import subprocess
15import sys
16
17from datetime import datetime, timedelta
18
19
20_datetime_format = "%Y-%m-%d %H:%M:%S"
21_dmesg_line_regex = re.compile("^\[(?P<time>\d+\.\d+)\](?P<line>.*)$")
22
23def exec_process(cmdline, silent, input=None, **kwargs):
24 """Execute a subprocess and returns the returncode, stdout buffer and stderr buffer.
25 Optionally prints stdout and stderr while running."""
26 try:
27 sub = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
28 stdout, stderr = sub.communicate(input=input)
29 returncode = sub.returncode
30 if not silent:
31 sys.stdout.write(stdout)
32 sys.stderr.write(stderr)
33 except OSError,e:
34 if e.errno == 2:
35 raise RuntimeError('"%s" is not present on this system' % cmdline[0])
36 else:
37 raise
38 if returncode != 0:
39 raise RuntimeError('Got return value %d while executing "%s", stderr output was:\n%s' % (returncode, " ".join(cmdline), stderr.rstrip("\n")))
40 return stdout
41
42def human_dmesg():
43 now = datetime.now()
44 uptime_diff = None
45 try:
46 with open('/proc/uptime') as f:
47 uptime_diff = f.read().strip().split()[0]
48 except IndexError:
49 return
50 else:
51 try:
52 uptime = now - timedelta(seconds=int(uptime_diff.split('.')[0]), microseconds=int(uptime_diff.split('.')[1]))
53 except IndexError:
54 return
55
56 dmesg_data = exec_process(['dmesg'], True)
57 for line in dmesg_data.split('\n'):
58 if not line:
59 continue
60 match = _dmesg_line_regex.match(line)
61 if match:
62 try:
63 seconds = int(match.groupdict().get('time', '').split('.')[0])
64 nanoseconds = int(match.groupdict().get('time', '').split('.')[1])
65 microseconds = int(round(nanoseconds * 0.001))
66 line = match.groupdict().get('line', '')
67 t = uptime + timedelta(seconds=seconds, microseconds=microseconds)
68 except IndexError:
69 pass
70 else:
71 print "[%s]%s" % (t.strftime(_datetime_format), line)
72
73
74if __name__ == '__main__':
75 human_dmesg()