Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- #
- usage() {
- echo "Usage: $0 [-c <critical seconds>] [-w <warning seconds>] [-f file pattern with path (required)] [-s strict file check]"
- exit 1
- }
- CRIT=
- WARN=
- FILE_PATTERN=
- STRICT_FILE_CHECK=0
- # Nagios Exit Codes
- OK=0
- CRITICAL=2
- WARNING=1
- UNKNOWN=3
- while getopts w:c:f:s:h option
- do
- case $option
- in
- c) CRIT=$OPTARG;;
- w) WARN=$OPTARG;;
- s) STRICT_FILE_CHECK=1;;
- f) FILE_PATTERN=$OPTARG;;
- h) usage
- exit $UNKNOWN;;
- esac
- done
- if [[ -z "$FILE_PATTERN" ]]; then
- usage
- exit $UNKNOWN
- fi
- YOUNGEST_FILE_LAST_MODIFIED_SECONDS=99999999
- YOUNGEST_FILE_NAME="undefined"
- YOUNGEST_FILE_SIZE=99999999
- FILES=$( ls $FILE_PATTERN 2>&1)
- if [ $? -ne 0 ]; then
- if [ ${STRICT_FILE_CHECK} -eq 1 ]; then
- echo "CRITICAL: No file found matching pattern $FILE_PATTERN"
- exit $CRITICAL
- else
- echo "OK: No file found matching pattern $FILE_PATTERN"
- exit $OK
- fi
- fi
- for file in $FILES; do
- AGE=$(($(date +%s) - $(date +%s -r "$file")))
- if [ $AGE ]; then
- if [ ${AGE} -lt ${YOUNGEST_FILE_LAST_MODIFIED_SECONDS} ]; then
- YOUNGEST_FILE_LAST_MODIFIED_SECONDS=$AGE
- YOUNGEST_FILE_NAME=$file
- fi
- fi
- done
- YOUNGEST_FILE_SIZE=$(stat -c%s $YOUNGEST_FILE_NAME)
- MESSAGE="$YOUNGEST_FILE_NAME is $YOUNGEST_FILE_LAST_MODIFIED_SECONDS seconds old and $YOUNGEST_FILE_SIZE bytes.[w=$WARN , c=$CRIT s]"
- if [ -n "$CRIT" ] && [ ${YOUNGEST_FILE_LAST_MODIFIED_SECONDS} -ge ${CRIT} ]; then
- echo "FILE_AGE CRITICAL: $MESSAGE"
- exit $CRITICAL
- fi
- if [ -n "$WARN" ] && [ ${YOUNGEST_FILE_LAST_MODIFIED_SECONDS} -ge ${WARN} ]; then
- echo "FILE_AGE WARNING: $MESSAGE"
- exit $WARNING
- fi
- echo "FILE_AGE OK: $MESSAGE"
- exit $OK
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement