#!/bin/ksh
#
# $Revision: 1.4+1 $ $Date: 2024-09-22 03:42:50-04 $
# $UUID: 0a55a8e8-8fbf-43b3-bf8e-0184da395cae $
#
#<zpool-check: check pool status on all systems.
# I got the idea from this:
#   
#   From: "Dave Cottlehuber" <dch@skunkwerks.at>
#   Subject: Re: Zpool status -- why does a suboptimal pool show as "ONLINE"?
#   Cc: questions <questions@freebsd.org>
#   Date: Thu, 12 Sep 2024 09:30:10 -0400
#   Message-Id: <312af967-e5bf-4e83-b48b-7c2841719373@app.fastmail.com>
#   
#   Practically, what I do is run:
#       zpool status | grep -v 'with 0 errors' | sha256
#   
#   and check that this hash remains the same over time.
#   It's obviously different for each pool.
#
# It's easier to just run with '-x' option and check the return code.

export PATH=/usr/local/bin:/bin:/usr/bin
set -o nounset
tag=${0##*/}

# Frequently used -- DRY.
zpool='/sbin/zpool'
ident="/home/vogelke/.ssh/hairball_ed25519"

# If root's running this, use setuidgid.
id | grep 'uid=0(root)' > /dev/null
case "$?" in
    0) remote="setuidgid myname ssh -q -i $ident hairball $zpool" ;;
    *) remote="ssh -q -i $ident hairball $zpool" ;;
esac

# This sets up the entire associative array.
HEALTH=(
    [furbag]=(                  # local production system
        CMD="$zpool status -x"
    )
    [hairball]=(                # remote backup system
        CMD="$remote status -x"
    )
)

# Real work starts here.
printf "ZFS POOL HEALTH\n---------------\n"

for sys in ${!HEALTH[*]}; do
    printf "%-10s " "${sys}:"
    ${HEALTH[$sys].CMD} 2> /dev/null
done

exit 0
