-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_backup_folder
executable file
·99 lines (85 loc) · 2.84 KB
/
check_backup_folder
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
#!/bin/bash
# Written by Alexis Bezverkhyy <[email protected]> in july 2010
# This is free and unencumbered software released into the public domain.
# For more information, please refer to <http://unlicense.org/>
#
# Small tweaks by Edoardo Causarano <[email protected]>
function PRINT_USAGE(){
echo "This Nagios plugin checks backup folders :
-d DIRECTORY the directory to search for backup files
-p PATTERN an optionnal pattern for backup files
-t HOURS maximum age in hours for the latest backup before a warning is issued
-T HOURS maximum age in hours for the latest backup before a critical alert is issued
-s KBYTES minimum size in kilo bytes for the latest backup before a warning is issued
-S KBYTES minimum size in kilo bytes for the latest backup before a critical alert is issued
-a GZ|BZ whether to test for compressed archive integrity (gzip, bzip)
-h prints out this help
You must at least specify a directory and a minimal size or a minimal age."
exit 0
}
WTIME=0;CTIME=0;WSIZE=0;CSIZE=0;DIR='';PATTERN='';AT=''
declare -i CTIME
declare -i WTIME
declare -i CSIZE
declare -i WSIZE
while true ; do
getopts 't:T:s:S:d:p:ha:' OPT
if [ "$OPT" = '?' ] ; then break; fi;
case "$OPT" in
"t") WTIME="$OPTARG";;
"T") CTIME="$OPTARG";;
"s") WSIZE="$OPTARG";;
"S") CSIZE="$OPTARG";;
"d") DIR="$OPTARG";;
"p") PATTERN="$OPTARG";;
"a") AT="$OPTARG";;
"h") PRINT_USAGE;;
esac
done
if [ -z "$DIR" -o '(' "$WTIME" = '0' -a "$CTIME" = '0'\
-a "$WSIZE" = '0' -a "$CSIZE" = '0' ')' ] ; then
PRINT_USAGE
fi
LASTFILE=$(ls -lt --time-style=+%s "$DIR" | grep -v "^total " | grep "$PATTERN"\
| head -n 1 | sed 's/\s\+/ /g')
if [ -z "$LASTFILE" ] ; then
echo "CRITICAL - no backup found in $DIR"
exit 2
fi
TIMESTAMP=$(cut -d ' ' -f 6 <<< "$LASTFILE")
BYTES=$(cut -d ' ' -f 5 <<< "$LASTFILE")
let "SIZE = $BYTES / 1024"
FILENAME=$(cut -d ' ' -f 7 <<< "$LASTFILE")
let "AGE = ( $(date +%s) - $TIMESTAMP ) / 3600"
if [ "$CTIME" -gt 0 -a "$AGE" -gt "$CTIME" ] ; then
echo "CRITICAL - $FILENAME is out of date ($AGE hours old)"
exit 2
fi
if [ "$WTIME" -gt 0 -a "$AGE" -gt "$WTIME" ] ; then
echo "WARNING - $FILENAME is out of date ($AGE hours old)"
exit 1
fi
if [ "$CSIZE" -gt 0 -a "$SIZE" -lt "$CSIZE" ] ; then
echo "CRITICAL - $FILENAME is too small ($SIZE kb)"
exit 2
fi
if [ "$WSIZE" -gt 0 -a "$SIZE" -lt "$WSIZE" ] ; then
echo "WARNING - $FILENAME is too small ($SIZE kb)"
exit 1
fi
if [ "$AT" = "gz" -o "$AT" = "GZ" ] ; then
gzip -t $DIR/$FILENAME > /dev/null 2>&1
if [ $? -ne 0 ] ; then
echo "CRITICAL - $FILENAME is corrupt!"
exit 1
fi
fi
if [ "$AT" = "bz" -o "$AT" = "BZ" ] ; then
bzip -t $DIR/$FILENAME > /dev/null 2>&1
if [ $? -ne 0 ] ; then
echo "CRITICAL - $FILENAME is corrupt!"
exit 1
fi
fi
echo "OK - $FILENAME ($AGE hours old, $SIZE kb)"
exit 0