2

Like aa-notify, which shows a notification on my desktop when AppArmor denies something.

Jacob Vlijm
  • 85,475
Artyom
  • 1,803

1 Answers1

1

Running the script below in the background will both show a notification:

enter image description here

...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

  1. 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(!).
  2. Add the script to the sudoers file, as described e.g. here. This is necessary, because the script is run by using sudo. The script reads from dmesg, and clears dmesg' history after reading, to prevent accumulating the amount of output to read. Clearing dmesg needs sudo privileges.
    This means however that if you need the output of dmesg for other purposes also, we need to work around. If so please mention.
  3. Test- run it with the command:

    sudo check_btrfs
    

    if you copied it into a directory in $PATH. If not, include the path to the script.

  4. 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 dmesg to keep the script "low on juice".
Jacob Vlijm
  • 85,475