Like aa-notify, which shows a notification on my desktop when AppArmor denies something.
Asked
Active
Viewed 1,105 times
1 Answers
1
Running the script below in the background will both show a notification:
...and add the error message with time stamp to the log file (~/btrfs_errors.txt), looking like:
Mon Oct 10 08:25:12 2016
BTRFS error (device md2): csum failed ino 7551 off 2310144 csum 623932426 expected csum 3810482428
The script
#!/usr/bin/env python3
import subprocess
import time
import os
log = os.path.join(os.environ["HOME"], "btrfs_errors.txt")
while True:
msginfo = subprocess.check_output(["dmesg", "--read-clear"]).decode("utf-8")
match = [l for l in msginfo.splitlines() if all(["btrfs" in l, "error" in l])]
if match:
with open(log, "a+") as out:
out.write(time.ctime()+"\n")
for l in match:
out.write(l)
out.write("\n\n")
subprocess.Popen(["notify-send", "-i", "gnome-disks",
"BTRFS error","Please see ~/btrfs_errors.txt for details"])
time.sleep(4)
How to use
- Copy the script above into an empty file, save it as
check_btrfs. Copy the script into a location where it cannot be edited without administrator's privileges, such as/usr/local/bin. Make the script executable(!). - Add the script to the
sudoersfile, as described e.g. here. This is necessary, because the script is run by usingsudo. The script reads fromdmesg, and clearsdmesg' history after reading, to prevent accumulating the amount of output to read. Clearingdmesgneeds sudo privileges.
This means however that if you need the output ofdmesgfor other purposes also, we need to work around. If so please mention. Test- run it with the command:
sudo check_btrfsif you copied it into a directory in
$PATH. If not, include the path to the script.If all works fine, add it to Startup Applications: Dash > Startup Applications > Add. Add the command:
sudo check_btrfs
Since I cannot test it with "real" btrfs errors, I ran it with checking for other events (pluging in a usb stick). In final the script, I used the message format in your link. The script checks on lines, containing both strings btrfs and error.
WHat it does
The script:
- once per four seconds reads the output of
dmesg - in case of an error, it shows a notification and adds the error to the file:
~/btrfs_errors.txt - clears the history of
dmesgto keep the script "low on juice".
Jacob Vlijm
- 85,475
