From 76668f21b42893fc25323de55925e7c7f686f682 Mon Sep 17 00:00:00 2001 From: Chl Date: Mon, 2 Oct 2023 15:54:37 +0200 Subject: [PATCH] check_btrfs: check for errors/corruption on devices --- nagios/check_btrfs.sh | 131 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100755 nagios/check_btrfs.sh diff --git a/nagios/check_btrfs.sh b/nagios/check_btrfs.sh new file mode 100755 index 0000000..df86c1d --- /dev/null +++ b/nagios/check_btrfs.sh @@ -0,0 +1,131 @@ +#!/bin/sh + +# Little check for device errors on Btrfs volumes +# +# GPL v3+ (copyright chl-dev@bugness.org) + +PROGPATH=$( echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,' ) +REVISION="0.1" + +# Stop at the first non-catched error +set -e + +# Include check_range() +# Not needed at the moment +#. $PROGPATH/utils.sh +STATE_OK=0 +STATE_WARNING=1 +STATE_CRITICAL=2 +STATE_UNKNOWN=3 +STATE_DEPENDENT=4 + +# Default values +WARNING_STATS=0 +CRITICAL_STATS=0 + +# Output +OUTPUT_EXIT_STATUS=$STATE_OK +OUTPUT_DETAIL_OK="" +OUTPUT_DETAIL_WARNING="" +OUTPUT_DETAIL_CRITICAL="" +OUTPUT_PERFDATA="" +NB_DEVICES=0 + +# +# Help function +# +usage() { + cat </dev/null 2>&1 +# if [ "$?" -eq "2" ]; then +# return 1 + +# Some early checks +if ! which btrfs 2>&1 >/dev/null; then + echo "UNKNOWN 'btrfs' command not found." + exit $STATE_UNKNOWN +fi + +# +# Loop on parameters + tests +# +while getopts hw:c:V: f; do + case "$f" in + 'h') + usage + exit + ;; + + 'V') + VOLUME="$OPTARG" + + # Quickly check if we have enough permission to launch btrfs commands + if ! btrfs device stats "$VOLUME" >/dev/null 2>&1 ; then + echo "UNKNOWN: unable to launch 'btrfs device stats $VOLUME' command (permissions ?)." + exit $STATE_UNKNOWN + fi + + # For information, add the number of devices to the perfdata + NB_DEVICES="$(( $NB_DEVICES + $( btrfs fi show --raw "$VOLUME" | sed -n 's/^[[:space:]]*Total devices \([0-9]\+\) .*/\1/p' ) ))" + + # Check the device stats + # (the kinda weird <