Skip to content

Commit 118f9b3

Browse files
committed
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
1 parent be42cb6 commit 118f9b3

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

btrfs-issuemail.service

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Unit]
2+
Description=Check for btrfs issues and send an email if any were found
3+
Documentation=man:btrfs
4+
5+
[Service]
6+
Type=simple
7+
ExecStart=/usr/share/btrfsmaintenance/btrfs-issuemail.sh

btrfs-issuemail.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2022 Matthias Klumpp <[email protected]>
4+
5+
umask 022
6+
PATH=/sbin:/bin:/usr/sbin:/usr/bin
7+
export PATH
8+
9+
if [ -f /etc/sysconfig/btrfsmaintenance ] ; then
10+
. /etc/sysconfig/btrfsmaintenance
11+
fi
12+
13+
if [ -f /etc/default/btrfsmaintenance ] ; then
14+
. /etc/default/btrfsmaintenance
15+
fi
16+
17+
. $(dirname $(realpath "$0"))/btrfsmaintenance-functions
18+
19+
if [ -z "$BTRFS_MAILADDR" ]
20+
then
21+
# no email set, nothing to do for us
22+
exit 0
23+
fi
24+
25+
if ! command -v sendmail &> /dev/null
26+
then
27+
echo "Failed to find sendmail, can not send emails about issues!" >/dev/stderr
28+
exit 1
29+
fi
30+
31+
ISSUE_MAIL_SENT_FILE="/run/btrfs-issue-mail-sent"
32+
if [[ $(find "$ISSUE_MAIL_SENT_FILE" -mtime +1 -print) ]]; then
33+
# delete issue sent file if it is older than a day, so
34+
# we will send all notifications again
35+
rm $ISSUE_MAIL_SENT_FILE
36+
fi
37+
38+
BTRFS_STATS_MOUNTPOINTS=$(expand_auto_mountpoint "auto")
39+
OIFS="$IFS"
40+
IFS=:
41+
for MM in $BTRFS_STATS_MOUNTPOINTS; do
42+
if ! is_btrfs "$MM"; then
43+
echo "Path $MM is not btrfs, skipping"
44+
continue
45+
fi
46+
DEVSTATS=$(btrfs device stats --check $MM 2>&1)
47+
if [ $? -ne 0 ]; then
48+
49+
if [ -f "$ISSUE_MAIL_SENT_FILE" ]; then
50+
# check if we already sent an email
51+
if grep -Fxq "$MM" "$ISSUE_MAIL_SENT_FILE"; then
52+
# we've already mailed a report for issues on this
53+
# mountpoint today, don't send another one just yet
54+
continue
55+
fi
56+
fi
57+
58+
sendmail -t <<EOF
59+
To: $BTRFS_MAILADDR
60+
Subject: Btrfs device issue on $MM @ $HOSTNAME
61+
62+
This is an automatically generated mail message from btrfs-issuemail
63+
running on $HOSTNAME
64+
65+
An issue has been detected on the btrfs device mounted as $MM.
66+
67+
Faithfully yours, etc.
68+
69+
P.S. The 'btrfs device stats' output is:
70+
$DEVSTATS
71+
72+
Filesystem usage:
73+
$(btrfs fi df $MM 2>&1)
74+
EOF
75+
# set flag that we already sent a mail about this today
76+
echo "$MM" >> $ISSUE_MAIL_SENT_FILE
77+
fi
78+
done
79+
80+
exit 0

btrfs-issuemail.timer

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Unit]
2+
Description=Check for btrfs issues and send mail if any were found
3+
Documentation=man:btrfs
4+
5+
[Timer]
6+
OnCalendar=hourly
7+
Persistent=true
8+
9+
[Install]
10+
WantedBy=timers.target

sysconfig.btrfsmaintenance

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,13 @@ BTRFS_TRIM_MOUNTPOINTS="/"
152152
# the timer for these tasks(s) elapsed while the system was suspended
153153
# or powered off.
154154
BTRFS_ALLOW_CONCURRENCY="false"
155+
156+
## Path: System/File systems/btrfs
157+
## Description: Mail address to send issue reports to
158+
## Type: string
159+
## Default: ""
160+
#
161+
# If this is set to an email address or an username like "root", btrfs device stats
162+
# data will be checked every hour and an email will be sent to the given address
163+
# if any issues(like data corruption or read issues) were found.
164+
BTRFS_MAILADDR=""

0 commit comments

Comments
 (0)