Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- #set -x
- # Exit codes for OP5: 0, 1, 2 or 3 for OK, Warning, Critical or Unknown
- NFS_DIR="/mnt/vol_nfs_nextgen_backup01"
- BACKUPS=$(find "${NFS_DIR}" -type f -iname "*.tar.gz")
- MAX_AGE=$(bc <<< '24*60*60')
- declare -A OLD_FILES
- declare -A ALERT
- get_file_age(){
- for _FILE in $(echo "${BACKUPS}"); do
- FILE_AGE=$(($(date +%s) - $(stat -c '%Y' "${_FILE}")))
- FILE_NAME=$(awk -F'/' '{print $NF}' <<< "${_FILE}")
- if [[ ${FILE_AGE} -gt ${MAX_AGE} ]]; then
- # An old backup was found, adding to array
- FILE_AGE=$(date -d "now - ${FILE_AGE} seconds")
- OLD_FILES[$_FILE]="${FILE_AGE}"
- fi
- done
- }
- find_new_files(){
- for _BACKUP in "${!OLD_FILES[@]}"; do
- FILE_AGE=$(($(date +%s) - $(stat -c '%Y' "${_BACKUP}")))
- FILE_NAME=$(awk -F'/' '{print $NF}' <<< "${_BACKUP}")
- DIR_NAME="${_BACKUP//$FILE_NAME/}"
- NEW_FILES=$(find "${DIR_NAME}" -type f -mtime -1 -ls)
- if [[ ! -z "${NEW_FILES}" ]]; then
- # An old backup was found and no new backups were found in the same directory, adding to alert
- FILE_AGE=$(date -d "now - ${FILE_AGE} seconds")
- ALERT["${DIR_NAME}${FILE_NAME}"]="${FILE_AGE}"
- fi
- done
- }
- get_file_age || exit 3
- if [[ ${#OLD_FILES[@]} -gt 0 ]]; then
- # An old backup was found, checking for newer backups in same directory
- find_new_files || exit 3
- if [[ ${#ALERT[@]} -gt 0 ]]; then
- # An old backup was found and no new backups were found in the same directory, printing alert
- for x in "${!ALERT[@]}"; do
- printf "%-30s %s\n" "${ALERT[$x]}" "$x"
- done
- exit 2
- fi
- else
- # No issues found
- exit 0
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement