diff options
Diffstat (limited to 'contrib/scripts/pydmesg')
-rwxr-xr-x | contrib/scripts/pydmesg | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/contrib/scripts/pydmesg b/contrib/scripts/pydmesg new file mode 100755 index 000000000..d60e08fe3 --- /dev/null +++ b/contrib/scripts/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 | """ | ||
8 | pydmesg: dmesg with human-readable timestamps | ||
9 | """ | ||
10 | |||
11 | from __future__ import with_statement | ||
12 | |||
13 | import re | ||
14 | import subprocess | ||
15 | import sys | ||
16 | |||
17 | from 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 | |||
23 | def 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 | |||
42 | def 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 | |||
74 | if __name__ == '__main__': | ||
75 | human_dmesg() | ||