-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_file_age
109 lines (94 loc) · 2.23 KB
/
check_file_age
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
#----------------------------------------
# File Present Check plugin for Nagios
#
#----------------------------------------
# Plugin variable description
PROGNAME=$(basename $0)
RELEASE="Version 1.0"
AUTHOR="Tim Bowers"
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# Functions plugin usage
print_release() {
echo "$RELEASE $AUTHOR"
exit 0
}
print_usage() {
echo ""
echo "$PROGNAME $RELEASE - Check age of files in a directory, alerting if over a certain value"
echo ""
echo "Usage: check_file.sh"
echo ""
echo " -h Show help page"
echo " -w Warning age in minutes (minimum 1 minute)"
echo " -c Critical age in minutes (minimum 1 minute)"
echo ""
echo "Usage: $PROGNAME"
echo "Usage: $PROGNAME --help"
echo ""
}
print_help() {
print_usage
echo ""
print_release $PROGNAME $RELEASE
echo ""
echo "This plugin will check if a file is present or missing in a given directory"
echo "-h is for opening this help list"
echo ""
}
# Make sure the correct number of command line arguments have been
# supplied
if [ $# -lt 1 ]; then
print_usage
exit $STATE_UNKNOWN
fi
while [ $# -gt 1 ]; do
case "$1" in
-h | --help)
print_help
exit $STATE_OK
;;
-v | --version)
print_release
exit $STATE_OK
;;
-w) WARNINGVAL="$2"
shift
;;
-c) CRITICALVAL="$2"
shift
;;
-d) DIR="$2"
shift
;;
*) echo "Unknown argument: $1"
print_usage
exit $STATE_UNKNOWN
;;
esac
shift
done
## List files in ${DIR}
## Remove -maxdepth 1 to search subdir's as well
FILES_CRIT=($(find "${DIR}" -maxdepth 1 -type f -mmin +${CRITICALVAL}))
FILES_WARN=($(find "${DIR}" -maxdepth 1 -type f -mmin +${WARNINGVAL}))
#echo ${FILES_CRIT}
#echo ${CRITICALVAL}
if [ ! -z "$FILES_CRIT" ]; then
# $FILES_CRIT is defined
echo "CRIT: Files older than ${CRITICALVAL} minutes found!"
exit $STATE_CRITICAL
else
# $FILES_CRIT is not defined; Check $FILES_WARN
if [ ! -z "$FILES_WARN" ]; then
echo "WARN: Files older than ${WARNINGVAL} minutes found!"
exit $STATE_WARNING
fi
echo "No outdated files found"
exit $STATUS_OK
fi
echo "Unknown error"
exit $STATE_UNKNOWN