-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_file_age_size.sh
executable file
·145 lines (128 loc) · 3.7 KB
/
check_file_age_size.sh
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/sh
#
# (c)2015 Christian Kujau <lists@nerdbynature.de>
# Nagios plugin to check the age and the size of a file.
#
if [ $# -lt 9 ]; then
# NOTE: Bash "getopts" does not support multi-character arguments, so we have to make up
# stupid letters like "-e" or "-z" when I really want to use "-ac" or "-sc". GNU/getopts
# seems to support --long options but this may not be installed on other platforms.
echo "Usage: $(basename "$0") [-f file] [-a age_warning] [-e age_critical] [-s size_warning] [-z size_critical]"
echo
echo "* age_warning and age_critical in seconds or suffixed with [mhdw] (minutes, hours, days weeks)"
echo "* size_warning and size_critical in bytes or suffixed with [KMG] (KB, MB, GB)"
echo
echo "Note: for now, this script assumes that \"age\" should be LESS than the specified"
echo "margins and \"size\" should be ABOVE the specified margins."
echo
echo "Example: check if \"foo.log\" is no more than 1 hour old (max: 2 hours) and"
echo " no smaller than 2 MB in size (min: 1 MB)"
echo
echo " \$ $(basename "$0") -f foo.log -a 3600 -e 7200 -s 2097152 -z 1048576"
echo
exit 1
fi
_convert_time() {
case $1 in
*m) TIME=$(( ${1%%m} * 60));;
*h) TIME=$(( ${1%%h} * 60 * 60));;
*d) TIME=$(( ${1%%d} * 60 * 60 * 24));;
*w) TIME=$(( ${1%%w} * 60 * 60 * 24));;
*) TIME=$1;;
esac
}
_convert_size() {
case $1 in
*K) SIZE=$(( ${1%%K} * 1024));;
*M) SIZE=$(( ${1%%M} * 1024 * 1024));;
*G) SIZE=$(( ${1%%G} * 1024 * 1024 * 1024));;
*) SIZE=$1;;
esac
}
while getopts "a:e:s:z:f:" opt; do
case $opt in
f)
file=$OPTARG
;;
a)
_convert_time "$OPTARG"
age_warning=$TIME
;;
e)
_convert_time "$OPTARG"
age_critical=$TIME
;;
s)
_convert_size "$OPTARG"
size_warning=$SIZE
;;
z)
_convert_size "$OPTARG"
size_critical=$SIZE
;;
*)
exit 3
;;
esac
done
# Sanity check
if [ ! -f "$file" ]; then
echo "File $file cannot be found!"
exit 3
else
# Get current file size, modification time & age
file_size=$(stat -c %s "$file")
file_time=$(stat -c %Y "$file")
file_age=$(( $(date +%s) - "$file_time"))
fi
# Convert age & size into human readable units
# FIXME: this needs to be done per $age_* variable. And more elegantly too.
# if [ $age_critical -gt 600 ]; then
# age_critical_h=$(expr $age_critical / 60)
## echo "$age_critical_h min"
# if [ $age_critical_h -gt 48 ]; then
# age_critical_h=$(expr $age_critical_h / 60)
## echo "$age_critical_h hrs"
# if [ $age_critical_h -gt 240 ]; then
# age_critical_h=$(expr $age_critical_h / 24)
## echo "$age_critical_h days"
# if [ $age_critical_h -gt 14 ]; then
# age_critical_h=$(expr $age_critical_h / 7)
## echo "$age_critical_h weeks"
# if [ $age_critical_h -gt 8 ]; then
# age_critical_h=$(expr $age_critical_h / 4)
## echo "$age_critical_h months"
# fi
# fi
# fi
# fi
#fi
# File age
if [ "$file_age" -gt "$age_critical" ]; then
AGE_ERROR=2
AGE_MSG="CRITICAL: $file is older than $age_critical seconds!"
elif [ "$file_age" -gt "$age_warning" ]; then
AGE_ERROR=1
AGE_MSG="WARNING: $file is older than $age_warning seconds!"
else
AGE_ERROR=0
AGE_MSG="OK: $file is newer than $age_warning seconds."
fi
# File size
if [ "$file_size" -lt "$size_critical" ]; then
SIZE_ERROR=2
SIZE_MSG="CRITICAL: $file is smaller than $size_critical bytes!"
elif [ "$file_size" -lt "$size_warning" ]; then
SIZE_ERROR=1
SIZE_MSG="WARNING: $file is smaller than $size_warning bytes!"
else
SIZE_ERROR=0
SIZE_MSG="OK: $file is larger than $size_warning bytes."
fi
# DEBUG
# echo "AGE_ERROR: $AGE_ERROR SIZE_ERROR: $SIZE_ERROR"
# FIXME: surely, there must be a better way, no?
ERROR=$(printf "%s" "$AGE_ERROR $SIZE_ERROR" | xargs -n1 | sort -n | tail -1)
# Leave a message before the beep
echo "$AGE_MSG - $SIZE_MSG"
exit "$ERROR"