#!/bin/bash
# ======================================================================
#
# Check file age
#
# ----------------------------------------------------------------------
# 2024-06-21  v1.0  <martin.gasser@unibe.ch>
# ======================================================================

. $( dirname $0 )/inc_pluginfunctions

export self_APPVERSION=1.0

dir_files=/var/iml-backup
filter_files='*.*'

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 files are not out of date.
You can customize the values for
* directory
* limits for warning and critical
* filename filter pattern

This plugin sends performancedata.

SYNTAX:

  $_self [-h] [--dir PATH] [--filter FILTER] [--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_files}

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

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

  -f --filter FILTER
     filter for filenames (default: ${filter_files}

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 files in dir in alphabetic order
function _getfiles(){
        find "${dir_files}" -type f -name "${filter_files}" | sort
}


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


# --- check param -h

while [[ "$#" -gt 0 ]]; do case $1 in
    -h|--help)      showHelp; exit 0;;
    -d|--dir)       dir_files=$2; shift ;shift;;
    -c|--critcal)   iLimitCritical=$2; shift ;shift;;
    -w|--warning)   iLimitWarning=$2; shift ;shift;;
    -f|--filter)    filter_files=$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_files" ]; then
        ph.abort "ERROR: directory [$dir_files] does not exist. Set it with -d. Get help with -h."
fi

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


# --- output data

data="$( for myfile in $( _getfiles )
        do
                iAgeSec=$( _getFileAge "$myfile" )
                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 $myfile )"
        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 "File age $dir_files - $iTotal total - $iCritical critical ($iLimitCritical d) .. $iWarning warnings ($iLimitWarning d) .. $iOK OK"

echo "All 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

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