]> git.pld-linux.org Git - packages/linux-pstore.git/blame - linux-pstore.py
- rel 4; mount with nosuid,nodev,noexec
[packages/linux-pstore.git] / linux-pstore.py
CommitLineData
89eac3bb
AM
1#!/usr/bin/python3
2# arekm@maven.pl
3
4import datetime
5import os
6import re
4729278e 7import socket
89eac3bb 8import shutil
e4d4122e
AM
9import subprocess
10import sys
4729278e
AM
11import time
12
13uptime = True
14try:
15 import psutil
16except ModuleNotFoundError as e:
17 uptime = False
89eac3bb
AM
18
19pstoredir = '/sys/fs/pstore'
20archivedir = '/var/log/pstore'
21
22os.umask(0o077)
23
e4d4122e
AM
24def 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
37tdate = datetime.datetime.now().strftime("%Y-%m-%d")
38tdir = os.path.join(archivedir, tdate)
39
e4d4122e
AM
40mount_pstore()
41
89eac3bb
AM
42files = sorted(os.listdir(pstoredir))
43
44if len(files) and not os.path.isdir(tdir):
45 os.mkdir(tdir)
46
4729278e
AM
47msg = "Hostname: %s\n" % socket.getfqdn()
48if uptime:
49 msg += "Uptime: %s\n" % str(datetime.timedelta(seconds=time.time()-psutil.boot_time()))
50msg += "Files in pstore: %d\n" % len(files)
89eac3bb
AM
51
52for 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
82if len(files):
83 print(msg)
This page took 0.093349 seconds and 4 git commands to generate.