Skip to content

Instantly share code, notes, and snippets.

@lgaggini
Created December 15, 2017 10:05
Show Gist options
  • Save lgaggini/89d56b4692f6379a5262b1b5f2a489a5 to your computer and use it in GitHub Desktop.
Save lgaggini/89d56b4692f6379a5262b1b5f2a489a5 to your computer and use it in GitHub Desktop.
Check if a file matching file pattern in a path matching path pattern was modified in the last N minutes
#!/bin/bash
#
# Author: Lorenzo Gaggini - [email protected]
# Check if a file matching file pattern in a path matching path
# pattern was modified in the last N minutes and it raise a CRITICAL
# if it's the case, it's OK otherwise.
#
# Usage: ./check_mod_file_glob.sh -p path_pattern -f file_pattern -c minutes
function usage()
{
echo "Usage:"
echo "./check_mod_file_glob.sh -p path_pattern -f file_pattern -c minutes"
echo "-p (required): pattern matching pathname where to search for files"
echo "-f (required): pattern matching filename to search for "
echo "-c (required): number of minutes from the last modify of the file"
}
while getopts ":hp:f:c:" opt; do
case ${opt} in
h ) usage && exit 0;;
p ) path_pattern=$OPTARG;;
f ) file_pattern=$OPTARG;;
c ) minutes=$OPTARG;;
\?|*) usage && exit 1;;
esac
done
if [ -z ${path_pattern+x} ] || [ -z ${file_pattern+x} ] || [ -z ${minutes+x} ]; then
usage && exit 1
fi
matches=$(find ${path_pattern} -type f -mmin -"${minutes}" -name "${file_pattern}" -print)
if [ -z ${matches} ]; then
echo "OK, no modified files in the last ${minutes} matching ${path_pattern} ${file_pattern} found"
exit 0
else
echo "KO, found files matching ${path_pattern} ${file_pattern} modified in the last ${minutes}: ${matches}"
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment