Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to send mail in case btrfs issues were detected #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions btrfs-issuemail.service
Original file line number Diff line number Diff line change
@@ -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
82 changes: 82 additions & 0 deletions btrfs-issuemail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash
#
# Copyright (c) 2022 Matthias Klumpp <[email protected]>

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 <<EOF
To: $BTRFS_MAILADDR
Subject: Btrfs device issue on $MM @ $HOSTNAME

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.

ximion marked this conversation as resolved.
Show resolved Hide resolved
Faithfully yours, etc.

P.S. The 'btrfs device stats' output is:
$DEVSTATS

Filesystem usage:
$(btrfs fi df $MM 2>&1)
EOF
# set flag that we already sent a mail about this today
echo "$MM" >> $ISSUE_MAIL_SENT_FILE
fi
done

exit 0
10 changes: 10 additions & 0 deletions btrfs-issuemail.timer
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions sysconfig.btrfsmaintenance
Original file line number Diff line number Diff line change
Expand Up @@ -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=""