Skip to content

Instantly share code, notes, and snippets.

@drtomasso
Created January 20, 2016 13:47
Show Gist options
  • Save drtomasso/f38ebbef085e6022307e to your computer and use it in GitHub Desktop.
Save drtomasso/f38ebbef085e6022307e to your computer and use it in GitHub Desktop.
Check ZFS snapshot creation date. For Nagios NRPE
#!/bin/bash
FSNAME=$1
WARNAGE=$2
CRITAGE=$3
NOW=$(date +%s)
ZFS="sudo /sbin/zfs"
AWK="/usr/bin/awk"
# get all snapshots of zfs filesystem and search for newest snapshot
CREATIONDATE=$($ZFS get creation -Hpr -t snapshot $FSNAME | $AWK 'BEGIN {max = 0} {if ($3>max) max=$3} END {print max}')
DIFF=$((($NOW-$CREATIONDATE)/86400))
if [[ $WARNAGE > $CRITAGE ]]; then
echo "usage: $0 <fsname> <warntime> <crittime>"
echo " warntime must be smaller than crittime, written in days"
exit 3
fi
RETVAL=3 # UNKNOWN
MSG=""
if [[ $DIFF > $CRITAGE ]]; then
RETVAL=2 # CRITICAL
MSG="SNAPSHOT CRITICAL:"
elif [[ $DIFF > $WARNAGE ]]; then
RETVAL=1 # WARNING
MSG="SNAPSHOT WARNING:"
else
RETVAL=0 # OK
MSG="SNAPSHOT OK:"
fi
DATEFMT=$(date -d @$CREATIONDATE +"%Y-%m-%d %H:%M")
echo "$MSG last snapshot $FSNAME created $DATEFMT"
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment