Skip to content
Snippets Groups Projects
check_rearbackup 4.27 KiB
#!/bin/bash
# ======================================================================
#
# Check REAR BACKUP
#
# ----------------------------------------------------------------------
# 2023-12-22  v1.0  <axel.hahn@unibe.ch>
# ======================================================================

. $( dirname $0 )/inc_pluginfunctions

export self_APPVERSION=1.0

dir_reardata=/rearshare/rear-backup

typeset -i iAgeSec
typeset -i iAgeD

typeset -i iTotal
typeset -i iOK
typeset -i iWarning
typeset -i iCritical

typeset -i iLimitWarning=7
typeset -i iLimitCritical=14


# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------

# show help text
function showHelp(){
    local _self; _self=$(basename $0)
cat <<EOF
$( ph.showImlHelpHeader )

Check if iso files of rear backup are not out of date.
You can customize the values for
* backup directory
* limits for warning and critical

This plugin sends performancedata.

SYNTAX:

  $_self [-h] [--dir PATH] [--critical VALUE] [--warning VALUE]

OPTIONS:

  -h, --help 
      this help

PARAMETERS:

  -d, --dir PATH
      set installation dir of iml deployment to find its check skript
      default dir: ${dir_reardata}

  -c, --critical VALUE
      critical level in days (default: $iLimitCritical)

  -w. --warning VALUE
      warning level in days (default: $iLimitWarning)

EXAMPLE:

  $_self
      Check backup data with initial values

  $_self -d /data/mybackups
      Check iso files a given directory
    
  $_self -d /data/mybackups -w 14 -c 28
      Check iso files a given directory and customized limits

EOF
}


# get age of a file in sec
# param  string  filename to test
function _getFileAge(){
    echo $(($(date +%s) - $(date +%s -r "$1")))
}

# get a list of iso files in rear backup dir in alphabetic order
function _getIsofiles(){
        find "${dir_reardata}" -type f -name "*.iso" | sort
}


# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------


# --- check param -h

while [[ "$#" -gt 0 ]]; do case $1 in
    -h|--help)      showHelp; exit 0;;
    -d|--dir)       dir_reardata=$2; shift ;shift;;
    -c|--critcal)   iLimitCritical=$2; shift ;shift;;
    -w|--warning)   iLimitWarning=$2; shift ;shift;;
    *) echo "ERROR: Unknown parameter: $1"; showHelp; exit 1;
esac; done


# --- test

if [ $iLimitWarning -ge $iLimitCritical ]; then
        ph.abort "ERROR: limit for warning ($iLimitWarning) must be lower than critical ($iLimitCritical). Get help with -h."
fi

if [ ! -d "$dir_reardata" ]; then
        ph.abort "ERROR: directory [$dir_reardata] does not exist. Set it with -d. Get help with -h."
fi

if [ ! -r "$dir_reardata" ]; then
        ph.abort "ERROR: unable to read the existing dir [$dir_reardata]. Check the permissions for $USER."
fi


# --- output data

data="$( for myiso in $( _getIsofiles )
        do
                iAgeSec=$( _getFileAge "$myiso" )
                iAgeD=$iAgeSec/60/60/24
                sBar=$( yes '#' | head -n $iAgeD | tr -d "\n" )
                sStatus="OK"

                if [ $iAgeD -ge $iLimitWarning ]; then
                        if [ $iAgeD -ge $iLimitCritical ]; then
                                sStatus="CRITICAL"
                        else
                                sStatus="WARNING"
                        fi
                fi

                printf "%-9s %3s d %-10s  %s\n" $sStatus $iAgeD "$sBar" "$( ls -lh $myiso )"
        done
)"

# --- Counters

iTotal=$(    grep -c "."         <<< "$data" )
iOK=$(       grep -c "^OK"       <<< "$data" )
iWarning=$(  grep -c "^WARNING"  <<< "$data" )
iCritical=$( grep -c "^CRITICAL" <<< "$data" )

test $iWarning -gt 0  && ph.setStatus "warning"
test $iCritical -gt 0 && ph.setStatus "critical"

# --- ouput

ph.status "Rear backup $dir_reardata - $iTotal total - $iCritical critical ($iLimitCritical d) .. $iWarning warnings ($iLimitWarning d) .. $iOK OK"

echo "All iso files in alphabetic order:"
echo "$data"

# --- performance data

ph.perfadd "ok"       "$iOK"       "" "" 0 $iTotal
ph.perfadd "warning"  "$iWarning"  "" "" 0 $iTotal
ph.perfadd "critical" "$iCritical" "" "" 0 $iTotal

ph.exit

# ----------------------------------------------------------------------