|  | 
|  | 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 | 
0 commit comments