]>
Commit | Line | Data |
---|---|---|
89eac3bb AM |
1 | #!/usr/bin/python3 |
2 | # arekm@maven.pl | |
3 | ||
4 | import datetime | |
5 | import os | |
6 | import re | |
4729278e | 7 | import socket |
89eac3bb | 8 | import shutil |
e4d4122e AM |
9 | import subprocess |
10 | import sys | |
4729278e AM |
11 | import time |
12 | ||
13 | uptime = True | |
14 | try: | |
15 | import psutil | |
16 | except ModuleNotFoundError as e: | |
17 | uptime = False | |
89eac3bb AM |
18 | |
19 | pstoredir = '/sys/fs/pstore' | |
20 | archivedir = '/var/log/pstore' | |
21 | ||
22 | os.umask(0o077) | |
23 | ||
e4d4122e AM |
24 | def mount_pstore(): |
25 | try: | |
26 | if open('/proc/self/mounts', 'r').read().find('/sys/fs/pstore') >= 0: | |
27 | return | |
22c89e20 AM |
28 | subprocess.run(["/bin/mount", "none", "/sys/fs/pstore", "-t", "pstore", "-o", "nosuid,nodev,noexec"], |
29 | timeout=60, check=True, capture_output=True, text=True) | |
e4d4122e AM |
30 | except subprocess.CalledProcessError as e: |
31 | print("%s: %s\nstdout=%s\nstderr=%s" % (sys.argv[0], e, e.stdout, e.stderr), file=sys.stderr) | |
32 | sys.exit(1) | |
33 | except Exception as e: | |
34 | print("%s: %s" % (sys.argv[0], e), file=sys.stderr) | |
35 | sys.exit(1) | |
36 | ||
89eac3bb AM |
37 | tdate = datetime.datetime.now().strftime("%Y-%m-%d") |
38 | tdir = os.path.join(archivedir, tdate) | |
39 | ||
e4d4122e AM |
40 | mount_pstore() |
41 | ||
89eac3bb AM |
42 | files = sorted(os.listdir(pstoredir)) |
43 | ||
44 | if len(files) and not os.path.isdir(tdir): | |
45 | os.mkdir(tdir) | |
46 | ||
4729278e AM |
47 | msg = "Hostname: %s\n" % socket.getfqdn() |
48 | if uptime: | |
49 | msg += "Uptime: %s\n" % str(datetime.timedelta(seconds=time.time()-psutil.boot_time())) | |
50 | msg += "Files in pstore: %d\n" % len(files) | |
89eac3bb AM |
51 | |
52 | for file in files: | |
53 | fpath = os.path.join(pstoredir, file) | |
54 | mtime = datetime.datetime.fromtimestamp(os.path.getmtime(fpath)) | |
55 | ||
56 | msg += "File: [%s], Last modified: [%s], Contents:\n\n" % (file, mtime.strftime("%Y-%m-%d %H:%M:%S")) | |
57 | ||
58 | mv = True | |
59 | fdata = "" | |
60 | try: | |
61 | fdata = open(fpath, "rt", errors='ignore').read() | |
62 | except Exception as e: | |
63 | msg += "(reading contents of [%s] failed: %s)\n\n" % (file, e) | |
64 | mv = False | |
65 | ||
66 | try: | |
67 | shutil.copy2(fpath, tdir) | |
68 | except Exception as e: | |
69 | msg += "(copying [%s] to [%s] dir failed: %s)\n\n" % (file, tdir, e) | |
70 | mv = False | |
71 | ||
72 | if mv: | |
73 | os.unlink(fpath) | |
74 | ||
75 | if file.endswith(".z"): | |
76 | msg += "(content compressed which usually means it is corrupted for pstore)\n\n" | |
77 | fdata = re.sub(r'[^\x20-\x7E\n\t\r]+', lambda m: '.' * len(m.group()), fdata) | |
78 | ||
79 | msg += fdata | |
80 | msg += "\n\n" | |
81 | ||
82 | if len(files): | |
83 | print(msg) |