From 6a23666411da1693695953db2fbfe1304570ce38 Mon Sep 17 00:00:00 2001 From: Matthias Klumpp Date: Sat, 19 Mar 2022 02:34:55 +0100 Subject: [PATCH 1/2] Add script to send mail in case btrfs issues were detected This can be very useful for smaller setups where the admin still would like to receive an email in case a disk in a btrfs RAID array fails. Partially resolves #88 --- btrfs-issuemail.service | 7 ++++ btrfs-issuemail.sh | 82 ++++++++++++++++++++++++++++++++++++++ btrfs-issuemail.timer | 10 +++++ sysconfig.btrfsmaintenance | 10 +++++ 4 files changed, 109 insertions(+) create mode 100644 btrfs-issuemail.service create mode 100755 btrfs-issuemail.sh create mode 100644 btrfs-issuemail.timer diff --git a/btrfs-issuemail.service b/btrfs-issuemail.service new file mode 100644 index 0000000..171ce6d --- /dev/null +++ b/btrfs-issuemail.service @@ -0,0 +1,7 @@ +[Unit] +Description=Check for btrfs issues and send an email if any were found +Documentation=man:btrfs + +[Service] +Type=simple +ExecStart=/usr/share/btrfsmaintenance/btrfs-issuemail.sh diff --git a/btrfs-issuemail.sh b/btrfs-issuemail.sh new file mode 100755 index 0000000..d1d06cd --- /dev/null +++ b/btrfs-issuemail.sh @@ -0,0 +1,82 @@ +#!/bin/bash +# +# Copyright (c) 2022 Matthias Klumpp + +umask 022 +PATH=/sbin:/bin:/usr/sbin:/usr/bin +export PATH + +if [ -f /etc/sysconfig/btrfsmaintenance ] ; then + . /etc/sysconfig/btrfsmaintenance +fi + +if [ -f /etc/default/btrfsmaintenance ] ; then + . /etc/default/btrfsmaintenance +fi + +. $(dirname $(realpath "$0"))/btrfsmaintenance-functions + +if [ -z "$BTRFS_MAILADDR" ] +then + # no email set, nothing to do for us + exit 0 +fi + +if ! command -v sendmail &> /dev/null +then + echo "Failed to find sendmail, can not send emails about issues!" >/dev/stderr + exit 1 +fi + +ISSUE_MAIL_SENT_FILE="/run/btrfs-issue-mail-sent" +if [ -f "$ISSUE_MAIL_SENT_FILE" ]; then + if [[ $(find "$ISSUE_MAIL_SENT_FILE" -mtime +1 -print) ]]; then + # delete issue sent file if it is older than a day, so + # we will send all notifications again + rm $ISSUE_MAIL_SENT_FILE + fi +fi + +BTRFS_STATS_MOUNTPOINTS=$(expand_auto_mountpoint "auto") +OIFS="$IFS" +IFS=: +for MM in $BTRFS_STATS_MOUNTPOINTS; do + if ! is_btrfs "$MM"; then + echo "Path $MM is not btrfs, skipping" + continue + fi + DEVSTATS=$(btrfs device stats --check $MM 2>&1) + if [ $? -ne 0 ]; then + + if [ -f "$ISSUE_MAIL_SENT_FILE" ]; then + # check if we already sent an email + if grep -Fxq "$MM" "$ISSUE_MAIL_SENT_FILE"; then + # we've already mailed a report for issues on this + # mountpoint today, don't send another one just yet + continue + fi + fi + + sendmail -t <&1) +EOF + # set flag that we already sent a mail about this today + echo "$MM" >> $ISSUE_MAIL_SENT_FILE + fi +done + +exit 0 diff --git a/btrfs-issuemail.timer b/btrfs-issuemail.timer new file mode 100644 index 0000000..0df31e4 --- /dev/null +++ b/btrfs-issuemail.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Check for btrfs issues and send mail if any were found +Documentation=man:btrfs + +[Timer] +OnCalendar=hourly +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/sysconfig.btrfsmaintenance b/sysconfig.btrfsmaintenance index cfaa81e..6444685 100644 --- a/sysconfig.btrfsmaintenance +++ b/sysconfig.btrfsmaintenance @@ -152,3 +152,13 @@ BTRFS_TRIM_MOUNTPOINTS="/" # the timer for these tasks(s) elapsed while the system was suspended # or powered off. BTRFS_ALLOW_CONCURRENCY="false" + +## Path: System/File systems/btrfs +## Description: Mail address to send issue reports to +## Type: string +## Default: "" +# +# If this is set to an email address or an username like "root", btrfs device stats +# data will be checked every hour and an email will be sent to the given address +# if any issues(like data corruption or read issues) were found. +BTRFS_MAILADDR="" From 4b2f8989d090b72a6cfa3e7b4cd6391e06895059 Mon Sep 17 00:00:00 2001 From: Matthias Klumpp Date: Mon, 20 Feb 2023 11:39:55 +0100 Subject: [PATCH 2/2] Update btrfs-issuemail.sh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Adam Uhlíř --- btrfs-issuemail.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/btrfs-issuemail.sh b/btrfs-issuemail.sh index d1d06cd..e9b266f 100755 --- a/btrfs-issuemail.sh +++ b/btrfs-issuemail.sh @@ -65,7 +65,7 @@ This is an automatically generated mail message from btrfs-issuemail running on $HOSTNAME An issue has been detected on the btrfs device mounted as $MM. - +You will be getting this email daily until you clear the issue with 'btrfs device stats --reset $MM' Faithfully yours, etc. P.S. The 'btrfs device stats' output is: