Last active
December 18, 2017 14:44
-
-
Save nikolaev-rd/dc7ad7d4d8174ef95e0bf188f29df1ae to your computer and use it in GitHub Desktop.
Nagios check (bash script)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# DEBUG | |
# For debug purposes generate test file like this: | |
# touch -t [[CC]YY]MMDDhhmm[.ss] /tmp/test_file_age | |
# | |
# Base path to this script: | |
SCRIPT_FULLNAME=${0##*/} | |
# | |
# Name of this script without extension: | |
SCRIPT_NAME=${SCRIPT_FULLNAME%.*} | |
# | |
# Version: | |
SCRIPT_VERSION="3.2" | |
# | |
# | |
# Default values... | |
# | |
# Maximum depth for directories search: | |
max_depth=3 | |
# | |
# Age of files in minutes: | |
age_minutes=60 | |
# | |
# Thresholds: | |
threshold_warning="none" | |
threshold_critical=1 | |
# | |
# Alternative mode: | |
alt_mode=false | |
# | |
# Verbose mode: | |
verbose_mode=false | |
# Usage message: | |
usage() { | |
cat << END | |
Script checks the age of file(s). | |
Search in the specified directories recursively for file(s), | |
which modification time is greater or less than specified age (see -m|--minutes options for more details). | |
Compare the total number of founded files with thresholds (if specified): | |
- Standart (default) mode: total count are greater or equal, then threshold(s). | |
- Alternative mode (-a|alt-mode): total count are less, then threshold(s). | |
Usage: | |
$SCRIPT_FULLNAME [options] | |
$SCRIPT_FULLNAME -p "PATH-1 [PATH-2] [PATH-N]" [-d|--depth N] [-m|--minutes N] [-a|--alt-mode] [-w|--warning N] [-c|--critical N] [-V|--verbose] | |
$SCRIPT_FULLNAME -h|--help | |
$SCRIPT_FULLNAME -v|--version | |
END | |
cat << END | column -s\& -t | |
Options: | |
-p, --paths & Specify the path(s) where to search for file(s). | |
& You can specify path(s) to directory or path(s) to the file. Use quotes to specify multiple paths (see examples for more details). | |
& | |
-d, --depth & Maximum depth of subdirectories for recursive search (default: 3). | |
& Optional. | |
& | |
-m, --minutes & The age of file(s) in minutes (default: 60). | |
& In case integer number (example: 10) - search for files, which was modified within specified minutes ago ("fresh files"). | |
& Use number with plus prefix (example: +10) to search for files, which was modified more than specified minutes ago ("old files"). | |
& Optional. | |
& | |
-a, --alt-mode & Alternative check mode - compare search results in a different (opposite) manner. | |
& Optional. | |
& | |
-w, --warning & Warning threshold - to compare how much files was founded for specified criteria (default: none). | |
& Optional. | |
& | |
-c, --critical & Critical threshold - to compare how much files was founded for specified criteria (default: 1). | |
& Optional. | |
& | |
-V, --verbose & Show more detailed statistics in output | |
& | |
-h, --help & Show this message. | |
& | |
-v, --version & Show version information. | |
& | |
END | |
cat << END | |
Examples: | |
# ./$SCRIPT_FULLNAME -p "/path/to/one/file.name" -m 120 | |
Return CRITICAL status if specified file was modified in the last 119 minutes. | |
# ./$SCRIPT_FULLNAME -p "/path/to/dir/one /path/to/dir/two" -m 30 -w 10 -c 15 | |
Search in two specified directories for files with last moification time < 30 minutes, and compare with thresholds. | |
# ./$SCRIPT_FULLNAME -p "/path/to/dir/two" -m 10 -d 2 -w 10 -c 15 | |
Search in specified directory and in subdirectoties (one level deeper) for files with last moification time < 10 minutes. | |
WARNING status if was founded 10 files or more, CRITICAL status - 15 files or more. | |
# ./$SCRIPT_FULLNAME -p /path/to/some/dir -a -m 1440 | |
Search in specified directory and return OK status if found one or more files, which was modified in the last 24 hours. In other case it will return CRITICAL status. | |
# ./$SCRIPT_FULLNAME -p /some/dir/path -m +15 | |
Search in specified directory for files with last moification time > 15 minutes. | |
END | |
} | |
# Show help and exit if no options was specified | |
[[ $# == 0 ]] && usage && exit | |
# Use getops(1) for parsing options and arguments (short and long options): | |
OPTS=$(getopt --options p:d:m:aw:c:Vhv --longoptions paths:,depth:,minutes:,alt-mode,warning:,critical:,verbose,help,version --name $SCRIPT_NAME -- "$@") | |
eval set -- "$OPTS" | |
# Regular expression to check if variable is a number. | |
is_number='^[0-9]+$'; | |
# Regular expression to check if variable is a positive number. | |
is_positive_number='^[+][0-9]+$'; | |
while true | |
do | |
case "$1" in | |
-p|--paths) | |
paths=${2} | |
shift 2 | |
;; | |
-d|--depth) | |
[[ ${2} =~ $is_number ]] && max_depth=${2} | |
shift 2 | |
;; | |
-m|--minutes) | |
[[ ${2} =~ $is_number ]] && age_minutes="-${2}" | |
[[ ${2} =~ $is_positive_number ]] && age_minutes="${2}" | |
shift 2 | |
;; | |
-a|--alt-mode) | |
alt_mode=true | |
shift | |
;; | |
-w|--warning) | |
[[ ${2} =~ $is_number ]] && threshold_warning=${2} | |
shift 2 | |
;; | |
-c|--critical) | |
[[ ${2} =~ $is_number ]] && threshold_critical=${2} | |
shift 2 | |
;; | |
-V|--verbose) | |
verbose_mode=true | |
shift | |
;; | |
-h|--help) | |
usage | |
exit | |
;; | |
-v|--version) | |
echo "$SCRIPT_NAME v.$SCRIPT_VERSION" | |
exit | |
;; | |
--) | |
shift | |
break | |
;; | |
*) | |
echo "ERROR: Bad parameters or arguments." | |
exit 4 | |
;; | |
esac | |
done | |
for path in ${paths} | |
do | |
if [[ -d ${path} ]] || [[ -f ${path} ]] | |
then | |
c=0 | |
c=$(find ${path} -type f -maxdepth ${max_depth} -mmin ${age_minutes} 2> /dev/null | wc -l 2> /dev/null) | |
if $verbose_mode ; then | |
stat="${stat}${path} : $c; " | |
fi | |
((count=$count+$c)) | |
else | |
stat="$stat[${path}]: not a directory or file; " | |
fi | |
done | |
stat="${stat}TOTAL : $count (Age: ${age_minutes}m; Warning threshold: ${threshold_warning}; Critical threshold: ${threshold_critical})" | |
if $alt_mode ; then | |
if (( $count < $threshold_warning && $threshold_warning != "none")); then | |
echo "WARNING :: $stat" | |
exit 1 | |
elif (( $count < $threshold_critical )); then | |
echo "CRITICAL :: $stat" | |
exit 2 | |
else | |
echo "OK :: $stat" | |
exit 0 | |
fi | |
else | |
if (( $count >= $threshold_critical )); then | |
echo "CRITICAL :: $stat" | |
exit 2 | |
elif (( $count >= $threshold_warning && $threshold_warning != "none")); then | |
echo "WARNING :: $stat" | |
exit 1 | |
else | |
echo "OK :: $stat" | |
exit 0 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment