#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --dir <каталог> [--max-age-hours 26] [--min-files 1] [--min-size-mb 0]" } dir="" max_age="26" min_files="1" min_size_mb="0" while [[ $# -gt 0 ]]; do case "$1" in --dir) dir="${2:-}"; shift 2 ;; --max-age-hours) max_age="${2:-}"; shift 2 ;; --min-files) min_files="${2:-}"; shift 2 ;; --min-size-mb) min_size_mb="${2:-}"; shift 2 ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$dir" ]]; then echo "Укажите каталог через --dir" >&2 exit 2 fi if [[ ! -d "$dir" ]]; then echo "Каталог не найден: $dir" >&2 exit 2 fi stat_mtime() { if stat -c %Y "$1" >/dev/null 2>&1; then stat -c %Y "$1" else stat -f %m "$1" fi } stat_size() { if stat -c %s "$1" >/dev/null 2>&1; then stat -c %s "$1" else stat -f %z "$1" fi } count=0 total=0 newest=0 newest_file="" while IFS= read -r -d '' file; do mtime="$(stat_mtime "$file")" || continue size="$(stat_size "$file")" || continue count=$((count + 1)) total=$((total + size)) if (( mtime > newest )); then newest=$mtime newest_file="$file" fi done < <(find "$dir" -type f -print0) if (( count == 0 )); then echo "CRITICAL: в каталоге нет файлов бэкапа" >&2 exit 2 fi now="$(date +%s)" if (( newest > now )); then age_hours=0 else age_hours=$(( (now - newest) / 3600 )) fi if date -d "@$newest" '+%Y-%m-%d %H:%M:%S' >/dev/null 2>&1; then newest_text="$(date -d "@$newest" '+%Y-%m-%d %H:%M:%S')" else newest_text="$(date -r "$newest" '+%Y-%m-%d %H:%M:%S')" fi total_mb=$((total / 1024 / 1024)) min_bytes=$((min_size_mb * 1024 * 1024)) printf 'Каталог: %s\nФайлов: %d\nСуммарный размер: %d MB\nНовый файл: %s (%s)\nВозраст: %d ч (порог %s)\n' \ "$dir" "$count" "$total_mb" "$newest_file" "$newest_text" "$age_hours" "$max_age" status=0 if (( age_hours > max_age )); then echo "CRITICAL: бэкап старше допустимого возраста" >&2 status=2 fi if (( count < min_files )); then echo "CRITICAL: файлов меньше ожидаемого" >&2 status=2 fi if (( total < min_bytes )); then echo "WARNING: суммарный размер меньше ожидаемого" >&2 if (( status == 0 )); then status=1 fi fi if (( status == 0 )); then echo "OK" fi exit "$status"