-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_btrfs_scrub
executable file
·49 lines (42 loc) · 1.86 KB
/
check_btrfs_scrub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
BTRFS="/bin/btrfs"
AGE_WARNING=""
while [ $# -gt 0 ]; do
[ "$1" == "--volume" ] && shift && FS="$1" && shift && continue
[ "$1" == "--age-warning" ] && shift && AGE_WARNING="$1" && shift && continue
echo "CRITICAL: Unknown option: $1"
exit 2
done
[ -z "$FS" -o ! -e "$FS" ] && echo "No such directory: $FS" && exit 2
RAW="$("$BTRFS" scrub status -R "$FS")"
if [ $? -ne 0 ]; then
echo "CRITICAL: Cannot get scrub status of $FS"
exit 2
fi
if ! egrep -q "scrub (started|resumed)" <<<"$RAW"; then
echo "CRITICAL: Filesystem has never been scrubbed."
exit 2
fi
ERRORS="$(echo "$RAW" | egrep '_errors' | egrep -v ' 0$')"
if [ -n "$ERRORS" ]; then
echo "CRITICAL: Scrub errors found:" $ERRORS
exit 2
fi
if egrep -q "scrub (started|resumed) .* finished .*" <<<"$RAW"; then
# scrub started at Sat Oct 14 00:10:00 2017 and finished after 08:57:46
DURATION="$(echo "$RAW" | egrep -o 'finished after ..:..:..$' | sed 's/finished after //g')";
START="$(echo "$RAW" | egrep -o "scrub [^ ]* at ... ... .. ..:..:.. ...." | sed 's/scrub [^ ]* at //g')"
END="$(date -d "$START + $(echo "$DURATION" | sed 's/\(..\):\(..\):\(..\)/\1 hours + \2 minutes + \3 seconds/')")"
AGE=$(( $(date +%s) - $(date -d "$END" +%s) ))
if [ -z "$START" -o -z "$END" -o -z "$AGE" ]; then
echo "WARNING: CANNOT DETERMINE AGE OF SCRUB: Last scrub finished without finding errors:" $(egrep "scrub (started|resumed) .* finished .*" <<<"$RAW")
fi
if [ -n "$AGE_WARNING" ] && [ "$AGE_WARNING" -lt "$AGE" ]; then
echo "WARNING: SCRUB TOO OLD: Scrub finished $AGE seconds ago without finding errors:" $(egrep "scrub (started|resumed) .* finished .*" <<<"$RAW")
exit 1
fi
echo "OK: Scrub finished $AGE seconds ago without finding errors:" $(egrep "scrub (started|resumed) .* finished .*" <<<"$RAW")
exit 0
fi
echo WARNING: $(egrep "scrub (started|resumed)" <<<"$RAW")
exit 1