#! /bin/bash
#
# check_file_age
# Alerts if a file's age is beyond a specific threshold
# Copyright (C) 2021 Vintage Salt <rehashedsalt@cock.li>
#
# Distributed under terms of the MIT license.
#
set -e

# Read-only set-once variables
declare -r _name="$(basename -- "$0")"
# Options
declare -i _optcritthresh
declare -i _optwarnthresh
declare -i _opthelp
declare -i _optverbose
# Working variables
declare -a _args
declare _return

# Helper functions
log() {
	# Print a line to the terminal if _optverbose is greater than $2
	# $2 defaults to 0
	# loglevel 0: Daily-use messages
	# loglevel 1: Detailed but not quite debugging
	# loglevel 2: Definitely debugging
	[ -z "$1" ] && return 1
	if (( _optverbose >= ${2:-0} )); then
		printf "%s\\n" "$1"
	fi
}
warn() {
	# Print a yellow line to the terminal, respecting _optverbose
	[ -z "$1" ] && return 1
	if (( _optverbose >= ${2:-0} )); then
		if [ -t 1 ]; then
			printf "\\e[33m%s\\e[0m\\n" "$1"
		else
			printf "WARN: %s\\n" "$1"
		fi
	fi
}
error() {
	# Print a red line to the terminal, exit if $2 is specified
	[ -z "$1" ] && return 1
	if [ -t 2 ]; then
		printf "\\e[31m%s\\e[0m\\n" "$1" 1>&2
	else
		printf "ERROR: %s\\n" "$1" 1>&2
	fi
	[ -z "$2" ] && return
	exit "${2:-1}"
}
has() {
	# Parse out all arguments and try to find them in path
	# If an argument cannot be found, set _return and fail
	for prog in "$@"; do
		if ! command -v "$prog" > /dev/null 2>&1; then
			_return="$prog"
			return 1
		fi
	done
	return 0
}

# Core program functions
checkage() {
	# Check a file's age against the values of _optwarnthres and _optcritthresh
	file="${_args[0]}"
	log "Checking file $file" 1
	# Get some times
	filetimestamp="$(stat -c %Y -- "$file")"
	now="$(date +%s)"
	# Get our age
	age="$(( now - filetimestamp ))"
	log "File is $age seconds old" 1
	# Throw out warns and crits as necessary
	if (( age >= _optcritthresh )); then
		echo "CRITICAL: $file is $age seconds old (max $_optcritthresh)"
		exit 2
	elif (( age >= _optwarnthresh)); then
		echo "WARNING: $file is $age seconds old (max $_optwarnthresh)"
		exit 1
	else
		echo "OK: $file"
		exit 0
	fi
}
printhelp() {
	cat << EOF
Usage: $_name <FILE> [OPTION]...
A Nagios monitoring plugin for checking a file's age

Arguments:
  FILE			The file to monitor

Flags:
  -c			The maximum age of the file in seconds before a CRITICAL status is
			returned
  -h			Print this help text
  -v			Print more status messages. Stacks
  -w			The maximum age of the file in seconds before a WARNING status is
			returned

Copyright (c) 2021 rehashedsalt@cock.li
Licensed under the MIT license
EOF
}

# Main
main() {
	# Parse out arguments
	while [ -n "$1" ]; do
		# Parse out flags
		while getopts ":c:hvw:" opt; do
			case $opt in
				c)
					_optcritthresh="$OPTARG"
					;;
				h)
					_opthelp=1
					;;
				v)
					_optverbose+=1
					;;
				w)
					_optwarnthresh="$OPTARG"
					;;
				:)
					error "Option requires argument: -$OPTARG" 2
					;;
				*)
					error "Invalid option: -$OPTARG" 2
					;;
			esac
		done
		# Store arguments
		shift $((OPTIND - 1))
		if [ -n "$1" ]; then
			_args+=("$1")
			shift
		fi
		unset OPTIND
	done
	# Early hook for help
	[ -n "$_opthelp" ] && printhelp && exit 0
	# Validate critical options
	if [ -z "${_args[0]}" ]; then
		error "Must specify a file" 50
	fi
	if ! [ -r "${_args[0]}" ]; then
		error "File is unreadable: ${_args[0]}" 50
	fi
	if ! (( _optcritthresh > 0 )) 2>/dev/null; then
		error "Critical threshold must be an integer greater than 0" 50
	fi
	if ! (( _optwarnthresh > 0 )) 2>/dev/null; then
		error "Warning threshold must be an integer greater than 0" 50
	fi
	# Validate core program dependencies
	log "Validating dependencies" 2
	if ! has basename; then
		error "Failed to find program: $_return" 50
	fi

	# Do the do
	checkage
}

main "$@"