-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmesg.py
More file actions
executable file
·56 lines (45 loc) · 1.45 KB
/
dmesg.py
File metadata and controls
executable file
·56 lines (45 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python3
__doc__ = """
Small utility script that will log the contents output by dmesg to a file.
./dmesg.py [append]
The option append set to append will make the script only append to
/var/log/dmesg.log, without it, or set to loop, the script will also rotate
and compress log files. Option append set to loop, makes the script loop
forever, appending.
"""
import glob, os, sys, time
def sorting(a):
return int(a.split('.')[2])
def increment_filename(filename):
number = int(filename.split('.')[2])
number += 1
return "/var/log/dmesg.log." + str(number) + ".gz"
append = loop = False
try:
append = sys.argv[1]
if append == 'loop':
loop = True
append = True
elif append == 'append':
append = True
except IndexError:
pass
if not append or loop:
dmesg_files = glob.glob("/var/log/dmesg.log.*.gz")
if len(dmesg_files) > 0:
dmesg_files = sorted(dmesg_files, key=sorting, reverse=True)
print(dmesg_files)
for filename in dmesg_files:
os.rename(filename, increment_filename(filename))
dmesg_file_old = glob.glob("/var/log/dmesg.log.0")
if dmesg_file_old:
os.rename("/var/log/dmesg.log.0", "/var/log/dmesg.log.1")
os.system("gzip -9 /var/log/dmesg.log.1")
dmesg_file = glob.glob("/var/log/dmesg.log")
if dmesg_file:
os.rename("/var/log/dmesg.log", "/var/log/dmesg.log.0")
while True:
os.system("dmesg -cHPT >> /var/log/dmesg.log 2>&1")
os.system("/bin/sync")
if not loop: break
time.sleep(5)