#!/bin/sh #- # Copyright (c) 2017 Philip Paeps # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # # Nagios monitoring plugin for Tarsnap. Checks if the latest archive is # sufficiently fresh and returns archive statistics as performance data. # tmpfile=`mktemp /tmp/check-tarsnap.XXXXXX` || exit 3 cleanup() { rm -f ${tmpfile} } trap cleanup EXIT # Sensible defaults warn=86400 # one day crit=259200 # three days keyfile="$HOME/tarsnap-nagios.key" cflag= wflag= xflag= while getopts c:w:k:x: flag do case $flag in c) cflag=1 cval="$OPTARG";; w) wflag=1 wval="$OPTARG";; k) keyfile="$OPTARG";; x) extra="$OPTARG";; ?) printf "Usage: %s: [-k keyfile] [-c critical] [-w warning]\n" $0 printf " [-x extra_arguments_to_tarsnap]\n" exit 3;; esac done if [ ! -z "$cflag" ]; then case "$cval" in (*[!0-9]*|'') printf "Invalid arg for -c option: must be a positive integer\n" exit 3;; (*) crit=$cval;; esac fi if [ ! -z "$wflag" ]; then case "$wval" in (*[!0-9]*|'') printf "Invalid arg for -w option: must be a positive integer\n" exit 3;; (*) warn=$wval;; esac fi tarsnap="tarsnap --keyfile ${keyfile} ${extra}" begin=$( date +%s ) ${tarsnap} --list-archives -v >${tmpfile} 2>&1 if [ $? -ne 0 ]; then printf "UNKNOWN Couldn't list Tarsnap archives\n" cat ${tmpfile} exit 3 fi last=$( cat ${tmpfile} | cut -f 2 | sort -u | tail -n 1 ) ${tarsnap} --print-stats >${tmpfile} 2>&1 if [ $? -ne 0 ]; then # XXX Do we care if statistics fail? stats="-1 -1 -1 -1" else stats=$( cat ${tmpfile} | grep -o '[[:digit:]]\+' ) fi runtime=$(( $(date +%s) - begin )) _printstats() { printf "All_archives_total_size=%dB;;;0; " $1 printf "All_archives_compressed_size=%dB;;;0; " $2 printf "Unique_data_total_size=%dB;;;0; " $3 printf "Unique_data_compressed_size=%dB;;;0; " $4 printf "Check_time=%ds\n" $5 } when=$( date -j -f "%F %T" "${last}" +%s ) if [ ${when} -le $(( ${begin}-${crit} )) ]; then printf "CRITICAL last archive was made %s|" "${last}" rc=2 elif [ ${when} -le $(( ${begin}-${warn} )) ]; then printf "WARNING last archive was made %s)|" "${last}" rc=1 else printf "OK last archive was made %s)|" "${last}" rc=0 fi _printstats ${stats} ${runtime} exit $rc